MetalinkEntryTest.cc 4.9 KB

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