MultiDiskAdaptorTest.cc 5.9 KB

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