BtPostDownloadHandlerTest.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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()
  22. {
  23. option_.reset(new Option());
  24. }
  25. void testCanHandle_extension();
  26. void testCanHandle_contentType();
  27. void testGetNextRequestGroups();
  28. };
  29. CPPUNIT_TEST_SUITE_REGISTRATION( BtPostDownloadHandlerTest );
  30. void BtPostDownloadHandlerTest::testCanHandle_extension()
  31. {
  32. std::shared_ptr<DownloadContext> dctx
  33. (new DownloadContext(0, 0, A2_TEST_DIR"/test.torrent"));
  34. RequestGroup rg(GroupId::create(), option_);
  35. rg.setDownloadContext(dctx);
  36. BtPostDownloadHandler handler;
  37. CPPUNIT_ASSERT(handler.canHandle(&rg));
  38. dctx->getFirstFileEntry()->setPath(A2_TEST_DIR"/test.torrent2");
  39. CPPUNIT_ASSERT(!handler.canHandle(&rg));
  40. }
  41. void BtPostDownloadHandlerTest::testCanHandle_contentType()
  42. {
  43. std::shared_ptr<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
  44. dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
  45. RequestGroup rg(GroupId::create(), option_);
  46. rg.setDownloadContext(dctx);
  47. BtPostDownloadHandler handler;
  48. CPPUNIT_ASSERT(handler.canHandle(&rg));
  49. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  50. CPPUNIT_ASSERT(!handler.canHandle(&rg));
  51. }
  52. void BtPostDownloadHandlerTest::testGetNextRequestGroups()
  53. {
  54. std::shared_ptr<DownloadContext> dctx
  55. (new DownloadContext(1024, 0, A2_TEST_DIR"/test.torrent"));
  56. RequestGroup rg(GroupId::create(), option_);
  57. rg.setDownloadContext(dctx);
  58. rg.initPieceStorage();
  59. rg.getPieceStorage()->getDiskAdaptor()->enableReadOnly();
  60. BtPostDownloadHandler handler;
  61. std::vector<std::shared_ptr<RequestGroup> > groups;
  62. handler.getNextRequestGroups(groups, &rg);
  63. CPPUNIT_ASSERT_EQUAL((size_t)1, groups.size());
  64. CPPUNIT_ASSERT_EQUAL
  65. (std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  66. bittorrent::getInfoHashString(groups.front()->getDownloadContext()));
  67. CPPUNIT_ASSERT(std::find(rg.followedBy().begin(), rg.followedBy().end(),
  68. groups.front()->getGID()) != rg.followedBy().end());
  69. }
  70. } // namespace aria2