Skip to content

Commit 5c4648d

Browse files
committed
Fix "invalid message size" test
This test originally made a message with an invalid stated length, and an invalid checksum. This was because only the header was changed, but the checksum stayed the same. This was fine for now because we check the header first to see if it has a valid stated size, and we disconnect if it does not, so we never end up checking for the checksum. If this behavior was to change, this test would become a problem. (Indeed I discovered this when playing around with this behavior). By instead creating a message with an oversized payload from the start, we create a message with an invalid stated length but a valid checksum, as intended. Additionally, this takes advantage to the newly module-global VALID_DATA_LIMIT as opposed to the magic 0x02000000. Yes, 4MB < 32MiB, but at the moment when receiving a message we check both, so this makes the test tighter.
1 parent ff1e7b8 commit 5c4648d

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

test/functional/p2p_invalid_messages.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test node responses to invalid network messages."""
66
import asyncio
7-
import struct
87

98
from test_framework.messages import (
109
CBlockHeader,
@@ -123,13 +122,9 @@ def test_checksum(self):
123122
def test_size(self):
124123
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
125124
with self.nodes[0].assert_debug_log(['']):
126-
msg = conn.build_message(msg_unrecognized(str_data="d"))
127-
cut_len = (
128-
4 + # magic
129-
12 # msgtype
130-
)
131-
# modify len to MAX_SIZE + 1
132-
msg = msg[:cut_len] + struct.pack("<I", 0x02000000 + 1) + msg[cut_len + 4:]
125+
# Create a message with oversized payload
126+
msg = msg_unrecognized(str_data="d"*(VALID_DATA_LIMIT + 1))
127+
msg = conn.build_message(msg)
133128
self.nodes[0].p2p.send_raw_message(msg)
134129
conn.wait_for_disconnect(timeout=1)
135130
self.nodes[0].disconnect_p2ps()

0 commit comments

Comments
 (0)