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"
16 class CAddrManSerializationMock
: public CAddrMan
19 virtual void Serialize(CDataStream
& s
) const = 0;
21 //! Ensure that bucket placement is always the same for testing purposes.
22 void MakeDeterministic()
25 insecure_rand
= FastRandomContext(true);
29 class CAddrManUncorrupted
: public CAddrManSerializationMock
32 void Serialize(CDataStream
& s
) const override
34 CAddrMan::Serialize(s
);
38 class CAddrManCorrupted
: public CAddrManSerializationMock
41 void Serialize(CDataStream
& s
) const override
43 // Produces corrupt output that claims addrman has 20 addrs when it only has one addr.
44 unsigned char nVersion
= 1;
46 s
<< ((unsigned char)32);
51 int nUBuckets
= ADDRMAN_NEW_BUCKET_COUNT
^ (1 << 30);
55 Lookup("252.1.1.1", serv
, 7777, false);
56 CAddress addr
= CAddress(serv
, NODE_NONE
);
58 LookupHost("252.2.2.2", resolved
, false);
59 CAddrInfo info
= CAddrInfo(addr
, resolved
);
64 CDataStream
AddrmanToStream(CAddrManSerializationMock
& _addrman
)
66 CDataStream
ssPeersIn(SER_DISK
, CLIENT_VERSION
);
67 ssPeersIn
<< FLATDATA(Params().MessageStart());
68 ssPeersIn
<< _addrman
;
69 std::string str
= ssPeersIn
.str();
70 std::vector
<unsigned char> vchData(str
.begin(), str
.end());
71 return CDataStream(vchData
, SER_DISK
, CLIENT_VERSION
);
74 BOOST_FIXTURE_TEST_SUITE(net_tests
, BasicTestingSetup
)
76 BOOST_AUTO_TEST_CASE(cnode_listen_port
)
79 unsigned short port
= GetListenPort();
80 BOOST_CHECK(port
== Params().GetDefaultPort());
82 unsigned short altPort
= 12345;
83 gArgs
.SoftSetArg("-port", std::to_string(altPort
));
84 port
= GetListenPort();
85 BOOST_CHECK(port
== altPort
);
88 BOOST_AUTO_TEST_CASE(caddrdb_read
)
90 CAddrManUncorrupted addrmanUncorrupted
;
91 addrmanUncorrupted
.MakeDeterministic();
93 CService addr1
, addr2
, addr3
;
94 Lookup("250.7.1.1", addr1
, 8333, false);
95 Lookup("250.7.2.2", addr2
, 9999, false);
96 Lookup("250.7.3.3", addr3
, 9999, false);
98 // Add three addresses to new table.
100 Lookup("252.5.1.1", source
, 8333, false);
101 addrmanUncorrupted
.Add(CAddress(addr1
, NODE_NONE
), source
);
102 addrmanUncorrupted
.Add(CAddress(addr2
, NODE_NONE
), source
);
103 addrmanUncorrupted
.Add(CAddress(addr3
, NODE_NONE
), source
);
105 // Test that the de-serialization does not throw an exception.
106 CDataStream ssPeers1
= AddrmanToStream(addrmanUncorrupted
);
107 bool exceptionThrown
= false;
110 BOOST_CHECK(addrman1
.size() == 0);
112 unsigned char pchMsgTmp
[4];
113 ssPeers1
>> FLATDATA(pchMsgTmp
);
114 ssPeers1
>> addrman1
;
115 } catch (const std::exception
& e
) {
116 exceptionThrown
= true;
119 BOOST_CHECK(addrman1
.size() == 3);
120 BOOST_CHECK(exceptionThrown
== false);
122 // Test that CAddrDB::Read creates an addrman with the correct number of addrs.
123 CDataStream ssPeers2
= AddrmanToStream(addrmanUncorrupted
);
127 BOOST_CHECK(addrman2
.size() == 0);
128 adb
.Read(addrman2
, ssPeers2
);
129 BOOST_CHECK(addrman2
.size() == 3);
133 BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted
)
135 CAddrManCorrupted addrmanCorrupted
;
136 addrmanCorrupted
.MakeDeterministic();
138 // Test that the de-serialization of corrupted addrman throws an exception.
139 CDataStream ssPeers1
= AddrmanToStream(addrmanCorrupted
);
140 bool exceptionThrown
= false;
142 BOOST_CHECK(addrman1
.size() == 0);
144 unsigned char pchMsgTmp
[4];
145 ssPeers1
>> FLATDATA(pchMsgTmp
);
146 ssPeers1
>> addrman1
;
147 } catch (const std::exception
& e
) {
148 exceptionThrown
= true;
150 // Even through de-serialization failed addrman is not left in a clean state.
151 BOOST_CHECK(addrman1
.size() == 1);
152 BOOST_CHECK(exceptionThrown
);
154 // Test that CAddrDB::Read leaves addrman in a clean state if de-serialization fails.
155 CDataStream ssPeers2
= AddrmanToStream(addrmanCorrupted
);
159 BOOST_CHECK(addrman2
.size() == 0);
160 adb
.Read(addrman2
, ssPeers2
);
161 BOOST_CHECK(addrman2
.size() == 0);
164 BOOST_AUTO_TEST_CASE(cnode_simple_test
)
166 SOCKET hSocket
= INVALID_SOCKET
;
171 ipv4Addr
.s_addr
= 0xa0b0c001;
173 CAddress addr
= CAddress(CService(ipv4Addr
, 7777), NODE_NETWORK
);
174 std::string pszDest
= "";
175 bool fInboundIn
= false;
177 // Test that fFeeler is false by default.
178 std::unique_ptr
<CNode
> pnode1(new CNode(id
++, NODE_NETWORK
, height
, hSocket
, addr
, 0, 0, CAddress(), pszDest
, fInboundIn
));
179 BOOST_CHECK(pnode1
->fInbound
== false);
180 BOOST_CHECK(pnode1
->fFeeler
== false);
183 std::unique_ptr
<CNode
> pnode2(new CNode(id
++, NODE_NETWORK
, height
, hSocket
, addr
, 1, 1, CAddress(), pszDest
, fInboundIn
));
184 BOOST_CHECK(pnode2
->fInbound
== true);
185 BOOST_CHECK(pnode2
->fFeeler
== false);
188 BOOST_AUTO_TEST_SUITE_END()