File: UTMetadataPostDownloadHandlerTest.cc

package info (click to toggle)
aria2 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 14,748 kB
  • ctags: 13,441
  • sloc: cpp: 86,740; ansic: 16,496; sh: 4,916; makefile: 1,312; ruby: 397; yacc: 291; xml: 170; sed: 16
file content (139 lines) | stat: -rw-r--r-- 4,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "UTMetadataPostDownloadHandler.h"

#include <cppunit/extensions/HelperMacros.h>

#include "DownloadContext.h"
#include "RequestGroup.h"
#include "Option.h"
#include "FileEntry.h"
#include "bittorrent_helper.h"
#include "A2STR.h"
#include "ByteArrayDiskWriterFactory.h"
#include "PieceStorage.h"
#include "DiskAdaptor.h"
#include "util.h"
#include "MessageDigestHelper.h"
#include "prefs.h"

namespace aria2 {

class UTMetadataPostDownloadHandlerTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(UTMetadataPostDownloadHandlerTest);
  CPPUNIT_TEST(testCanHandle);
  CPPUNIT_TEST(testGetNextRequestGroups);
  CPPUNIT_TEST_SUITE_END();
private:
  SharedHandle<Option> option_;
  SharedHandle<DownloadContext> dctx_;
  SharedHandle<RequestGroup> requestGroup_;
public:
  void setUp()
  {
    option_.reset(new Option());
    option_->put("HELLO", "WORLD");
    option_->put(PREF_DIR, ".");
    dctx_.reset(new DownloadContext(0, 0, "something"));
    requestGroup_.reset(new RequestGroup(option_));
    requestGroup_->setDownloadContext(dctx_);
  }

  void testCanHandle();
  void testGetNextRequestGroups();
};


CPPUNIT_TEST_SUITE_REGISTRATION( UTMetadataPostDownloadHandlerTest );

void UTMetadataPostDownloadHandlerTest::testCanHandle()
{
  UTMetadataPostDownloadHandler handler;

  CPPUNIT_ASSERT(!handler.canHandle(requestGroup_.get()));

  SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
  dctx_->setAttribute(bittorrent::BITTORRENT, attrs);

  CPPUNIT_ASSERT(handler.canHandle(requestGroup_.get()));

  // Only checks whether metadata is empty or not
  attrs->metadata = "metadata";

  CPPUNIT_ASSERT(!handler.canHandle(requestGroup_.get()));
}

void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups()
{
  File trfile("cd41c7fdddfd034a15a04d7ff881216e01c4ceaf.torrent");
  if(trfile.exists()) {
    trfile.remove();
  }
  std::string metadata =
    "d6:lengthi384e4:name19:aria2-0.8.2.tar.bz212:piece lengthi128e"
    "6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCe";
  unsigned char infoHash[20];
  MessageDigestHelper::digest
    (infoHash, sizeof(infoHash), MessageDigestContext::SHA1,
     reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size());
  dctx_->getFirstFileEntry()->setLength(metadata.size());
  SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
  attrs->infoHash = std::string(&infoHash[0], &infoHash[20]);
  std::vector<std::vector<std::string> > announceList;
  std::vector<std::string> announceTier;
  announceTier.push_back("http://tracker");
  announceList.push_back(announceTier);
  attrs->announceList = announceList;
  dctx_->setAttribute(bittorrent::BITTORRENT, attrs);
  requestGroup_->setDiskWriterFactory
    (SharedHandle<DiskWriterFactory>(new ByteArrayDiskWriterFactory()));
  requestGroup_->initPieceStorage();
  requestGroup_->getPieceStorage()->getDiskAdaptor()->writeData
    (reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size(),
     0);

  UTMetadataPostDownloadHandler handler;
  std::vector<SharedHandle<RequestGroup> > results;
  handler.getNextRequestGroups(results, requestGroup_.get());

  CPPUNIT_ASSERT_EQUAL((size_t)1, results.size());
  SharedHandle<RequestGroup> newRg = results.front();
  SharedHandle<DownloadContext> newDctx = newRg->getDownloadContext();
  SharedHandle<TorrentAttribute> newAttrs =
    bittorrent::getTorrentAttrs(newDctx);
  CPPUNIT_ASSERT_EQUAL(bittorrent::getInfoHashString(dctx_),
                       bittorrent::getInfoHashString(newDctx));
  const std::vector<std::vector<std::string> >& newAnnounceList =
    newAttrs->announceList;
  CPPUNIT_ASSERT_EQUAL((size_t)1, newAnnounceList.size());
  CPPUNIT_ASSERT_EQUAL(std::string("http://tracker"), newAnnounceList[0][0]);
  CPPUNIT_ASSERT_EQUAL(option_->get("Hello"),
                       newRg->getOption()->get("Hello"));
  CPPUNIT_ASSERT
    (std::find(requestGroup_->followedBy().begin(),
               requestGroup_->followedBy().end(),
               newRg->getGID()) != requestGroup_->followedBy().end());
  CPPUNIT_ASSERT(!trfile.exists());

  results.clear();

  requestGroup_->getOption()->put(PREF_BT_SAVE_METADATA, V_TRUE);
  handler.getNextRequestGroups(results, requestGroup_.get());
  CPPUNIT_ASSERT(trfile.exists());

  results.clear();

  // See failure with bad metadata
  metadata = "d6:lengthi384e4:name19:aria2-0.8.2.tar.bz212:piece lengthi128e";
  requestGroup_->initPieceStorage();
  requestGroup_->getPieceStorage()->getDiskAdaptor()->writeData
    (reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size(),
     0);
  try {
    handler.getNextRequestGroups(results, requestGroup_.get());
    CPPUNIT_FAIL("exception must be thrown.");
  } catch(RecoverableException& e) {
    // success
  }
}

} // namespace aria2