1 // Copyright (c) 2012-2016 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.
5 #include "test/test_bitcoin.h"
7 #include <boost/test/unit_test.hpp>
13 #include "chainparams.h"
17 class CAddrManSerializationMock
: public CAddrMan
20 virtual void Serialize(CDataStream
& s
) const = 0;
22 //! Ensure that bucket placement is always the same for testing purposes.
23 void MakeDeterministic()
26 insecure_rand
= FastRandomContext(true);
30 class CAddrManUncorrupted
: public CAddrManSerializationMock
33 void Serialize(CDataStream
& s
) const
35 CAddrMan::Serialize(s
);
39 class CAddrManCorrupted
: public CAddrManSerializationMock
42 void Serialize(CDataStream
& s
) const
44 // Produces corrupt output that claims addrman has 20 addrs when it only has one addr.
45 unsigned char nVersion
= 1;
47 s
<< ((unsigned char)32);
52 int nUBuckets
= ADDRMAN_NEW_BUCKET_COUNT
^ (1 << 30);
56 Lookup("252.1.1.1", serv
, 7777, false);
57 CAddress addr
= CAddress(serv
, NODE_NONE
);
59 LookupHost("252.2.2.2", resolved
, false);
60 CAddrInfo info
= CAddrInfo(addr
, resolved
);
65 CDataStream
AddrmanToStream(CAddrManSerializationMock
& _addrman
)
67 CDataStream
ssPeersIn(SER_DISK
, CLIENT_VERSION
);
68 ssPeersIn
<< FLATDATA(Params().MessageStart());
69 ssPeersIn
<< _addrman
;
70 std::string str
= ssPeersIn
.str();
71 vector
<unsigned char> vchData(str
.begin(), str
.end());
72 return CDataStream(vchData
, SER_DISK
, CLIENT_VERSION
);
75 BOOST_FIXTURE_TEST_SUITE(net_tests
, BasicTestingSetup
)
77 BOOST_AUTO_TEST_CASE(caddrdb_read
)
79 CAddrManUncorrupted addrmanUncorrupted
;
80 addrmanUncorrupted
.MakeDeterministic();
82 CService addr1
, addr2
, addr3
;
83 Lookup("250.7.1.1", addr1
, 8333, false);
84 Lookup("250.7.2.2", addr2
, 9999, false);
85 Lookup("250.7.3.3", addr3
, 9999, false);
87 // Add three addresses to new table.
89 Lookup("252.5.1.1", source
, 8333, false);
90 addrmanUncorrupted
.Add(CAddress(addr1
, NODE_NONE
), source
);
91 addrmanUncorrupted
.Add(CAddress(addr2
, NODE_NONE
), source
);
92 addrmanUncorrupted
.Add(CAddress(addr3
, NODE_NONE
), source
);
94 // Test that the de-serialization does not throw an exception.
95 CDataStream ssPeers1
= AddrmanToStream(addrmanUncorrupted
);
96 bool exceptionThrown
= false;
99 BOOST_CHECK(addrman1
.size() == 0);
101 unsigned char pchMsgTmp
[4];
102 ssPeers1
>> FLATDATA(pchMsgTmp
);
103 ssPeers1
>> addrman1
;
104 } catch (const std::exception
& e
) {
105 exceptionThrown
= true;
108 BOOST_CHECK(addrman1
.size() == 3);
109 BOOST_CHECK(exceptionThrown
== false);
111 // Test that CAddrDB::Read creates an addrman with the correct number of addrs.
112 CDataStream ssPeers2
= AddrmanToStream(addrmanUncorrupted
);
116 BOOST_CHECK(addrman2
.size() == 0);
117 adb
.Read(addrman2
, ssPeers2
);
118 BOOST_CHECK(addrman2
.size() == 3);
122 BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted
)
124 CAddrManCorrupted addrmanCorrupted
;
125 addrmanCorrupted
.MakeDeterministic();
127 // Test that the de-serialization of corrupted addrman throws an exception.
128 CDataStream ssPeers1
= AddrmanToStream(addrmanCorrupted
);
129 bool exceptionThrown
= false;
131 BOOST_CHECK(addrman1
.size() == 0);
133 unsigned char pchMsgTmp
[4];
134 ssPeers1
>> FLATDATA(pchMsgTmp
);
135 ssPeers1
>> addrman1
;
136 } catch (const std::exception
& e
) {
137 exceptionThrown
= true;
139 // Even through de-serialization failed addrman is not left in a clean state.
140 BOOST_CHECK(addrman1
.size() == 1);
141 BOOST_CHECK(exceptionThrown
);
143 // Test that CAddrDB::Read leaves addrman in a clean state if de-serialization fails.
144 CDataStream ssPeers2
= AddrmanToStream(addrmanCorrupted
);
148 BOOST_CHECK(addrman2
.size() == 0);
149 adb
.Read(addrman2
, ssPeers2
);
150 BOOST_CHECK(addrman2
.size() == 0);
153 BOOST_AUTO_TEST_CASE(cnode_simple_test
)
155 SOCKET hSocket
= INVALID_SOCKET
;
160 ipv4Addr
.s_addr
= 0xa0b0c001;
162 CAddress addr
= CAddress(CService(ipv4Addr
, 7777), NODE_NETWORK
);
163 std::string pszDest
= "";
164 bool fInboundIn
= false;
166 // Test that fFeeler is false by default.
167 CNode
* pnode1
= new CNode(id
++, NODE_NETWORK
, height
, hSocket
, addr
, 0, 0, pszDest
, fInboundIn
);
168 BOOST_CHECK(pnode1
->fInbound
== false);
169 BOOST_CHECK(pnode1
->fFeeler
== false);
172 CNode
* pnode2
= new CNode(id
++, NODE_NETWORK
, height
, hSocket
, addr
, 1, 1, pszDest
, fInboundIn
);
173 BOOST_CHECK(pnode2
->fInbound
== true);
174 BOOST_CHECK(pnode2
->fFeeler
== false);
177 BOOST_AUTO_TEST_SUITE_END()