Skip to content

Commit 57890ab

Browse files
committed
Remove two unneeded tests
Test 1 is a duplicate of test_size() later in the file. Inexplicably, this test does not work on macOS, whereas test_size() does. Test 2 is problematic for two reasons. First, it always fails with an invalid checksum, which is probably not what was intended. Second, it's not defined at this layer what the behavior should be. Hypothetically, if this test was fixed so that it gave messages with valid checksums, then the message would pass successfully thought the network layer and fail only in the processing layer. A priori the network layer has no idea what the size of a message "actually" is. The "Why does behavior change at 78 bytes" is because of the following: print(len(node.p2p.build_message(msg))) # 125 => Payload size = 125 - 24 = 101 If we take 77 bytes, then there are 101 - 77 = 24 left That's exactly the size of a header So, bitcoind deserializes the header and rejects it for some other reason (Almost always an invalid size (too large)) But, if we take 78 bytes, then there are 101 - 78 = 23 left That's not enough to fill a header, so the socket stays open waiting for more data. That's why we sometimes have to push additional data in order for the peer to disconnect. Additionally, both of these tests use the "conn" variable. For fun, go look at where it's declared. (Hint: test_large_inv(). Don't we all love python's idea of scope?)
1 parent b3091b2 commit 57890ab

File tree

1 file changed

+0
-86
lines changed

1 file changed

+0
-86
lines changed

test/functional/p2p_invalid_messages.py

-86
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"""Test node responses to invalid network messages."""
66
import asyncio
77
import struct
8-
import sys
98

109
from test_framework.messages import (
1110
CBlockHeader,
@@ -50,11 +49,6 @@ def run_test(self):
5049
. Test msg header
5150
0. Send a bunch of large (4MB) messages of an unrecognized type. Check to see
5251
that it isn't an effective DoS against the node.
53-
54-
1. Send an oversized (4MB+) message and check that we're disconnected.
55-
56-
2. Send a few messages with an incorrect data size in the header, ensure the
57-
messages are ignored.
5852
"""
5953
self.test_magic_bytes()
6054
self.test_checksum()
@@ -95,66 +89,6 @@ def run_test(self):
9589
node.p2p.sync_with_ping(timeout=400)
9690
assert node.p2p.is_connected
9791

98-
#
99-
# 1.
100-
#
101-
# Send an oversized message, ensure we're disconnected.
102-
#
103-
# Under macOS this test is skipped due to an unexpected error code
104-
# returned from the closing socket which python/asyncio does not
105-
# yet know how to handle.
106-
#
107-
if sys.platform != 'darwin':
108-
msg_over_size = msg_unrecognized(str_data="b" * (valid_data_limit + 1))
109-
assert len(msg_over_size.serialize()) == (msg_limit + 1)
110-
111-
# An unknown message type (or *any* message type) over
112-
# MAX_PROTOCOL_MESSAGE_LENGTH should result in a disconnect.
113-
node.p2p.send_message(msg_over_size)
114-
node.p2p.wait_for_disconnect(timeout=4)
115-
116-
node.disconnect_p2ps()
117-
conn = node.add_p2p_connection(P2PDataStore())
118-
conn.wait_for_verack()
119-
else:
120-
self.log.info("Skipping test p2p_invalid_messages/1 (oversized message) under macOS")
121-
122-
#
123-
# 2.
124-
#
125-
# Send messages with an incorrect data size in the header.
126-
#
127-
actual_size = 100
128-
msg = msg_unrecognized(str_data="b" * actual_size)
129-
130-
# TODO: handle larger-than cases. I haven't been able to pin down what behavior to expect.
131-
for wrong_size in (2, 77, 78, 79):
132-
self.log.info("Sending a message with incorrect size of {}".format(wrong_size))
133-
134-
# Unmodified message should submit okay.
135-
node.p2p.send_and_ping(msg)
136-
137-
# A message lying about its data size results in a disconnect when the incorrect
138-
# data size is less than the actual size.
139-
#
140-
# TODO: why does behavior change at 78 bytes?
141-
#
142-
node.p2p.send_raw_message(self._tweak_msg_data_size(msg, wrong_size))
143-
144-
# For some reason unknown to me, we sometimes have to push additional data to the
145-
# peer in order for it to realize a disconnect.
146-
try:
147-
node.p2p.send_message(msg_ping(nonce=123123))
148-
except IOError:
149-
pass
150-
151-
node.p2p.wait_for_disconnect(timeout=10)
152-
node.disconnect_p2ps()
153-
node.add_p2p_connection(P2PDataStore())
154-
155-
# Node is still up.
156-
conn = node.add_p2p_connection(P2PDataStore())
157-
15892
def test_magic_bytes(self):
15993
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
16094

@@ -225,26 +159,6 @@ def test_large_inv(self):
225159
conn.send_and_ping(msg)
226160
self.nodes[0].disconnect_p2ps()
227161

228-
def _tweak_msg_data_size(self, message, wrong_size):
229-
"""
230-
Return a raw message based on another message but with an incorrect data size in
231-
the message header.
232-
"""
233-
raw_msg = self.node.p2p.build_message(message)
234-
235-
bad_size_bytes = struct.pack("<I", wrong_size)
236-
num_header_bytes_before_size = 4 + 12
237-
238-
# Replace the correct data size in the message with an incorrect one.
239-
raw_msg_with_wrong_size = (
240-
raw_msg[:num_header_bytes_before_size] +
241-
bad_size_bytes +
242-
raw_msg[(num_header_bytes_before_size + len(bad_size_bytes)):]
243-
)
244-
assert len(raw_msg) == len(raw_msg_with_wrong_size)
245-
246-
return raw_msg_with_wrong_size
247-
248162

249163
if __name__ == '__main__':
250164
InvalidMessagesTest().main()

0 commit comments

Comments
 (0)