RequestGroupTest.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "RequestGroup.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Option.h"
  4. #include "DownloadContext.h"
  5. #include "FileEntry.h"
  6. #include "PieceStorage.h"
  7. #include "DownloadResult.h"
  8. namespace aria2 {
  9. class RequestGroupTest : public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(RequestGroupTest);
  11. CPPUNIT_TEST(testGetFirstFilePath);
  12. CPPUNIT_TEST(testTryAutoFileRenaming);
  13. CPPUNIT_TEST(testCreateDownloadResult);
  14. CPPUNIT_TEST_SUITE_END();
  15. private:
  16. std::shared_ptr<Option> option_;
  17. public:
  18. void setUp() { option_.reset(new Option()); }
  19. void testGetFirstFilePath();
  20. void testTryAutoFileRenaming();
  21. void testCreateDownloadResult();
  22. };
  23. CPPUNIT_TEST_SUITE_REGISTRATION(RequestGroupTest);
  24. void RequestGroupTest::testGetFirstFilePath()
  25. {
  26. std::shared_ptr<DownloadContext> ctx(
  27. new DownloadContext(1_k, 1_k, "/tmp/myfile"));
  28. RequestGroup group(GroupId::create(), option_);
  29. group.setDownloadContext(ctx);
  30. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFirstFilePath());
  31. group.markInMemoryDownload();
  32. CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFirstFilePath());
  33. }
  34. void RequestGroupTest::testTryAutoFileRenaming()
  35. {
  36. std::shared_ptr<DownloadContext> ctx(
  37. new DownloadContext(1_k, 1_k, "/tmp/myfile"));
  38. RequestGroup group(GroupId::create(), option_);
  39. group.setDownloadContext(ctx);
  40. option_->put(PREF_AUTO_FILE_RENAMING, "false");
  41. try {
  42. group.tryAutoFileRenaming();
  43. }
  44. catch (const Exception& ex) {
  45. CPPUNIT_ASSERT_EQUAL(error_code::FILE_ALREADY_EXISTS, ex.getErrorCode());
  46. }
  47. option_->put(PREF_AUTO_FILE_RENAMING, "true");
  48. group.tryAutoFileRenaming();
  49. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile.1"), group.getFirstFilePath());
  50. ctx->getFirstFileEntry()->setPath("/tmp/myfile.txt");
  51. group.tryAutoFileRenaming();
  52. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile.1.txt"),
  53. group.getFirstFilePath());
  54. ctx->getFirstFileEntry()->setPath("/tmp.txt/myfile");
  55. group.tryAutoFileRenaming();
  56. CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/myfile.1"),
  57. group.getFirstFilePath());
  58. ctx->getFirstFileEntry()->setPath("/tmp.txt/myfile.txt");
  59. group.tryAutoFileRenaming();
  60. CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/myfile.1.txt"),
  61. group.getFirstFilePath());
  62. ctx->getFirstFileEntry()->setPath(".bashrc");
  63. group.tryAutoFileRenaming();
  64. CPPUNIT_ASSERT_EQUAL(std::string(".bashrc.1"), group.getFirstFilePath());
  65. ctx->getFirstFileEntry()->setPath(".bashrc.txt");
  66. group.tryAutoFileRenaming();
  67. CPPUNIT_ASSERT_EQUAL(std::string(".bashrc.1.txt"), group.getFirstFilePath());
  68. ctx->getFirstFileEntry()->setPath("/tmp.txt/.bashrc");
  69. group.tryAutoFileRenaming();
  70. CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/.bashrc.1"),
  71. group.getFirstFilePath());
  72. ctx->getFirstFileEntry()->setPath("/tmp.txt/.bashrc.txt");
  73. group.tryAutoFileRenaming();
  74. CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/.bashrc.1.txt"),
  75. group.getFirstFilePath());
  76. }
  77. void RequestGroupTest::testCreateDownloadResult()
  78. {
  79. std::shared_ptr<DownloadContext> ctx(
  80. new DownloadContext(1_k, 1_m, "/tmp/myfile"));
  81. RequestGroup group(GroupId::create(), option_);
  82. group.setDownloadContext(ctx);
  83. group.initPieceStorage();
  84. {
  85. std::shared_ptr<DownloadResult> result = group.createDownloadResult();
  86. CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"),
  87. result->fileEntries[0]->getPath());
  88. CPPUNIT_ASSERT_EQUAL((int64_t)1_m,
  89. result->fileEntries.back()->getLastOffset());
  90. CPPUNIT_ASSERT_EQUAL((uint64_t)0, result->sessionDownloadLength);
  91. CPPUNIT_ASSERT_EQUAL((int64_t)0, result->sessionTime.count());
  92. // result is UNKNOWN_ERROR if download has not completed and no specific
  93. // error has been reported
  94. CPPUNIT_ASSERT_EQUAL(error_code::UNKNOWN_ERROR, result->result);
  95. // if haltReason is set to RequestGroup::USER_REQUEST, download
  96. // result will become REMOVED.
  97. group.setHaltRequested(true, RequestGroup::USER_REQUEST);
  98. result = group.createDownloadResult();
  99. CPPUNIT_ASSERT_EQUAL(error_code::REMOVED, result->result);
  100. // if haltReason is set to RequestGroup::SHUTDOWN_SIGNAL, download
  101. // result will become IN_PROGRESS.
  102. group.setHaltRequested(true, RequestGroup::SHUTDOWN_SIGNAL);
  103. result = group.createDownloadResult();
  104. CPPUNIT_ASSERT_EQUAL(error_code::IN_PROGRESS, result->result);
  105. }
  106. {
  107. group.setLastErrorCode(error_code::RESOURCE_NOT_FOUND);
  108. std::shared_ptr<DownloadResult> result = group.createDownloadResult();
  109. CPPUNIT_ASSERT_EQUAL(error_code::RESOURCE_NOT_FOUND, result->result);
  110. }
  111. {
  112. group.getPieceStorage()->markAllPiecesDone();
  113. std::shared_ptr<DownloadResult> result = group.createDownloadResult();
  114. CPPUNIT_ASSERT_EQUAL(error_code::FINISHED, result->result);
  115. }
  116. }
  117. } // namespace aria2