BtPostDownloadHandlerTest.cc 2.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include "BtPostDownloadHandler.h"

#include <cppunit/extensions/HelperMacros.h>

#include "DownloadContext.h"
#include "RequestGroup.h"
#include "Option.h"
#include "FileEntry.h"
#include "bittorrent_helper.h"
#include "PieceStorage.h"
#include "DiskAdaptor.h"
12
#include "RequestGroupCriteria.h"
13 14 15

namespace aria2 {

16
class BtPostDownloadHandlerTest : public CppUnit::TestFixture {
17 18 19 20 21 22

  CPPUNIT_TEST_SUITE(BtPostDownloadHandlerTest);
  CPPUNIT_TEST(testCanHandle_extension);
  CPPUNIT_TEST(testCanHandle_contentType);
  CPPUNIT_TEST(testGetNextRequestGroups);
  CPPUNIT_TEST_SUITE_END();
23

24
private:
25
  std::shared_ptr<Option> option_;
26

27
public:
28
  void setUp() { option_.reset(new Option()); }
29 30 31 32 33 34

  void testCanHandle_extension();
  void testCanHandle_contentType();
  void testGetNextRequestGroups();
};

35
CPPUNIT_TEST_SUITE_REGISTRATION(BtPostDownloadHandlerTest);
36 37 38

void BtPostDownloadHandlerTest::testCanHandle_extension()
{
39 40
  std::shared_ptr<DownloadContext> dctx(
      new DownloadContext(0, 0, A2_TEST_DIR "/test.torrent"));
41
  RequestGroup rg(GroupId::create(), option_);
42 43 44 45 46 47
  rg.setDownloadContext(dctx);

  BtPostDownloadHandler handler;

  CPPUNIT_ASSERT(handler.canHandle(&rg));

48
  dctx->getFirstFileEntry()->setPath(A2_TEST_DIR "/test.torrent2");
49 50 51 52 53
  CPPUNIT_ASSERT(!handler.canHandle(&rg));
}

void BtPostDownloadHandlerTest::testCanHandle_contentType()
{
54
  std::shared_ptr<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
55
  dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
56
  RequestGroup rg(GroupId::create(), option_);
57 58 59 60 61 62 63 64 65 66 67 68
  rg.setDownloadContext(dctx);

  BtPostDownloadHandler handler;

  CPPUNIT_ASSERT(handler.canHandle(&rg));

  dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  CPPUNIT_ASSERT(!handler.canHandle(&rg));
}

void BtPostDownloadHandlerTest::testGetNextRequestGroups()
{
69 70
  std::shared_ptr<DownloadContext> dctx(
      new DownloadContext(1_k, 0, A2_TEST_DIR "/test.torrent"));
71
  RequestGroup rg(GroupId::create(), option_);
72 73 74 75 76
  rg.setDownloadContext(dctx);
  rg.initPieceStorage();
  rg.getPieceStorage()->getDiskAdaptor()->enableReadOnly();

  BtPostDownloadHandler handler;
77
  std::vector<std::shared_ptr<RequestGroup>> groups;
78 79
  handler.getNextRequestGroups(groups, &rg);
  CPPUNIT_ASSERT_EQUAL((size_t)1, groups.size());
80 81 82
  CPPUNIT_ASSERT_EQUAL(
      std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"),
      bittorrent::getInfoHashString(groups.front()->getDownloadContext()));
83 84
  CPPUNIT_ASSERT(std::find(rg.followedBy().begin(), rg.followedBy().end(),
                           groups.front()->getGID()) != rg.followedBy().end());
85 86 87
  for (auto& nrg : groups) {
    CPPUNIT_ASSERT_EQUAL(rg.getGID(), nrg->following());
  }
88 89 90
}

} // namespace aria2