]> Git Repo - VerusCoin.git/blob - src/test/addrman_tests.cpp
Squashed commit of the following:
[VerusCoin.git] / src / test / addrman_tests.cpp
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 "hash.h"
10 #include "random.h"
11
12 using namespace std;
13
14 class CAddrManTest : public CAddrMan
15 {
16     uint64_t state;
17
18 public:
19     CAddrManTest()
20     {
21         state = 1;
22     }
23
24     //! Ensure that bucket placement is always the same for testing purposes.
25     void MakeDeterministic()
26     {
27         nKey.SetNull();
28         seed_insecure_rand(true);
29     }
30
31     int RandomInt(int nMax)
32     {
33         state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash();
34         return (unsigned int)(state % nMax);
35     }
36
37     CAddrInfo* Find(const CNetAddr& addr, int* pnId = NULL)
38     {
39         return CAddrMan::Find(addr, pnId);
40     }
41
42     CAddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = NULL)
43     {
44         return CAddrMan::Create(addr, addrSource, pnId);
45     }
46
47     void Delete(int nId)
48     {
49         CAddrMan::Delete(nId);
50     }
51 };
52
53 BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
54
55 BOOST_AUTO_TEST_CASE(addrman_simple)
56 {
57     CAddrManTest addrman;
58
59     // Set addrman addr placement to be deterministic.
60     addrman.MakeDeterministic();
61
62     CNetAddr source = CNetAddr("252.2.2.2");
63
64     // Test 1: Does Addrman respond correctly when empty.
65     BOOST_CHECK(addrman.size() == 0);
66     CAddrInfo addr_null = addrman.Select();
67     BOOST_CHECK(addr_null.ToString() == "[::]:0");
68
69     // Test 2: Does Addrman::Add work as expected.
70     CService addr1 = CService("250.1.1.1", 8333);
71     addrman.Add(CAddress(addr1), source);
72     BOOST_CHECK(addrman.size() == 1);
73     CAddrInfo addr_ret1 = addrman.Select();
74     BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333");
75
76     // Test 3: Does IP address deduplication work correctly.
77     //  Expected dup IP should not be added.
78     CService addr1_dup = CService("250.1.1.1", 8333);
79     addrman.Add(CAddress(addr1_dup), source);
80     BOOST_CHECK(addrman.size() == 1);
81
82
83     // Test 5: New table has one addr and we add a diff addr we should
84     //  have two addrs.
85     CService addr2 = CService("250.1.1.2", 8333);
86     addrman.Add(CAddress(addr2), source);
87     BOOST_CHECK(addrman.size() == 2);
88
89     // Test 6: AddrMan::Clear() should empty the new table.
90     addrman.Clear();
91     BOOST_CHECK(addrman.size() == 0);
92     CAddrInfo addr_null2 = addrman.Select();
93     BOOST_CHECK(addr_null2.ToString() == "[::]:0");
94 }
95
96 BOOST_AUTO_TEST_CASE(addrman_ports)
97 {
98     CAddrManTest addrman;
99
100     // Set addrman addr placement to be deterministic.
101     addrman.MakeDeterministic();
102
103     CNetAddr source = CNetAddr("252.2.2.2");
104
105     BOOST_CHECK(addrman.size() == 0);
106
107     // Test 7; Addr with same IP but diff port does not replace existing addr.
108     CService addr1 = CService("250.1.1.1", 8333);
109     addrman.Add(CAddress(addr1), source);
110     BOOST_CHECK(addrman.size() == 1);
111
112     CService addr1_port = CService("250.1.1.1", 8334);
113     addrman.Add(CAddress(addr1_port), source);
114     BOOST_CHECK(addrman.size() == 1);
115     CAddrInfo addr_ret2 = addrman.Select();
116     BOOST_CHECK(addr_ret2.ToString() == "250.1.1.1:8333");
117
118     // Test 8: Add same IP but diff port to tried table, it doesn't get added.
119     //  Perhaps this is not ideal behavior but it is the current behavior.
120     addrman.Good(CAddress(addr1_port));
121     BOOST_CHECK(addrman.size() == 1);
122     bool newOnly = true;
123     CAddrInfo addr_ret3 = addrman.Select(newOnly);
124     BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333");
125 }
126
127
128 BOOST_AUTO_TEST_CASE(addrman_select)
129 {
130     CAddrManTest addrman;
131
132     // Set addrman addr placement to be deterministic.
133     addrman.MakeDeterministic();
134
135     CNetAddr source = CNetAddr("252.2.2.2");
136
137     // Test 9: Select from new with 1 addr in new.
138     CService addr1 = CService("250.1.1.1", 8333);
139     addrman.Add(CAddress(addr1), source);
140     BOOST_CHECK(addrman.size() == 1);
141
142     bool newOnly = true;
143     CAddrInfo addr_ret1 = addrman.Select(newOnly);
144     BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333");
145
146     // Test 10: move addr to tried, select from new expected nothing returned.
147     addrman.Good(CAddress(addr1));
148     BOOST_CHECK(addrman.size() == 1);
149     CAddrInfo addr_ret2 = addrman.Select(newOnly);
150     BOOST_CHECK(addr_ret2.ToString() == "[::]:0");
151
152     CAddrInfo addr_ret3 = addrman.Select();
153     BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333");
154
155     BOOST_CHECK(addrman.size() == 1);
156
157
158     // Add three addresses to new table.
159     CService addr2 = CService("250.3.1.1", 8333);
160     CService addr3 = CService("250.3.2.2", 9999);
161     CService addr4 = CService("250.3.3.3", 9999);
162
163     addrman.Add(CAddress(addr2), CService("250.3.1.1", 8333));
164     addrman.Add(CAddress(addr3), CService("250.3.1.1", 8333));
165     addrman.Add(CAddress(addr4), CService("250.4.1.1", 8333));
166
167     // Add three addresses to tried table.
168     CService addr5 = CService("250.4.4.4", 8333);
169     CService addr6 = CService("250.4.5.5", 7777);
170     CService addr7 = CService("250.4.6.6", 8333);
171
172     addrman.Add(CAddress(addr5), CService("250.3.1.1", 8333));
173     addrman.Good(CAddress(addr5));
174     addrman.Add(CAddress(addr6), CService("250.3.1.1", 8333));
175     addrman.Good(CAddress(addr6));
176     addrman.Add(CAddress(addr7), CService("250.1.1.3", 8333));
177     addrman.Good(CAddress(addr7));
178
179     // Test 11: 6 addrs + 1 addr from last test = 7.
180     BOOST_CHECK(addrman.size() == 7);
181
182     // Test 12: Select pulls from new and tried regardless of port number.
183     BOOST_CHECK(addrman.Select().ToString() == "250.4.6.6:8333");
184     BOOST_CHECK(addrman.Select().ToString() == "250.3.2.2:9999");
185     BOOST_CHECK(addrman.Select().ToString() == "250.3.3.3:9999");
186     BOOST_CHECK(addrman.Select().ToString() == "250.4.4.4:8333");
187 }
188
189 BOOST_AUTO_TEST_CASE(addrman_new_collisions)
190 {
191     CAddrManTest addrman;
192
193     // Set addrman addr placement to be deterministic.
194     addrman.MakeDeterministic();
195
196     CNetAddr source = CNetAddr("252.2.2.2");
197
198     BOOST_CHECK(addrman.size() == 0);
199
200     for (unsigned int i = 1; i < 18; i++) {
201         CService addr = CService("250.1.1." + boost::to_string(i));
202         addrman.Add(CAddress(addr), source);
203
204         //Test 13: No collision in new table yet.
205         BOOST_CHECK(addrman.size() == i);
206     }
207
208     //Test 14: new table collision!
209     CService addr1 = CService("250.1.1.18");
210     addrman.Add(CAddress(addr1), source);
211     BOOST_CHECK(addrman.size() == 17);
212
213     CService addr2 = CService("250.1.1.19");
214     addrman.Add(CAddress(addr2), source);
215     BOOST_CHECK(addrman.size() == 18);
216 }
217
218 BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
219 {
220     CAddrManTest addrman;
221
222     // Set addrman addr placement to be deterministic.
223     addrman.MakeDeterministic();
224
225     CNetAddr source = CNetAddr("252.2.2.2");
226
227     BOOST_CHECK(addrman.size() == 0);
228
229     for (unsigned int i = 1; i < 80; i++) {
230         CService addr = CService("250.1.1." + boost::to_string(i));
231         addrman.Add(CAddress(addr), source);
232         addrman.Good(CAddress(addr));
233
234         //Test 15: No collision in tried table yet.
235         BOOST_TEST_MESSAGE(addrman.size());
236         BOOST_CHECK(addrman.size() == i);
237     }
238
239     //Test 16: tried table collision!
240     CService addr1 = CService("250.1.1.80");
241     addrman.Add(CAddress(addr1), source);
242     BOOST_CHECK(addrman.size() == 79);
243
244     CService addr2 = CService("250.1.1.81");
245     addrman.Add(CAddress(addr2), source);
246     BOOST_CHECK(addrman.size() == 80);
247 }
248
249 BOOST_AUTO_TEST_CASE(addrman_find)
250 {
251     CAddrManTest addrman;
252
253     // Set addrman addr placement to be deterministic.
254     addrman.MakeDeterministic();
255
256     BOOST_CHECK(addrman.size() == 0);
257
258     CAddress addr1 = CAddress(CService("250.1.2.1", 8333));
259     CAddress addr2 = CAddress(CService("250.1.2.1", 9999));
260     CAddress addr3 = CAddress(CService("251.255.2.1", 8333));
261
262     CNetAddr source1 = CNetAddr("250.1.2.1");
263     CNetAddr source2 = CNetAddr("250.1.2.2");
264
265     addrman.Add(addr1, source1);
266     addrman.Add(addr2, source2);
267     addrman.Add(addr3, source1);
268
269     // Test 17: ensure Find returns an IP matching what we searched on.
270     CAddrInfo* info1 = addrman.Find(addr1);
271     BOOST_CHECK(info1);
272     if (info1)
273         BOOST_CHECK(info1->ToString() == "250.1.2.1:8333");
274
275     // Test 18; Find does not discriminate by port number.
276     CAddrInfo* info2 = addrman.Find(addr2);
277     BOOST_CHECK(info2);
278     if (info2)
279         BOOST_CHECK(info2->ToString() == info1->ToString());
280
281     // Test 19: Find returns another IP matching what we searched on.
282     CAddrInfo* info3 = addrman.Find(addr3);
283     BOOST_CHECK(info3);
284     if (info3)
285         BOOST_CHECK(info3->ToString() == "251.255.2.1:8333");
286 }
287
288 BOOST_AUTO_TEST_CASE(addrman_create)
289 {
290     CAddrManTest addrman;
291
292     // Set addrman addr placement to be deterministic.
293     addrman.MakeDeterministic();
294
295     BOOST_CHECK(addrman.size() == 0);
296
297     CAddress addr1 = CAddress(CService("250.1.2.1", 8333));
298     CNetAddr source1 = CNetAddr("250.1.2.1");
299
300     int nId;
301     CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId);
302
303     // Test 20: The result should be the same as the input addr.
304     BOOST_CHECK(pinfo->ToString() == "250.1.2.1:8333");
305
306     CAddrInfo* info2 = addrman.Find(addr1);
307     BOOST_CHECK(info2->ToString() == "250.1.2.1:8333");
308 }
309
310
311 BOOST_AUTO_TEST_CASE(addrman_delete)
312 {
313     CAddrManTest addrman;
314
315     // Set addrman addr placement to be deterministic.
316     addrman.MakeDeterministic();
317
318     BOOST_CHECK(addrman.size() == 0);
319
320     CAddress addr1 = CAddress(CService("250.1.2.1", 8333));
321     CNetAddr source1 = CNetAddr("250.1.2.1");
322
323     int nId;
324     addrman.Create(addr1, source1, &nId);
325
326     // Test 21: Delete should actually delete the addr.
327     BOOST_CHECK(addrman.size() == 1);
328     addrman.Delete(nId);
329     BOOST_CHECK(addrman.size() == 0);
330     CAddrInfo* info2 = addrman.Find(addr1);
331     BOOST_CHECK(info2 == NULL);
332 }
333
334 BOOST_AUTO_TEST_CASE(addrman_getaddr)
335 {
336     CAddrManTest addrman;
337
338     // Set addrman addr placement to be deterministic.
339     addrman.MakeDeterministic();
340
341     // Test 22: Sanity check, GetAddr should never return anything if addrman
342     //  is empty.
343     BOOST_CHECK(addrman.size() == 0);
344     vector<CAddress> vAddr1 = addrman.GetAddr();
345     BOOST_CHECK(vAddr1.size() == 0);
346
347     CAddress addr1 = CAddress(CService("250.250.2.1", 8333));
348     addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false
349     CAddress addr2 = CAddress(CService("250.251.2.2", 9999));
350     addr2.nTime = GetAdjustedTime();
351     CAddress addr3 = CAddress(CService("251.252.2.3", 8333));
352     addr3.nTime = GetAdjustedTime();
353     CAddress addr4 = CAddress(CService("252.253.3.4", 8333));
354     addr4.nTime = GetAdjustedTime();
355     CAddress addr5 = CAddress(CService("252.254.4.5", 8333));
356     addr5.nTime = GetAdjustedTime();
357     CNetAddr source1 = CNetAddr("250.1.2.1");
358     CNetAddr source2 = CNetAddr("250.2.3.3");
359
360     // Test 23: Ensure GetAddr works with new addresses.
361     addrman.Add(addr1, source1);
362     addrman.Add(addr2, source2);
363     addrman.Add(addr3, source1);
364     addrman.Add(addr4, source2);
365     addrman.Add(addr5, source1);
366
367     // GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down.
368     BOOST_CHECK(addrman.GetAddr().size() == 1); 
369
370     // Test 24: Ensure GetAddr works with new and tried addresses.
371     addrman.Good(CAddress(addr1));
372     addrman.Good(CAddress(addr2));
373     BOOST_CHECK(addrman.GetAddr().size() == 1);
374
375     // Test 25: Ensure GetAddr still returns 23% when addrman has many addrs.
376     for (unsigned int i = 1; i < (8 * 256); i++) {
377         int octet1 = i % 256;
378         int octet2 = (i / 256) % 256;
379         int octet3 = (i / (256 * 2)) % 256;
380         string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23";
381         CAddress addr = CAddress(CService(strAddr));
382         
383         // Ensure that for all addrs in addrman, isTerrible == false.
384         addr.nTime = GetAdjustedTime();
385         addrman.Add(addr, CNetAddr(strAddr));
386         if (i % 8 == 0)
387             addrman.Good(addr);
388     }
389     vector<CAddress> vAddr = addrman.GetAddr();
390
391     size_t percent23 = (addrman.size() * 23) / 100;
392     BOOST_CHECK(vAddr.size() == percent23);
393     BOOST_CHECK(vAddr.size() == 461);
394     // (Addrman.size() < number of addresses added) due to address collisons.
395     BOOST_CHECK(addrman.size() == 2007);
396 }
397
398
399 BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
400 {
401     CAddrManTest addrman;
402
403     // Set addrman addr placement to be deterministic.
404     addrman.MakeDeterministic();
405
406     CAddress addr1 = CAddress(CService("250.1.1.1", 8333));
407     CAddress addr2 = CAddress(CService("250.1.1.1", 9999));
408
409     CNetAddr source1 = CNetAddr("250.1.1.1");
410
411
412     CAddrInfo info1 = CAddrInfo(addr1, source1);
413
414     uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
415     uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
416
417
418     BOOST_CHECK(info1.GetTriedBucket(nKey1) == 40);
419
420     // Test 26: Make sure key actually randomizes bucket placement. A fail on
421     //  this test could be a security issue.
422     BOOST_CHECK(info1.GetTriedBucket(nKey1) != info1.GetTriedBucket(nKey2));
423
424     // Test 27: Two addresses with same IP but different ports can map to
425     //  different buckets because they have different keys.
426     CAddrInfo info2 = CAddrInfo(addr2, source1);
427
428     BOOST_CHECK(info1.GetKey() != info2.GetKey());
429     BOOST_CHECK(info1.GetTriedBucket(nKey1) != info2.GetTriedBucket(nKey1));
430
431     set<int> buckets;
432     for (int i = 0; i < 255; i++) {
433         CAddrInfo infoi = CAddrInfo(
434             CAddress(CService("250.1.1." + boost::to_string(i))),
435             CNetAddr("250.1.1." + boost::to_string(i)));
436         int bucket = infoi.GetTriedBucket(nKey1);
437         buckets.insert(bucket);
438     }
439     // Test 28: IP addresses in the same group (\16 prefix for IPv4) should
440     //  never get more than 8 buckets
441     BOOST_CHECK(buckets.size() == 8);
442
443     buckets.clear();
444     for (int j = 0; j < 255; j++) {
445         CAddrInfo infoj = CAddrInfo(
446             CAddress(CService("250." + boost::to_string(j) + ".1.1")),
447             CNetAddr("250." + boost::to_string(j) + ".1.1"));
448         int bucket = infoj.GetTriedBucket(nKey1);
449         buckets.insert(bucket);
450     }
451     // Test 29: IP addresses in the different groups should map to more than
452     //  8 buckets.
453     BOOST_CHECK(buckets.size() == 160);
454 }
455
456 BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
457 {
458     CAddrManTest addrman;
459
460     // Set addrman addr placement to be deterministic.
461     addrman.MakeDeterministic();
462
463     CAddress addr1 = CAddress(CService("250.1.2.1", 8333));
464     CAddress addr2 = CAddress(CService("250.1.2.1", 9999));
465
466     CNetAddr source1 = CNetAddr("250.1.2.1");
467
468     CAddrInfo info1 = CAddrInfo(addr1, source1);
469
470     uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
471     uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
472
473     BOOST_CHECK(info1.GetNewBucket(nKey1) == 786);
474
475     // Test 30: Make sure key actually randomizes bucket placement. A fail on
476     //  this test could be a security issue.
477     BOOST_CHECK(info1.GetNewBucket(nKey1) != info1.GetNewBucket(nKey2));
478
479     // Test 31: Ports should not affect bucket placement in the addr
480     CAddrInfo info2 = CAddrInfo(addr2, source1);
481     BOOST_CHECK(info1.GetKey() != info2.GetKey());
482     BOOST_CHECK(info1.GetNewBucket(nKey1) == info2.GetNewBucket(nKey1));
483
484     set<int> buckets;
485     for (int i = 0; i < 255; i++) {
486         CAddrInfo infoi = CAddrInfo(
487             CAddress(CService("250.1.1." + boost::to_string(i))),
488             CNetAddr("250.1.1." + boost::to_string(i)));
489         int bucket = infoi.GetNewBucket(nKey1);
490         buckets.insert(bucket);
491     }
492     // Test 32: IP addresses in the same group (\16 prefix for IPv4) should
493     //  always map to the same bucket.
494     BOOST_CHECK(buckets.size() == 1);
495
496     buckets.clear();
497     for (int j = 0; j < 4 * 255; j++) {
498         CAddrInfo infoj = CAddrInfo(CAddress(
499                                         CService(
500                                             boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1")),
501             CNetAddr("251.4.1.1"));
502         int bucket = infoj.GetNewBucket(nKey1);
503         buckets.insert(bucket);
504     }
505     // Test 33: IP addresses in the same source groups should map to no more
506     //  than 64 buckets.
507     BOOST_CHECK(buckets.size() <= 64);
508
509     buckets.clear();
510     for (int p = 0; p < 255; p++) {
511         CAddrInfo infoj = CAddrInfo(
512             CAddress(CService("250.1.1.1")),
513             CNetAddr("250." + boost::to_string(p) + ".1.1"));
514         int bucket = infoj.GetNewBucket(nKey1);
515         buckets.insert(bucket);
516     }
517     // Test 34: IP addresses in the different source groups should map to more
518     //  than 64 buckets.
519     BOOST_CHECK(buckets.size() > 64);
520 }
521 BOOST_AUTO_TEST_SUITE_END()
This page took 0.056794 seconds and 4 git commands to generate.