Skip to content

Commit

Permalink
[backport#19264] [tests] Don't import asyncio to test magic bytes
Browse files Browse the repository at this point in the history
Summary:
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.

---

Backport of [[bitcoin/bitcoin#19264 | core#19264]]

Depends on D9519

Test Plan:
  ninja all && ./test/functional/test_runner.py p2p_invalid_messages

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D9520
  • Loading branch information
fanquake authored and majcosta committed May 13, 2021
1 parent c91add3 commit b97f1a9
Showing 1 changed file with 7 additions and 26 deletions.
33 changes: 7 additions & 26 deletions test/functional/p2p_invalid_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test node responses to invalid network messages."""
import asyncio
import struct
import time

Expand All @@ -16,12 +15,10 @@
msg_getdata,
msg_headers,
msg_inv,
msg_ping,
MSG_TX,
ser_string,
)
from test_framework.mininode import (
NetworkThread,
P2PDataStore,
P2PInterface,
)
Expand Down Expand Up @@ -72,36 +69,20 @@ def run_test(self):

def test_magic_bytes(self):
conn = self.nodes[0].add_p2p_connection(P2PDataStore())

async def swap_magic_bytes():
# Need to ignore all incoming messages from now, since they come
# with "invalid" magic bytes
conn._on_data = lambda: None
conn.magic_bytes = b'\x00\x11\x22\x32'

# 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))
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART badmsg']):
msg = conn.build_message(msg_unrecognized(str_data="d"))
# modify magic bytes
msg = b'\xff' * 4 + msg[4:]
conn.send_raw_message(msg)
conn.wait_for_disconnect(timeout=1)
self.nodes[0].disconnect_p2ps()

def test_checksum(self):
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
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"))
cut_len = (
# magic
4 +
# msgtype
12 +
# len
4
)
# Checksum is after start bytes (4B), message type (12B), len (4B)
cut_len = 4 + 12 + 4
# modify checksum
msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:]
self.nodes[0].p2p.send_raw_message(msg)
Expand Down

0 comments on commit b97f1a9

Please sign in to comment.