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 | ||||
import time | import time | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
CBlockHeader, | CBlockHeader, | ||||
CInv, | CInv, | ||||
msg_avahello, | |||||
msg_avapoll, | |||||
msg_avaresponse, | |||||
msg_getdata, | msg_getdata, | ||||
msg_headers, | msg_headers, | ||||
msg_inv, | msg_inv, | ||||
msg_ping, | msg_ping, | ||||
MSG_TX, | MSG_TX, | ||||
ser_string, | ser_string, | ||||
) | ) | ||||
from test_framework.mininode import ( | from test_framework.mininode import ( | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
self.test_checksum() | self.test_checksum() | ||||
self.test_size() | self.test_size() | ||||
self.test_msgtype() | self.test_msgtype() | ||||
self.test_addrv2_empty() | self.test_addrv2_empty() | ||||
self.test_addrv2_no_addresses() | self.test_addrv2_no_addresses() | ||||
self.test_addrv2_too_long_address() | self.test_addrv2_too_long_address() | ||||
self.test_addrv2_unrecognized_network() | self.test_addrv2_unrecognized_network() | ||||
self.test_large_inv() | self.test_large_inv() | ||||
self.test_unsolicited_ava_messages() | |||||
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 263 Lines • ▼ Show 20 Lines | def test_large_inv(self): | ||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=8 (20 -> 40): too-many-inv: message getdata size() = 50001']): | with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=8 (20 -> 40): too-many-inv: message getdata size() = 50001']): | ||||
msg = msg_getdata([CInv(MSG_TX, 1)] * 50001) | msg = msg_getdata([CInv(MSG_TX, 1)] * 50001) | ||||
conn.send_and_ping(msg) | conn.send_and_ping(msg) | ||||
with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=8 (40 -> 60): too-many-headers: headers message size = 2001']): | with self.nodes[0].assert_debug_log(['Misbehaving', 'peer=8 (40 -> 60): too-many-headers: headers message size = 2001']): | ||||
msg = msg_headers([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 test_unsolicited_ava_messages(self): | |||||
"""Node 0 has avalanche disabled by default. If a node does not | |||||
advertise the avalanche service flag, it does not expect to receive | |||||
any avalanche related message and should consider it as spam. | |||||
""" | |||||
conn = self.nodes[0].add_p2p_connection(P2PInterface()) | |||||
with self.nodes[0].assert_debug_log( | |||||
['Misbehaving', 'peer=9 (0 -> 20): unsolicited-avahello']): | |||||
msg = msg_avahello() | |||||
conn.send_and_ping(msg) | |||||
with self.nodes[0].assert_debug_log( | |||||
['Misbehaving', 'peer=9 (20 -> 40): unsolicited-avapoll']): | |||||
msg = msg_avapoll() | |||||
conn.send_and_ping(msg) | |||||
with self.nodes[0].assert_debug_log( | |||||
['Misbehaving', 'peer=9 (40 -> 60): unsolicited-avaresponse']): | |||||
msg = msg_avaresponse() | |||||
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 |