BtPostDownloadHandlerTest.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. namespace aria2 {
  9. class BtPostDownloadHandlerTest:public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(BtPostDownloadHandlerTest);
  11. CPPUNIT_TEST(testCanHandle_extension);
  12. CPPUNIT_TEST(testCanHandle_contentType);
  13. CPPUNIT_TEST(testGetNextRequestGroups);
  14. CPPUNIT_TEST_SUITE_END();
  15. private:
  16. SharedHandle<Option> option_;
  17. public:
  18. void setUp()
  19. {
  20. option_.reset(new Option());
  21. }
  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. SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test.torrent"));
  30. RequestGroup rg(option_);
  31. rg.setDownloadContext(dctx);
  32. BtPostDownloadHandler handler;
  33. CPPUNIT_ASSERT(handler.canHandle(&rg));
  34. dctx->getFirstFileEntry()->setPath("test.torrent2");
  35. CPPUNIT_ASSERT(!handler.canHandle(&rg));
  36. }
  37. void BtPostDownloadHandlerTest::testCanHandle_contentType()
  38. {
  39. SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
  40. dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
  41. RequestGroup rg(option_);
  42. rg.setDownloadContext(dctx);
  43. BtPostDownloadHandler handler;
  44. CPPUNIT_ASSERT(handler.canHandle(&rg));
  45. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  46. CPPUNIT_ASSERT(!handler.canHandle(&rg));
  47. }
  48. void BtPostDownloadHandlerTest::testGetNextRequestGroups()
  49. {
  50. SharedHandle<DownloadContext> dctx
  51. (new DownloadContext(1024, 0, "test.torrent"));
  52. RequestGroup rg(option_);
  53. rg.setDownloadContext(dctx);
  54. rg.initPieceStorage();
  55. BtPostDownloadHandler handler;
  56. std::vector<SharedHandle<RequestGroup> > groups;
  57. handler.getNextRequestGroups(groups, &rg);
  58. CPPUNIT_ASSERT_EQUAL((size_t)1, groups.size());
  59. CPPUNIT_ASSERT_EQUAL
  60. (std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
  61. bittorrent::getInfoHashString(groups.front()->getDownloadContext()));
  62. CPPUNIT_ASSERT(std::find(rg.followedBy().begin(), rg.followedBy().end(),
  63. groups.front()->getGID()) != rg.followedBy().end());
  64. }
  65. } // namespace aria2