Skip to content

Commit 0ca0e50

Browse files
stratospherjanus
authored andcommitted
test: Check that disconnection happens when >4095 garbage bytes is sent
This test type is represented using EXCESS_GARBAGE.
1 parent a45ec31 commit 0ca0e50

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

test/functional/p2p_v2_earlykeyresponse.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6+
import random
67
from enum import Enum
78

89
from test_framework.messages import MAGIC_BYTES
910
from test_framework.p2p import P2PInterface
1011
from test_framework.test_framework import BGLTestFramework
11-
from test_framework.v2_p2p import EncryptedP2PState
12+
from test_framework.v2_p2p import (
13+
EncryptedP2PState,
14+
MAX_GARBAGE_LEN,
15+
)
1216

1317

1418
class TestType(Enum):
1519
""" Scenarios to be tested:
1620
1721
1. EARLY_KEY_RESPONSE - The responder needs to wait until one byte is received which does not match the 16 bytes
1822
consisting of network magic followed by "version\x00\x00\x00\x00\x00" before sending out its ellswift + garbage bytes
23+
2. EXCESS_GARBAGE - Disconnection happens when > MAX_GARBAGE_LEN bytes garbage is sent
1924
"""
2025
EARLY_KEY_RESPONSE = 0
26+
EXCESS_GARBAGE = 1
2127

2228

2329
class EarlyKeyResponseState(EncryptedP2PState):
@@ -32,6 +38,13 @@ def initiate_v2_handshake(self):
3238
return b""
3339

3440

41+
class ExcessGarbageState(EncryptedP2PState):
42+
"""Generate > MAX_GARBAGE_LEN garbage bytes"""
43+
def generate_keypair_and_garbage(self):
44+
garbage_len = MAX_GARBAGE_LEN + random.randrange(1, MAX_GARBAGE_LEN + 1)
45+
return super().generate_keypair_and_garbage(garbage_len)
46+
47+
3548
class MisbehavingV2Peer(P2PInterface):
3649
"""Custom implementation of P2PInterface which uses modified v2 P2P protocol functions for testing purposes."""
3750
def __init__(self, test_type):
@@ -41,6 +54,8 @@ def __init__(self, test_type):
4154
def connection_made(self, transport):
4255
if self.test_type == TestType.EARLY_KEY_RESPONSE:
4356
self.v2_state = EarlyKeyResponseState(initiating=True, net='regtest')
57+
elif self.test_type == TestType.EXCESS_GARBAGE:
58+
self.v2_state = ExcessGarbageState(initiating=True, net='regtest')
4459
super().connection_made(transport)
4560

4661
def data_received(self, t):
@@ -58,6 +73,7 @@ def set_test_params(self):
5873

5974
def run_test(self):
6075
self.test_earlykeyresponse()
76+
self.test_v2disconnection()
6177

6278
def test_earlykeyresponse(self):
6379
self.log.info('Sending ellswift bytes in parts to ensure that response from responder is received only when')
@@ -75,6 +91,21 @@ def test_earlykeyresponse(self):
7591
peer1.wait_for_disconnect(timeout=5)
7692
self.log.info('successful disconnection since modified ellswift was sent as response')
7793

94+
def test_v2disconnection(self):
95+
# test v2 disconnection scenarios
96+
node0 = self.nodes[0]
97+
expected_debug_message = [
98+
[], # EARLY_KEY_RESPONSE
99+
["V2 transport error: missing garbage terminator, peer=1"], # EXCESS_GARBAGE
100+
]
101+
for test_type in TestType:
102+
if test_type == TestType.EARLY_KEY_RESPONSE:
103+
continue
104+
with node0.assert_debug_log(expected_debug_message[test_type.value], timeout=5):
105+
peer = node0.add_p2p_connection(MisbehavingV2Peer(test_type), wait_for_verack=False, send_version=False, supports_v2_p2p=True, expect_success=False)
106+
peer.wait_for_disconnect()
107+
self.log.info(f"Expected disconnection for {test_type.name}")
108+
78109

79110
if __name__ == '__main__':
80111
EncryptedP2PMisbehaving().main()

test/functional/test_framework/v2_p2p.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ def v2_ecdh(priv, ellswift_theirs, ellswift_ours, initiating):
111111
# Responding, place their public key encoding first.
112112
return TaggedHash("bip324_ellswift_xonly_ecdh", ellswift_theirs + ellswift_ours + ecdh_point_x32)
113113

114-
def generate_keypair_and_garbage(self):
114+
def generate_keypair_and_garbage(self, garbage_len=None):
115115
"""Generates ellswift keypair and 4095 bytes garbage at max"""
116116
self.privkey_ours, self.ellswift_ours = ellswift_create()
117-
garbage_len = random.randrange(MAX_GARBAGE_LEN + 1)
117+
if garbage_len is None:
118+
garbage_len = random.randrange(MAX_GARBAGE_LEN + 1)
118119
self.sent_garbage = random.randbytes(garbage_len)
119120
logger.debug(f"sending {garbage_len} bytes of garbage data")
120121
return self.ellswift_ours + self.sent_garbage

0 commit comments

Comments
 (0)