Merge pull request #25808 from CastagnaIT/fix_url_parse
[xbmc.git] / xbmc / filesystem / test / TestFile.cpp
blobabefe469e0f2e3e24f4dba7778a6d8c3be092932
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "filesystem/File.h"
10 #include "test/TestUtils.h"
12 #include <errno.h>
13 #include <string>
15 #include <gtest/gtest.h>
17 TEST(TestFile, Read)
19 const std::string newLine = CXBMCTestUtils::Instance().getNewLineCharacters();
20 const size_t size = 1616;
21 const size_t lines = 25;
22 size_t addPerLine = newLine.length() - 1;
23 size_t realSize = size + lines * addPerLine;
25 const std::string firstBuf = "About" + newLine + "-----" + newLine + "XBMC is ";
26 const std::string secondBuf = "an award-winning fre";
27 const std::string thirdBuf = "ent hub for digital ";
28 const std::string fourthBuf = "rs, XBMC is a non-pr";
29 const std::string fifthBuf = "multimedia jukebox." + newLine;
31 XFILE::CFile file;
32 char buf[23] = {};
34 size_t currentPos;
35 ASSERT_TRUE(file.Open(
36 XBMC_REF_FILE_PATH("/xbmc/filesystem/test/reffile.txt")));
37 EXPECT_EQ(0, file.GetPosition());
38 EXPECT_EQ(realSize, file.GetLength());
39 EXPECT_EQ(firstBuf.length(), static_cast<size_t>(file.Read(buf, firstBuf.length())));
40 file.Flush();
41 currentPos = firstBuf.length();
42 EXPECT_EQ(currentPos, file.GetPosition());
43 EXPECT_EQ(0, memcmp(firstBuf.c_str(), buf, firstBuf.length()));
44 EXPECT_EQ(secondBuf.length(), static_cast<size_t>(file.Read(buf, secondBuf.length())));
45 currentPos += secondBuf.length();
46 EXPECT_EQ(currentPos, file.GetPosition());
47 EXPECT_EQ(0, memcmp(secondBuf.c_str(), buf, secondBuf.length()));
48 currentPos = 100 + addPerLine * 3;
49 EXPECT_EQ(currentPos, file.Seek(currentPos));
50 EXPECT_EQ(currentPos, file.GetPosition());
51 EXPECT_EQ(thirdBuf.length(), static_cast<size_t>(file.Read(buf, thirdBuf.length())));
52 file.Flush();
53 currentPos += thirdBuf.length();
54 EXPECT_EQ(currentPos, file.GetPosition());
55 EXPECT_EQ(0, memcmp(thirdBuf.c_str(), buf, thirdBuf.length()));
56 currentPos += 100 + addPerLine * 1;
57 EXPECT_EQ(currentPos, file.Seek(100 + addPerLine * 1, SEEK_CUR));
58 EXPECT_EQ(currentPos, file.GetPosition());
59 EXPECT_EQ(fourthBuf.length(), static_cast<size_t>(file.Read(buf, fourthBuf.length())));
60 file.Flush();
61 currentPos += fourthBuf.length();
62 EXPECT_EQ(currentPos, file.GetPosition());
63 EXPECT_EQ(0, memcmp(fourthBuf.c_str(), buf, fourthBuf.length()));
64 currentPos = realSize - fifthBuf.length();
65 EXPECT_EQ(currentPos, file.Seek(-(int64_t)fifthBuf.length(), SEEK_END));
66 EXPECT_EQ(currentPos, file.GetPosition());
67 EXPECT_EQ(fifthBuf.length(), static_cast<size_t>(file.Read(buf, fifthBuf.length())));
68 file.Flush();
69 currentPos += fifthBuf.length();
70 EXPECT_EQ(currentPos, file.GetPosition());
71 EXPECT_EQ(0, memcmp(fifthBuf.c_str(), buf, fifthBuf.length()));
72 currentPos += 100;
73 EXPECT_EQ(currentPos, file.Seek(100, SEEK_CUR));
74 EXPECT_EQ(currentPos, file.GetPosition());
75 currentPos = 0;
76 EXPECT_EQ(currentPos, file.Seek(currentPos, SEEK_SET));
77 EXPECT_EQ(firstBuf.length(), static_cast<size_t>(file.Read(buf, firstBuf.length())));
78 file.Flush();
79 currentPos += firstBuf.length();
80 EXPECT_EQ(currentPos, file.GetPosition());
81 EXPECT_EQ(0, memcmp(firstBuf.c_str(), buf, firstBuf.length()));
82 EXPECT_EQ(0, file.Seek(0, SEEK_SET));
83 EXPECT_EQ(-1, file.Seek(-100, SEEK_SET));
84 file.Close();
87 TEST(TestFile, Write)
89 XFILE::CFile *file;
90 const char str[] = "TestFile.Write test string\n";
91 char buf[30] = {};
93 ASSERT_NE(nullptr, file = XBMC_CREATETEMPFILE(""));
94 file->Close();
95 ASSERT_TRUE(file->OpenForWrite(XBMC_TEMPFILEPATH(file), true));
96 EXPECT_EQ((int)sizeof(str), file->Write(str, sizeof(str)));
97 file->Flush();
98 EXPECT_EQ((int64_t)sizeof(str), file->GetPosition());
99 file->Close();
100 ASSERT_TRUE(file->Open(XBMC_TEMPFILEPATH(file)));
101 EXPECT_EQ(0, file->GetPosition());
102 EXPECT_EQ((int64_t)sizeof(str), file->Seek(0, SEEK_END));
103 EXPECT_EQ(0, file->Seek(0, SEEK_SET));
104 EXPECT_EQ((int64_t)sizeof(str), file->GetLength());
105 EXPECT_EQ(sizeof(str), static_cast<size_t>(file->Read(buf, sizeof(buf))));
106 file->Flush();
107 EXPECT_EQ((int64_t)sizeof(str), file->GetPosition());
108 EXPECT_EQ(0, memcmp(str, buf, sizeof(str)));
109 file->Close();
110 EXPECT_TRUE(XBMC_DELETETEMPFILE(file));
113 TEST(TestFile, Exists)
115 XFILE::CFile *file;
117 ASSERT_NE(nullptr, file = XBMC_CREATETEMPFILE(""));
118 file->Close();
119 EXPECT_TRUE(XFILE::CFile::Exists(XBMC_TEMPFILEPATH(file)));
120 EXPECT_FALSE(XFILE::CFile::Exists(""));
121 EXPECT_TRUE(XBMC_DELETETEMPFILE(file));
124 TEST(TestFile, Stat)
126 XFILE::CFile *file;
127 struct __stat64 buffer;
129 ASSERT_NE(nullptr, file = XBMC_CREATETEMPFILE(""));
130 EXPECT_EQ(0, file->Stat(&buffer));
131 file->Close();
132 EXPECT_NE(0U, buffer.st_mode | _S_IFREG);
133 EXPECT_EQ(-1, XFILE::CFile::Stat("", &buffer));
134 EXPECT_EQ(ENOENT, errno);
135 EXPECT_TRUE(XBMC_DELETETEMPFILE(file));
138 TEST(TestFile, Delete)
140 XFILE::CFile *file;
141 std::string path;
143 ASSERT_NE(nullptr, file = XBMC_CREATETEMPFILE(""));
144 file->Close();
145 path = XBMC_TEMPFILEPATH(file);
146 EXPECT_TRUE(XFILE::CFile::Exists(path));
147 EXPECT_TRUE(XFILE::CFile::Delete(path));
148 EXPECT_FALSE(XFILE::CFile::Exists(path));
149 EXPECT_FALSE(XBMC_DELETETEMPFILE(file));
152 TEST(TestFile, Rename)
154 XFILE::CFile *file1, *file2;
155 std::string path1, path2;
157 ASSERT_NE(nullptr, file1 = XBMC_CREATETEMPFILE(""));
158 file1->Close();
159 path1 = XBMC_TEMPFILEPATH(file1);
160 ASSERT_NE(nullptr, file2 = XBMC_CREATETEMPFILE(""));
161 file2->Close();
162 path2 = XBMC_TEMPFILEPATH(file2);
163 EXPECT_TRUE(XFILE::CFile::Delete(path1));
164 EXPECT_FALSE(XFILE::CFile::Exists(path1));
165 EXPECT_TRUE(XFILE::CFile::Exists(path2));
166 EXPECT_TRUE(XFILE::CFile::Rename(path2, path1));
167 EXPECT_TRUE(XFILE::CFile::Exists(path1));
168 EXPECT_FALSE(XFILE::CFile::Exists(path2));
169 EXPECT_TRUE(XFILE::CFile::Delete(path1));
170 EXPECT_FALSE(XBMC_DELETETEMPFILE(file1));
171 EXPECT_FALSE(XBMC_DELETETEMPFILE(file2));
174 TEST(TestFile, Copy)
176 XFILE::CFile *file1, *file2;
177 std::string path1, path2;
179 ASSERT_NE(nullptr, file1 = XBMC_CREATETEMPFILE(""));
180 file1->Close();
181 path1 = XBMC_TEMPFILEPATH(file1);
182 ASSERT_NE(nullptr, file2 = XBMC_CREATETEMPFILE(""));
183 file2->Close();
184 path2 = XBMC_TEMPFILEPATH(file2);
185 EXPECT_TRUE(XFILE::CFile::Delete(path1));
186 EXPECT_FALSE(XFILE::CFile::Exists(path1));
187 EXPECT_TRUE(XFILE::CFile::Exists(path2));
188 EXPECT_TRUE(XFILE::CFile::Copy(path2, path1));
189 EXPECT_TRUE(XFILE::CFile::Exists(path1));
190 EXPECT_TRUE(XFILE::CFile::Exists(path2));
191 EXPECT_TRUE(XFILE::CFile::Delete(path1));
192 EXPECT_TRUE(XFILE::CFile::Delete(path2));
193 EXPECT_FALSE(XBMC_DELETETEMPFILE(file1));
194 EXPECT_FALSE(XBMC_DELETETEMPFILE(file2));
197 TEST(TestFile, SetHidden)
199 XFILE::CFile *file;
201 ASSERT_NE(nullptr, file = XBMC_CREATETEMPFILE(""));
202 file->Close();
203 EXPECT_TRUE(XFILE::CFile::Exists(XBMC_TEMPFILEPATH(file)));
204 bool result = XFILE::CFile::SetHidden(XBMC_TEMPFILEPATH(file), true);
205 #ifdef TARGET_WINDOWS
206 EXPECT_TRUE(result);
207 #else
208 EXPECT_FALSE(result);
209 #endif
210 EXPECT_TRUE(XFILE::CFile::Exists(XBMC_TEMPFILEPATH(file)));
211 EXPECT_TRUE(XBMC_DELETETEMPFILE(file));