BtPostDownloadHandlerTest.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "BtPostDownloadHandler.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "DownloadContext.h"
  4. #include "RequestGroup.h"
  5. #include "Option.h"
  6. #include "FileEntry.h"
  7. #include "bittorrent_helper.h"
  8. #include "PieceStorage.h"
  9. #include "DiskAdaptor.h"
  10. #include "RequestGroupCriteria.h"
  11. namespace aria2 {
  12. class BtPostDownloadHandlerTest : public CppUnit::TestFixture {
  13. CPPUNIT_TEST_SUITE(BtPostDownloadHandlerTest);
  14. CPPUNIT_TEST(testCanHandle_extension);
  15. CPPUNIT_TEST(testCanHandle_contentType);
  16. CPPUNIT_TEST(testGetNextRequestGroups);
  17. CPPUNIT_TEST_SUITE_END();
  18. private:
  19. std::shared_ptr<Option> option_;
  20. public:
  21. void setUp() { option_.reset(new Option()); }
  22. void testCanHandle_extension();
  23. void testCanHandle_contentType();
  24. void testGetNextRequestGroups();
  25. };
  26. CPPUNIT_TEST_SUITE_REGISTRATION(BtPostDownloadHandlerTest);
  27. void BtPostDownloadHandlerTest::testCanHandle_extension()
  28. {
  29. std::shared_ptr<DownloadContext> dctx(
  30. new DownloadContext(0, 0, A2_TEST_DIR "/test.torrent"));
  31. RequestGroup rg(GroupId::create(), option_);
  32. rg.setDownloadContext(dctx);
  33. BtPostDownloadHandler handler;
  34. CPPUNIT_ASSERT(handler.canHandle(&rg));
  35. dctx->getFirstFileEntry()->setPath(A2_TEST_DIR "/test.torrent2");
  36. CPPUNIT_ASSERT(!handler.canHandle(&rg));
  37. }
  38. void BtPostDownloadHandlerTest::testCanHandle_contentType()
  39. {
  40. std::shared_ptr<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
  41. dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
  42. RequestGroup rg(GroupId::create(), option_);
  43. rg.setDownloadContext(dctx);
  44. BtPostDownloadHandler handler;
  45. CPPUNIT_ASSERT(handler.canHandle(&rg));
  46. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  47. CPPUNIT_ASSERT(!handler.canHandle(&rg));
  48. }
  49. void BtPostDownloadHandlerTest::testGetNextRequestGroups()
  50. {
  51. std::shared_ptr<DownloadContext> dctx(
  52. new DownloadContext(1_k, 0, A2_TEST_DIR "/test.torrent"));
  53. RequestGroup rg(GroupId::create(), option_);
  54. rg.setDownloadContext(dctx);
  55. rg.initPieceStorage();
  56. rg.getPieceStorage()->getDiskAdaptor()->enableReadOnly();
  57. BtPostDownloadHandler handler;
  58. std::vector<std::shared_ptr<RequestGroup>> groups;
  59. handler.getNextRequestGroups(groups, &rg);
  60. CPPUNIT_ASSERT_EQUAL((size_t)1, groups.size());
  61. CPPUNIT_ASSERT_EQUAL(
  62. std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  63. bittorrent::getInfoHashString(groups.front()->getDownloadContext()));
  64. CPPUNIT_ASSERT(std::find(rg.followedBy().begin(), rg.followedBy().end(),
  65. groups.front()->getGID()) != rg.followedBy().end());
  66. for (auto& nrg : groups) {
  67. CPPUNIT_ASSERT_EQUAL(rg.getGID(), nrg->following());
  68. }
  69. }
  70. } // namespace aria2