summaryrefslogtreecommitdiff
path: root/test/Metalink2RequestGroupTest.cc
blob: f8c00a86ba3d9adbce126e712696a376214a16a2 (plain)
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
#include "Metalink2RequestGroup.h"
#include "SingleFileDownloadContext.h"
#include "prefs.h"
#include "Option.h"
#include "RequestGroup.h"
#include "FileEntry.h"
#include <algorithm>
#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

class Metalink2RequestGroupTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(Metalink2RequestGroupTest);
  CPPUNIT_TEST(testGenerate);
  CPPUNIT_TEST_SUITE_END();
private:
  SharedHandle<Option> _option;

public:
  void setUp()
  {
    _option.reset(new Option());
    _option->put(PREF_SPLIT, "1");
  }

  void testGenerate();
};


CPPUNIT_TEST_SUITE_REGISTRATION( Metalink2RequestGroupTest );

void Metalink2RequestGroupTest::testGenerate()
{
  std::deque<SharedHandle<RequestGroup> > groups;
  Option requestOption;
  requestOption.put(PREF_DIR, "/tmp");
  Metalink2RequestGroup(_option.get()).generate(groups, "test.xml",
						requestOption);
  // first file
  {
    SharedHandle<RequestGroup> rg = groups[0];
    std::deque<std::string> uris;
    rg->getURIs(uris);
    std::sort(uris.begin(), uris.end());
    CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());
    CPPUNIT_ASSERT_EQUAL
      (std::string("ftp://ftphost/aria2-0.5.2.tar.bz2"), uris[0]);
    CPPUNIT_ASSERT_EQUAL
      (std::string("http://httphost/aria2-0.5.2.tar.bz2"), uris[1]);

    SharedHandle<SingleFileDownloadContext> dctx
      (dynamic_pointer_cast<SingleFileDownloadContext>
       (rg->getDownloadContext()));

    CPPUNIT_ASSERT(!dctx.isNull());
    CPPUNIT_ASSERT_EQUAL((uint64_t)0ULL, dctx->getTotalLength());
    CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), dctx->getDir());
#ifdef ENABLE_MESSAGE_DIGEST
    CPPUNIT_ASSERT_EQUAL(std::string("sha1"), dctx->getChecksumHashAlgo());
    CPPUNIT_ASSERT_EQUAL
      (std::string("a96cf3f0266b91d87d5124cf94326422800b627d"),
			 dctx->getChecksum());
#endif // ENABLE_MESSAGE_DIGEST
    CPPUNIT_ASSERT(!dctx->getSignature().isNull());
    CPPUNIT_ASSERT_EQUAL(std::string("pgp"), dctx->getSignature()->getType());
  }
  // second file
  {
    SharedHandle<RequestGroup> rg = groups[1];
    std::deque<std::string> uris;
    rg->getURIs(uris);
    CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size());

    SharedHandle<SingleFileDownloadContext> dctx
      (dynamic_pointer_cast<SingleFileDownloadContext>
       (rg->getDownloadContext()));

    CPPUNIT_ASSERT(!dctx.isNull());
    CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), dctx->getDir());
#ifdef ENABLE_MESSAGE_DIGEST
    CPPUNIT_ASSERT_EQUAL(std::string("sha1"), dctx->getPieceHashAlgo());
    CPPUNIT_ASSERT_EQUAL((size_t)2, dctx->getPieceHashes().size());
    CPPUNIT_ASSERT_EQUAL((size_t)262144, dctx->getPieceLength());
    CPPUNIT_ASSERT_EQUAL(std::string(""), dctx->getChecksumHashAlgo());
    CPPUNIT_ASSERT_EQUAL(std::string(""), dctx->getChecksum());
#endif // ENABLE_MESSAGE_DIGEST
    CPPUNIT_ASSERT(dctx->getSignature().isNull());
  }

#ifdef ENABLE_BITTORRENT
  // fifth file <- downloading .torrent file
  {
    SharedHandle<RequestGroup> rg = groups[4];
    std::deque<std::string> uris;
    rg->getURIs(uris);
    CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
    CPPUNIT_ASSERT_EQUAL
      (std::string("http://host/torrent-http.integrated.torrent"), uris[0]);
    SharedHandle<SingleFileDownloadContext> dctx
      (dynamic_pointer_cast<SingleFileDownloadContext>
       (rg->getDownloadContext()));

    CPPUNIT_ASSERT(!dctx.isNull());
    // PREF_DIR has no effect for internal torrent file download
    CPPUNIT_ASSERT_EQUAL(std::string("."), dctx->getDir());
  }
#endif // ENABLE_BITTORRENT

  // sixth file <- depends on thrid file
  {
#ifdef ENABLE_BITTORRENT
    SharedHandle<RequestGroup> rg = groups[5];
#else
    SharedHandle<RequestGroup> rg = groups[4];
#endif // ENABLE_BITTORRENT
    std::deque<std::string> uris;
    rg->getURIs(uris);
    CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size());
    CPPUNIT_ASSERT_EQUAL
      (std::string("http://host/torrent-http.integrated"), uris[0]);

    SharedHandle<SingleFileDownloadContext> dctx
      (dynamic_pointer_cast<SingleFileDownloadContext>
       (rg->getDownloadContext()));

    CPPUNIT_ASSERT(!dctx.isNull());
    CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), dctx->getDir());
  }
}

} // namespace aria2