Skip to content

Commit 8328112

Browse files
committed
test: Check that non empty version packet is ignored and no disconnection happens
This test type is represented using SEND_NON_EMPTY_VERSION_PACKET.
1 parent 6267b87 commit 8328112

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

test/functional/p2p_v2_misbehaving.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class TestType(Enum):
2626
2. EXCESS_GARBAGE - Disconnection happens when > MAX_GARBAGE_LEN bytes garbage is sent
2727
3. WRONG_GARBAGE_TERMINATOR - Disconnection happens when incorrect garbage terminator is sent
2828
4. WRONG_GARBAGE - Disconnection happens when garbage bytes that is sent is different from what the peer receives
29+
5. SEND_NON_EMPTY_VERSION_PACKET - non-empty version packet is simply ignored
2930
"""
3031
EARLY_KEY_RESPONSE = 0
3132
EXCESS_GARBAGE = 1
3233
WRONG_GARBAGE_TERMINATOR = 2
3334
WRONG_GARBAGE = 3
35+
SEND_NON_EMPTY_VERSION_PACKET = 4
3436

3537

3638
class TestEncryptedP2PState(EncryptedP2PState):
@@ -79,9 +81,9 @@ def initiate_v2_handshake(self):
7981
return super().initiate_v2_handshake()
8082

8183
def complete_handshake(self, response):
82-
"""Add option for sending wrong garbage terminator. Also option for when
84+
"""Add option for sending wrong garbage terminator or a non-empty transport version packet. Also option for when
8385
there's a mismatch between garbage bytes used here vs garbage bytes which were sent out.
84-
For TestType = (WRONG_GARBAGE_TERMINATOR, WRONG_GARBAGE)
86+
For TestType = (WRONG_GARBAGE_TERMINATOR, WRONG_GARBAGE, SEND_NON_EMPTY_VERSION_PACKET)
8587
"""
8688
ellswift_theirs = self.received_prefix + response.read(64 - len(self.received_prefix))
8789
# return b"" if we need to receive more bytes
@@ -102,7 +104,10 @@ def complete_handshake(self, response):
102104
msg_to_send += self.v2_enc_packet(decoy_content_len * b'\x00', aad=aad, ignore=True)
103105
aad = b''
104106
# Send version packet.
105-
msg_to_send += self.v2_enc_packet(TRANSPORT_VERSION, aad=aad)
107+
if self.test_type == TestType.SEND_NON_EMPTY_VERSION_PACKET:
108+
msg_to_send += self.v2_enc_packet(random.randbytes(5), aad=aad)
109+
else:
110+
msg_to_send += self.v2_enc_packet(TRANSPORT_VERSION, aad=aad)
106111
return 64 - len(self.received_prefix), msg_to_send
107112

108113

@@ -165,13 +170,18 @@ def test_v2disconnection(self):
165170
["V2 transport error: missing garbage terminator, peer=1"], # EXCESS_GARBAGE
166171
["version handshake timeout peer=2"], # WRONG_GARBAGE_TERMINATOR
167172
["V2 transport error: packet decryption failure"], # WRONG_GARBAGE
173+
[], # SEND_NON_EMPTY_VERSION_PACKET
168174
]
169175
for test_type in TestType:
170176
if test_type == TestType.EARLY_KEY_RESPONSE:
171177
continue
172-
with self.nodes[0].assert_debug_log(expected_debug_message[test_type.value], timeout=5):
173-
node0.add_p2p_connection(MisbehavingV2Peer(test_type), wait_for_verack=False, send_version=False, supports_v2_p2p=True, wait_for_disconnect=True)
174-
self.log.info(f"Expected disconnection for {test_type.name}")
178+
elif test_type == TestType.SEND_NON_EMPTY_VERSION_PACKET:
179+
node0.add_p2p_connection(MisbehavingV2Peer(test_type), wait_for_verack=True, send_version=True, supports_v2_p2p=True)
180+
self.log.info(f"No disconnection for {test_type.name}")
181+
else:
182+
with self.nodes[0].assert_debug_log(expected_debug_message[test_type.value], timeout=5):
183+
node0.add_p2p_connection(MisbehavingV2Peer(test_type), wait_for_verack=False, send_version=False, supports_v2_p2p=True, wait_for_disconnect=True)
184+
self.log.info(f"Expected disconnection for {test_type.name}")
175185

176186

177187
if __name__ == '__main__':

0 commit comments

Comments
 (0)