BtRegistryTest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. SharedHandle<DownloadContext> dctx(new DownloadContext());
  36. SharedHandle<BtObject> btObject(new BtObject());
  37. btObject->downloadContext = dctx;
  38. btRegistry.put(1, btObject);
  39. CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext(1).get());
  40. }
  41. namespace {
  42. void addTwoDownloadContext(BtRegistry& btRegistry)
  43. {
  44. SharedHandle<DownloadContext> dctx1(new DownloadContext());
  45. SharedHandle<DownloadContext> dctx2(new DownloadContext());
  46. SharedHandle<BtObject> btObject1(new BtObject());
  47. btObject1->downloadContext = dctx1;
  48. SharedHandle<BtObject> btObject2(new BtObject());
  49. btObject2->downloadContext = dctx2;
  50. btRegistry.put(1, btObject1);
  51. btRegistry.put(2, btObject2);
  52. }
  53. } // namespace
  54. void BtRegistryTest::testGetDownloadContext_infoHash()
  55. {
  56. BtRegistry btRegistry;
  57. addTwoDownloadContext(btRegistry);
  58. SharedHandle<TorrentAttribute> attrs1(new TorrentAttribute());
  59. attrs1->infoHash = "hash1";
  60. SharedHandle<TorrentAttribute> attrs2(new TorrentAttribute());
  61. attrs2->infoHash = "hash2";
  62. btRegistry.getDownloadContext(1)->setAttribute(CTX_ATTR_BT, attrs1);
  63. btRegistry.getDownloadContext(2)->setAttribute(CTX_ATTR_BT, attrs2);
  64. CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1"));
  65. CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1").get() ==
  66. btRegistry.getDownloadContext(1).get());
  67. CPPUNIT_ASSERT(!btRegistry.getDownloadContext("not exists"));
  68. }
  69. void BtRegistryTest::testGetAllDownloadContext()
  70. {
  71. BtRegistry btRegistry;
  72. addTwoDownloadContext(btRegistry);
  73. std::vector<SharedHandle<DownloadContext> > result;
  74. btRegistry.getAllDownloadContext(std::back_inserter(result));
  75. CPPUNIT_ASSERT_EQUAL((size_t)2, result.size());
  76. }
  77. void BtRegistryTest::testRemove()
  78. {
  79. BtRegistry btRegistry;
  80. addTwoDownloadContext(btRegistry);
  81. CPPUNIT_ASSERT(btRegistry.remove(1));
  82. CPPUNIT_ASSERT(!btRegistry.get(1));
  83. CPPUNIT_ASSERT(btRegistry.get(2));
  84. }
  85. void BtRegistryTest::testRemoveAll()
  86. {
  87. BtRegistry btRegistry;
  88. addTwoDownloadContext(btRegistry);
  89. btRegistry.removeAll();
  90. CPPUNIT_ASSERT(!btRegistry.get(1));
  91. CPPUNIT_ASSERT(!btRegistry.get(2));
  92. }
  93. } // namespace aria2