MultiDiskAdaptorTest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "MultiDiskAdaptor.h"
  2. #include "FileEntry.h"
  3. #include "Exception.h"
  4. #include "a2io.h"
  5. #include "array_fun.h"
  6. #include "TestUtil.h"
  7. #include <string>
  8. #include <cerrno>
  9. #include <cstring>
  10. #include <cppunit/extensions/HelperMacros.h>
  11. namespace aria2 {
  12. class MultiDiskAdaptorTest:public CppUnit::TestFixture {
  13. CPPUNIT_TEST_SUITE(MultiDiskAdaptorTest);
  14. CPPUNIT_TEST(testWriteData);
  15. CPPUNIT_TEST(testReadData);
  16. CPPUNIT_TEST(testCutTrailingGarbage);
  17. CPPUNIT_TEST_SUITE_END();
  18. private:
  19. SharedHandle<MultiDiskAdaptor> adaptor;
  20. public:
  21. void setUp() {
  22. adaptor.reset(new MultiDiskAdaptor());
  23. adaptor->setPieceLength(2);
  24. adaptor->setStoreDir(".");
  25. adaptor->setTopDir(".");
  26. }
  27. void testWriteData();
  28. void testReadData();
  29. void testCutTrailingGarbage();
  30. };
  31. CPPUNIT_TEST_SUITE_REGISTRATION( MultiDiskAdaptorTest );
  32. std::deque<SharedHandle<FileEntry> > createEntries() {
  33. SharedHandle<FileEntry> entry1(new FileEntry("file1.txt", 15, 0));
  34. SharedHandle<FileEntry> entry2(new FileEntry("file2.txt", 7, 15));
  35. SharedHandle<FileEntry> entry3(new FileEntry("file3.txt", 3, 22));
  36. unlink("file1.txt");
  37. unlink("file2.txt");
  38. unlink("file3.txt");
  39. std::deque<SharedHandle<FileEntry> > entries;
  40. entries.push_back(entry1);
  41. entries.push_back(entry2);
  42. entries.push_back(entry3);
  43. return entries;
  44. }
  45. void readFile(const std::string& filename, char* buf, int bufLength) {
  46. FILE* f = fopen(filename.c_str(), "r");
  47. if(f == NULL) {
  48. CPPUNIT_FAIL(strerror(errno));
  49. }
  50. int retval = fread(buf, bufLength, 1, f);
  51. fclose(f);
  52. if(retval != 1) {
  53. CPPUNIT_FAIL("return value is not 1");
  54. }
  55. }
  56. void MultiDiskAdaptorTest::testWriteData() {
  57. try {
  58. adaptor->setFileEntries(createEntries());
  59. adaptor->openFile();
  60. std::string msg = "12345";
  61. adaptor->writeData((const unsigned char*)msg.c_str(), msg.size(), 0);
  62. adaptor->closeFile();
  63. char buf[128];
  64. readFile("file1.txt", buf, 5);
  65. buf[5] = '\0';
  66. CPPUNIT_ASSERT_EQUAL(msg, std::string(buf));
  67. adaptor->openFile();
  68. std::string msg2 = "67890ABCDEF";
  69. adaptor->writeData((const unsigned char*)msg2.c_str(), msg2.size(), 5);
  70. adaptor->closeFile();
  71. readFile("file1.txt", buf, 15);
  72. buf[15] = '\0';
  73. CPPUNIT_ASSERT_EQUAL(std::string("1234567890ABCDE"), std::string(buf));
  74. readFile("file2.txt", buf, 1);
  75. buf[1] = '\0';
  76. CPPUNIT_ASSERT_EQUAL(std::string("F"), std::string(buf));
  77. adaptor->openFile();
  78. std::string msg3 = "12345123456712";
  79. adaptor->writeData((const unsigned char*)msg3.c_str(), msg3.size(), 10);
  80. adaptor->closeFile();
  81. readFile("file1.txt", buf, 15);
  82. buf[15] = '\0';
  83. CPPUNIT_ASSERT_EQUAL(std::string("123456789012345"), std::string(buf));
  84. readFile("file2.txt", buf, 7);
  85. buf[7] = '\0';
  86. CPPUNIT_ASSERT_EQUAL(std::string("1234567"), std::string(buf));
  87. readFile("file3.txt", buf, 2);
  88. buf[2] = '\0';
  89. CPPUNIT_ASSERT_EQUAL(std::string("12"), std::string(buf));
  90. } catch(Exception& e) {
  91. CPPUNIT_FAIL(e.stackTrace());
  92. }
  93. }
  94. void MultiDiskAdaptorTest::testReadData() {
  95. SharedHandle<FileEntry> entry1(new FileEntry("file1r.txt", 15, 0));
  96. SharedHandle<FileEntry> entry2(new FileEntry("file2r.txt", 7, 15));
  97. SharedHandle<FileEntry> entry3(new FileEntry("file3r.txt", 3, 22));
  98. std::deque<SharedHandle<FileEntry> > entries;
  99. entries.push_back(entry1);
  100. entries.push_back(entry2);
  101. entries.push_back(entry3);
  102. adaptor->setFileEntries(entries);
  103. adaptor->openFile();
  104. unsigned char buf[128];
  105. adaptor->readData(buf, 15, 0);
  106. buf[15] = '\0';
  107. CPPUNIT_ASSERT_EQUAL(std::string("1234567890ABCDE"), std::string((char*)buf));
  108. adaptor->readData(buf, 10, 6);
  109. buf[10] = '\0';
  110. CPPUNIT_ASSERT_EQUAL(std::string("7890ABCDEF"), std::string((char*)buf));
  111. adaptor->readData(buf, 4, 20);
  112. buf[4] = '\0';
  113. CPPUNIT_ASSERT_EQUAL(std::string("KLMN"), std::string((char*)buf));
  114. adaptor->readData(buf, 25, 0);
  115. buf[25] = '\0';
  116. CPPUNIT_ASSERT_EQUAL(std::string("1234567890ABCDEFGHIJKLMNO"), std::string((char*)buf));
  117. }
  118. void MultiDiskAdaptorTest::testCutTrailingGarbage()
  119. {
  120. std::string dir = "/tmp";
  121. std::string topDir = ".";
  122. std::string topDirPath = dir+"/"+topDir;
  123. std::string prefix = "aria2_MultiDiskAdaptorTest_testCutTrailingGarbage_";
  124. SharedHandle<FileEntry> entries[] = {
  125. SharedHandle<FileEntry>(new FileEntry(prefix+"1", 256, 0)),
  126. SharedHandle<FileEntry>(new FileEntry(prefix+"2", 512, 256))
  127. };
  128. for(size_t i = 0; i < arrayLength(entries); ++i) {
  129. createFile(topDirPath+"/"+entries[i]->getPath(),
  130. entries[i]->getLength()+100);
  131. }
  132. std::deque<SharedHandle<FileEntry> > fileEntries
  133. (&entries[0], &entries[arrayLength(entries)]);
  134. MultiDiskAdaptor adaptor;
  135. adaptor.setStoreDir(dir);
  136. adaptor.setTopDir(topDir);
  137. adaptor.setFileEntries(fileEntries);
  138. adaptor.setMaxOpenFiles(1);
  139. adaptor.openFile();
  140. adaptor.cutTrailingGarbage();
  141. CPPUNIT_ASSERT_EQUAL((uint64_t)256,
  142. File(topDirPath+"/"+entries[0]->getPath()).size());
  143. CPPUNIT_ASSERT_EQUAL((uint64_t)512,
  144. File(topDirPath+"/"+entries[1]->getPath()).size());
  145. }
  146. } // namespace aria2