FileTest.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_SUITE_END();
  19. private:
  20. public:
  21. void setUp() {
  22. }
  23. void testExists();
  24. void testIsFile();
  25. void testIsDir();
  26. void testRemove();
  27. void testSize();
  28. void testMkdir();
  29. void testGetDirname();
  30. void testGetBasename();
  31. };
  32. CPPUNIT_TEST_SUITE_REGISTRATION( FileTest );
  33. void FileTest::testExists() {
  34. File f("FileTest.cc");
  35. CPPUNIT_ASSERT(f.exists());
  36. File f2("NonExistentFile");
  37. CPPUNIT_ASSERT(!f2.exists());
  38. File d1("../test");
  39. CPPUNIT_ASSERT(d1.exists());
  40. }
  41. void FileTest::testIsFile() {
  42. File f("FileTest.cc");
  43. CPPUNIT_ASSERT(f.isFile());
  44. File f2("NonExistentFile");
  45. CPPUNIT_ASSERT(!f2.isFile());
  46. File d1("../test");
  47. CPPUNIT_ASSERT(!d1.isFile());
  48. }
  49. void FileTest::testIsDir() {
  50. File f("FileTest.cc");
  51. CPPUNIT_ASSERT(!f.isDir());
  52. File f2("NonExistentFile");
  53. CPPUNIT_ASSERT(!f2.isDir());
  54. File d1("../test");
  55. CPPUNIT_ASSERT(d1.isDir());
  56. }
  57. void FileTest::testRemove() {
  58. int fd;
  59. string name = "/tmp/aria2test";
  60. unlink(name.c_str());
  61. if((fd = creat(name.c_str(), S_IRUSR|S_IWUSR)) < 0) {
  62. CPPUNIT_FAIL("cannot create test file");
  63. }
  64. close(fd);
  65. File f(name);
  66. CPPUNIT_ASSERT(f.isFile());
  67. CPPUNIT_ASSERT(f.remove());
  68. CPPUNIT_ASSERT(!f.exists());
  69. // delete the file again
  70. CPPUNIT_ASSERT(!f.remove());
  71. string dir = "/tmp/aria2testdir";
  72. #ifdef __MINGW32__
  73. mkdir(dir.c_str());
  74. #else
  75. mkdir(dir.c_str(), 0777);
  76. #endif // __MINGW32__
  77. File d(dir);
  78. CPPUNIT_ASSERT(d.exists());
  79. CPPUNIT_ASSERT(d.remove());
  80. CPPUNIT_ASSERT(!d.exists());
  81. // delete the directory again
  82. CPPUNIT_ASSERT(!d.remove());
  83. }
  84. void FileTest::testSize() {
  85. File f("4096chunk.txt");
  86. CPPUNIT_ASSERT_EQUAL(4096, (int)f.size());
  87. }
  88. void FileTest::testMkdir() {
  89. string dir = "/tmp/aria2test2/test";
  90. File d(dir);
  91. if(d.exists()) {
  92. CPPUNIT_ASSERT(d.remove());
  93. }
  94. CPPUNIT_ASSERT(!d.exists());
  95. CPPUNIT_ASSERT(d.mkdirs());
  96. CPPUNIT_ASSERT(d.exists());
  97. // this test failes because d.mkdir returns false when the directory is
  98. // already exists.
  99. CPPUNIT_ASSERT(!d.mkdirs());
  100. }
  101. void FileTest::testGetDirname()
  102. {
  103. File f("/tmp/dist/aria2.tar.bz2");
  104. CPPUNIT_ASSERT_EQUAL(string("/tmp/dist"), f.getDirname());
  105. }
  106. void FileTest::testGetBasename()
  107. {
  108. File f("/tmp/dist/aria2.tar.bz2");
  109. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), f.getBasename());
  110. }