22 #include <boost/test/unit_test.hpp> 36 namespace bi = ba::ip;
54 void start() { startWorking(); }
56 void doneWorking() { m_io.reset(); m_io.poll(); m_io.reset(); }
69 std::vector<std::pair<KeyPair,unsigned>> ret;
71 static uint16_t s_basePort = 30500;
74 for (
unsigned i = 0; i < _count; i++)
77 ret.push_back(make_pair(k,s_basePort+i));
83 void pingTestNodes(std::vector<std::pair<KeyPair,unsigned>>
const& _testNodes)
85 bi::address ourIp = bi::address::from_string(
"127.0.0.1");
86 for (
auto& n: _testNodes)
89 this_thread::sleep_for(chrono::milliseconds(2));
93 void populateTestNodes(std::vector<std::pair<KeyPair,unsigned>>
const& _testNodes,
size_t _count = 0)
96 _count = _testNodes.size();
98 bi::address ourIp = bi::address::from_string(
"127.0.0.1");
99 for (
auto& n: _testNodes)
106 node->pending =
false;
107 m_nodes[node->id] =
node;
109 noteActiveNode(n.first.pub(), bi::udp::endpoint(ourIp, n.second));
118 for (
auto& n: m_state) n.nodes.clear();
130 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)); }
132 void pingAll() {
for (
auto& t: nodeTables) t->pingTestNodes(testNodes); }
134 void populateAll(
size_t _count = 0) {
for (
auto& t: nodeTables) t->populateTestNodes(testNodes, _count); }
136 void populate(
size_t _count = 0) { nodeTable->populateTestNodes(testNodes, _count); }
152 shared_ptr<UDPSocket<TestUDPSocket, 1024>>
m_socket;
154 bool success =
false;
159 if (test::Options::get().nonetwork)
163 using RequestTimeout = std::pair<NodeID, TimePoint>;
165 std::chrono::milliseconds timeout(300);
166 std::list<RequestTimeout> timeouts;
170 timeouts.push_back(make_pair(nodeA, chrono::steady_clock::now()));
171 this_thread::sleep_for(std::chrono::milliseconds(100));
172 timeouts.push_back(make_pair(nodeB, chrono::steady_clock::now()));
173 this_thread::sleep_for(std::chrono::milliseconds(210));
175 bool nodeAtriggered =
false;
176 bool nodeBtriggered =
false;
177 timeouts.remove_if([&](RequestTimeout
const& t)
179 auto now = chrono::steady_clock::now();
180 auto diff = now - t.second;
181 if (t.first == nodeA &&
diff < timeout)
182 nodeAtriggered =
true;
183 if (t.first == nodeB &&
diff < timeout)
184 nodeBtriggered =
true;
185 return (t.first == nodeA || t.first == nodeB);
188 BOOST_REQUIRE(nodeAtriggered ==
false);
189 BOOST_REQUIRE(nodeBtriggered ==
true);
190 BOOST_REQUIRE(timeouts.size() == 0);
195 string wildcard =
"0.0.0.0";
196 BOOST_REQUIRE(bi::address::from_string(wildcard).is_unspecified());
199 BOOST_REQUIRE_THROW(bi::address::from_string(empty).is_unspecified(), std::exception);
201 string publicAddress192 =
"192.169.0.0";
206 string publicAddress172 =
"172.32.0.0";
211 string privateAddress192 =
"192.168.1.0";
216 string privateAddress172 =
"172.16.0.0";
221 string privateAddress10 =
"10.0.0.0";
229 if (test::Options::get().nonetwork)
234 bi::udp::endpoint to(boost::asio::ip::address::from_string(
"127.0.0.1"), 30000);
237 static unsigned const nlimit = (1280 - 109) / 90;
238 for (
unsigned offset = 0; offset < testNodes.size(); offset += nlimit)
242 auto limit = nlimit ?
std::min(testNodes.size(), (size_t)(offset + nlimit)) : testNodes.size();
243 for (
auto i = offset; i < limit; i++)
245 Node n(testNodes[i].first.pub(),
NodeIPEndpoint(boost::asio::ip::address::from_string(
"200.200.200.200"), testNodes[i].second, testNodes[i].second));
251 BOOST_REQUIRE_LE(out.
data.size(), 1280);
257 if (test::Options::get().nonetwork)
262 bi::udp::endpoint to(boost::asio::ip::address::from_string(
"127.0.0.1"), 30000);
265 for (
auto n: testNodes)
267 Node node(n.first.pub(),
NodeIPEndpoint(boost::asio::ip::address::from_string(
"200.200.200.200"), n.second, n.second));
274 auto in = DiscoveryDatagram::interpretUDP(to, packet);
276 for (
auto n: dynamic_cast<Neighbours&>(*in).neighbours)
278 BOOST_REQUIRE_EQUAL(testNodes[count].second, n.endpoint.udpPort);
279 BOOST_REQUIRE_EQUAL(testNodes[count].first.pub(), n.node);
280 BOOST_REQUIRE_EQUAL(
sha3(testNodes[count].first.pub()),
sha3(n.node));
294 if (test::Options::get().nonetwork)
306 this_thread::sleep_for(chrono::milliseconds(2000));
307 BOOST_REQUIRE_EQUAL(node.
nodeTable->count(), 8);
312 if (test::Options::get().nonetwork)
315 UDPDatagram d(bi::udp::endpoint(boost::asio::ip::address::from_string(
"127.0.0.1"), 30300),
bytes({65,65,65,65}));
318 this_thread::sleep_for(chrono::seconds(1));
319 BOOST_REQUIRE_EQUAL(
true, a.
success);
324 BOOST_AUTO_TEST_SUITE(netTypes)
367 if (test::Options::get().nonetwork)
375 BOOST_REQUIRE(n != node);
378 BOOST_REQUIRE_EQUAL(node, nodeEq);
383 if (test::Options::get().nonetwork)
389 BOOST_REQUIRE(
false);
std::vector< std::pair< KeyPair, unsigned > > testNodes
void onReceived(UDPSocketFace *, bi::udp::endpoint const &, bytesConstRef _packet)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Node node(NodeID const &_id)
Returns the Node to the corresponding node id or the empty Node if that id is not found...
TestNodeTableHost(unsigned _count=8)
bool isPrivateAddress(bi::address const &_addressToCheck)
void doWork()
Called continuously following sleep for m_idleWaitMs.
bool isLocalHostAddress(bi::address const &_addressToCheck)
UDP Interface Handler must implement UDPSocketEvents.
Simple class that represents a "key pair".
BOOST_AUTO_TEST_CASE(requestTimeout)
bool isPublicAddress(bi::address const &_addressToCheck)
std::hash for asio::adress
Secret const & secret() const
h512 Public
A public key: 64 bytes.
void populateTestNodes(std::vector< std::pair< KeyPair, unsigned >> const &_testNodes, size_t _count=0)
std::lock_guard< std::mutex > Guard
void doneWorking()
Called when is to be stopped, just prior to thread being joined.
NodeTable using modified kademlia for node discovery and preference.
std::vector< byte > bytes
const Node UnspecifiedNode
TestNodeTable(ba::io_service &_io, KeyPair _alias, bi::address const &_addr, uint16_t _port=30300)
Constructor.
UniValue ping(const JSONRPCRequest &request)
shared_ptr< UDPSocket< TestUDPSocket, 1024 > > m_socket
virtual h256 sign(Secret const &_from)
std::vector< shared_ptr< TestNodeTable > > nodeTables
std::vector< Neighbour > neighbours
#define BOOST_FIXTURE_TEST_SUITE(a, b)
static std::vector< std::pair< KeyPair, unsigned > > createTestNodes(unsigned _count)
N diff(N const &_a, N const &_b)
clock::time_point time_point
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
#define BOOST_AUTO_TEST_SUITE_END()
Interface which UDPSocket will implement.
struct evm_uint160be address(struct evm_env *env)
void pingTestNodes(std::vector< std::pair< KeyPair, unsigned >> const &_testNodes)
void populate(size_t _count=0)
std::string toString() const
void onDisconnected(UDPSocketFace *)
void populateAll(size_t _count=0)
static bool test_allowLocal
Setting true causes isAllowed to return true for all addresses. (Used by test fixtures) ...
shared_ptr< TestNodeTable > nodeTable
Helper functions to work with json::spirit and test files.
Node Packet: One or more node packets are sent in response to FindNode.
Interface which a UDPSocket's owner must implement.