1//
2// FileTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "FileTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/File.h"
15#include "Poco/TemporaryFile.h"
16#include "Poco/Path.h"
17#include "Poco/Exception.h"
18#include "Poco/Thread.h"
19#include <fstream>
20#include <set>
21
22
23#ifndef MAX_PATH
24 #define MAX_PATH 260
25#endif
26
27
28using Poco::File;
29using Poco::TemporaryFile;
30using Poco::Path;
31using Poco::Exception;
32using Poco::Timestamp;
33using Poco::Thread;
34
35
36FileTest::FileTest(const std::string& rName): CppUnit::TestCase(rName)
37{
38}
39
40
41FileTest::~FileTest()
42{
43}
44
45
46void FileTest::testFileAttributes1()
47{
48 File f("testfile.dat");
49 assertTrue (!f.exists());
50
51 try
52 {
53 bool flag = f.canRead();
54 failmsg("file does not exist - must throw exception");
55 }
56 catch (Exception&)
57 {
58 }
59
60 try
61 {
62 bool flag = f.canWrite();
63 failmsg("file does not exist - must throw exception");
64 }
65 catch (Exception&)
66 {
67 }
68
69 try
70 {
71 bool flag = f.isFile();
72 failmsg("file does not exist - must throw exception");
73 }
74 catch (Exception&)
75 {
76 }
77
78 try
79 {
80 bool flag = f.isDirectory();
81 failmsg("file does not exist - must throw exception");
82 }
83 catch (Exception&)
84 {
85 }
86
87 try
88 {
89 Timestamp ts = f.created();
90 failmsg("file does not exist - must throw exception");
91 }
92 catch (Exception&)
93 {
94 }
95
96 try
97 {
98 Timestamp ts = f.getLastModified();
99 failmsg("file does not exist - must throw exception");
100 }
101 catch (Exception&)
102 {
103 }
104
105 try
106 {
107 Timestamp ts;
108 f.setLastModified(ts);
109 failmsg("file does not exist - must throw exception");
110 }
111 catch (Exception&)
112 {
113 }
114
115 try
116 {
117 File::FileSize fs = f.getSize();
118 failmsg("file does not exist - must throw exception");
119 }
120 catch (Exception&)
121 {
122 }
123
124 try
125 {
126 f.setSize(0);
127 failmsg("file does not exist - must throw exception");
128 }
129 catch (Exception&)
130 {
131 }
132
133 try
134 {
135 f.setWriteable();
136 failmsg("file does not exist - must throw exception");
137 }
138 catch (Exception&)
139 {
140 }
141
142 try
143 {
144 f.setReadOnly();
145 failmsg("file does not exist - must throw exception");
146 }
147 catch (Exception&)
148 {
149 }
150
151 try
152 {
153 f.copyTo("copy.dat");
154 failmsg("file does not exist - must throw exception");
155 }
156 catch (Exception&)
157 {
158 }
159
160 try
161 {
162 f.moveTo("copy.dat");
163 failmsg("file does not exist - must throw exception");
164 }
165 catch (Exception&)
166 {
167 }
168
169 try
170 {
171 f.renameTo("copy.dat");
172 failmsg("file does not exist - must throw exception");
173 }
174 catch (Exception&)
175 {
176 }
177
178 try
179 {
180 f.remove();
181 failmsg("file does not exist - must throw exception");
182 }
183 catch (Exception&)
184 {
185 }
186
187 try
188 {
189 f.totalSpace();
190 failmsg("file does not exist - must throw exception");
191 }
192 catch (Exception&)
193 {
194 }
195
196 try
197 {
198 f.usableSpace();
199 failmsg("file does not exist - must throw exception");
200 }
201 catch (Exception&)
202 {
203 }
204
205 try
206 {
207 f.freeSpace();
208 failmsg("file does not exist - must throw exception");
209 }
210 catch (Exception&)
211 {
212 }
213}
214
215
216void FileTest::testCreateFile()
217{
218 File f("testfile.dat");
219 bool created = f.createFile();
220 assertTrue (created);
221 assertTrue (!f.isHidden());
222 created = f.createFile();
223 assertTrue (!created);
224}
225
226
227void FileTest::testFileAttributes2()
228{
229 TemporaryFile f;
230 bool created = f.createFile();
231 Timestamp ts;
232 assertTrue (created);
233
234 assertTrue (f.exists());
235 assertTrue (f.canRead());
236 assertTrue (f.canWrite());
237 assertTrue (f.isFile());
238 assertTrue (!f.isDirectory());
239 Timestamp tsc = f.created();
240 Timestamp tsm = f.getLastModified();
241 assertTrue (tsc - ts >= -2000000 && tsc - ts <= 2000000);
242 assertTrue (tsm - ts >= -2000000 && tsm - ts <= 2000000);
243
244 f.setWriteable(false);
245 assertTrue (!f.canWrite());
246 assertTrue (f.canRead());
247
248 f.setReadOnly(false);
249 assertTrue (f.canWrite());
250 assertTrue (f.canRead());
251
252 ts = Timestamp::fromEpochTime(1000000);
253 f.setLastModified(ts);
254 assertTrue (f.getLastModified() == ts);
255}
256
257
258void FileTest::testFileAttributes3()
259{
260#if defined(POCO_OS_FAMILY_UNIX)
261#if POCO_OS==POCO_OS_CYGWIN
262 File f("/dev/tty");
263#else
264 File f("/dev/null");
265#endif
266#elif defined(POCO_OS_FAMILY_WINDOWS) && !defined(_WIN32_WCE)
267 File f("CON");
268#endif
269
270#if !defined(_WIN32_WCE)
271 assertTrue (f.isDevice());
272 assertTrue (!f.isFile());
273 assertTrue (!f.isDirectory());
274#endif
275}
276
277
278void FileTest::testCompare()
279{
280 File f1("abc.txt");
281 File f2("def.txt");
282 File f3("abc.txt");
283
284 assertTrue (f1 == f3);
285 assertTrue (!(f1 == f2));
286 assertTrue (f1 != f2);
287 assertTrue (!(f1 != f3));
288 assertTrue (!(f1 == f2));
289 assertTrue (f1 < f2);
290 assertTrue (f1 <= f2);
291 assertTrue (!(f2 < f1));
292 assertTrue (!(f2 <= f1));
293 assertTrue (f2 > f1);
294 assertTrue (f2 >= f1);
295 assertTrue (!(f1 > f2));
296 assertTrue (!(f1 >= f2));
297
298 assertTrue (f1 <= f3);
299 assertTrue (f1 >= f3);
300}
301
302
303void FileTest::testRootDir()
304{
305#if defined(POCO_OS_FAMILY_WINDOWS)
306#if defined(_WIN32_WCE)
307 File f1("\\");
308 File f2("/");
309 assertTrue (f1.exists());
310 assertTrue (f2.exists());
311#else
312 File f1("/");
313 File f2("c:/");
314 File f3("c:\\");
315 File f4("\\");
316 assertTrue (f1.exists());
317 assertTrue (f2.exists());
318 assertTrue (f3.exists());
319 assertTrue (f4.exists());
320#endif
321#else
322 File f1("/");
323 assertTrue (f1.exists());
324#endif
325}
326
327
328void FileTest::testSwap()
329{
330 File f1("abc.txt");
331 File f2("def.txt");
332 f1.swap(f2);
333 assertTrue (f1.path() == "def.txt");
334 assertTrue (f2.path() == "abc.txt");
335}
336
337
338void FileTest::testSize()
339{
340 std::ofstream ostr("testfile.dat");
341 ostr << "Hello, world!" << std::endl;
342 ostr.close();
343 File f("testfile.dat");
344 assertTrue (f.getSize() > 0);
345 f.setSize(0);
346 assertTrue (f.getSize() == 0);
347}
348
349
350void FileTest::testSpace()
351{
352 File f(Path::temp());
353 assertTrue (f.totalSpace() > 0);
354 assertTrue (f.usableSpace() > 0);
355 assertTrue (f.freeSpace() > 0);
356}
357
358
359void FileTest::testDirectory()
360{
361 File d("testdir");
362 try
363 {
364 d.remove(true);
365 }
366 catch (...)
367 {
368 }
369 TemporaryFile::registerForDeletion("testdir");
370
371 bool created = d.createDirectory();
372 assertTrue (created);
373 assertTrue (d.isDirectory());
374 assertTrue (!d.isFile());
375 std::vector<std::string> files;
376 d.list(files);
377 assertTrue (files.empty());
378
379 File f = Path("testdir/file1", Path::PATH_UNIX);
380 f.createFile();
381 f = Path("testdir/file2", Path::PATH_UNIX);
382 f.createFile();
383 f = Path("testdir/file3", Path::PATH_UNIX);
384 f.createFile();
385
386 d.list(files);
387 assertTrue (files.size() == 3);
388
389 std::set<std::string> fs;
390 fs.insert(files.begin(), files.end());
391 assertTrue (fs.find("file1") != fs.end());
392 assertTrue (fs.find("file2") != fs.end());
393 assertTrue (fs.find("file3") != fs.end());
394
395 File dd(Path("testdir/testdir2/testdir3", Path::PATH_UNIX));
396 dd.createDirectories();
397 assertTrue (dd.exists());
398 assertTrue (dd.isDirectory());
399
400 File ddd(Path("testdir/testdirB/testdirC/testdirD", Path::PATH_UNIX));
401 ddd.createDirectories();
402 assertTrue (ddd.exists());
403 assertTrue (ddd.isDirectory());
404
405 d.remove(true);
406}
407
408
409void FileTest::testCopy()
410{
411 std::ofstream ostr("testfile.dat");
412 ostr << "Hello, world!" << std::endl;
413 ostr.close();
414
415 File f1("testfile.dat");
416 TemporaryFile f2;
417 f1.setReadOnly().copyTo(f2.path());
418 assertTrue (f2.exists());
419 assertTrue (!f2.canWrite());
420 assertTrue (f1.getSize() == f2.getSize());
421 f1.setWriteable().remove();
422}
423
424
425void FileTest::testMove()
426{
427 std::ofstream ostr("testfile.dat");
428 ostr << "Hello, world!" << std::endl;
429 ostr.close();
430
431 File f1("testfile.dat");
432 File::FileSize sz = f1.getSize();
433 TemporaryFile f2;
434 f1.moveTo(f2.path());
435 assertTrue (f2.exists());
436 assertTrue (f2.getSize() == sz);
437 assertTrue (f1.exists());
438 assertTrue (f1 == f2);
439}
440
441
442void FileTest::testCopyDirectory()
443{
444 Path pd1("testdir");
445 File fd1(pd1);
446 try
447 {
448 fd1.remove(true);
449 }
450 catch (...)
451 {
452 }
453 fd1.createDirectories();
454 Path pd2(pd1, "subdir");
455 File fd2(pd2);
456 fd2.createDirectories();
457 Path pf1(pd1, "testfile1.dat");
458 std::ofstream ostr1(pf1.toString().c_str());
459 ostr1 << "Hello, world!" << std::endl;
460 ostr1.close();
461 Path pf2(pd1, "testfile2.dat");
462 std::ofstream ostr2(pf2.toString().c_str());
463 ostr2 << "Hello, world!" << std::endl;
464 ostr2.close();
465 Path pf3(pd2, "testfile3.dat");
466 std::ofstream ostr3(pf3.toString().c_str());
467 ostr3 << "Hello, world!" << std::endl;
468 ostr3.close();
469
470 File fd3("testdir2");
471
472 try
473 {
474 fd3.remove(true);
475 }
476 catch (...)
477 {
478 }
479
480 fd1.copyTo("testdir2");
481
482 Path pd1t("testdir2");
483 File fd1t(pd1t);
484 assertTrue (fd1t.exists());
485 assertTrue (fd1t.isDirectory());
486
487 Path pd2t(pd1t, "subdir");
488 File fd2t(pd2t);
489 assertTrue (fd2t.exists());
490 assertTrue (fd2t.isDirectory());
491
492 Path pf1t(pd1t, "testfile1.dat");
493 File ff1t(pf1t);
494 assertTrue (ff1t.exists());
495 assertTrue (ff1t.isFile());
496
497 Path pf2t(pd1t, "testfile2.dat");
498 File ff2t(pf2t);
499 assertTrue (ff2t.exists());
500 assertTrue (ff2t.isFile());
501
502 Path pf3t(pd2t, "testfile3.dat");
503 File ff3t(pf3t);
504 assertTrue (ff3t.exists());
505 assertTrue (ff3t.isFile());
506
507 fd1.remove(true);
508 fd3.remove(true);
509}
510
511
512void FileTest::testRename()
513{
514 std::ofstream ostr("testfile.dat");
515 ostr << "Hello, world!" << std::endl;
516 ostr.close();
517
518 File f1("testfile.dat");
519 File f2("testfile2.dat");
520 f1.renameTo(f2.path());
521
522 assertTrue (f2.exists());
523 assertTrue (f1.exists());
524 assertTrue (f1 == f2);
525
526 f2.remove();
527}
528
529
530void FileTest::testLongPath()
531{
532#if defined(POCO_OS_FAMILY_WINDOWS) && !defined(_WIN32_WCE)
533 Poco::Path p("longpathtest");
534 p.makeAbsolute();
535 std::string longpath(p.toString());
536 while (longpath.size() < MAX_PATH*4)
537 {
538 longpath.append("\\");
539 longpath.append(64, 'x');
540 }
541
542 Poco::File d(longpath);
543 d.createDirectories();
544
545 assertTrue (d.exists());
546 assertTrue (d.isDirectory());
547
548 Poco::File f(p.toString());
549 f.remove(true);
550#endif
551}
552
553
554void FileTest::setUp()
555{
556 File f("testfile.dat");
557 try
558 {
559 f.remove();
560 }
561 catch (...)
562 {
563 }
564}
565
566
567void FileTest::tearDown()
568{
569 File f("testfile.dat");
570 try
571 {
572 f.remove();
573 }
574 catch (...)
575 {
576 }
577}
578
579
580CppUnit::Test* FileTest::suite()
581{
582 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FileTest");
583
584 CppUnit_addTest(pSuite, FileTest, testCreateFile);
585 CppUnit_addTest(pSuite, FileTest, testFileAttributes1);
586 CppUnit_addTest(pSuite, FileTest, testFileAttributes2);
587 CppUnit_addTest(pSuite, FileTest, testFileAttributes3);
588 CppUnit_addTest(pSuite, FileTest, testCompare);
589 CppUnit_addTest(pSuite, FileTest, testSwap);
590 CppUnit_addTest(pSuite, FileTest, testSize);
591 CppUnit_addTest(pSuite, FileTest, testSpace);
592 CppUnit_addTest(pSuite, FileTest, testDirectory);
593 CppUnit_addTest(pSuite, FileTest, testCopy);
594 CppUnit_addTest(pSuite, FileTest, testMove);
595 CppUnit_addTest(pSuite, FileTest, testCopyDirectory);
596 CppUnit_addTest(pSuite, FileTest, testRename);
597 CppUnit_addTest(pSuite, FileTest, testRootDir);
598 CppUnit_addTest(pSuite, FileTest, testLongPath);
599
600 return pSuite;
601}
602