1 #include "MetalinkEntry.h"
2 
3 #include <cppunit/extensions/HelperMacros.h>
4 
5 #include "MetalinkResource.h"
6 #include "a2functional.h"
7 
8 namespace aria2 {
9 
10 class MetalinkEntryTest : public CppUnit::TestFixture {
11 
12   CPPUNIT_TEST_SUITE(MetalinkEntryTest);
13   CPPUNIT_TEST(testDropUnsupportedResource);
14   CPPUNIT_TEST(testReorderResourcesByPriority);
15   CPPUNIT_TEST(testSetLocationPriority);
16   CPPUNIT_TEST(testSetProtocolPriority);
17   CPPUNIT_TEST_SUITE_END();
18 
19 private:
20 public:
setUp()21   void setUp() {}
tearDown()22   void tearDown() {}
23 
24   void testDropUnsupportedResource();
25   void testReorderResourcesByPriority();
26   void testSetLocationPriority();
27   void testSetProtocolPriority();
28 };
29 
30 CPPUNIT_TEST_SUITE_REGISTRATION(MetalinkEntryTest);
31 
createTestEntry()32 std::unique_ptr<MetalinkEntry> createTestEntry()
33 {
34   auto entry = make_unique<MetalinkEntry>();
35   auto res1 = make_unique<MetalinkResource>();
36   res1->url = "ftp://myhost/aria2.tar.bz2";
37   res1->type = MetalinkResource::TYPE_FTP;
38   res1->location = "ro";
39   res1->priority = 50;
40   auto res2 = make_unique<MetalinkResource>();
41   res2->url = "http://myhost/aria2.tar.bz2";
42   res2->type = MetalinkResource::TYPE_HTTP;
43   res2->location = "at";
44   res2->priority = 1;
45   auto res3 = make_unique<MetalinkResource>();
46   res3->url = "http://myhost/aria2.torrent";
47   res3->type = MetalinkResource::TYPE_BITTORRENT;
48   res3->location = "al";
49   res3->priority = 40;
50   auto res4 = make_unique<MetalinkResource>();
51   res4->url = "http://myhost/aria2.ext";
52   res4->type = MetalinkResource::TYPE_NOT_SUPPORTED;
53   res4->location = "ad";
54   res4->priority = 90;
55   auto res5 = make_unique<MetalinkResource>();
56   res5->url = "https://myhost/aria2.tar.bz2";
57   res5->type = MetalinkResource::TYPE_HTTPS;
58   res5->location = "jp";
59   res5->priority = 10;
60 
61   entry->resources.push_back(std::move(res1));
62   entry->resources.push_back(std::move(res2));
63   entry->resources.push_back(std::move(res3));
64   entry->resources.push_back(std::move(res4));
65   entry->resources.push_back(std::move(res5));
66   return entry;
67 }
68 
testDropUnsupportedResource()69 void MetalinkEntryTest::testDropUnsupportedResource()
70 {
71   auto entry = createTestEntry();
72 
73   entry->dropUnsupportedResource();
74 #if defined(ENABLE_SSL) && defined(ENABLE_BITTORRENT)
75   CPPUNIT_ASSERT_EQUAL((size_t)4, entry->resources.size());
76 #elif defined(ENABLE_SSL) || defined(ENABLE_BITTORRENT)
77   CPPUNIT_ASSERT_EQUAL((size_t)3, entry->resources.size());
78 #else  // defined(ENABLE_SSL) || defined(ENABLE_BITTORRENT)
79   CPPUNIT_ASSERT_EQUAL((size_t)2, entry->resources.size());
80 #endif // defined(ENABLE_SSL) || defined(ENABLE_BITTORRENT)
81 
82   auto itr = std::begin(entry->resources);
83   CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_FTP, (*itr++)->type);
84   CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_HTTP, (*itr++)->type);
85 #ifdef ENABLE_BITTORRENT
86   CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_BITTORRENT, (*itr++)->type);
87 #endif // ENABLE_BITTORRENT
88 #ifdef ENABLE_SSL
89   CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_HTTPS, (*itr++)->type);
90 #endif // ENABLE_SSL
91 }
92 
testReorderResourcesByPriority()93 void MetalinkEntryTest::testReorderResourcesByPriority()
94 {
95   auto entry = createTestEntry();
96 
97   entry->reorderResourcesByPriority();
98 
99   CPPUNIT_ASSERT_EQUAL(1, entry->resources.at(0)->priority);
100   CPPUNIT_ASSERT_EQUAL(10, entry->resources.at(1)->priority);
101   CPPUNIT_ASSERT_EQUAL(40, entry->resources.at(2)->priority);
102   CPPUNIT_ASSERT_EQUAL(50, entry->resources.at(3)->priority);
103   CPPUNIT_ASSERT_EQUAL(90, entry->resources.at(4)->priority);
104 }
105 
testSetLocationPriority()106 void MetalinkEntryTest::testSetLocationPriority()
107 {
108   auto entry = createTestEntry();
109 
110   auto locations = std::vector<std::string>{"jp", "al", "ro"};
111 
112   entry->setLocationPriority(locations, -100);
113 
114   CPPUNIT_ASSERT_EQUAL(std::string("ro"), entry->resources[0]->location);
115   CPPUNIT_ASSERT_EQUAL(-50, entry->resources[0]->priority);
116   CPPUNIT_ASSERT_EQUAL(std::string("at"), entry->resources[1]->location);
117   CPPUNIT_ASSERT_EQUAL(1, entry->resources[1]->priority);
118   CPPUNIT_ASSERT_EQUAL(std::string("al"), entry->resources[2]->location);
119   CPPUNIT_ASSERT_EQUAL(-60, entry->resources[2]->priority);
120   CPPUNIT_ASSERT_EQUAL(std::string("ad"), entry->resources[3]->location);
121   CPPUNIT_ASSERT_EQUAL(90, entry->resources[3]->priority);
122   CPPUNIT_ASSERT_EQUAL(std::string("jp"), entry->resources[4]->location);
123   CPPUNIT_ASSERT_EQUAL(-90, entry->resources[4]->priority);
124 }
125 
testSetProtocolPriority()126 void MetalinkEntryTest::testSetProtocolPriority()
127 {
128   auto entry = createTestEntry();
129   entry->setProtocolPriority("http", -1);
130   CPPUNIT_ASSERT_EQUAL(50, entry->resources[0]->priority); // ftp
131   CPPUNIT_ASSERT_EQUAL(0, entry->resources[1]->priority);  // http, -1
132   CPPUNIT_ASSERT_EQUAL(40, entry->resources[2]->priority); // bittorrent
133   CPPUNIT_ASSERT_EQUAL(90, entry->resources[3]->priority); // not supported
134   CPPUNIT_ASSERT_EQUAL(10, entry->resources[4]->priority); // https
135 }
136 
137 } // namespace aria2
138