net.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file net.cpp
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @date 2014
  17. */
  18. #include <boost/test/unit_test.hpp>
  19. #include <libdevcore/Worker.h>
  20. #include <libdevcore/Assertions.h>
  21. #include <libdevcrypto/Common.h>
  22. #include <libp2p/UDP.h>
  23. #include <libp2p/NodeTable.h>
  24. #include <test/TestHelper.h>
  25. using namespace std;
  26. using namespace dev;
  27. using namespace dev::test;
  28. using namespace dev::p2p;
  29. namespace ba = boost::asio;
  30. namespace bi = ba::ip;
  31. struct NetFixture: public TestOutputHelper
  32. {
  33. NetFixture() { dev::p2p::NodeIPEndpoint::test_allowLocal = true; }
  34. ~NetFixture() { dev::p2p::NodeIPEndpoint::test_allowLocal = false; }
  35. };
  36. BOOST_FIXTURE_TEST_SUITE(net, NetFixture)
  37. /**
  38. * Only used for testing. Not useful beyond tests.
  39. */
  40. class TestHost: public Worker
  41. {
  42. public:
  43. TestHost(): Worker("test",0), m_io() {};
  44. virtual ~TestHost() { m_io.stop(); stopWorking(); }
  45. void start() { startWorking(); }
  46. void doWork() { m_io.run(); }
  47. void doneWorking() { m_io.reset(); m_io.poll(); m_io.reset(); }
  48. protected:
  49. ba::io_service m_io;
  50. };
  51. struct TestNodeTable: public NodeTable
  52. {
  53. /// Constructor
  54. TestNodeTable(ba::io_service& _io, KeyPair _alias, bi::address const& _addr, uint16_t _port = 30300): NodeTable(_io, _alias, NodeIPEndpoint(_addr, _port, _port)) {}
  55. static std::vector<std::pair<KeyPair,unsigned>> createTestNodes(unsigned _count)
  56. {
  57. std::vector<std::pair<KeyPair,unsigned>> ret;
  58. asserts(_count < 1000);
  59. static uint16_t s_basePort = 30500;
  60. ret.clear();
  61. for (unsigned i = 0; i < _count; i++)
  62. {
  63. KeyPair k = KeyPair::create();
  64. ret.push_back(make_pair(k,s_basePort+i));
  65. }
  66. return ret;
  67. }
  68. void pingTestNodes(std::vector<std::pair<KeyPair,unsigned>> const& _testNodes)
  69. {
  70. bi::address ourIp = bi::address::from_string("127.0.0.1");
  71. for (auto& n: _testNodes)
  72. {
  73. ping(NodeIPEndpoint(ourIp, n.second, n.second));
  74. this_thread::sleep_for(chrono::milliseconds(2));
  75. }
  76. }
  77. void populateTestNodes(std::vector<std::pair<KeyPair,unsigned>> const& _testNodes, size_t _count = 0)
  78. {
  79. if (!_count)
  80. _count = _testNodes.size();
  81. bi::address ourIp = bi::address::from_string("127.0.0.1");
  82. for (auto& n: _testNodes)
  83. if (_count--)
  84. {
  85. // manually add node for test
  86. {
  87. Guard ln(x_nodes);
  88. shared_ptr<NodeEntry> node(new NodeEntry(m_node.id, n.first.pub(), NodeIPEndpoint(ourIp, n.second, n.second)));
  89. node->pending = false;
  90. m_nodes[node->id] = node;
  91. }
  92. noteActiveNode(n.first.pub(), bi::udp::endpoint(ourIp, n.second));
  93. }
  94. else
  95. break;
  96. }
  97. void reset()
  98. {
  99. Guard l(x_state);
  100. for (auto& n: m_state) n.nodes.clear();
  101. }
  102. };
  103. /**
  104. * Only used for testing. Not useful beyond tests.
  105. */
  106. struct TestNodeTableHost: public TestHost
  107. {
  108. TestNodeTableHost(unsigned _count = 8): m_alias(KeyPair::create()), nodeTable(new TestNodeTable(m_io, m_alias, bi::address::from_string("127.0.0.1"))), testNodes(TestNodeTable::createTestNodes(_count)) {};
  109. ~TestNodeTableHost() { m_io.stop(); stopWorking(); }
  110. void setup() { for (auto n: testNodes) nodeTables.push_back(make_shared<TestNodeTable>(m_io,n.first, bi::address::from_string("127.0.0.1"),n.second)); }
  111. void pingAll() { for (auto& t: nodeTables) t->pingTestNodes(testNodes); }
  112. void populateAll(size_t _count = 0) { for (auto& t: nodeTables) t->populateTestNodes(testNodes, _count); }
  113. void populate(size_t _count = 0) { nodeTable->populateTestNodes(testNodes, _count); }
  114. KeyPair m_alias;
  115. shared_ptr<TestNodeTable> nodeTable;
  116. std::vector<std::pair<KeyPair,unsigned>> testNodes; // keypair and port
  117. std::vector<shared_ptr<TestNodeTable>> nodeTables;
  118. };
  119. class TestUDPSocket: UDPSocketEvents, public TestHost
  120. {
  121. public:
  122. TestUDPSocket(): m_socket(new UDPSocket<TestUDPSocket, 1024>(m_io, *this, 30300)) {}
  123. void onDisconnected(UDPSocketFace*) {};
  124. void onReceived(UDPSocketFace*, bi::udp::endpoint const&, bytesConstRef _packet) { if (_packet.toString() == "AAAA") success = true; }
  125. shared_ptr<UDPSocket<TestUDPSocket, 1024>> m_socket;
  126. bool success = false;
  127. };
  128. BOOST_AUTO_TEST_CASE(requestTimeout)
  129. {
  130. if (test::Options::get().nonetwork)
  131. return;
  132. using TimePoint = std::chrono::steady_clock::time_point;
  133. using RequestTimeout = std::pair<NodeID, TimePoint>;
  134. std::chrono::milliseconds timeout(300);
  135. std::list<RequestTimeout> timeouts;
  136. NodeID nodeA(sha3("a"));
  137. NodeID nodeB(sha3("b"));
  138. timeouts.push_back(make_pair(nodeA, chrono::steady_clock::now()));
  139. this_thread::sleep_for(std::chrono::milliseconds(100));
  140. timeouts.push_back(make_pair(nodeB, chrono::steady_clock::now()));
  141. this_thread::sleep_for(std::chrono::milliseconds(210));
  142. bool nodeAtriggered = false;
  143. bool nodeBtriggered = false;
  144. timeouts.remove_if([&](RequestTimeout const& t)
  145. {
  146. auto now = chrono::steady_clock::now();
  147. auto diff = now - t.second;
  148. if (t.first == nodeA && diff < timeout)
  149. nodeAtriggered = true;
  150. if (t.first == nodeB && diff < timeout)
  151. nodeBtriggered = true;
  152. return (t.first == nodeA || t.first == nodeB);
  153. });
  154. BOOST_REQUIRE(nodeAtriggered == false);
  155. BOOST_REQUIRE(nodeBtriggered == true);
  156. BOOST_REQUIRE(timeouts.size() == 0);
  157. }
  158. BOOST_AUTO_TEST_CASE(isIPAddressType)
  159. {
  160. string wildcard = "0.0.0.0";
  161. BOOST_REQUIRE(bi::address::from_string(wildcard).is_unspecified());
  162. string empty = "";
  163. BOOST_REQUIRE_THROW(bi::address::from_string(empty).is_unspecified(), std::exception);
  164. string publicAddress192 = "192.169.0.0";
  165. BOOST_REQUIRE(isPublicAddress(publicAddress192));
  166. BOOST_REQUIRE(!isPrivateAddress(publicAddress192));
  167. BOOST_REQUIRE(!isLocalHostAddress(publicAddress192));
  168. string publicAddress172 = "172.32.0.0";
  169. BOOST_REQUIRE(isPublicAddress(publicAddress172));
  170. BOOST_REQUIRE(!isPrivateAddress(publicAddress172));
  171. BOOST_REQUIRE(!isLocalHostAddress(publicAddress172));
  172. string privateAddress192 = "192.168.1.0";
  173. BOOST_REQUIRE(isPrivateAddress(privateAddress192));
  174. BOOST_REQUIRE(!isPublicAddress(privateAddress192));
  175. BOOST_REQUIRE(!isLocalHostAddress(privateAddress192));
  176. string privateAddress172 = "172.16.0.0";
  177. BOOST_REQUIRE(isPrivateAddress(privateAddress172));
  178. BOOST_REQUIRE(!isPublicAddress(privateAddress172));
  179. BOOST_REQUIRE(!isLocalHostAddress(privateAddress172));
  180. string privateAddress10 = "10.0.0.0";
  181. BOOST_REQUIRE(isPrivateAddress(privateAddress10));
  182. BOOST_REQUIRE(!isPublicAddress(privateAddress10));
  183. BOOST_REQUIRE(!isLocalHostAddress(privateAddress10));
  184. }
  185. BOOST_AUTO_TEST_CASE(neighboursPacketLength)
  186. {
  187. if (test::Options::get().nonetwork)
  188. return;
  189. KeyPair k = KeyPair::create();
  190. std::vector<std::pair<KeyPair,unsigned>> testNodes(TestNodeTable::createTestNodes(16));
  191. bi::udp::endpoint to(boost::asio::ip::address::from_string("127.0.0.1"), 30000);
  192. // hash(32), signature(65), overhead: packetSz(3), type(1), nodeListSz(3), ts(5),
  193. static unsigned const nlimit = (1280 - 109) / 90; // neighbour: 2 + 65 + 3 + 3 + 17
  194. for (unsigned offset = 0; offset < testNodes.size(); offset += nlimit)
  195. {
  196. Neighbours out(to);
  197. auto limit = nlimit ? std::min(testNodes.size(), (size_t)(offset + nlimit)) : testNodes.size();
  198. for (auto i = offset; i < limit; i++)
  199. {
  200. Node n(testNodes[i].first.pub(), NodeIPEndpoint(boost::asio::ip::address::from_string("200.200.200.200"), testNodes[i].second, testNodes[i].second));
  201. Neighbours::Neighbour neighbour(n);
  202. out.neighbours.push_back(neighbour);
  203. }
  204. out.sign(k.sec());
  205. BOOST_REQUIRE_LE(out.data.size(), 1280);
  206. }
  207. }
  208. BOOST_AUTO_TEST_CASE(neighboursPacket)
  209. {
  210. if (test::Options::get().nonetwork)
  211. return;
  212. KeyPair k = KeyPair::create();
  213. std::vector<std::pair<KeyPair,unsigned>> testNodes(TestNodeTable::createTestNodes(16));
  214. bi::udp::endpoint to(boost::asio::ip::address::from_string("127.0.0.1"), 30000);
  215. Neighbours out(to);
  216. for (auto n: testNodes)
  217. {
  218. Node node(n.first.pub(), NodeIPEndpoint(boost::asio::ip::address::from_string("200.200.200.200"), n.second, n.second));
  219. Neighbours::Neighbour neighbour(node);
  220. out.neighbours.push_back(neighbour);
  221. }
  222. out.sign(k.sec());
  223. bytesConstRef packet(out.data.data(), out.data.size());
  224. auto in = DiscoveryDatagram::interpretUDP(to, packet);
  225. int count = 0;
  226. for (auto n: dynamic_cast<Neighbours&>(*in).neighbours)
  227. {
  228. BOOST_REQUIRE_EQUAL(testNodes[count].second, n.endpoint.udpPort);
  229. BOOST_REQUIRE_EQUAL(testNodes[count].first.pub(), n.node);
  230. BOOST_REQUIRE_EQUAL(sha3(testNodes[count].first.pub()), sha3(n.node));
  231. count++;
  232. }
  233. }
  234. BOOST_AUTO_TEST_CASE(test_findnode_neighbours)
  235. {
  236. // Executing findNode should result in a list which is serialized
  237. // into Neighbours packet. Neighbours packet should then be deserialized
  238. // into the same list of nearest nodes.
  239. }
  240. BOOST_AUTO_TEST_CASE(kademlia)
  241. {
  242. if (test::Options::get().nonetwork)
  243. return;
  244. TestNodeTableHost node(8);
  245. node.start();
  246. node.setup();
  247. node.populate();
  248. node.populateAll();
  249. auto nodes = node.nodeTable->nodes();
  250. nodes.sort();
  251. node.nodeTable->reset();
  252. node.populate(1);
  253. this_thread::sleep_for(chrono::milliseconds(2000));
  254. BOOST_REQUIRE_EQUAL(node.nodeTable->count(), 8);
  255. }
  256. BOOST_AUTO_TEST_CASE(udpOnce)
  257. {
  258. if (test::Options::get().nonetwork)
  259. return;
  260. UDPDatagram d(bi::udp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 30300), bytes({65,65,65,65}));
  261. TestUDPSocket a; a.m_socket->connect(); a.start();
  262. a.m_socket->send(d);
  263. this_thread::sleep_for(chrono::seconds(1));
  264. BOOST_REQUIRE_EQUAL(true, a.success);
  265. }
  266. BOOST_AUTO_TEST_SUITE_END()
  267. BOOST_AUTO_TEST_SUITE(netTypes)
  268. /*
  269. Test disabled by Bob Summerwill on 1st Sep 2016, because it is
  270. consistently failing on Ubuntu within TravisCI, and also appears
  271. to be testing timing-specific behaviour within Boost code which
  272. we did not author.
  273. See https://github.com/ethereum/cpp-ethereum/issues/3253.
  274. TODO - Work out why this test was written in the first place,
  275. and why it has started failing. Re-add it or remove it.
  276. BOOST_AUTO_TEST_CASE(deadlineTimer)
  277. {
  278. if (test::Options::get().nonetwork)
  279. return;
  280. ba::io_service io;
  281. ba::deadline_timer t(io);
  282. bool start = false;
  283. boost::system::error_code ec;
  284. std::atomic<unsigned> fired(0);
  285. thread thread([&](){ while(!start) this_thread::sleep_for(chrono::milliseconds(10)); io.run(); });
  286. t.expires_from_now(boost::posix_time::milliseconds(200));
  287. start = true;
  288. t.async_wait([&](boost::system::error_code const& _ec){ ec = _ec; fired++; });
  289. BOOST_REQUIRE_NO_THROW(t.wait());
  290. this_thread::sleep_for(chrono::milliseconds(250));
  291. auto expire = t.expires_from_now().total_milliseconds();
  292. BOOST_REQUIRE(expire <= 0);
  293. BOOST_REQUIRE(fired == 1);
  294. BOOST_REQUIRE(!ec);
  295. io.stop();
  296. thread.join();
  297. }
  298. */
  299. BOOST_AUTO_TEST_CASE(unspecifiedNode)
  300. {
  301. if (test::Options::get().nonetwork)
  302. return;
  303. Node n = UnspecifiedNode;
  304. BOOST_REQUIRE(!n);
  305. Node node(Public(sha3("0")), NodeIPEndpoint(bi::address(), 0, 0));
  306. BOOST_REQUIRE(node);
  307. BOOST_REQUIRE(n != node);
  308. Node nodeEq(Public(sha3("0")), NodeIPEndpoint(bi::address(), 0, 0));
  309. BOOST_REQUIRE_EQUAL(node, nodeEq);
  310. }
  311. BOOST_AUTO_TEST_CASE(nodeTableReturnsUnspecifiedNode)
  312. {
  313. if (test::Options::get().nonetwork)
  314. return;
  315. ba::io_service io;
  316. NodeTable t(io, KeyPair::create(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), 30303, 30303));
  317. if (Node n = t.node(NodeID()))
  318. BOOST_REQUIRE(false);
  319. else
  320. BOOST_REQUIRE(n == UnspecifiedNode);
  321. }
  322. BOOST_AUTO_TEST_SUITE_END()