File: DHTPeerAnnounceEntryTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (113 lines) | stat: -rw-r--r-- 3,391 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "DHTPeerAnnounceEntry.h"

#include <cstring>

#include <cppunit/extensions/HelperMacros.h>

#include "Exception.h"
#include "util.h"
#include "FileEntry.h"
#include "Peer.h"

namespace aria2 {

class DHTPeerAnnounceEntryTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(DHTPeerAnnounceEntryTest);
  CPPUNIT_TEST(testRemoveStalePeerAddrEntry);
  CPPUNIT_TEST(testEmpty);
  CPPUNIT_TEST(testAddPeerAddrEntry);
  CPPUNIT_TEST(testGetPeers);
  CPPUNIT_TEST_SUITE_END();

public:
  void testRemoveStalePeerAddrEntry();
  void testEmpty();
  void testAddPeerAddrEntry();
  void testGetPeers();
};

CPPUNIT_TEST_SUITE_REGISTRATION(DHTPeerAnnounceEntryTest);

void DHTPeerAnnounceEntryTest::testRemoveStalePeerAddrEntry()
{
  unsigned char infohash[DHT_ID_LENGTH];
  memset(infohash, 0xff, DHT_ID_LENGTH);
  DHTPeerAnnounceEntry entry(infohash);

  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));
  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882, Timer::zero()));
  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.3", 6883));
  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.4", 6884, Timer::zero()));

  entry.removeStalePeerAddrEntry(10_s);

  CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());

  const std::vector<PeerAddrEntry>& peerAddrEntries =
      entry.getPeerAddrEntries();
  CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"),
                       peerAddrEntries[0].getIPAddress());
  CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"),
                       peerAddrEntries[1].getIPAddress());
}

void DHTPeerAnnounceEntryTest::testEmpty()
{
  unsigned char infohash[DHT_ID_LENGTH];
  memset(infohash, 0xff, DHT_ID_LENGTH);
  {
    DHTPeerAnnounceEntry entry(infohash);
    entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));
    CPPUNIT_ASSERT(!entry.empty());
  }
  {
    DHTPeerAnnounceEntry entry(infohash);
    CPPUNIT_ASSERT(entry.empty());
  }
}

void DHTPeerAnnounceEntryTest::testAddPeerAddrEntry()
{
  unsigned char infohash[DHT_ID_LENGTH];
  memset(infohash, 0xff, DHT_ID_LENGTH);

  DHTPeerAnnounceEntry entry(infohash);
  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Timer::zero()));
  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6882));

  CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());

  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881));

  CPPUNIT_ASSERT_EQUAL((size_t)2, entry.countPeerAddrEntry());
  CPPUNIT_ASSERT(!entry.getPeerAddrEntries()[0].getLastUpdated().isZero());
}

void DHTPeerAnnounceEntryTest::testGetPeers()
{
  unsigned char infohash[DHT_ID_LENGTH];
  memset(infohash, 0xff, DHT_ID_LENGTH);

  DHTPeerAnnounceEntry entry(infohash);
  {
    std::vector<std::shared_ptr<Peer>> peers;
    entry.getPeers(peers);
    CPPUNIT_ASSERT_EQUAL((size_t)0, peers.size());
  }

  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.1", 6881, Timer::zero()));
  entry.addPeerAddrEntry(PeerAddrEntry("192.168.0.2", 6882));

  {
    std::vector<std::shared_ptr<Peer>> peers;
    entry.getPeers(peers);
    CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
    CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peers[0]->getIPAddress());
    CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peers[0]->getPort());
    CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), peers[1]->getIPAddress());
    CPPUNIT_ASSERT_EQUAL((uint16_t)6882, peers[1]->getPort());
  }
}

} // namespace aria2