FileTest.cc 6.2 KB

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