MetalinkEntryTest.cc 4.7 KB

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