Skip to content

Commit

Permalink
test: add basic tests for sendmsgtopeer to rpc_net.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mzumsande committed Aug 22, 2023
1 parent a9a1d69 commit 3557aa4
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/functional/rpc_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def run_test(self):
self.test_service_flags()
self.test_getnodeaddresses()
self.test_addpeeraddress()
self.test_sendmsgtopeer()

def test_connection_count(self):
self.log.info("Test getconnectioncount")
Expand Down Expand Up @@ -328,6 +329,37 @@ def test_addpeeraddress(self):
addrs = node.getnodeaddresses(count=0) # getnodeaddresses re-runs the addrman checks
assert_equal(len(addrs), 2)

def test_sendmsgtopeer(self):
node = self.nodes[0]

self.restart_node(0)
self.connect_nodes(0, 1)

self.log.info("Test sendmsgtopeer")
self.log.debug("Send a valid message")
with self.nodes[1].assert_debug_log(expected_msgs=["received: addr"]):
node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FFFFFF")

self.log.debug("Test error for sending to non-existing peer")
assert_raises_rpc_error(-1, "Error: Could not send message to peer", node.sendmsgtopeer, peer_id=100, msg_type="addr", msg="FF")

self.log.debug("Test that zero-length msg_type is allowed")
node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="")

self.log.debug("Test error for msg_type that is too long")
assert_raises_rpc_error(-8, "Error: msg_type too long, max length is 12", node.sendmsgtopeer, peer_id=0, msg_type="long_msg_type", msg="FF")

self.log.debug("Test that unknown msg_type is allowed")
node.sendmsgtopeer(peer_id=0, msg_type="unknown", msg="FF")

self.log.debug("Test that empty msg is allowed")
node.sendmsgtopeer(peer_id=0, msg_type="addr", msg="FF")

self.log.debug("Test that oversized messages are allowed, but get us disconnected")
zero_byte_string = b'\x00' * 4000001
node.sendmsgtopeer(peer_id=0, msg_type="addr", msg=zero_byte_string.hex())
self.wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0, timeout=10)


if __name__ == '__main__':
NetTest().main()

0 comments on commit 3557aa4

Please sign in to comment.