Changeset View
Changeset View
Standalone View
Standalone View
test/functional/p2p_invalid_messages.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2019 The Bitcoin Core developers | # Copyright (c) 2015-2019 The Bitcoin Core developers | ||||
# 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 | import asyncio | ||||
import struct | import struct | ||||
from test_framework import messages | from test_framework.messages import ( | ||||
CBlockHeader, | |||||
CInv, | |||||
msg_getdata, | |||||
msg_headers, | |||||
msg_inv, | |||||
msg_ping, | |||||
MSG_TX, | |||||
ser_string, | |||||
) | |||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
NetworkThread, | NetworkThread, | ||||
P2PDataStore, | P2PDataStore, | ||||
P2PInterface, | P2PInterface, | ||||
) | ) | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
class msg_unrecognized: | class msg_unrecognized: | ||||
"""Nonsensical message. Modeled after similar types in test_framework.messages.""" | """Nonsensical message. Modeled after similar types in test_framework.messages.""" | ||||
msgtype = b'badmsg' | msgtype = b'badmsg' | ||||
def __init__(self, *, str_data): | def __init__(self, *, str_data): | ||||
self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data | self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data | ||||
def serialize(self): | def serialize(self): | ||||
return messages.ser_string(self.str_data) | return ser_string(self.str_data) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "{}(data={})".format(self.msgtype, self.str_data) | return "{}(data={})".format(self.msgtype, self.str_data) | ||||
class InvalidMessagesTest(BitcoinTestFramework): | class InvalidMessagesTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
▲ Show 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# | # | ||||
node.p2p.send_raw_message( | node.p2p.send_raw_message( | ||||
self._tweak_msg_data_size( | self._tweak_msg_data_size( | ||||
msg, wrong_size)) | msg, wrong_size)) | ||||
# For some reason unknown to me, we sometimes have to push additional data to the | # For some reason unknown to me, we sometimes have to push additional data to the | ||||
# peer in order for it to realize a disconnect. | # peer in order for it to realize a disconnect. | ||||
try: | try: | ||||
node.p2p.send_message(messages.msg_ping(nonce=123123)) | node.p2p.send_message(msg_ping(nonce=123123)) | ||||
except IOError: | except IOError: | ||||
pass | pass | ||||
node.p2p.wait_for_disconnect(timeout=10) | node.p2p.wait_for_disconnect(timeout=10) | ||||
node.disconnect_p2ps() | node.disconnect_p2ps() | ||||
node.add_p2p_connection(P2PDataStore()) | node.add_p2p_connection(P2PDataStore()) | ||||
# Node is still up. | # Node is still up. | ||||
Show All 10 Lines | def test_magic_bytes(self): | ||||
# Call .result() to block until the atomic swap is complete, otherwise | # Call .result() to block until the atomic swap is complete, otherwise | ||||
# we might run into races later on | # we might run into races later on | ||||
asyncio.run_coroutine_threadsafe( | asyncio.run_coroutine_threadsafe( | ||||
swap_magic_bytes(), | swap_magic_bytes(), | ||||
NetworkThread.network_event_loop).result() | NetworkThread.network_event_loop).result() | ||||
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART ping']): | with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: INVALID MESSAGESTART ping']): | ||||
conn.send_message(messages.msg_ping(nonce=0xff)) | 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() | ||||
def test_checksum(self): | def test_checksum(self): | ||||
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 = ( | cut_len = ( | ||||
Show All 37 Lines | def test_msgtype(self): | ||||
msg = msg[:7] + b'\x00' + msg[7 + 1:] | msg = msg[:7] + b'\x00' + msg[7 + 1:] | ||||
self.nodes[0].p2p.send_raw_message(msg) | self.nodes[0].p2p.send_raw_message(msg) | ||||
conn.sync_with_ping(timeout=1) | conn.sync_with_ping(timeout=1) | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
def test_large_inv(self): | def test_large_inv(self): | ||||
conn = self.nodes[0].add_p2p_connection(P2PInterface()) | conn = self.nodes[0].add_p2p_connection(P2PInterface()) | ||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (0 -> 20): oversized-inv: message inv size() = 50001']): | with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (0 -> 20): oversized-inv: message inv size() = 50001']): | ||||
msg = messages.msg_inv([messages.CInv(messages.MSG_TX, 1)] * 50001) | msg = msg_inv([CInv(MSG_TX, 1)] * 50001) | ||||
conn.send_and_ping(msg) | conn.send_and_ping(msg) | ||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (20 -> 40): too-many-inv: message getdata size() = 50001']): | with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (20 -> 40): too-many-inv: message getdata size() = 50001']): | ||||
msg = messages.msg_getdata( | msg = msg_getdata([CInv(MSG_TX, 1)] * 50001) | ||||
[messages.CInv(messages.MSG_TX, 1)] * 50001) | |||||
conn.send_and_ping(msg) | conn.send_and_ping(msg) | ||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (40 -> 60): too-many-headers: headers message size = 2001']): | with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (40 -> 60): too-many-headers: headers message size = 2001']): | ||||
msg = messages.msg_headers([messages.CBlockHeader()] * 2001) | msg = msg_headers([CBlockHeader()] * 2001) | ||||
conn.send_and_ping(msg) | conn.send_and_ping(msg) | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
def _tweak_msg_data_size(self, message, wrong_size): | def _tweak_msg_data_size(self, message, wrong_size): | ||||
""" | """ | ||||
Return a raw message based on another message but with an incorrect data size in | Return a raw message based on another message but with an incorrect data size in | ||||
the message header. | the message header. | ||||
""" | """ | ||||
Show All 18 Lines |