LCOV - code coverage report
Current view: top level - src/test - addrman_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 86 86 100.0 %
Date: 2015-10-12 22:39:14 Functions: 12 24 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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          10 : class CAddrManTest : public CAddrMan{};
      14             : 
      15           1 : BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
      16             : 
      17           6 : BOOST_AUTO_TEST_CASE(addrman_simple)
      18             : {
      19             :     CAddrManTest addrman;
      20             : 
      21             :     // Set addrman addr placement to be deterministic.
      22             :     addrman.MakeDeterministic();
      23             : 
      24           1 :     CNetAddr source = CNetAddr("252.2.2.2:8333");
      25             : 
      26             :     // Test 1: Does Addrman respond correctly when empty.
      27           8 :     BOOST_CHECK(addrman.size() == 0);
      28           1 :     CAddrInfo addr_null = addrman.Select();
      29          10 :     BOOST_CHECK(addr_null.ToString() == "[::]:0");
      30             : 
      31             :     // Test 2: Does Addrman::Add work as expected.
      32           1 :     CService addr1 = CService("250.1.1.1:8333");
      33           1 :     addrman.Add(CAddress(addr1), source);
      34           8 :     BOOST_CHECK(addrman.size() == 1);
      35           1 :     CAddrInfo addr_ret1 = addrman.Select();
      36          10 :     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           1 :     CService addr1_dup = CService("250.1.1.1:8333");
      41           1 :     addrman.Add(CAddress(addr1_dup), source);
      42           7 :     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           1 :     CService addr2 = CService("250.1.1.2:8333");
      48           1 :     addrman.Add(CAddress(addr2), source);
      49           8 :     BOOST_CHECK(addrman.size() == 2);
      50             : 
      51             :     // Test 6: AddrMan::Clear() should empty the new table. 
      52           1 :     addrman.Clear();
      53           8 :     BOOST_CHECK(addrman.size() == 0);
      54           1 :     CAddrInfo addr_null2 = addrman.Select();
      55          10 :     BOOST_CHECK(addr_null2.ToString() == "[::]:0");
      56           1 : }
      57             : 
      58           6 : BOOST_AUTO_TEST_CASE(addrman_ports)
      59             : {
      60             :     CAddrManTest addrman;
      61             : 
      62             :     // Set addrman addr placement to be deterministic.
      63             :     addrman.MakeDeterministic();
      64             : 
      65           1 :     CNetAddr source = CNetAddr("252.2.2.2:8333");
      66             : 
      67           8 :     BOOST_CHECK(addrman.size() == 0);
      68             : 
      69             :     // Test 7; Addr with same IP but diff port does not replace existing addr.
      70           1 :     CService addr1 = CService("250.1.1.1:8333");
      71           1 :     addrman.Add(CAddress(addr1), source);
      72           8 :     BOOST_CHECK(addrman.size() == 1);
      73             : 
      74           1 :     CService addr1_port = CService("250.1.1.1:8334");
      75           1 :     addrman.Add(CAddress(addr1_port), source);
      76           8 :     BOOST_CHECK(addrman.size() == 1);
      77           1 :     CAddrInfo addr_ret2 = addrman.Select();
      78          10 :     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           1 :     addrman.Good(CAddress(addr1_port));
      83           8 :     BOOST_CHECK(addrman.size() == 1);
      84           1 :     bool newOnly = true;
      85           1 :     CAddrInfo addr_ret3 = addrman.Select(newOnly);
      86          10 :     BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333");
      87           1 : }
      88             : 
      89             : 
      90           6 : BOOST_AUTO_TEST_CASE(addrman_select)
      91             : {
      92             :     CAddrManTest addrman;
      93             : 
      94             :     // Set addrman addr placement to be deterministic.
      95             :     addrman.MakeDeterministic();
      96             : 
      97           1 :     CNetAddr source = CNetAddr("252.2.2.2:8333");
      98             : 
      99             :     // Test 9: Select from new with 1 addr in new.
     100           1 :     CService addr1 = CService("250.1.1.1:8333");
     101           1 :     addrman.Add(CAddress(addr1), source);
     102           8 :     BOOST_CHECK(addrman.size() == 1);
     103             : 
     104           1 :     bool newOnly = true;
     105           1 :     CAddrInfo addr_ret1 = addrman.Select(newOnly);
     106          10 :     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           1 :     addrman.Good(CAddress(addr1));
     111           8 :     BOOST_CHECK(addrman.size() == 1);
     112           1 :     CAddrInfo addr_ret2 = addrman.Select(newOnly);
     113          10 :     BOOST_CHECK(addr_ret2.ToString() == "[::]:0");
     114             : 
     115           1 :     CAddrInfo addr_ret3 = addrman.Select();
     116          10 :     BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333");
     117           1 : }
     118             : 
     119           6 : 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           1 :     CNetAddr source = CNetAddr("252.2.2.2:8333");
     127             : 
     128           8 :     BOOST_CHECK(addrman.size() == 0);
     129             : 
     130           4 :     for (unsigned int i = 1; i < 4; i++){
     131           9 :         CService addr = CService("250.1.1."+boost::to_string(i));
     132           3 :         addrman.Add(CAddress(addr), source);
     133             : 
     134             :         //Test 11: No collision in new table yet.
     135          24 :         BOOST_CHECK(addrman.size() == i);
     136             :     }
     137             : 
     138             :     //Test 12: new table collision!
     139           1 :     CService addr1 = CService("250.1.1.4");
     140           1 :     addrman.Add(CAddress(addr1), source);
     141           8 :     BOOST_CHECK(addrman.size() == 3);
     142             : 
     143           1 :     CService addr2 = CService("250.1.1.5");
     144           1 :     addrman.Add(CAddress(addr2), source);
     145           8 :     BOOST_CHECK(addrman.size() == 4);
     146           1 : }
     147             : 
     148           6 : 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           1 :     CNetAddr source = CNetAddr("252.2.2.2:8333");
     156             : 
     157           8 :     BOOST_CHECK(addrman.size() == 0);
     158             : 
     159          75 :     for (unsigned int i = 1; i < 75; i++){
     160         222 :         CService addr = CService("250.1.1."+boost::to_string(i));
     161          74 :         addrman.Add(CAddress(addr), source);
     162          74 :         addrman.Good(CAddress(addr));
     163             : 
     164             :         //Test 13: No collision in tried table yet.
     165         296 :         BOOST_TEST_MESSAGE(addrman.size());
     166         592 :         BOOST_CHECK(addrman.size() == i);
     167             :     }
     168             : 
     169             :     //Test 14: tried table collision!
     170           1 :     CService addr1 = CService("250.1.1.76");
     171           1 :     addrman.Add(CAddress(addr1), source);
     172           8 :     BOOST_CHECK(addrman.size() == 74);
     173             : 
     174           1 :     CService addr2 = CService("250.1.1.77");
     175           1 :     addrman.Add(CAddress(addr2), source);
     176           8 :     BOOST_CHECK(addrman.size() == 75);
     177           1 : }
     178             : 
     179             : 
     180           3 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.11