Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 45f73d3

Browse files
committed
Test for receiving a Pong with changed NodeID
1 parent 594f141 commit 45f73d3

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/unittests/libp2p/net.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,54 @@ BOOST_AUTO_TEST_CASE(validPong)
670670
BOOST_CHECK(addedNode->lastPongReceivedTime > 0);
671671
}
672672

673+
BOOST_AUTO_TEST_CASE(pongWithChangedNodeID)
674+
{
675+
// NodeTable sending PING
676+
TestNodeTableHost nodeTableHost(0);
677+
nodeTableHost.start();
678+
auto& nodeTable = nodeTableHost.nodeTable;
679+
nodeTable->setRequestTimeToLive(std::chrono::seconds(1));
680+
681+
// socket receiving PING
682+
TestUDPSocketHost nodeSocketHost{getRandomPortNumber()};
683+
nodeSocketHost.start();
684+
uint16_t nodePort = nodeSocketHost.port;
685+
686+
// add a node to node table, initiating PING
687+
auto nodeEndpoint = NodeIPEndpoint{bi::address::from_string(c_localhostIp), nodePort, nodePort};
688+
auto nodeKeyPair = KeyPair::create();
689+
auto nodePubKey = nodeKeyPair.pub();
690+
nodeTable->addNode(Node{nodePubKey, nodeEndpoint});
691+
692+
// handle received PING
693+
auto pingDataReceived = nodeSocketHost.packetsReceived.pop();
694+
auto pingDatagram =
695+
DiscoveryDatagram::interpretUDP(bi::udp::endpoint{}, dev::ref(pingDataReceived));
696+
auto ping = dynamic_cast<PingNode const&>(*pingDatagram);
697+
698+
// send PONG with different NodeID
699+
Pong pong(nodeTable->m_hostNodeEndpoint);
700+
pong.echo = ping.echo;
701+
auto newNodeKeyPair = KeyPair::create();
702+
auto newNodePubKey = newNodeKeyPair.pub();
703+
pong.sign(newNodeKeyPair.secret());
704+
nodeSocketHost.socket->send(pong);
705+
706+
// wait for PONG to be received and handled
707+
nodeTable->packetsReceived.pop();
708+
709+
BOOST_REQUIRE(nodeTable->nodeExists(newNodeKeyPair.pub()));
710+
auto addedNode = nodeTable->nodeEntry(newNodePubKey);
711+
BOOST_CHECK(addedNode->lastPongReceivedTime > 0);
712+
713+
// wait for old node to be kicked out after timeout
714+
this_thread::sleep_for(std::chrono::seconds(6));
715+
716+
BOOST_CHECK(!nodeTable->nodeExists(nodePubKey));
717+
auto sentPing = nodeTable->nodeValidation(nodePubKey);
718+
BOOST_CHECK(!sentPing.is_initialized());
719+
}
720+
673721
BOOST_AUTO_TEST_CASE(pingTimeout)
674722
{
675723
// NodeTable sending PING

0 commit comments

Comments
 (0)