1 #include "DownloadContext.h"
2
3 #include <cppunit/extensions/HelperMacros.h>
4
5 #include "FileEntry.h"
6 #include "array_fun.h"
7
8 namespace aria2 {
9
10 class DownloadContextTest : public CppUnit::TestFixture {
11
12 CPPUNIT_TEST_SUITE(DownloadContextTest);
13 CPPUNIT_TEST(testFindFileEntryByOffset);
14 CPPUNIT_TEST(testGetPieceHash);
15 CPPUNIT_TEST(testGetNumPieces);
16 CPPUNIT_TEST(testGetBasePath);
17 CPPUNIT_TEST(testSetFileFilter);
18 CPPUNIT_TEST_SUITE_END();
19
20 public:
21 void testFindFileEntryByOffset();
22 void testGetPieceHash();
23 void testGetNumPieces();
24 void testGetBasePath();
25 void testSetFileFilter();
26 };
27
28 CPPUNIT_TEST_SUITE_REGISTRATION(DownloadContextTest);
29
testFindFileEntryByOffset()30 void DownloadContextTest::testFindFileEntryByOffset()
31 {
32 DownloadContext ctx;
33
34 CPPUNIT_ASSERT(!ctx.findFileEntryByOffset(0));
35
36 const std::shared_ptr<FileEntry> fileEntries[] = {
37 std::shared_ptr<FileEntry>(new FileEntry("file1", 1000, 0)),
38 std::shared_ptr<FileEntry>(new FileEntry("file2", 0, 1000)),
39 std::shared_ptr<FileEntry>(new FileEntry("file3", 0, 1000)),
40 std::shared_ptr<FileEntry>(new FileEntry("file4", 2000, 1000)),
41 std::shared_ptr<FileEntry>(new FileEntry("file5", 3000, 3000)),
42 std::shared_ptr<FileEntry>(new FileEntry("file6", 0, 6000))};
43 ctx.setFileEntries(std::begin(fileEntries), std::end(fileEntries));
44
45 CPPUNIT_ASSERT_EQUAL(std::string("file1"),
46 ctx.findFileEntryByOffset(0)->getPath());
47 CPPUNIT_ASSERT_EQUAL(std::string("file4"),
48 ctx.findFileEntryByOffset(1500)->getPath());
49 CPPUNIT_ASSERT_EQUAL(std::string("file5"),
50 ctx.findFileEntryByOffset(5999)->getPath());
51 CPPUNIT_ASSERT(!ctx.findFileEntryByOffset(6000));
52 }
53
testGetPieceHash()54 void DownloadContextTest::testGetPieceHash()
55 {
56 DownloadContext ctx;
57 const std::string pieceHashes[] = {"hash1", "hash2", "shash3"};
58 ctx.setPieceHashes("sha-1", &pieceHashes[0], &pieceHashes[3]);
59 CPPUNIT_ASSERT_EQUAL(std::string("hash1"), ctx.getPieceHash(0));
60 CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getPieceHash(3));
61 }
62
testGetNumPieces()63 void DownloadContextTest::testGetNumPieces()
64 {
65 DownloadContext ctx(345, 9889, "");
66 CPPUNIT_ASSERT_EQUAL((size_t)29, ctx.getNumPieces());
67 }
68
testGetBasePath()69 void DownloadContextTest::testGetBasePath()
70 {
71 DownloadContext ctx(0, 0, "");
72 CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getBasePath());
73 ctx.getFirstFileEntry()->setPath("aria2.tar.bz2");
74 CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getBasePath());
75 }
76
testSetFileFilter()77 void DownloadContextTest::testSetFileFilter()
78 {
79 DownloadContext ctx;
80 std::vector<std::shared_ptr<FileEntry>> files;
81 for (int i = 0; i < 10; ++i) {
82 files.push_back(std::shared_ptr<FileEntry>(new FileEntry("file", 1, i)));
83 }
84 ctx.setFileEntries(files.begin(), files.end());
85 auto sgl = util::parseIntSegments("6-8,2-4");
86 sgl.normalize();
87 ctx.setFileFilter(std::move(sgl));
88 const std::vector<std::shared_ptr<FileEntry>>& res = ctx.getFileEntries();
89 CPPUNIT_ASSERT(!res[0]->isRequested());
90 CPPUNIT_ASSERT(res[1]->isRequested());
91 CPPUNIT_ASSERT(res[2]->isRequested());
92 CPPUNIT_ASSERT(res[3]->isRequested());
93 CPPUNIT_ASSERT(!res[4]->isRequested());
94 CPPUNIT_ASSERT(res[5]->isRequested());
95 CPPUNIT_ASSERT(res[6]->isRequested());
96 CPPUNIT_ASSERT(res[7]->isRequested());
97 CPPUNIT_ASSERT(!res[8]->isRequested());
98 CPPUNIT_ASSERT(!res[9]->isRequested());
99 }
100
101 } // namespace aria2
102