Skip to content

Commit b5e6238

Browse files
committed
test: Check that disconnection happens when garbage sent/received are different
This test type is represented using WRONG_GARBAGE. Here, garbage bytes sent to TestNode are assumed to be tampered with and do not correspond to the garbage bytes which P2PInterface calculated and uses.
1 parent ad1482d commit b5e6238

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Diff for: test/functional/p2p_v2_misbehaving.py

+14
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ class TestType(Enum):
2323
consisting of network magic followed by "version\x00\x00\x00\x00\x00" before sending out its ellswift + garbage bytes
2424
2. EXCESS_GARBAGE - Disconnection happens when > MAX_GARBAGE_LEN bytes garbage is sent
2525
3. WRONG_GARBAGE_TERMINATOR - Disconnection happens when incorrect garbage terminator is sent
26+
4. WRONG_GARBAGE - Disconnection happens when garbage bytes that is sent is different from what the peer receives
2627
"""
2728
EARLY_KEY_RESPONSE = 0
2829
EXCESS_GARBAGE = 1
2930
WRONG_GARBAGE_TERMINATOR = 2
31+
WRONG_GARBAGE = 3
3032

3133

3234
class EarlyKeyResponseState(EncryptedP2PState):
@@ -61,6 +63,15 @@ def complete_handshake(self, response):
6163
return length, wrong_garbage_terminator + handshake_bytes[16:]
6264

6365

66+
class WrongGarbageState(EncryptedP2PState):
67+
"""Generate tampered garbage bytes"""
68+
def generate_keypair_and_garbage(self):
69+
garbage_len = random.randrange(1, MAX_GARBAGE_LEN)
70+
ellswift_garbage_bytes = super().generate_keypair_and_garbage(garbage_len)
71+
# assume that garbage bytes sent to TestNode were tampered with
72+
return ellswift_garbage_bytes[:64] + random_bitflip(ellswift_garbage_bytes[64:])
73+
74+
6475
class MisbehavingV2Peer(P2PInterface):
6576
"""Custom implementation of P2PInterface which uses modified v2 P2P protocol functions for testing purposes."""
6677
def __init__(self, test_type):
@@ -74,6 +85,8 @@ def connection_made(self, transport):
7485
self.v2_state = ExcessGarbageState(initiating=True, net='regtest')
7586
elif self.test_type == TestType.WRONG_GARBAGE_TERMINATOR:
7687
self.v2_state = WrongGarbageTerminatorState(initiating=True, net='regtest')
88+
elif self.test_type == TestType.WRONG_GARBAGE:
89+
self.v2_state = WrongGarbageState(initiating=True, net='regtest')
7790
super().connection_made(transport)
7891

7992
def data_received(self, t):
@@ -116,6 +129,7 @@ def test_v2disconnection(self):
116129
[], # EARLY_KEY_RESPONSE
117130
["V2 transport error: missing garbage terminator, peer=1"], # EXCESS_GARBAGE
118131
["V2 handshake timeout peer=2"], # WRONG_GARBAGE_TERMINATOR
132+
["V2 transport error: packet decryption failure"], # WRONG_GARBAGE
119133
]
120134
for test_type in TestType:
121135
if test_type == TestType.EARLY_KEY_RESPONSE:

0 commit comments

Comments
 (0)