BtRegistryTest.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "BtRegistry.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "Exception.h"
  4. #include "DownloadContext.h"
  5. #include "MockPeerStorage.h"
  6. #include "MockPieceStorage.h"
  7. #include "MockBtAnnounce.h"
  8. #include "MockBtProgressInfoFile.h"
  9. #include "BtRuntime.h"
  10. #include "FileEntry.h"
  11. #include "bittorrent_helper.h"
  12. #include "UDPTrackerRequest.h"
  13. namespace aria2 {
  14. class BtRegistryTest:public CppUnit::TestFixture {
  15. CPPUNIT_TEST_SUITE(BtRegistryTest);
  16. CPPUNIT_TEST(testGetDownloadContext);
  17. CPPUNIT_TEST(testGetDownloadContext_infoHash);
  18. CPPUNIT_TEST(testGetAllDownloadContext);
  19. CPPUNIT_TEST(testRemove);
  20. CPPUNIT_TEST(testRemoveAll);
  21. CPPUNIT_TEST_SUITE_END();
  22. private:
  23. public:
  24. void testGetDownloadContext();
  25. void testGetDownloadContext_infoHash();
  26. void testGetAllDownloadContext();
  27. void testRemove();
  28. void testRemoveAll();
  29. };
  30. CPPUNIT_TEST_SUITE_REGISTRATION( BtRegistryTest );
  31. void BtRegistryTest::testGetDownloadContext()
  32. {
  33. BtRegistry btRegistry;
  34. CPPUNIT_ASSERT(!btRegistry.getDownloadContext(1));
  35. auto dctx = std::make_shared<DownloadContext>();
  36. auto btObject = make_unique<BtObject>();
  37. btObject->downloadContext = dctx;
  38. btRegistry.put(1, std::move(btObject));
  39. CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext(1).get());
  40. }
  41. namespace {
  42. void addTwoDownloadContext(BtRegistry& btRegistry)
  43. {
  44. auto dctx1 = std::make_shared<DownloadContext>();
  45. auto dctx2 = std::make_shared<DownloadContext>();
  46. auto btObject1 = make_unique<BtObject>();
  47. btObject1->downloadContext = dctx1;
  48. auto btObject2 = make_unique<BtObject>();
  49. btObject2->downloadContext = dctx2;
  50. btRegistry.put(1, std::move(btObject1));
  51. btRegistry.put(2, std::move(btObject2));
  52. }
  53. } // namespace
  54. void BtRegistryTest::testGetDownloadContext_infoHash()
  55. {
  56. BtRegistry btRegistry;
  57. addTwoDownloadContext(btRegistry);
  58. {
  59. auto attrs1 = make_unique<TorrentAttribute>();
  60. attrs1->infoHash = "hash1";
  61. auto attrs2 = make_unique<TorrentAttribute>();
  62. attrs2->infoHash = "hash2";
  63. btRegistry.getDownloadContext(1)->setAttribute(CTX_ATTR_BT,
  64. std::move(attrs1));
  65. btRegistry.getDownloadContext(2)->setAttribute(CTX_ATTR_BT,
  66. std::move(attrs2));
  67. }
  68. CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1"));
  69. CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1").get() ==
  70. btRegistry.getDownloadContext(1).get());
  71. CPPUNIT_ASSERT(!btRegistry.getDownloadContext("not exists"));
  72. }
  73. void BtRegistryTest::testGetAllDownloadContext()
  74. {
  75. BtRegistry btRegistry;
  76. addTwoDownloadContext(btRegistry);
  77. std::vector<std::shared_ptr<DownloadContext> > result;
  78. btRegistry.getAllDownloadContext(std::back_inserter(result));
  79. CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
  80. }
  81. void BtRegistryTest::testRemove()
  82. {
  83. BtRegistry btRegistry;
  84. addTwoDownloadContext(btRegistry);
  85. CPPUNIT_ASSERT(btRegistry.remove(1));
  86. CPPUNIT_ASSERT(!btRegistry.get(1));
  87. CPPUNIT_ASSERT(btRegistry.get(2));
  88. }
  89. void BtRegistryTest::testRemoveAll()
  90. {
  91. BtRegistry btRegistry;
  92. addTwoDownloadContext(btRegistry);
  93. btRegistry.removeAll();
  94. CPPUNIT_ASSERT(!btRegistry.get(1));
  95. CPPUNIT_ASSERT(!btRegistry.get(2));
  96. }
  97. } // namespace aria2