Skip to content

Commit 49236be

Browse files
committed
[tests] Don't import asyncio to test magic bytes
1 parent 8c97780 commit 49236be

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

test/functional/p2p_invalid_messages.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test node responses to invalid network messages."""
6-
import asyncio
7-
86
from test_framework.messages import (
97
CBlockHeader,
108
CInv,
119
msg_getdata,
1210
msg_headers,
1311
msg_inv,
14-
msg_ping,
1512
MSG_TX,
1613
ser_string,
1714
)
1815
from test_framework.mininode import (
19-
NetworkThread,
2016
P2PDataStore,
2117
P2PInterface,
2218
)
@@ -55,29 +51,20 @@ def run_test(self):
5551

5652
def test_magic_bytes(self):
5753
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
58-
59-
async def swap_magic_bytes():
60-
conn._on_data = lambda: None # Need to ignore all incoming messages from now, since they come with "invalid" magic bytes
61-
conn.magic_bytes = b'\x00\x11\x22\x32'
62-
63-
# Call .result() to block until the atomic swap is complete, otherwise
64-
# we might run into races later on
65-
asyncio.run_coroutine_threadsafe(swap_magic_bytes(), NetworkThread.network_event_loop).result()
66-
67-
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART ping']):
68-
conn.send_message(msg_ping(nonce=0xff))
54+
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART badmsg']):
55+
msg = conn.build_message(msg_unrecognized(str_data="d"))
56+
# modify magic bytes
57+
msg = b'\xff' * 4 + msg[4:]
58+
conn.send_raw_message(msg)
6959
conn.wait_for_disconnect(timeout=1)
7060
self.nodes[0].disconnect_p2ps()
7161

7262
def test_checksum(self):
7363
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
7464
with self.nodes[0].assert_debug_log(['CHECKSUM ERROR (badmsg, 2 bytes), expected 78df0a04 was ffffffff']):
7565
msg = conn.build_message(msg_unrecognized(str_data="d"))
76-
cut_len = (
77-
4 + # magic
78-
12 + # msgtype
79-
4 #len
80-
)
66+
# Checksum is after start bytes (4B), message type (12B), len (4B)
67+
cut_len = 4 + 12 + 4
8168
# modify checksum
8269
msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:]
8370
self.nodes[0].p2p.send_raw_message(msg)
@@ -88,7 +75,7 @@ def test_size(self):
8875
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
8976
with self.nodes[0].assert_debug_log(['']):
9077
# Create a message with oversized payload
91-
msg = msg_unrecognized(str_data="d"*(VALID_DATA_LIMIT + 1))
78+
msg = msg_unrecognized(str_data="d" * (VALID_DATA_LIMIT + 1))
9279
msg = conn.build_message(msg)
9380
self.nodes[0].p2p.send_raw_message(msg)
9481
conn.wait_for_disconnect(timeout=1)

0 commit comments

Comments
 (0)