FileTest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "File.h"
  2. #include "TestUtil.h"
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <string>
  7. #include <fstream>
  8. #include <cppunit/extensions/HelperMacros.h>
  9. namespace aria2 {
  10. class FileTest:public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(FileTest);
  12. CPPUNIT_TEST(testExists);
  13. CPPUNIT_TEST(testIsFile);
  14. CPPUNIT_TEST(testIsDir);
  15. CPPUNIT_TEST(testRemove);
  16. CPPUNIT_TEST(testSize);
  17. CPPUNIT_TEST(testMkdir);
  18. CPPUNIT_TEST(testGetDirname);
  19. CPPUNIT_TEST(testGetBasename);
  20. CPPUNIT_TEST(testRenameTo);
  21. CPPUNIT_TEST(testUtime);
  22. CPPUNIT_TEST_SUITE_END();
  23. private:
  24. public:
  25. void setUp() {
  26. }
  27. void testExists();
  28. void testIsFile();
  29. void testIsDir();
  30. void testRemove();
  31. void testSize();
  32. void testMkdir();
  33. void testGetDirname();
  34. void testGetBasename();
  35. void testRenameTo();
  36. void testUtime();
  37. };
  38. CPPUNIT_TEST_SUITE_REGISTRATION( FileTest );
  39. void FileTest::testExists() {
  40. File f("FileTest.cc");
  41. CPPUNIT_ASSERT(f.exists());
  42. File f2("NonExistentFile");
  43. CPPUNIT_ASSERT(!f2.exists());
  44. File d1("../test");
  45. CPPUNIT_ASSERT(d1.exists());
  46. }
  47. void FileTest::testIsFile() {
  48. File f("FileTest.cc");
  49. CPPUNIT_ASSERT(f.isFile());
  50. File f2("NonExistentFile");
  51. CPPUNIT_ASSERT(!f2.isFile());
  52. File d1("../test");
  53. CPPUNIT_ASSERT(!d1.isFile());
  54. }
  55. void FileTest::testIsDir() {
  56. File f("FileTest.cc");
  57. CPPUNIT_ASSERT(!f.isDir());
  58. File f2("NonExistentFile");
  59. CPPUNIT_ASSERT(!f2.isDir());
  60. File d1("../test");
  61. CPPUNIT_ASSERT(d1.isDir());
  62. }
  63. void FileTest::testRemove() {
  64. int fd;
  65. std::string name = "/tmp/aria2test";
  66. unlink(name.c_str());
  67. if((fd = creat(name.c_str(), S_IRUSR|S_IWUSR)) < 0) {
  68. CPPUNIT_FAIL("cannot create test file");
  69. }
  70. close(fd);
  71. File f(name);
  72. CPPUNIT_ASSERT(f.isFile());
  73. CPPUNIT_ASSERT(f.remove());
  74. CPPUNIT_ASSERT(!f.exists());
  75. // delete the file again
  76. CPPUNIT_ASSERT(!f.remove());
  77. std::string dir = "/tmp/aria2testdir";
  78. #ifdef __MINGW32__
  79. mkdir(dir.c_str());
  80. #else
  81. mkdir(dir.c_str(), 0777);
  82. #endif // __MINGW32__
  83. File d(dir);
  84. CPPUNIT_ASSERT(d.exists());
  85. CPPUNIT_ASSERT(d.remove());
  86. CPPUNIT_ASSERT(!d.exists());
  87. // delete the directory again
  88. CPPUNIT_ASSERT(!d.remove());
  89. }
  90. void FileTest::testSize() {
  91. File f("4096chunk.txt");
  92. CPPUNIT_ASSERT_EQUAL(4096, (int)f.size());
  93. }
  94. void FileTest::testMkdir() {
  95. std::string dir = "/tmp/aria2test2/test";
  96. File d(dir);
  97. if(d.exists()) {
  98. CPPUNIT_ASSERT(d.remove());
  99. }
  100. CPPUNIT_ASSERT(!d.exists());
  101. CPPUNIT_ASSERT(d.mkdirs());
  102. CPPUNIT_ASSERT(d.exists());
  103. // this test failes because d.mkdir returns false when the directory is
  104. // already exists.
  105. CPPUNIT_ASSERT(!d.mkdirs());
  106. }
  107. void FileTest::testGetDirname()
  108. {
  109. {
  110. File f("/usr/lib");
  111. CPPUNIT_ASSERT_EQUAL(std::string("/usr"), f.getDirname());
  112. }
  113. {
  114. File f("/usr/");
  115. CPPUNIT_ASSERT_EQUAL(std::string("/usr"), f.getDirname());
  116. }
  117. {
  118. File f("usr");
  119. CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname());
  120. }
  121. {
  122. File f("/");
  123. CPPUNIT_ASSERT_EQUAL(std::string("/"), f.getDirname());
  124. }
  125. {
  126. File f(".");
  127. CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname());
  128. }
  129. {
  130. File f("..");
  131. CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname());
  132. }
  133. {
  134. File f("");
  135. CPPUNIT_ASSERT_EQUAL(std::string(""), f.getDirname());
  136. }
  137. }
  138. void FileTest::testGetBasename()
  139. {
  140. {
  141. File f("/usr/lib");
  142. CPPUNIT_ASSERT_EQUAL(std::string("lib"), f.getBasename());
  143. }
  144. {
  145. File f("/usr/");
  146. CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
  147. }
  148. {
  149. File f("usr");
  150. CPPUNIT_ASSERT_EQUAL(std::string("usr"), f.getBasename());
  151. }
  152. {
  153. File f("/");
  154. CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
  155. }
  156. {
  157. File f(".");
  158. CPPUNIT_ASSERT_EQUAL(std::string("."), f.getBasename());
  159. }
  160. {
  161. File f("..");
  162. CPPUNIT_ASSERT_EQUAL(std::string(".."), f.getBasename());
  163. }
  164. {
  165. File f("");
  166. CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename());
  167. }
  168. }
  169. void FileTest::testRenameTo()
  170. {
  171. std::string fname = "FileTest_testRenameTo.txt";
  172. std::ofstream of(fname.c_str());
  173. of.close();
  174. File f(fname);
  175. std::string fnameTo = "FileTest_testRenameTo_dest.txt";
  176. CPPUNIT_ASSERT(f.renameTo(fnameTo));
  177. CPPUNIT_ASSERT(f.exists());
  178. CPPUNIT_ASSERT(!File(fname).exists());
  179. CPPUNIT_ASSERT_EQUAL(fnameTo, f.getBasename());
  180. // to see renameTo() work even when the destination file exists
  181. of.open(fname.c_str());
  182. of.close();
  183. CPPUNIT_ASSERT(f.renameTo(fname));
  184. }
  185. void FileTest::testUtime()
  186. {
  187. File f("/tmp/FileTest_testUTime");
  188. createFile(f.getPath(), 0);
  189. CPPUNIT_ASSERT(f.utime(Time(1000), Time(2000)));
  190. struct stat buf;
  191. CPPUNIT_ASSERT(0 == stat(f.getPath().c_str(), &buf));
  192. CPPUNIT_ASSERT_EQUAL((time_t)1000, buf.st_atime);
  193. CPPUNIT_ASSERT_EQUAL((time_t)2000, f.getModifiedTime().getTime());
  194. File notFound("/tmp/FileTest_testUTime_notFound");
  195. notFound.remove();
  196. CPPUNIT_ASSERT(!notFound.utime(Time(1000), Time(2000)));
  197. }
  198. } // namespace aria2