1
1
mirror of https://github.com/bitcoin/bitcoin.git synced 2024-05-17 23:56:39 +00:00

Merge #19264: [tests] Don't import asyncio to test magic bytes

49236be099c5e8b3cadbc98d5216313e7e1a5a45 [tests] Don't import asyncio to test magic bytes (John Newbery)

Pull request description:

  Simplify the test for invalid start bytes. No need to import asyncio and the Network thread.

ACKs for top commit:
  MarcoFalke:
    review ACK 49236be099c5e8b3cadbc98d5216313e7e1a5a45
  jonatack:
    ACK 49236be099c5e8b3cadbc98d5216313e7e1a5a45
  troygiorshev:
    ACK 49236be.  +0.1 on the additional `cut_len` reformat.

Tree-SHA512: 75cb695603cdc1be7035d7b5117dbef2a1fdb29fd4414a73d75b53d563d6fa800c31bfa9475004622c8bdea4978b51b2055d6fa7be0fe47c7ae34ccc2b0e89a0
This commit is contained in:
fanquake 2020-06-14 12:54:16 +08:00
commit 195822f1e0
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

View File

@ -3,20 +3,16 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test node responses to invalid network messages.""" """Test node responses to invalid network messages."""
import asyncio
from test_framework.messages import ( from test_framework.messages import (
CBlockHeader, CBlockHeader,
CInv, CInv,
msg_getdata, msg_getdata,
msg_headers, msg_headers,
msg_inv, msg_inv,
msg_ping,
MSG_TX, MSG_TX,
ser_string, ser_string,
) )
from test_framework.mininode import ( from test_framework.mininode import (
NetworkThread,
P2PDataStore, P2PDataStore,
P2PInterface, P2PInterface,
) )
@ -55,17 +51,11 @@ class InvalidMessagesTest(BitcoinTestFramework):
def test_magic_bytes(self): def test_magic_bytes(self):
conn = self.nodes[0].add_p2p_connection(P2PDataStore()) conn = self.nodes[0].add_p2p_connection(P2PDataStore())
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART badmsg']):
async def swap_magic_bytes(): msg = conn.build_message(msg_unrecognized(str_data="d"))
conn._on_data = lambda: None # Need to ignore all incoming messages from now, since they come with "invalid" magic bytes # modify magic bytes
conn.magic_bytes = b'\x00\x11\x22\x32' msg = b'\xff' * 4 + msg[4:]
conn.send_raw_message(msg)
# Call .result() to block until the atomic swap is complete, otherwise
# we might run into races later on
asyncio.run_coroutine_threadsafe(swap_magic_bytes(), NetworkThread.network_event_loop).result()
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART ping']):
conn.send_message(msg_ping(nonce=0xff))
conn.wait_for_disconnect(timeout=1) conn.wait_for_disconnect(timeout=1)
self.nodes[0].disconnect_p2ps() self.nodes[0].disconnect_p2ps()
@ -73,11 +63,8 @@ class InvalidMessagesTest(BitcoinTestFramework):
conn = self.nodes[0].add_p2p_connection(P2PDataStore()) conn = self.nodes[0].add_p2p_connection(P2PDataStore())
with self.nodes[0].assert_debug_log(['CHECKSUM ERROR (badmsg, 2 bytes), expected 78df0a04 was ffffffff']): with self.nodes[0].assert_debug_log(['CHECKSUM ERROR (badmsg, 2 bytes), expected 78df0a04 was ffffffff']):
msg = conn.build_message(msg_unrecognized(str_data="d")) msg = conn.build_message(msg_unrecognized(str_data="d"))
cut_len = ( # Checksum is after start bytes (4B), message type (12B), len (4B)
4 + # magic cut_len = 4 + 12 + 4
12 + # msgtype
4 #len
)
# modify checksum # modify checksum
msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:] msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:]
self.nodes[0].p2p.send_raw_message(msg) self.nodes[0].p2p.send_raw_message(msg)
@ -88,7 +75,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
conn = self.nodes[0].add_p2p_connection(P2PDataStore()) conn = self.nodes[0].add_p2p_connection(P2PDataStore())
with self.nodes[0].assert_debug_log(['']): with self.nodes[0].assert_debug_log(['']):
# Create a message with oversized payload # Create a message with oversized payload
msg = msg_unrecognized(str_data="d"*(VALID_DATA_LIMIT + 1)) msg = msg_unrecognized(str_data="d" * (VALID_DATA_LIMIT + 1))
msg = conn.build_message(msg) msg = conn.build_message(msg)
self.nodes[0].p2p.send_raw_message(msg) self.nodes[0].p2p.send_raw_message(msg)
conn.wait_for_disconnect(timeout=1) conn.wait_for_disconnect(timeout=1)