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 ( | ||||
NetworkThread, | |||||
P2PDataStore, | |||||
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' | ||||
Show All 22 Lines | def run_test(self): | ||||
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_msgtype() | self.test_msgtype() | ||||
self.test_large_inv() | |||||
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 147 Lines • ▼ Show 20 Lines | def test_msgtype(self): | ||||
msg.msgtype = b'\xff' * 12 | msg.msgtype = b'\xff' * 12 | ||||
msg = conn.build_message(msg) | msg = conn.build_message(msg) | ||||
# Modify msgtype | # 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 test_large_inv(self): | |||||
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']): | |||||
msg = messages.msg_inv([messages.CInv(1, 1)] * 50001) | |||||
conn.send_and_ping(msg) | |||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=4 (20 -> 40): too-many-inv: message getdata size() = 50001']): | |||||
msg = messages.msg_getdata([messages.CInv(1, 1)] * 50001) | |||||
conn.send_and_ping(msg) | |||||
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) | |||||
conn.send_and_ping(msg) | |||||
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. | ||||
""" | """ | ||||
raw_msg = self.node.p2p.build_message(message) | raw_msg = self.node.p2p.build_message(message) | ||||
bad_size_bytes = struct.pack("<I", wrong_size) | bad_size_bytes = struct.pack("<I", wrong_size) | ||||
Show All 15 Lines |