You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net_tests.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright (c) 2012-2016 The Starwels 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_starwels.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"
  14. #include "util.h"
  15. class CAddrManSerializationMock : public CAddrMan
  16. {
  17. public:
  18. virtual void Serialize(CDataStream& s) const = 0;
  19. //! Ensure that bucket placement is always the same for testing purposes.
  20. void MakeDeterministic()
  21. {
  22. nKey.SetNull();
  23. insecure_rand = FastRandomContext(true);
  24. }
  25. };
  26. class CAddrManUncorrupted : public CAddrManSerializationMock
  27. {
  28. public:
  29. void Serialize(CDataStream& s) const override
  30. {
  31. CAddrMan::Serialize(s);
  32. }
  33. };
  34. class CAddrManCorrupted : public CAddrManSerializationMock
  35. {
  36. public:
  37. void Serialize(CDataStream& s) const override
  38. {
  39. // Produces corrupt output that claims addrman has 20 addrs when it only has one addr.
  40. unsigned char nVersion = 1;
  41. s << nVersion;
  42. s << ((unsigned char)32);
  43. s << nKey;
  44. s << 10; // nNew
  45. s << 10; // nTried
  46. int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
  47. s << nUBuckets;
  48. CService serv;
  49. Lookup("252.1.1.1", serv, 7777, false);
  50. CAddress addr = CAddress(serv, NODE_NONE);
  51. CNetAddr resolved;
  52. LookupHost("252.2.2.2", resolved, false);
  53. CAddrInfo info = CAddrInfo(addr, resolved);
  54. s << info;
  55. }
  56. };
  57. CDataStream AddrmanToStream(CAddrManSerializationMock& _addrman)
  58. {
  59. CDataStream ssPeersIn(SER_DISK, CLIENT_VERSION);
  60. ssPeersIn << FLATDATA(Params().MessageStart());
  61. ssPeersIn << _addrman;
  62. std::string str = ssPeersIn.str();
  63. std::vector<unsigned char> vchData(str.begin(), str.end());
  64. return CDataStream(vchData, SER_DISK, CLIENT_VERSION);
  65. }
  66. BOOST_FIXTURE_TEST_SUITE(net_tests, BasicTestingSetup)
  67. BOOST_AUTO_TEST_CASE(cnode_listen_port)
  68. {
  69. // test default
  70. unsigned short port = GetListenPort();
  71. BOOST_CHECK(port == Params().GetDefaultPort());
  72. // test set port
  73. unsigned short altPort = 12345;
  74. gArgs.SoftSetArg("-port", std::to_string(altPort));
  75. port = GetListenPort();
  76. BOOST_CHECK(port == altPort);
  77. }
  78. BOOST_AUTO_TEST_CASE(caddrdb_read)
  79. {
  80. CAddrManUncorrupted addrmanUncorrupted;
  81. addrmanUncorrupted.MakeDeterministic();
  82. CService addr1, addr2, addr3;
  83. Lookup("250.7.1.1", addr1, 8353, false);
  84. Lookup("250.7.2.2", addr2, 9999, false);
  85. Lookup("250.7.3.3", addr3, 9999, false);
  86. // Add three addresses to new table.
  87. CService source;
  88. Lookup("252.5.1.1", source, 8353, false);
  89. addrmanUncorrupted.Add(CAddress(addr1, NODE_NONE), source);
  90. addrmanUncorrupted.Add(CAddress(addr2, NODE_NONE), source);
  91. addrmanUncorrupted.Add(CAddress(addr3, NODE_NONE), source);
  92. // Test that the de-serialization does not throw an exception.
  93. CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);
  94. bool exceptionThrown = false;
  95. CAddrMan addrman1;
  96. BOOST_CHECK(addrman1.size() == 0);
  97. try {
  98. unsigned char pchMsgTmp[4];
  99. ssPeers1 >> FLATDATA(pchMsgTmp);
  100. ssPeers1 >> addrman1;
  101. } catch (const std::exception& e) {
  102. exceptionThrown = true;
  103. }
  104. BOOST_CHECK(addrman1.size() == 3);
  105. BOOST_CHECK(exceptionThrown == false);
  106. // Test that CAddrDB::Read creates an addrman with the correct number of addrs.
  107. CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
  108. CAddrMan addrman2;
  109. CAddrDB adb;
  110. BOOST_CHECK(addrman2.size() == 0);
  111. adb.Read(addrman2, ssPeers2);
  112. BOOST_CHECK(addrman2.size() == 3);
  113. }
  114. BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
  115. {
  116. CAddrManCorrupted addrmanCorrupted;
  117. addrmanCorrupted.MakeDeterministic();
  118. // Test that the de-serialization of corrupted addrman throws an exception.
  119. CDataStream ssPeers1 = AddrmanToStream(addrmanCorrupted);
  120. bool exceptionThrown = false;
  121. CAddrMan addrman1;
  122. BOOST_CHECK(addrman1.size() == 0);
  123. try {
  124. unsigned char pchMsgTmp[4];
  125. ssPeers1 >> FLATDATA(pchMsgTmp);
  126. ssPeers1 >> addrman1;
  127. } catch (const std::exception& e) {
  128. exceptionThrown = true;
  129. }
  130. // Even through de-serialization failed addrman is not left in a clean state.
  131. BOOST_CHECK(addrman1.size() == 1);
  132. BOOST_CHECK(exceptionThrown);
  133. // Test that CAddrDB::Read leaves addrman in a clean state if de-serialization fails.
  134. CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
  135. CAddrMan addrman2;
  136. CAddrDB adb;
  137. BOOST_CHECK(addrman2.size() == 0);
  138. adb.Read(addrman2, ssPeers2);
  139. BOOST_CHECK(addrman2.size() == 0);
  140. }
  141. BOOST_AUTO_TEST_CASE(cnode_simple_test)
  142. {
  143. SOCKET hSocket = INVALID_SOCKET;
  144. NodeId id = 0;
  145. int height = 0;
  146. in_addr ipv4Addr;
  147. ipv4Addr.s_addr = 0xa0b0c001;
  148. CAddress addr = CAddress(CService(ipv4Addr, 7777), NODE_NETWORK);
  149. std::string pszDest = "";
  150. bool fInboundIn = false;
  151. // Test that fFeeler is false by default.
  152. std::unique_ptr<CNode> pnode1(new CNode(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, fInboundIn));
  153. BOOST_CHECK(pnode1->fInbound == false);
  154. BOOST_CHECK(pnode1->fFeeler == false);
  155. fInboundIn = true;
  156. std::unique_ptr<CNode> pnode2(new CNode(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, fInboundIn));
  157. BOOST_CHECK(pnode2->fInbound == true);
  158. BOOST_CHECK(pnode2->fFeeler == false);
  159. }
  160. BOOST_AUTO_TEST_SUITE_END()