forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadHelperTest.cc
371 lines (319 loc) · 13.6 KB
/
DownloadHelperTest.cc
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "download_helper.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <cppunit/extensions/HelperMacros.h>
#include "RequestGroup.h"
#include "DownloadContext.h"
#include "Option.h"
#include "array_fun.h"
#include "prefs.h"
#include "Exception.h"
#include "util.h"
#include "FileEntry.h"
#ifdef ENABLE_BITTORRENT
#include "bittorrent_helper.h"
#endif // ENABLE_BITTORRENT
namespace aria2 {
class DownloadHelperTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(DownloadHelperTest);
CPPUNIT_TEST(testCreateRequestGroupForUri);
CPPUNIT_TEST(testCreateRequestGroupForUri_parameterized);
CPPUNIT_TEST(testCreateRequestGroupForUriList);
#ifdef ENABLE_BITTORRENT
CPPUNIT_TEST(testCreateRequestGroupForUri_BitTorrent);
CPPUNIT_TEST(testCreateRequestGroupForBitTorrent);
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
CPPUNIT_TEST(testCreateRequestGroupForUri_Metalink);
CPPUNIT_TEST(testCreateRequestGroupForMetalink);
#endif // ENABLE_METALINK
CPPUNIT_TEST_SUITE_END();
private:
std::shared_ptr<Option> option_;
public:
void setUp() { option_.reset(new Option()); }
void tearDown() {}
void testCreateRequestGroupForUri();
void testCreateRequestGroupForUri_parameterized();
void testCreateRequestGroupForUriList();
#ifdef ENABLE_BITTORRENT
void testCreateRequestGroupForUri_BitTorrent();
void testCreateRequestGroupForBitTorrent();
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
void testCreateRequestGroupForUri_Metalink();
void testCreateRequestGroupForMetalink();
#endif // ENABLE_METALINK
};
CPPUNIT_TEST_SUITE_REGISTRATION(DownloadHelperTest);
void DownloadHelperTest::testCreateRequestGroupForUri()
{
std::vector<std::string> uris{"http://alpha/file", "http://bravo/file",
"http://charlie/file"};
option_->put(PREF_SPLIT, "7");
option_->put(PREF_MAX_CONNECTION_PER_SERVER, "2");
option_->put(PREF_DIR, "/tmp");
option_->put(PREF_OUT, "file.out");
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForUri(result, option_, uris);
CPPUNIT_ASSERT_EQUAL((size_t)1, result.size());
std::shared_ptr<RequestGroup> group = result[0];
auto xuris = group->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL((size_t)6, xuris.size());
for (size_t i = 0; i < 6; ++i) {
CPPUNIT_ASSERT_EQUAL(uris[i % 3], xuris[i]);
}
CPPUNIT_ASSERT_EQUAL(7, group->getNumConcurrentCommand());
std::shared_ptr<DownloadContext> ctx = group->getDownloadContext();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getBasePath());
}
option_->put(PREF_SPLIT, "5");
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForUri(result, option_, uris);
CPPUNIT_ASSERT_EQUAL((size_t)1, result.size());
std::shared_ptr<RequestGroup> group = result[0];
auto xuris = group->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL((size_t)5, xuris.size());
for (size_t i = 0; i < 5; ++i) {
CPPUNIT_ASSERT_EQUAL(uris[i % 3], xuris[i]);
}
}
option_->put(PREF_SPLIT, "2");
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForUri(result, option_, uris);
CPPUNIT_ASSERT_EQUAL((size_t)1, result.size());
std::shared_ptr<RequestGroup> group = result[0];
auto xuris = group->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL((size_t)3, xuris.size());
for (size_t i = 0; i < 3; ++i) {
CPPUNIT_ASSERT_EQUAL(uris[i % 3], xuris[i]);
}
}
option_->put(PREF_FORCE_SEQUENTIAL, A2_V_TRUE);
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForUri(result, option_, uris);
CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
// for alpha server
std::shared_ptr<RequestGroup> alphaGroup = result[0];
auto alphaURIs =
alphaGroup->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL((size_t)2, alphaURIs.size());
for (size_t i = 0; i < 2; ++i) {
CPPUNIT_ASSERT_EQUAL(uris[0], alphaURIs[i]);
}
CPPUNIT_ASSERT_EQUAL(2, alphaGroup->getNumConcurrentCommand());
std::shared_ptr<DownloadContext> alphaCtx =
alphaGroup->getDownloadContext();
// See filename is not assigned yet
CPPUNIT_ASSERT_EQUAL(std::string(""), alphaCtx->getBasePath());
}
}
void DownloadHelperTest::testCreateRequestGroupForUri_parameterized()
{
std::vector<std::string> uris{"http://{alpha, bravo}/file",
"http://charlie/file"};
option_->put(PREF_MAX_CONNECTION_PER_SERVER, "1");
option_->put(PREF_SPLIT, "3");
option_->put(PREF_DIR, "/tmp");
option_->put(PREF_OUT, "file.out");
option_->put(PREF_PARAMETERIZED_URI, A2_V_TRUE);
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForUri(result, option_, uris);
CPPUNIT_ASSERT_EQUAL((size_t)1, result.size());
std::shared_ptr<RequestGroup> group = result[0];
auto uris = group->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL((size_t)3, uris.size());
CPPUNIT_ASSERT_EQUAL(std::string("http://alpha/file"), uris[0]);
CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"), uris[1]);
CPPUNIT_ASSERT_EQUAL(std::string("http://charlie/file"), uris[2]);
CPPUNIT_ASSERT_EQUAL(3, group->getNumConcurrentCommand());
std::shared_ptr<DownloadContext> ctx = group->getDownloadContext();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getBasePath());
}
}
#ifdef ENABLE_BITTORRENT
void DownloadHelperTest::testCreateRequestGroupForUri_BitTorrent()
{
std::vector<std::string> uris{"http://alpha/file",
A2_TEST_DIR "/test.torrent",
"http://bravo/file", "http://charlie/file"};
option_->put(PREF_MAX_CONNECTION_PER_SERVER, "1");
option_->put(PREF_SPLIT, "3");
option_->put(PREF_DIR, "/tmp");
option_->put(PREF_OUT, "file.out");
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForUri(result, option_, uris);
CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
std::shared_ptr<RequestGroup> group = result[0];
auto xuris = group->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL((size_t)3, xuris.size());
CPPUNIT_ASSERT_EQUAL(uris[0], xuris[0]);
CPPUNIT_ASSERT_EQUAL(uris[2], xuris[1]);
CPPUNIT_ASSERT_EQUAL(uris[3], xuris[2]);
CPPUNIT_ASSERT_EQUAL(3, group->getNumConcurrentCommand());
std::shared_ptr<DownloadContext> ctx = group->getDownloadContext();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getBasePath());
std::shared_ptr<RequestGroup> torrentGroup = result[1];
auto auxURIs =
torrentGroup->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT(auxURIs.empty());
CPPUNIT_ASSERT_EQUAL(3, torrentGroup->getNumConcurrentCommand());
std::shared_ptr<DownloadContext> btctx = torrentGroup->getDownloadContext();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-test"), btctx->getBasePath());
}
}
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
void DownloadHelperTest::testCreateRequestGroupForUri_Metalink()
{
std::vector<std::string> uris{"http://alpha/file", "http://bravo/file",
"http://charlie/file", A2_TEST_DIR "/test.xml"};
option_->put(PREF_MAX_CONNECTION_PER_SERVER, "1");
option_->put(PREF_SPLIT, "2");
option_->put(PREF_DIR, "/tmp");
option_->put(PREF_OUT, "file.out");
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForUri(result, option_, uris);
// group1: http://alpha/file, ...
// group2-7: 6 file entry in Metalink and 1 torrent file download
#ifdef ENABLE_BITTORRENT
CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
#else // !ENABLE_BITTORRENT
CPPUNIT_ASSERT_EQUAL((size_t)6, result.size());
#endif // !ENABLE_BITTORRENT
std::shared_ptr<RequestGroup> group = result[0];
auto xuris = group->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL((size_t)3, xuris.size());
for (size_t i = 0; i < 3; ++i) {
CPPUNIT_ASSERT_EQUAL(uris[i], xuris[i]);
}
CPPUNIT_ASSERT_EQUAL(2, group->getNumConcurrentCommand());
std::shared_ptr<DownloadContext> ctx = group->getDownloadContext();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getBasePath());
std::shared_ptr<RequestGroup> aria2052Group = result[1];
CPPUNIT_ASSERT_EQUAL(1, // because of maxconnections attribute
aria2052Group->getNumConcurrentCommand());
std::shared_ptr<DownloadContext> aria2052Ctx =
aria2052Group->getDownloadContext();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-0.5.2.tar.bz2"),
aria2052Ctx->getBasePath());
std::shared_ptr<RequestGroup> aria2051Group = result[2];
CPPUNIT_ASSERT_EQUAL(2, aria2051Group->getNumConcurrentCommand());
}
}
#endif // ENABLE_METALINK
void DownloadHelperTest::testCreateRequestGroupForUriList()
{
option_->put(PREF_MAX_CONNECTION_PER_SERVER, "3");
option_->put(PREF_SPLIT, "3");
option_->put(PREF_INPUT_FILE, A2_TEST_DIR "/input_uris.txt");
option_->put(PREF_DIR, "/tmp");
option_->put(PREF_OUT, "file.out");
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForUriList(result, option_);
CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
std::shared_ptr<RequestGroup> fileGroup = result[0];
auto fileURIs =
fileGroup->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL(std::string("http://alpha/file"), fileURIs[0]);
CPPUNIT_ASSERT_EQUAL(std::string("http://bravo/file"), fileURIs[1]);
CPPUNIT_ASSERT_EQUAL(std::string("http://charlie/file"), fileURIs[2]);
CPPUNIT_ASSERT_EQUAL(3, fileGroup->getNumConcurrentCommand());
std::shared_ptr<DownloadContext> fileCtx = fileGroup->getDownloadContext();
CPPUNIT_ASSERT_EQUAL(std::string("/mydownloads/myfile.out"),
fileCtx->getBasePath());
std::shared_ptr<RequestGroup> fileISOGroup = result[1];
std::shared_ptr<DownloadContext> fileISOCtx =
fileISOGroup->getDownloadContext();
// PREF_OUT in option_ must be ignored.
CPPUNIT_ASSERT_EQUAL(std::string(), fileISOCtx->getBasePath());
}
#ifdef ENABLE_BITTORRENT
void DownloadHelperTest::testCreateRequestGroupForBitTorrent()
{
std::vector<std::string> auxURIs{"http://alpha/file", "http://bravo/file",
"http://charlie/file"};
option_->put(PREF_MAX_CONNECTION_PER_SERVER, "2");
option_->put(PREF_SPLIT, "5");
option_->put(PREF_TORRENT_FILE, A2_TEST_DIR "/test.torrent");
option_->put(PREF_DIR, "/tmp");
option_->put(PREF_OUT, "file.out");
option_->put(PREF_BT_EXCLUDE_TRACKER, "http://tracker1");
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForBitTorrent(result, option_, auxURIs,
option_->get(PREF_TORRENT_FILE));
CPPUNIT_ASSERT_EQUAL((size_t)1, result.size());
std::shared_ptr<RequestGroup> group = result[0];
auto uris = group->getDownloadContext()->getFirstFileEntry()->getUris();
std::sort(std::begin(uris), std::end(uris));
// See -s option is ignored. See processRootDictionary() in
// bittorrent_helper.cc
CPPUNIT_ASSERT_EQUAL((size_t)3, uris.size());
for (size_t i = 0; i < auxURIs.size(); ++i) {
CPPUNIT_ASSERT_EQUAL(auxURIs[i] + "/aria2-test/aria2/src/aria2c",
uris[i]);
}
CPPUNIT_ASSERT_EQUAL(5, group->getNumConcurrentCommand());
auto attrs = bittorrent::getTorrentAttrs(group->getDownloadContext());
// http://tracker1 was deleted.
CPPUNIT_ASSERT_EQUAL((size_t)2, attrs->announceList.size());
}
{
// no URIs are given
std::vector<std::shared_ptr<RequestGroup>> result;
std::vector<std::string> emptyURIs;
createRequestGroupForBitTorrent(result, option_, emptyURIs,
option_->get(PREF_TORRENT_FILE));
CPPUNIT_ASSERT_EQUAL((size_t)1, result.size());
std::shared_ptr<RequestGroup> group = result[0];
auto uris = group->getDownloadContext()->getFirstFileEntry()->getUris();
CPPUNIT_ASSERT_EQUAL((size_t)0, uris.size());
}
option_->put(PREF_FORCE_SEQUENTIAL, A2_V_TRUE);
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForBitTorrent(result, option_, auxURIs,
option_->get(PREF_TORRENT_FILE));
// See --force-requencial is ignored
CPPUNIT_ASSERT_EQUAL((size_t)1, result.size());
}
}
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
void DownloadHelperTest::testCreateRequestGroupForMetalink()
{
option_->put(PREF_SPLIT, "5");
option_->put(PREF_METALINK_FILE, A2_TEST_DIR "/test.xml");
option_->put(PREF_DIR, "/tmp");
option_->put(PREF_OUT, "file.out");
{
std::vector<std::shared_ptr<RequestGroup>> result;
createRequestGroupForMetalink(result, option_);
#ifdef ENABLE_BITTORRENT
CPPUNIT_ASSERT_EQUAL((size_t)6, result.size());
#else // !ENABLE_BITTORRENT
CPPUNIT_ASSERT_EQUAL((size_t)5, result.size());
#endif // !ENABLE_BITTORRENT
std::shared_ptr<RequestGroup> group = result[0];
auto uris = group->getDownloadContext()->getFirstFileEntry()->getUris();
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]);
// See numConcurrentCommand is 1 because of maxconnections attribute.
CPPUNIT_ASSERT_EQUAL(1, group->getNumConcurrentCommand());
}
}
#endif // ENABLE_METALINK
} // namespace aria2