SessionSerializerTest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. drs[3]->option->put(PREF_FORCE_SAVE, A2_V_TRUE);
  54. for(size_t i = 0; i < sizeof(drs)/sizeof(drs[0]); ++i) {
  55. rgman->addDownloadResult(drs[i]);
  56. }
  57. DownloadEngine e(SharedHandle<EventPoll>(new SelectEventPoll()));
  58. e.setOption(option.get());
  59. rgman->fillRequestGroupFromReserver(&e);
  60. CPPUNIT_ASSERT_EQUAL((size_t)1, rgman->getRequestGroups().size());
  61. std::string filename = A2_TEST_OUT_DIR"/aria2_SessionSerializerTest_testSave";
  62. s.save(filename);
  63. std::ifstream ss(filename.c_str(), std::ios::binary);
  64. std::string line;
  65. std::getline(ss, line);
  66. CPPUNIT_ASSERT_EQUAL(std::string("http://error\t"), line);
  67. std::getline(ss, line);
  68. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s", drs[1]->gid->toHex().c_str()), line);
  69. std::getline(ss, line);
  70. // finished and force-save option
  71. CPPUNIT_ASSERT_EQUAL(std::string("http://force-save\t"), line);
  72. std::getline(ss, line);
  73. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s", drs[3]->gid->toHex().c_str()), line);
  74. std::getline(ss, line);
  75. CPPUNIT_ASSERT_EQUAL(std::string(" force-save=true"), line);
  76. // Check active download is also saved
  77. std::getline(ss, line);
  78. CPPUNIT_ASSERT_EQUAL(uris[0]+"\t"+uris[1]+"\t", line);
  79. std::getline(ss, line);
  80. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s",
  81. GroupId::toHex(result[0]->getGID()).c_str()),
  82. line);
  83. std::getline(ss, line);
  84. CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
  85. std::getline(ss, line);
  86. CPPUNIT_ASSERT_EQUAL(uris[2], line);
  87. std::getline(ss, line);
  88. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s",
  89. GroupId::toHex(result[1]->getGID()).c_str()),
  90. line);
  91. std::getline(ss, line);
  92. CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
  93. std::getline(ss, line);
  94. CPPUNIT_ASSERT_EQUAL(uris[3], line);
  95. std::getline(ss, line);
  96. // local metalink download does not save meaningful GID
  97. CPPUNIT_ASSERT(fmt(" gid=%s",
  98. GroupId::toHex(result[2]->getGID()).c_str())
  99. != line);
  100. std::getline(ss, line);
  101. CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
  102. std::getline(ss, line);
  103. CPPUNIT_ASSERT_EQUAL(uris[4], line);
  104. std::getline(ss, line);
  105. CPPUNIT_ASSERT_EQUAL(fmt(" gid=%s",
  106. GroupId::toHex(result[4]->getGID()).c_str()),
  107. line);
  108. std::getline(ss, line);
  109. CPPUNIT_ASSERT_EQUAL(std::string(" dir=/tmp"), line);
  110. std::getline(ss, line);
  111. CPPUNIT_ASSERT_EQUAL(std::string(" pause=true"), line);
  112. std::getline(ss, line);
  113. CPPUNIT_ASSERT(!ss);
  114. #endif // defined(ENABLE_BITTORRENT) && defined(ENABLE_METALINK)
  115. }
  116. void SessionSerializerTest::testSaveErrorDownload()
  117. {
  118. SharedHandle<DownloadResult> dr = createDownloadResult(error_code::TIME_OUT,
  119. "http://error");
  120. dr->fileEntries[0]->getSpentUris().swap
  121. (dr->fileEntries[0]->getRemainingUris());
  122. SharedHandle<Option> option(new Option());
  123. option->put(PREF_MAX_DOWNLOAD_RESULT, "10");
  124. SharedHandle<RequestGroupMan> rgman
  125. (new RequestGroupMan(std::vector<SharedHandle<RequestGroup> >(), 1,
  126. option.get()));
  127. rgman->addDownloadResult(dr);
  128. SessionSerializer s(rgman);
  129. std::string filename =
  130. A2_TEST_OUT_DIR"/aria2_SessionSerializerTest_testSaveErrorDownload";
  131. CPPUNIT_ASSERT(s.save(filename));
  132. std::ifstream ss(filename.c_str(), std::ios::binary);
  133. std::string line;
  134. std::getline(ss, line);
  135. CPPUNIT_ASSERT_EQUAL(std::string("http://error\t"), line);
  136. }
  137. } // namespace aria2