mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
Merge #20028: test: Check that invalid peer traffic is accounted for
faa94cb167
test: Check that invalid peer traffic is accounted for (MarcoFalke)fae243f0cb
test: Remove confusing cast to same type (int to int) (MarcoFalke) Pull request description: Couldn't find a test for this and it seems something we should test, so I wrote one. ACKs for top commit: vasild: ACKfaa94cb16
practicalswift: ACKfaa94cb167
: patch looks correct Tree-SHA512: efcdd35960045cdfbd14480e16e0d1d09e81eb01670ac541ac2b105e1a63818a157c95853270242234a224880873e79957832bf4231374d7fe81de30f35e3abf
This commit is contained in:
commit
7ea649946d
1 changed files with 10 additions and 7 deletions
|
@ -22,12 +22,11 @@ from test_framework.p2p import (
|
||||||
P2PInterface,
|
P2PInterface,
|
||||||
)
|
)
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import (
|
from test_framework.util import assert_equal
|
||||||
assert_equal,
|
|
||||||
)
|
|
||||||
|
|
||||||
VALID_DATA_LIMIT = MAX_PROTOCOL_MESSAGE_LENGTH - 5 # Account for the 5-byte length prefix
|
VALID_DATA_LIMIT = MAX_PROTOCOL_MESSAGE_LENGTH - 5 # Account for the 5-byte length prefix
|
||||||
|
|
||||||
|
|
||||||
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."""
|
||||||
|
|
||||||
|
@ -66,11 +65,11 @@ class InvalidMessagesTest(BitcoinTestFramework):
|
||||||
msg = conn.build_message(msg_ping(nonce=12345))
|
msg = conn.build_message(msg_ping(nonce=12345))
|
||||||
cut_pos = 12 # Chosen at an arbitrary position within the header
|
cut_pos = 12 # Chosen at an arbitrary position within the header
|
||||||
# Send message in two pieces
|
# Send message in two pieces
|
||||||
before = int(self.nodes[0].getnettotals()['totalbytesrecv'])
|
before = self.nodes[0].getnettotals()['totalbytesrecv']
|
||||||
conn.send_raw_message(msg[:cut_pos])
|
conn.send_raw_message(msg[:cut_pos])
|
||||||
# Wait until node has processed the first half of the message
|
# Wait until node has processed the first half of the message
|
||||||
self.wait_until(lambda: int(self.nodes[0].getnettotals()['totalbytesrecv']) != before)
|
self.wait_until(lambda: self.nodes[0].getnettotals()['totalbytesrecv'] != before)
|
||||||
middle = int(self.nodes[0].getnettotals()['totalbytesrecv'])
|
middle = self.nodes[0].getnettotals()['totalbytesrecv']
|
||||||
# If this assert fails, we've hit an unlikely race
|
# If this assert fails, we've hit an unlikely race
|
||||||
# where the test framework sent a message in between the two halves
|
# where the test framework sent a message in between the two halves
|
||||||
assert_equal(middle, before + cut_pos)
|
assert_equal(middle, before + cut_pos)
|
||||||
|
@ -100,6 +99,8 @@ class InvalidMessagesTest(BitcoinTestFramework):
|
||||||
msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:]
|
msg = msg[:cut_len] + b'\xff' * 4 + msg[cut_len + 4:]
|
||||||
conn.send_raw_message(msg)
|
conn.send_raw_message(msg)
|
||||||
conn.sync_with_ping(timeout=1)
|
conn.sync_with_ping(timeout=1)
|
||||||
|
# Check that traffic is accounted for (24 bytes header + 2 bytes payload)
|
||||||
|
assert_equal(self.nodes[0].getpeerinfo()[0]['bytesrecv_per_msg']['*other*'], 26)
|
||||||
self.nodes[0].disconnect_p2ps()
|
self.nodes[0].disconnect_p2ps()
|
||||||
|
|
||||||
def test_size(self):
|
def test_size(self):
|
||||||
|
@ -123,6 +124,8 @@ class InvalidMessagesTest(BitcoinTestFramework):
|
||||||
msg = msg[:7] + b'\x00' + msg[7 + 1:]
|
msg = msg[:7] + b'\x00' + msg[7 + 1:]
|
||||||
conn.send_raw_message(msg)
|
conn.send_raw_message(msg)
|
||||||
conn.sync_with_ping(timeout=1)
|
conn.sync_with_ping(timeout=1)
|
||||||
|
# Check that traffic is accounted for (24 bytes header + 2 bytes payload)
|
||||||
|
assert_equal(self.nodes[0].getpeerinfo()[0]['bytesrecv_per_msg']['*other*'], 26)
|
||||||
self.nodes[0].disconnect_p2ps()
|
self.nodes[0].disconnect_p2ps()
|
||||||
|
|
||||||
def test_oversized_msg(self, msg, size):
|
def test_oversized_msg(self, msg, size):
|
||||||
|
|
Loading…
Add table
Reference in a new issue