MultiDiskAdaptorTest.cc 7.3 KB

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