SessionSerializerTest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "SessionSerializer.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "TestUtil.h"
  6. #include "RequestGroupMan.h"
  7. #include "array_fun.h"
  8. #include "download_helper.h"
  9. #include "prefs.h"
  10. #include "Option.h"
  11. #include "a2functional.h"
  12. #include "FileEntry.h"
  13. #include "SelectEventPoll.h"
  14. #include "DownloadEngine.h"
  15. namespace aria2 {
  16. class SessionSerializerTest:public CppUnit::TestFixture {
  17. CPPUNIT_TEST_SUITE(SessionSerializerTest);
  18. CPPUNIT_TEST(testSave);
  19. CPPUNIT_TEST(testSaveErrorDownload);
  20. CPPUNIT_TEST_SUITE_END();
  21. public:
  22. void testSave();
  23. void testSaveErrorDownload();
  24. };
  25. CPPUNIT_TEST_SUITE_REGISTRATION(SessionSerializerTest);
  26. void SessionSerializerTest::testSave()
  27. {
  28. #if defined(ENABLE_BITTORRENT) && defined(ENABLE_METALINK)
  29. const std::string URIs[] =
  30. { "http://localhost/file",
  31. "http://mirror/file",
  32. A2_TEST_DIR"/test.torrent",
  33. A2_TEST_DIR"/serialize_session.meta4",
  34. "magnet:?xt=urn:btih:248D0A1CD08284299DE78D5C1ED359BB46717D8C"};
  35. std::vector<std::string> uris(vbegin(URIs), vend(URIs));
  36. std::vector<SharedHandle<RequestGroup> > result;
  37. SharedHandle<Option> option(new Option());
  38. option->put(PREF_DIR, "/tmp");
  39. createRequestGroupForUri(result, option, uris);
  40. CPPUNIT_ASSERT_EQUAL((size_t)5, result.size());
  41. result[4]->getOption()->put(PREF_PAUSE, A2_V_TRUE);
  42. option->put(PREF_MAX_DOWNLOAD_RESULT, "10");
  43. SharedHandle<RequestGroupMan> rgman
  44. (new RequestGroupMan(result, 1, option.get()));
  45. SessionSerializer s(rgman);
  46. SharedHandle<DownloadResult> drs[] = {
  47. // REMOVED downloads will not be saved.
  48. createDownloadResult(error_code::REMOVED, "http://removed"),
  49. createDownloadResult(error_code::TIME_OUT, "http://error"),
  50. createDownloadResult(error_code::FINISHED, "http://finished"),
  51. createDownloadResult(error_code::FINISHED, "http://force-save")
  52. };
  53. // This URI will be discarded because same URI exists in remaining
  54. // URIs.
  55. drs[1]->fileEntries[0]->getSpentUris().push_back("http://error");
  56. drs[1]->fileEntries[0]->getSpentUris().push_back("http://error2");
  57. drs[3]->option->put(PREF_FORCE_SAVE, A2_V_TRUE);
  58. for(size_t i = 0; i < sizeof(drs)/sizeof(drs[0]); ++i) {
  59. rgman->addDownloadResult(drs[i]);
  60. }
  61. DownloadEngine e(SharedHandle<EventPoll>(new SelectEventPoll()));
  62. e.setOption(option.get());
  63. rgman->fillRequestGroupFromReserver(&e);
  64. CPPUNIT_ASSERT_EQUAL((size_t)1, rgman->getRequestGroups().size());
  65. std::string filename = A2_TEST_OUT_DIR"/aria2_SessionSerializerTest_testSave";
  66. s.save(filename);
  67. std::ifstream ss(filename.c_str(), std::ios::binary);
  68. std::string line;
  69. std::getline(ss, line);
  70. CPPUNIT_ASSERT_EQUAL(std::string("http://error2\thttp://error\t"), line);
  71. std::getline(ss, line);
  72. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s", drs[1]->gid->toHex().c_str()), line);
  73. std::getline(ss, line);
  74. // finished and force-save option
  75. CPPUNIT_ASSERT_EQUAL(std::string("http://force-save\t"), line);
  76. std::getline(ss, line);
  77. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s", drs[3]->gid->toHex().c_str()), line);
  78. std::getline(ss, line);
  79. CPPUNIT_ASSERT_EQUAL(std::string(" force-save=true"), line);
  80. // Check active download is also saved
  81. std::getline(ss, line);
  82. CPPUNIT_ASSERT_EQUAL(uris[0]+"\t"+uris[1]+"\t", line);
  83. std::getline(ss, line);
  84. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s",
  85. GroupId::toHex(result[0]->getGID()).c_str()),
  86. line);
  87. std::getline(ss, line);
  88. CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
  89. std::getline(ss, line);
  90. CPPUNIT_ASSERT_EQUAL(uris[2], line);
  91. std::getline(ss, line);
  92. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s",
  93. GroupId::toHex(result[1]->getGID()).c_str()),
  94. line);
  95. std::getline(ss, line);
  96. CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
  97. std::getline(ss, line);
  98. CPPUNIT_ASSERT_EQUAL(uris[3], line);
  99. std::getline(ss, line);
  100. // local metalink download does not save meaningful GID
  101. CPPUNIT_ASSERT(fmt(" gid=%s",
  102. GroupId::toHex(result[2]->getGID()).c_str())
  103. != line);
  104. std::getline(ss, line);
  105. CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
  106. std::getline(ss, line);
  107. CPPUNIT_ASSERT_EQUAL(uris[4], line);
  108. std::getline(ss, line);
  109. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s",
  110. GroupId::toHex(result[4]->getGID()).c_str()),
  111. line);
  112. std::getline(ss, line);
  113. CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
  114. std::getline(ss, line);
  115. CPPUNIT_ASSERT_EQUAL(std::string(" pause=true"), line);
  116. std::getline(ss, line);
  117. CPPUNIT_ASSERT(!ss);
  118. #endif // defined(ENABLE_BITTORRENT) && defined(ENABLE_METALINK)
  119. }
  120. void SessionSerializerTest::testSaveErrorDownload()
  121. {
  122. SharedHandle<DownloadResult> dr = createDownloadResult(error_code::TIME_OUT,
  123. "http://error");
  124. dr->fileEntries[0]->getSpentUris().swap
  125. (dr->fileEntries[0]->getRemainingUris());
  126. SharedHandle<Option> option(new Option());
  127. option->put(PREF_MAX_DOWNLOAD_RESULT, "10");
  128. SharedHandle<RequestGroupMan> rgman
  129. (new RequestGroupMan(std::vector<SharedHandle<RequestGroup> >(), 1,
  130. option.get()));
  131. rgman->addDownloadResult(dr);
  132. SessionSerializer s(rgman);
  133. std::string filename =
  134. A2_TEST_OUT_DIR"/aria2_SessionSerializerTest_testSaveErrorDownload";
  135. CPPUNIT_ASSERT(s.save(filename));
  136. std::ifstream ss(filename.c_str(), std::ios::binary);
  137. std::string line;
  138. std::getline(ss, line);
  139. CPPUNIT_ASSERT_EQUAL(std::string("http://error\t"), line);
  140. }
  141. } // namespace aria2