Skip to content

Commit ad1482d

Browse files
committed
test: Check that disconnection happens when wrong garbage terminator is sent
This test type is represented using WRONG_GARBAGE_TERMINATOR. since the wrong garbage terminator is sent to TestNode, TestNode will interpret all of the gabage bytes, wrong garbage terminator, decoy messages and version packet it receives as garbage bytes. If the length of all these is more than 4095 + 16, it will result in a missing garbage terminator error. otherwise, it will result in a V2 handshake timeout error. Send only MAX_GARBAGE_LEN//2 bytes of garbage data to TestNode so that the total length received by the TestNode is at max = (MAX_GARBAGE_LEN//2) + 16 + 10*120 + 20 = 3283 bytes (which is less than 4095 + 16 bytes) and we get a consistent V2 handshake timeout error message. If we do not limit the garbage length sent, we will intermittently get both missing garbage terminator error and V2 handshake timeout error based on the garbage length and decoy packets length which are chosen at random.
1 parent e351576 commit ad1482d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

test/functional/p2p_v2_misbehaving.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from test_framework.messages import MAGIC_BYTES
1010
from test_framework.p2p import P2PInterface
1111
from test_framework.test_framework import BitcoinTestFramework
12+
from test_framework.util import random_bitflip
1213
from test_framework.v2_p2p import (
1314
EncryptedP2PState,
1415
MAX_GARBAGE_LEN,
@@ -21,9 +22,11 @@ class TestType(Enum):
2122
1. EARLY_KEY_RESPONSE - The responder needs to wait until one byte is received which does not match the 16 bytes
2223
consisting of network magic followed by "version\x00\x00\x00\x00\x00" before sending out its ellswift + garbage bytes
2324
2. EXCESS_GARBAGE - Disconnection happens when > MAX_GARBAGE_LEN bytes garbage is sent
25+
3. WRONG_GARBAGE_TERMINATOR - Disconnection happens when incorrect garbage terminator is sent
2426
"""
2527
EARLY_KEY_RESPONSE = 0
2628
EXCESS_GARBAGE = 1
29+
WRONG_GARBAGE_TERMINATOR = 2
2730

2831

2932
class EarlyKeyResponseState(EncryptedP2PState):
@@ -45,6 +48,19 @@ def generate_keypair_and_garbage(self):
4548
return super().generate_keypair_and_garbage(garbage_len)
4649

4750

51+
class WrongGarbageTerminatorState(EncryptedP2PState):
52+
"""Add option for sending wrong garbage terminator"""
53+
def generate_keypair_and_garbage(self):
54+
garbage_len = random.randrange(MAX_GARBAGE_LEN//2)
55+
return super().generate_keypair_and_garbage(garbage_len)
56+
57+
def complete_handshake(self, response):
58+
length, handshake_bytes = super().complete_handshake(response)
59+
# first 16 bytes returned by complete_handshake() is the garbage terminator
60+
wrong_garbage_terminator = random_bitflip(handshake_bytes[:16])
61+
return length, wrong_garbage_terminator + handshake_bytes[16:]
62+
63+
4864
class MisbehavingV2Peer(P2PInterface):
4965
"""Custom implementation of P2PInterface which uses modified v2 P2P protocol functions for testing purposes."""
5066
def __init__(self, test_type):
@@ -56,6 +72,8 @@ def connection_made(self, transport):
5672
self.v2_state = EarlyKeyResponseState(initiating=True, net='regtest')
5773
elif self.test_type == TestType.EXCESS_GARBAGE:
5874
self.v2_state = ExcessGarbageState(initiating=True, net='regtest')
75+
elif self.test_type == TestType.WRONG_GARBAGE_TERMINATOR:
76+
self.v2_state = WrongGarbageTerminatorState(initiating=True, net='regtest')
5977
super().connection_made(transport)
6078

6179
def data_received(self, t):
@@ -97,6 +115,7 @@ def test_v2disconnection(self):
97115
expected_debug_message = [
98116
[], # EARLY_KEY_RESPONSE
99117
["V2 transport error: missing garbage terminator, peer=1"], # EXCESS_GARBAGE
118+
["V2 handshake timeout peer=2"], # WRONG_GARBAGE_TERMINATOR
100119
]
101120
for test_type in TestType:
102121
if test_type == TestType.EARLY_KEY_RESPONSE:

0 commit comments

Comments
 (0)