1
+ // Copyright (c) 2012-2013 The Bitcoin Core developers
2
+ // Distributed under the MIT software license, see the accompanying
3
+ // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
+ #include " addrman.h"
5
+ #include " test/test_bitcoin.h"
6
+ #include < string>
7
+ #include < boost/test/unit_test.hpp>
8
+
9
+ #include " random.h"
10
+
11
+ using namespace std ;
12
+
13
+ class CAddrManTest : public CAddrMan {};
14
+
15
+ BOOST_FIXTURE_TEST_SUITE (addrman_tests, BasicTestingSetup)
16
+
17
+ BOOST_AUTO_TEST_CASE(addrman_simple)
18
+ {
19
+ CAddrManTest addrman;
20
+
21
+ // Set addrman addr placement to be deterministic.
22
+ addrman.MakeDeterministic ();
23
+
24
+ CNetAddr source = CNetAddr (" 252.2.2.2:8333" );
25
+
26
+ // Test 1: Does Addrman respond correctly when empty.
27
+ BOOST_CHECK (addrman.size () == 0 );
28
+ CAddrInfo addr_null = addrman.Select ();
29
+ BOOST_CHECK (addr_null.ToString () == " [::]:0" );
30
+
31
+ // Test 2: Does Addrman::Add work as expected.
32
+ CService addr1 = CService (" 250.1.1.1:8333" );
33
+ addrman.Add (CAddress (addr1), source);
34
+ BOOST_CHECK (addrman.size () == 1 );
35
+ CAddrInfo addr_ret1 = addrman.Select ();
36
+ BOOST_CHECK (addr_ret1.ToString () == " 250.1.1.1:8333" );
37
+
38
+ // Test 3: Does IP address deduplication work correctly.
39
+ // Expected dup IP should not be added.
40
+ CService addr1_dup = CService (" 250.1.1.1:8333" );
41
+ addrman.Add (CAddress (addr1_dup), source);
42
+ BOOST_CHECK (addrman.size () == 1 );
43
+
44
+
45
+ // Test 5: New table has one addr and we add a diff addr we should
46
+ // have two addrs.
47
+ CService addr2 = CService (" 250.1.1.2:8333" );
48
+ addrman.Add (CAddress (addr2), source);
49
+ BOOST_CHECK (addrman.size () == 2 );
50
+
51
+ // Test 6: AddrMan::Clear() should empty the new table.
52
+ addrman.Clear ();
53
+ BOOST_CHECK (addrman.size () == 0 );
54
+ CAddrInfo addr_null2 = addrman.Select ();
55
+ BOOST_CHECK (addr_null2.ToString () == " [::]:0" );
56
+ }
57
+
58
+ BOOST_AUTO_TEST_CASE (addrman_ports)
59
+ {
60
+ CAddrManTest addrman;
61
+
62
+ // Set addrman addr placement to be deterministic.
63
+ addrman.MakeDeterministic ();
64
+
65
+ CNetAddr source = CNetAddr (" 252.2.2.2:8333" );
66
+
67
+ BOOST_CHECK (addrman.size () == 0 );
68
+
69
+ // Test 7; Addr with same IP but diff port does not replace existing addr.
70
+ CService addr1 = CService (" 250.1.1.1:8333" );
71
+ addrman.Add (CAddress (addr1), source);
72
+ BOOST_CHECK (addrman.size () == 1 );
73
+
74
+ CService addr1_port = CService (" 250.1.1.1:8334" );
75
+ addrman.Add (CAddress (addr1_port), source);
76
+ BOOST_CHECK (addrman.size () == 1 );
77
+ CAddrInfo addr_ret2 = addrman.Select ();
78
+ BOOST_CHECK (addr_ret2.ToString () == " 250.1.1.1:8333" );
79
+
80
+ // Test 8: Add same IP but diff port to tried table, it doesn't get added.
81
+ // Perhaps this is not ideal behavior but it is the current behavior.
82
+ addrman.Good (CAddress (addr1_port));
83
+ BOOST_CHECK (addrman.size () == 1 );
84
+ bool newOnly = true ;
85
+ CAddrInfo addr_ret3 = addrman.Select (newOnly);
86
+ BOOST_CHECK (addr_ret3.ToString () == " 250.1.1.1:8333" );
87
+ }
88
+
89
+
90
+ BOOST_AUTO_TEST_CASE (addrman_select)
91
+ {
92
+ CAddrManTest addrman;
93
+
94
+ // Set addrman addr placement to be deterministic.
95
+ addrman.MakeDeterministic ();
96
+
97
+ CNetAddr source = CNetAddr (" 252.2.2.2:8333" );
98
+
99
+ // Test 9: Select from new with 1 addr in new.
100
+ CService addr1 = CService (" 250.1.1.1:8333" );
101
+ addrman.Add (CAddress (addr1), source);
102
+ BOOST_CHECK (addrman.size () == 1 );
103
+
104
+ bool newOnly = true ;
105
+ CAddrInfo addr_ret1 = addrman.Select (newOnly);
106
+ BOOST_CHECK (addr_ret1.ToString () == " 250.1.1.1:8333" );
107
+
108
+
109
+ // Test 10: move addr to tried, select from new expected nothing returned.
110
+ addrman.Good (CAddress (addr1));
111
+ BOOST_CHECK (addrman.size () == 1 );
112
+ CAddrInfo addr_ret2 = addrman.Select (newOnly);
113
+ BOOST_CHECK (addr_ret2.ToString () == " [::]:0" );
114
+
115
+ CAddrInfo addr_ret3 = addrman.Select ();
116
+ BOOST_CHECK (addr_ret3.ToString () == " 250.1.1.1:8333" );
117
+ }
118
+
119
+ BOOST_AUTO_TEST_CASE (addrman_new_collisions)
120
+ {
121
+ CAddrManTest addrman;
122
+
123
+ // Set addrman addr placement to be deterministic.
124
+ addrman.MakeDeterministic ();
125
+
126
+ CNetAddr source = CNetAddr (" 252.2.2.2:8333" );
127
+
128
+ BOOST_CHECK (addrman.size () == 0 );
129
+
130
+ for (unsigned int i = 1 ; i < 4 ; i++){
131
+ CService addr = CService (" 250.1.1." +boost::to_string (i));
132
+ addrman.Add (CAddress (addr), source);
133
+
134
+ // Test 11: No collision in new table yet.
135
+ BOOST_CHECK (addrman.size () == i);
136
+ }
137
+
138
+ // Test 12: new table collision!
139
+ CService addr1 = CService (" 250.1.1.4" );
140
+ addrman.Add (CAddress (addr1), source);
141
+ BOOST_CHECK (addrman.size () == 3 );
142
+
143
+ CService addr2 = CService (" 250.1.1.5" );
144
+ addrman.Add (CAddress (addr2), source);
145
+ BOOST_CHECK (addrman.size () == 4 );
146
+ }
147
+
148
+ BOOST_AUTO_TEST_CASE (addrman_tried_collisions)
149
+ {
150
+ CAddrManTest addrman;
151
+
152
+ // Set addrman addr placement to be deterministic.
153
+ addrman.MakeDeterministic ();
154
+
155
+ CNetAddr source = CNetAddr (" 252.2.2.2:8333" );
156
+
157
+ BOOST_CHECK (addrman.size () == 0 );
158
+
159
+ for (unsigned int i = 1 ; i < 75 ; i++){
160
+ CService addr = CService (" 250.1.1." +boost::to_string (i));
161
+ addrman.Add (CAddress (addr), source);
162
+ addrman.Good (CAddress (addr));
163
+
164
+ // Test 13: No collision in tried table yet.
165
+ BOOST_TEST_MESSAGE (addrman.size ());
166
+ BOOST_CHECK (addrman.size () == i);
167
+ }
168
+
169
+ // Test 14: tried table collision!
170
+ CService addr1 = CService (" 250.1.1.76" );
171
+ addrman.Add (CAddress (addr1), source);
172
+ BOOST_CHECK (addrman.size () == 74 );
173
+
174
+ CService addr2 = CService (" 250.1.1.77" );
175
+ addrman.Add (CAddress (addr2), source);
176
+ BOOST_CHECK (addrman.size () == 75 );
177
+ }
178
+
179
+
180
+ BOOST_AUTO_TEST_SUITE_END ()
0 commit comments