FileTest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "File.h"
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <string>
  6. #include <cppunit/extensions/HelperMacros.h>
  7. using namespace std;
  8. class FileTest:public CppUnit::TestFixture {
  9. CPPUNIT_TEST_SUITE(FileTest);
  10. CPPUNIT_TEST(testExists);
  11. CPPUNIT_TEST(testIsFile);
  12. CPPUNIT_TEST(testIsDir);
  13. CPPUNIT_TEST(testRemove);
  14. CPPUNIT_TEST(testSize);
  15. CPPUNIT_TEST(testMkdir);
  16. CPPUNIT_TEST(testGetDirname);
  17. CPPUNIT_TEST(testGetBasename);
  18. CPPUNIT_TEST(testRenameTo);
  19. CPPUNIT_TEST_SUITE_END();
  20. private:
  21. public:
  22. void setUp() {
  23. }
  24. void testExists();
  25. void testIsFile();
  26. void testIsDir();
  27. void testRemove();
  28. void testSize();
  29. void testMkdir();
  30. void testGetDirname();
  31. void testGetBasename();
  32. void testRenameTo();
  33. };
  34. CPPUNIT_TEST_SUITE_REGISTRATION( FileTest );
  35. void FileTest::testExists() {
  36. File f("FileTest.cc");
  37. CPPUNIT_ASSERT(f.exists());
  38. File f2("NonExistentFile");
  39. CPPUNIT_ASSERT(!f2.exists());
  40. File d1("../test");
  41. CPPUNIT_ASSERT(d1.exists());
  42. }
  43. void FileTest::testIsFile() {
  44. File f("FileTest.cc");
  45. CPPUNIT_ASSERT(f.isFile());
  46. File f2("NonExistentFile");
  47. CPPUNIT_ASSERT(!f2.isFile());
  48. File d1("../test");
  49. CPPUNIT_ASSERT(!d1.isFile());
  50. }
  51. void FileTest::testIsDir() {
  52. File f("FileTest.cc");
  53. CPPUNIT_ASSERT(!f.isDir());
  54. File f2("NonExistentFile");
  55. CPPUNIT_ASSERT(!f2.isDir());
  56. File d1("../test");
  57. CPPUNIT_ASSERT(d1.isDir());
  58. }
  59. void FileTest::testRemove() {
  60. int fd;
  61. string name = "/tmp/aria2test";
  62. unlink(name.c_str());
  63. if((fd = creat(name.c_str(), S_IRUSR|S_IWUSR)) < 0) {
  64. CPPUNIT_FAIL("cannot create test file");
  65. }
  66. close(fd);
  67. File f(name);
  68. CPPUNIT_ASSERT(f.isFile());
  69. CPPUNIT_ASSERT(f.remove());
  70. CPPUNIT_ASSERT(!f.exists());
  71. // delete the file again
  72. CPPUNIT_ASSERT(!f.remove());
  73. string dir = "/tmp/aria2testdir";
  74. #ifdef __MINGW32__
  75. mkdir(dir.c_str());
  76. #else
  77. mkdir(dir.c_str(), 0777);
  78. #endif // __MINGW32__
  79. File d(dir);
  80. CPPUNIT_ASSERT(d.exists());
  81. CPPUNIT_ASSERT(d.remove());
  82. CPPUNIT_ASSERT(!d.exists());
  83. // delete the directory again
  84. CPPUNIT_ASSERT(!d.remove());
  85. }
  86. void FileTest::testSize() {
  87. File f("4096chunk.txt");
  88. CPPUNIT_ASSERT_EQUAL(4096, (int)f.size());
  89. }
  90. void FileTest::testMkdir() {
  91. string dir = "/tmp/aria2test2/test";
  92. File d(dir);
  93. if(d.exists()) {
  94. CPPUNIT_ASSERT(d.remove());
  95. }
  96. CPPUNIT_ASSERT(!d.exists());
  97. CPPUNIT_ASSERT(d.mkdirs());
  98. CPPUNIT_ASSERT(d.exists());
  99. // this test failes because d.mkdir returns false when the directory is
  100. // already exists.
  101. CPPUNIT_ASSERT(!d.mkdirs());
  102. }
  103. void FileTest::testGetDirname()
  104. {
  105. File f("/tmp/dist/aria2.tar.bz2");
  106. CPPUNIT_ASSERT_EQUAL(string("/tmp/dist"), f.getDirname());
  107. }
  108. void FileTest::testGetBasename()
  109. {
  110. File f("/tmp/dist/aria2.tar.bz2");
  111. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), f.getBasename());
  112. }
  113. void FileTest::testRenameTo()
  114. {
  115. string fname = "FileTest_testRenameTo.txt";
  116. ofstream of(fname.c_str());
  117. of.close();
  118. File f(fname);
  119. string fnameTo = "FileTest_testRenameTo_dest.txt";
  120. CPPUNIT_ASSERT(f.renameTo(fnameTo));
  121. CPPUNIT_ASSERT(f.exists());
  122. CPPUNIT_ASSERT(!File(fname).exists());
  123. CPPUNIT_ASSERT_EQUAL(fnameTo, f.getBasename());
  124. // to see renameTo() work even when the destination file exists
  125. of.open(fname.c_str());
  126. of.close();
  127. CPPUNIT_ASSERT(f.renameTo(fname));
  128. }