Skip to content

Commit 0aedf35

Browse files
jnewberyfurszy
authored andcommitted
[tests] Don't import asyncio to test magic bytes
1 parent 9bb5309 commit 0aedf35

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

test/functional/p2p_invalid_messages.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
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 import messages
97
from test_framework.mininode import (
10-
NetworkThread,
118
P2PDataStore,
129
P2PInterface,
1310
)
@@ -46,29 +43,20 @@ def run_test(self):
4643

4744
def test_magic_bytes(self):
4845
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
49-
50-
async def swap_magic_bytes():
51-
conn._on_data = lambda: None # Need to ignore all incoming messages from now, since they come with "invalid" magic bytes
52-
conn.magic_bytes = b'\x00\x11\x22\x32'
53-
54-
# Call .result() to block until the atomic swap is complete, otherwise
55-
# we might run into races later on
56-
asyncio.run_coroutine_threadsafe(swap_magic_bytes(), NetworkThread.network_event_loop).result()
57-
58-
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART ping peer=1']):
59-
conn.send_message(messages.msg_ping(nonce=0xff))
46+
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART badmsg']):
47+
msg = conn.build_message(msg_unrecognized(str_data="d"))
48+
# modify magic bytes
49+
msg = b'\xff' * 4 + msg[4:]
50+
conn.send_raw_message(msg)
6051
conn.wait_for_disconnect(timeout=1)
6152
self.nodes[0].disconnect_p2ps()
6253

6354
def test_checksum(self):
6455
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
6556
with self.nodes[0].assert_debug_log(['ProcessMessages(badmsg, 2 bytes): CHECKSUM ERROR expected 78df0a04 was ffffffff']):
6657
msg = conn.build_message(msg_unrecognized(str_data="d"))
67-
cut_len = (
68-
4 + # magic
69-
12 + # command
70-
4 #len
71-
)
58+
# Checksum is after start bytes (4B), message type (12B), len (4B)
59+
cut_len = 4 + 12 + 4
7260
# modify checksum
7361
msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:]
7462
self.nodes[0].p2p.send_raw_message(msg)
@@ -79,7 +67,7 @@ def test_size(self):
7967
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
8068
with self.nodes[0].assert_debug_log(['']):
8169
# Create a message with oversized payload
82-
msg = msg_unrecognized(str_data="d"*(VALID_DATA_LIMIT + 1))
70+
msg = msg_unrecognized(str_data="d" * (VALID_DATA_LIMIT + 1))
8371
msg = conn.build_message(msg)
8472
self.nodes[0].p2p.send_raw_message(msg)
8573
conn.wait_for_disconnect(timeout=1)

0 commit comments

Comments
 (0)