Get rid of nType and nVersion
[bitcoinplatinum.git] / src / test / net_tests.cpp
blob87cb38daac3d2766e9d285fe3fe25820bc6ea76a
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.
4 #include "addrman.h"
5 #include "test/test_bitcoin.h"
6 #include <string>
7 #include <boost/test/unit_test.hpp>
8 #include "hash.h"
9 #include "serialize.h"
10 #include "streams.h"
11 #include "net.h"
12 #include "netbase.h"
13 #include "chainparams.h"
15 using namespace std;
17 class CAddrManSerializationMock : public CAddrMan
19 public:
20 virtual void Serialize(CDataStream& s) const = 0;
22 //! Ensure that bucket placement is always the same for testing purposes.
23 void MakeDeterministic()
25 nKey.SetNull();
26 insecure_rand = FastRandomContext(true);
30 class CAddrManUncorrupted : public CAddrManSerializationMock
32 public:
33 void Serialize(CDataStream& s) const
35 CAddrMan::Serialize(s);
39 class CAddrManCorrupted : public CAddrManSerializationMock
41 public:
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;
46 s << nVersion;
47 s << ((unsigned char)32);
48 s << nKey;
49 s << 10; // nNew
50 s << 10; // nTried
52 int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
53 s << nUBuckets;
55 CService serv;
56 Lookup("252.1.1.1", serv, 7777, false);
57 CAddress addr = CAddress(serv, NODE_NONE);
58 CNetAddr resolved;
59 LookupHost("252.2.2.2", resolved, false);
60 CAddrInfo info = CAddrInfo(addr, resolved);
61 s << info;
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.
88 CService source;
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;
97 CAddrMan addrman1;
99 BOOST_CHECK(addrman1.size() == 0);
100 try {
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);
114 CAddrMan addrman2;
115 CAddrDB adb;
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;
130 CAddrMan addrman1;
131 BOOST_CHECK(addrman1.size() == 0);
132 try {
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);
146 CAddrMan addrman2;
147 CAddrDB adb;
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;
156 NodeId id = 0;
157 int height = 0;
159 in_addr ipv4Addr;
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);
171 fInboundIn = true;
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()