Skip to content

Commit 7e71734

Browse files
committed
test: send initial_v2_handshake bytes manually
1 parent b2b2b1e commit 7e71734

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

test/functional/p2p_v2_earlykeyresponse.py

+13-24
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TestEncryptedP2PState(EncryptedP2PState):
1515
""" Modify v2 P2P protocol functions for testing that "The responder waits until one byte is received which does
1616
not match the 16 bytes consisting of the network magic followed by "version\x00\x00\x00\x00\x00"." (see BIP 324)
1717
18-
- if `send_net_magic` is True, send first 4 bytes of ellswift (match network magic) else send remaining 60 bytes
18+
- `magic_sent` is initially False and set to True when network magic is sent
1919
- `can_data_be_received` is a variable used to assert if data is received on recvbuf.
2020
- v2 TestNode shouldn't respond back if we send V1_PREFIX and data shouldn't be received on recvbuf.
2121
This state is represented using `can_data_be_received` = False.
@@ -25,43 +25,30 @@ class TestEncryptedP2PState(EncryptedP2PState):
2525

2626
def __init__(self):
2727
super().__init__(initiating=True, net='regtest')
28-
self.send_net_magic = True
28+
self.magic_sent = False
2929
self.can_data_be_received = False
3030

3131
def initiate_v2_handshake(self, garbage_len=random.randrange(4096)):
32-
"""Initiator begins the v2 handshake by sending its ellswift bytes and garbage.
33-
Here, the 64 bytes ellswift is assumed to have it's 4 bytes match network magic bytes. It is sent in 2 phases:
34-
1. when `send_network_magic` = True, send first 4 bytes of ellswift (matches network magic bytes)
35-
2. when `send_network_magic` = False, send remaining 60 bytes of ellswift
36-
"""
37-
if self.send_net_magic:
38-
self.privkey_ours, self.ellswift_ours = ellswift_create()
39-
self.sent_garbage = random.randbytes(garbage_len)
40-
self.send_net_magic = False
41-
return b"\xfa\xbf\xb5\xda"
42-
else:
43-
self.can_data_be_received = True
44-
return self.ellswift_ours[4:] + self.sent_garbage
32+
self.privkey_ours, self.ellswift_ours = ellswift_create()
33+
self.sent_garbage = random.randbytes(random.randrange(4096))
34+
return b""
4535

4636

4737
class PeerEarlyKey(P2PInterface):
4838
"""Custom implementation of P2PInterface which uses modified v2 P2P protocol functions for testing purposes."""
4939
def __init__(self):
5040
super().__init__()
5141
self.v2_state = None
52-
self.connection_opened = False
5342

5443
def connection_made(self, transport):
5544
"""64 bytes ellswift is sent in 2 parts during `initial_v2_handshake()`"""
5645
self.v2_state = TestEncryptedP2PState()
5746
super().connection_made(transport)
5847

5948
def data_received(self, t):
60-
# check that data can be received on recvbuf only when mismatch from V1_PREFIX happens (send_net_magic = False)
61-
assert self.v2_state.can_data_be_received and not self.v2_state.send_net_magic
49+
# check that data can be received on recvbuf only when mismatch from V1_PREFIX happens (magic_sent = True)
50+
assert self.v2_state.can_data_be_received and self.v2_state.magic_sent
6251

63-
def on_open(self):
64-
self.connection_opened = True
6552

6653
class P2PEarlyKey(BitcoinTestFramework):
6754
def set_test_params(self):
@@ -74,13 +61,15 @@ def run_test(self):
7461
node0 = self.nodes[0]
7562
self.log.info('Sending first 4 bytes of ellswift which match network magic')
7663
self.log.info('If a response is received, assertion failure would happen in our custom data_received() function')
77-
# send happens in `initiate_v2_handshake()` in `connection_made()`
7864
peer1 = node0.add_p2p_connection(PeerEarlyKey(), wait_for_verack=False, send_version=False, supports_v2_p2p=True)
79-
self.wait_until(lambda: peer1.connection_opened)
65+
# Send only the network magic first
66+
peer1.send_raw_message(b"\xfa\xbf\xb5\xda") # peer1.send_raw_message(peer1.v2_state.ellswift_ours[:4])
67+
peer1.v2_state.magic_sent = True
8068
self.log.info('Sending remaining ellswift and garbage which are different from V1_PREFIX. Since a response is')
8169
self.log.info('expected now, our custom data_received() function wouldn\'t result in assertion failure')
82-
ellswift_and_garbage_data = peer1.v2_state.initiate_v2_handshake()
83-
peer1.send_raw_message(ellswift_and_garbage_data)
70+
peer1.send_raw_message(peer1.v2_state.ellswift_ours[4:] + peer1.v2_state.sent_garbage)
71+
import time; time.sleep(2)
72+
peer1.v2_state.can_data_be_received = True
8473
peer1.wait_for_disconnect(timeout=5)
8574
self.log.info('successful disconnection when MITM happens in the key exchange phase')
8675

0 commit comments

Comments
 (0)