BtPostDownloadHandlerTest.cc 2.5 KB

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