forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTest.cc
276 lines (240 loc) · 6.17 KB
/
FileTest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "File.h"
#include "TestUtil.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include <fstream>
#include <cppunit/extensions/HelperMacros.h>
#include "util.h"
namespace aria2 {
class FileTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(FileTest);
CPPUNIT_TEST(testExists);
CPPUNIT_TEST(testIsFile);
CPPUNIT_TEST(testIsDir);
CPPUNIT_TEST(testRemove);
CPPUNIT_TEST(testSize);
CPPUNIT_TEST(testMkdir);
CPPUNIT_TEST(testGetDirname);
CPPUNIT_TEST(testGetBasename);
CPPUNIT_TEST(testRenameTo);
CPPUNIT_TEST(testUtime);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {}
void testExists();
void testIsFile();
void testIsDir();
void testRemove();
void testSize();
void testMkdir();
void testGetDirname();
void testGetBasename();
void testRenameTo();
void testUtime();
};
CPPUNIT_TEST_SUITE_REGISTRATION(FileTest);
void FileTest::testExists()
{
File f(A2_TEST_DIR "/FileTest.cc");
CPPUNIT_ASSERT(f.exists());
File f2("NonExistentFile");
CPPUNIT_ASSERT(!f2.exists());
File d1(A2_TEST_DIR);
CPPUNIT_ASSERT(d1.exists());
}
void FileTest::testIsFile()
{
File f(A2_TEST_DIR "/FileTest.cc");
CPPUNIT_ASSERT(f.isFile());
File f2("NonExistentFile");
CPPUNIT_ASSERT(!f2.isFile());
File d1(A2_TEST_DIR);
CPPUNIT_ASSERT(!d1.isFile());
}
void FileTest::testIsDir()
{
File f(A2_TEST_DIR "/FileTest.cc");
CPPUNIT_ASSERT(!f.isDir());
File f2("NonExistentFile");
CPPUNIT_ASSERT(!f2.isDir());
File d1(A2_TEST_DIR);
CPPUNIT_ASSERT(d1.isDir());
}
void FileTest::testRemove()
{
int fd;
std::string name = A2_TEST_OUT_DIR "/aria2_FileTest_testRemove_testregfile";
unlink(name.c_str());
if ((fd = creat(name.c_str(), S_IRUSR | S_IWUSR)) < 0) {
CPPUNIT_FAIL("cannot create test file");
}
close(fd);
File f(name);
CPPUNIT_ASSERT(f.isFile());
CPPUNIT_ASSERT(f.remove());
CPPUNIT_ASSERT(!f.exists());
// delete the file again
CPPUNIT_ASSERT(!f.remove());
std::string dir = A2_TEST_OUT_DIR "/aria2_FileTest_testRemove_testdir";
#ifdef __MINGW32__
mkdir(dir.c_str());
#else
mkdir(dir.c_str(), 0777);
#endif // __MINGW32__
File d(dir);
CPPUNIT_ASSERT(d.exists());
CPPUNIT_ASSERT(d.remove());
CPPUNIT_ASSERT(!d.exists());
// delete the directory again
CPPUNIT_ASSERT(!d.remove());
}
void FileTest::testSize()
{
File f(A2_TEST_DIR "/4096chunk.txt");
CPPUNIT_ASSERT_EQUAL((int64_t)4_k, f.size());
}
void FileTest::testMkdir()
{
{
std::string dir = A2_TEST_OUT_DIR "/aria2_FileTest_testMkdir/test";
File d(dir);
if (d.exists()) {
CPPUNIT_ASSERT(d.remove());
}
CPPUNIT_ASSERT(!d.exists());
CPPUNIT_ASSERT(d.mkdirs());
CPPUNIT_ASSERT(d.exists());
// this test failes because d.mkdir returns false when the directory is
// already exists.
CPPUNIT_ASSERT(!d.mkdirs());
}
{
std::string dir =
A2_TEST_OUT_DIR "////aria2_FileTest_testMkdir////test2///";
std::string nDir = A2_TEST_OUT_DIR "/aria2_FileTest_testMkdir/test2";
File d(dir);
File nd(nDir);
if (d.exists()) {
CPPUNIT_ASSERT(d.remove());
}
CPPUNIT_ASSERT(!nd.exists());
CPPUNIT_ASSERT(d.mkdirs());
CPPUNIT_ASSERT(nd.exists());
// this test failes because d.mkdir returns false when the directory is
// already exists.
CPPUNIT_ASSERT(!d.mkdirs());
}
}
void FileTest::testGetDirname()
{
{
File f("/usr/lib");
CPPUNIT_ASSERT_EQUAL(std::string("/usr"), f.getDirname());
}
{
File f("/usr/");
CPPUNIT_ASSERT_EQUAL(std::string("/usr"), f.getDirname());
}
{
File f("usr");
CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname());
}
{
File f("/");
CPPUNIT_ASSERT_EQUAL(std::string("/"), f.getDirname());
}
{
File f(".");
CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname());
}
{
File f("..");
CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname());
}
{
File f("");
CPPUNIT_ASSERT_EQUAL(std::string(""), f.getDirname());
}
#ifdef __MINGW32__
{
File f("c:\\foo\\bar");
CPPUNIT_ASSERT_EQUAL(std::string("c:\\foo"), f.getDirname());
}
#endif // __MINGW32__
}
void FileTest::testGetBasename()
{
{
File f("/usr/lib");
CPPUNIT_ASSERT_EQUAL(std::string("lib"), f.getBasename());
}
{
File f("/usr/");
CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
}
{
File f("usr");
CPPUNIT_ASSERT_EQUAL(std::string("usr"), f.getBasename());
}
{
File f("/");
CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
}
{
File f(".");
CPPUNIT_ASSERT_EQUAL(std::string("."), f.getBasename());
}
{
File f("..");
CPPUNIT_ASSERT_EQUAL(std::string(".."), f.getBasename());
}
{
File f("");
CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
}
#ifdef __MINGW32__
{
File f("c:\\foo\\bar");
CPPUNIT_ASSERT_EQUAL(std::string("bar"), f.getBasename());
}
{
File f("c:\\foo\\");
CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
}
#endif // __MINGW32__
}
void FileTest::testRenameTo()
{
std::string fname = A2_TEST_OUT_DIR "/aria2_FileTest_testRenameTo.txt";
std::ofstream of(fname.c_str(), std::ios::binary);
of.close();
File f(fname);
std::string fnameTo = A2_TEST_OUT_DIR "/aria2_FileTest_testRenameTo_dest.txt";
CPPUNIT_ASSERT(f.renameTo(fnameTo));
CPPUNIT_ASSERT(f.exists());
CPPUNIT_ASSERT(!File(fname).exists());
CPPUNIT_ASSERT_EQUAL(File(fnameTo).getBasename(), f.getBasename());
// to see renameTo() work even when the destination file exists
of.open(fname.c_str());
of.close();
CPPUNIT_ASSERT(f.renameTo(fname));
}
void FileTest::testUtime()
{
File f(A2_TEST_OUT_DIR "/aria2_FileTest_testUTime");
createFile(f.getPath(), 0);
time_t atime = (time_t)100000;
time_t mtime = (time_t)200000;
CPPUNIT_ASSERT(f.utime(Time(atime), Time(mtime)));
a2_struct_stat buf;
CPPUNIT_ASSERT(0 == a2stat(utf8ToWChar(f.getPath()).c_str(), &buf));
CPPUNIT_ASSERT_EQUAL((time_t)atime, (time_t)buf.st_atime);
CPPUNIT_ASSERT_EQUAL((time_t)mtime, f.getModifiedTime().getTimeFromEpoch());
File notFound(A2_TEST_OUT_DIR "/aria2_FileTest_testUTime_notFound");
notFound.remove();
CPPUNIT_ASSERT(!notFound.utime(Time(atime), Time(mtime)));
}
} // namespace aria2