MetalinkEntryTest.cc 4.8 KB

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