FileTest.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. mkdir(dir.c_str(), 0777);
  73. File d(dir);
  74. CPPUNIT_ASSERT(d.exists());
  75. CPPUNIT_ASSERT(d.remove());
  76. CPPUNIT_ASSERT(!d.exists());
  77. // delete the directory again
  78. CPPUNIT_ASSERT(!d.remove());
  79. }
  80. void FileTest::testSize() {
  81. File f("4096chunk.txt");
  82. CPPUNIT_ASSERT_EQUAL(4096, (int)f.size());
  83. }
  84. void FileTest::testMkdir() {
  85. string dir = "/tmp/aria2test2/test";
  86. File d(dir);
  87. if(d.exists()) {
  88. CPPUNIT_ASSERT(d.remove());
  89. }
  90. CPPUNIT_ASSERT(!d.exists());
  91. CPPUNIT_ASSERT(d.mkdirs());
  92. CPPUNIT_ASSERT(d.exists());
  93. // this test failes because d.mkdir returns false when the directory is
  94. // already exists.
  95. CPPUNIT_ASSERT(!d.mkdirs());
  96. }
  97. void FileTest::testGetDirname()
  98. {
  99. File f("/tmp/dist/aria2.tar.bz2");
  100. CPPUNIT_ASSERT_EQUAL(string("/tmp/dist"), f.getDirname());
  101. }
  102. void FileTest::testGetBasename()
  103. {
  104. File f("/tmp/dist/aria2.tar.bz2");
  105. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), f.getBasename());
  106. }