DHTPeerAnnounceEntryTest.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "DHTPeerAnnounceEntry.h"
  2. #include <cstring>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "Exception.h"
  5. #include "util.h"
  6. #include "FileEntry.h"
  7. #include "Peer.h"
  8. namespace aria2 {
  9. class DHTPeerAnnounceEntryTest:public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(DHTPeerAnnounceEntryTest);
  11. CPPUNIT_TEST(testRemoveStalePeerAddrEntry);
  12. CPPUNIT_TEST(testEmpty);
  13. CPPUNIT_TEST(testAddPeerAddrEntry);
  14. CPPUNIT_TEST(testGetPeers);
  15. CPPUNIT_TEST_SUITE_END();
  16. public:
  17. void testRemoveStalePeerAddrEntry();
  18. void testEmpty();
  19. void testAddPeerAddrEntry();
  20. void testGetPeers();
  21. };
  22. CPPUNIT_TEST_SUITE_REGISTRATION(DHTPeerAnnounceEntryTest);
  23. void DHTPeerAnnounceEntryTest::testRemoveStalePeerAddrEntry()
  24. {
  25. unsigned char infohash[DHT_ID_LENGTH];
  26. memset(infohash, 0xff, DHT_ID_LENGTH);
  27. DHTPeerAnnounceEntry entry(infohash);
  28. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));
  29. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882, Timer(0)));
  30. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.3", 6883));
  31. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.4", 6884, Timer(0)));
  32. entry.removeStalePeerAddrEntry(10);
  33. CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());
  34. const std::vector<PeerAddrEntry>& peerAddrEntries =
  35. entry.getPeerAddrEntries();
  36. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peerAddrEntries[0].getIPAddress());
  37. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peerAddrEntries[1].getIPAddress());
  38. }
  39. void DHTPeerAnnounceEntryTest::testEmpty()
  40. {
  41. unsigned char infohash[DHT_ID_LENGTH];
  42. memset(infohash, 0xff, DHT_ID_LENGTH);
  43. {
  44. DHTPeerAnnounceEntry entry(infohash);
  45. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));
  46. CPPUNIT_ASSERT(!entry.empty());
  47. }
  48. {
  49. DHTPeerAnnounceEntry entry(infohash);
  50. CPPUNIT_ASSERT(entry.empty());
  51. }
  52. }
  53. void DHTPeerAnnounceEntryTest::testAddPeerAddrEntry()
  54. {
  55. unsigned char infohash[DHT_ID_LENGTH];
  56. memset(infohash, 0xff, DHT_ID_LENGTH);
  57. DHTPeerAnnounceEntry entry(infohash);
  58. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Timer(0)));
  59. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6882));
  60. CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());
  61. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));
  62. CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());
  63. CPPUNIT_ASSERT(0 != entry.getPeerAddrEntries()[0].getLastUpdated().getTime());
  64. }
  65. void DHTPeerAnnounceEntryTest::testGetPeers()
  66. {
  67. unsigned char infohash[DHT_ID_LENGTH];
  68. memset(infohash, 0xff, DHT_ID_LENGTH);
  69. DHTPeerAnnounceEntry entry(infohash);
  70. {
  71. std::vector<std::shared_ptr<Peer> > peers;
  72. entry.getPeers(peers);
  73. CPPUNIT_ASSERT_EQUAL((size_t)0, peers.size());
  74. }
  75. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Timer(0)));
  76. entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882));
  77. {
  78. std::vector<std::shared_ptr<Peer> > peers;
  79. entry.getPeers(peers);
  80. CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
  81. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peers[0]->getIPAddress());
  82. CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->getPort());
  83. CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peers[1]->getIPAddress());
  84. CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->getPort());
  85. }
  86. }
  87. } // namespace aria2