3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test node responses to invalid network messages."""
6
- import asyncio
7
-
8
6
from test_framework .messages import (
9
7
CBlockHeader ,
10
8
CInv ,
11
9
msg_getdata ,
12
10
msg_headers ,
13
11
msg_inv ,
14
- msg_ping ,
15
12
MSG_TX ,
16
13
ser_string ,
17
14
)
18
15
from test_framework .mininode import (
19
- NetworkThread ,
20
16
P2PDataStore ,
21
17
P2PInterface ,
22
18
)
@@ -55,29 +51,20 @@ def run_test(self):
55
51
56
52
def test_magic_bytes (self ):
57
53
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 )
69
59
conn .wait_for_disconnect (timeout = 1 )
70
60
self .nodes [0 ].disconnect_p2ps ()
71
61
72
62
def test_checksum (self ):
73
63
conn = self .nodes [0 ].add_p2p_connection (P2PDataStore ())
74
64
with self .nodes [0 ].assert_debug_log (['CHECKSUM ERROR (badmsg, 2 bytes), expected 78df0a04 was ffffffff' ]):
75
65
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
81
68
# modify checksum
82
69
msg = msg [:cut_len ] + b'\xff ' * 4 + msg [cut_len + 4 :]
83
70
self .nodes [0 ].p2p .send_raw_message (msg )
@@ -88,7 +75,7 @@ def test_size(self):
88
75
conn = self .nodes [0 ].add_p2p_connection (P2PDataStore ())
89
76
with self .nodes [0 ].assert_debug_log (['' ]):
90
77
# 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 ))
92
79
msg = conn .build_message (msg )
93
80
self .nodes [0 ].p2p .send_raw_message (msg )
94
81
conn .wait_for_disconnect (timeout = 1 )
0 commit comments