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 import messages | ||||
from test_framework.mininode import P2PDataStore, NetworkThread | from test_framework.mininode import P2PDataStore, NetworkThread | ||||
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.""" | ||||
command = 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 messages.ser_string(self.str_data) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "{}(data={})".format(self.command, 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 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
def run_test(self): | def run_test(self): | ||||
""" | """ | ||||
. Test msg header | . Test msg header | ||||
0. Send a bunch of large (2MB) messages of an unrecognized type. Check to see | 0. Send a bunch of large (2MB) messages of an unrecognized type. Check to see | ||||
that it isn't an effective DoS against the node. | that it isn't an effective DoS against the node. | ||||
1. Send an oversized (2MB+) message and check that we're disconnected. | 1. Send an oversized (2MB+) message and check that we're disconnected. | ||||
2. Send a few messages with an incorrect data size in the header, ensure the | 2. Send a few messages with an incorrect data size in the header, ensure the | ||||
messages are ignored. | messages are ignored. | ||||
""" | """ | ||||
self.test_magic_bytes() | self.test_magic_bytes() | ||||
self.test_checksum() | self.test_checksum() | ||||
self.test_size() | self.test_size() | ||||
self.test_command() | self.test_msgtype() | ||||
node = self.nodes[0] | node = self.nodes[0] | ||||
self.node = node | self.node = node | ||||
node.add_p2p_connection(P2PDataStore()) | node.add_p2p_connection(P2PDataStore()) | ||||
conn2 = node.add_p2p_connection(P2PDataStore()) | conn2 = node.add_p2p_connection(P2PDataStore()) | ||||
# 2MB, per MAX_PROTOCOL_MESSAGE_LENGTH | # 2MB, per MAX_PROTOCOL_MESSAGE_LENGTH | ||||
msg_limit = 2 * 1024 * 1024 | msg_limit = 2 * 1024 * 1024 | ||||
▲ Show 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | class InvalidMessagesTest(BitcoinTestFramework): | ||||
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 = ( | ||||
# magic | # magic | ||||
4 + | 4 + | ||||
# command | # msgtype | ||||
12 + | 12 + | ||||
# len | # len | ||||
4 | 4 | ||||
) | ) | ||||
# 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) | ||||
conn.wait_for_disconnect() | conn.wait_for_disconnect() | ||||
Show All 11 Lines | def test_size(self): | ||||
) | ) | ||||
# modify len to MAX_SIZE + 1 | # modify len to MAX_SIZE + 1 | ||||
msg = msg[:cut_len] + \ | msg = msg[:cut_len] + \ | ||||
struct.pack("<I", 0x02000000 + 1) + msg[cut_len + 4:] | struct.pack("<I", 0x02000000 + 1) + msg[cut_len + 4:] | ||||
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) | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
def test_command(self): | def test_msgtype(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: ERRORS IN HEADER']): | with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: ERRORS IN HEADER']): | ||||
msg = msg_unrecognized(str_data="d") | msg = msg_unrecognized(str_data="d") | ||||
msg.command = b'\xff' * 12 | msg.msgtype = b'\xff' * 12 | ||||
msg = conn.build_message(msg) | msg = conn.build_message(msg) | ||||
# Modify command | # Modify msgtype | ||||
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 _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 | ||||
Show All 20 Lines |