@@ -25,12 +25,14 @@ class TestType(Enum):
2525 3. WRONG_GARBAGE_TERMINATOR - Disconnection happens when incorrect garbage terminator is sent
2626 4. WRONG_GARBAGE - Disconnection happens when garbage bytes that is sent is different from what the peer receives
2727 5. SEND_NO_AAD - Disconnection happens when AAD of first encrypted packet after the garbage terminator is not filled
28+ 6. SEND_NON_EMPTY_VERSION_PACKET - non-empty version packet is simply ignored
2829 """
2930 EARLY_KEY_RESPONSE = 0
3031 EXCESS_GARBAGE = 1
3132 WRONG_GARBAGE_TERMINATOR = 2
3233 WRONG_GARBAGE = 3
3334 SEND_NO_AAD = 4
35+ SEND_NON_EMPTY_VERSION_PACKET = 5
3436
3537
3638class EarlyKeyResponseState (EncryptedP2PState ):
@@ -85,6 +87,13 @@ def complete_handshake(self, response):
8587 return super ().complete_handshake (response )
8688
8789
90+ class NonEmptyVersionPacketState (EncryptedP2PState ):
91+ """"Add option for sending non-empty transport version packet."""
92+ def complete_handshake (self , response ):
93+ self .transport_version = random .randbytes (5 )
94+ return super ().complete_handshake (response )
95+
96+
8897class MisbehavingV2Peer (P2PInterface ):
8998 """Custom implementation of P2PInterface which uses modified v2 P2P protocol functions for testing purposes."""
9099 def __init__ (self , test_type ):
@@ -102,6 +111,8 @@ def connection_made(self, transport):
102111 self .v2_state = WrongGarbageState (initiating = True , net = 'regtest' )
103112 elif self .test_type == TestType .SEND_NO_AAD :
104113 self .v2_state = NoAADState (initiating = True , net = 'regtest' )
114+ elif TestType .SEND_NON_EMPTY_VERSION_PACKET :
115+ self .v2_state = NonEmptyVersionPacketState (initiating = True , net = 'regtest' )
105116 super ().connection_made (transport )
106117
107118 def data_received (self , t ):
@@ -146,14 +157,19 @@ def test_v2disconnection(self):
146157 ["V2 handshake timeout peer=2" ], # WRONG_GARBAGE_TERMINATOR
147158 ["V2 transport error: packet decryption failure" ], # WRONG_GARBAGE
148159 ["V2 transport error: packet decryption failure" ], # SEND_NO_AAD
160+ [], # SEND_NON_EMPTY_VERSION_PACKET
149161 ]
150162 for test_type in TestType :
151163 if test_type == TestType .EARLY_KEY_RESPONSE :
152164 continue
153- with node0 .assert_debug_log (expected_debug_message [test_type .value ], timeout = 5 ):
154- peer = node0 .add_p2p_connection (MisbehavingV2Peer (test_type ), wait_for_verack = False , send_version = False , supports_v2_p2p = True , expect_success = False )
155- peer .wait_for_disconnect ()
156- self .log .info (f"Expected disconnection for { test_type .name } " )
165+ elif test_type == TestType .SEND_NON_EMPTY_VERSION_PACKET :
166+ node0 .add_p2p_connection (MisbehavingV2Peer (test_type ), wait_for_verack = True , send_version = True , supports_v2_p2p = True )
167+ self .log .info (f"No disconnection for { test_type .name } " )
168+ else :
169+ with node0 .assert_debug_log (expected_debug_message [test_type .value ], timeout = 5 ):
170+ peer = node0 .add_p2p_connection (MisbehavingV2Peer (test_type ), wait_for_verack = False , send_version = False , supports_v2_p2p = True , expect_success = False )
171+ peer .wait_for_disconnect ()
172+ self .log .info (f"Expected disconnection for { test_type .name } " )
157173
158174
159175if __name__ == '__main__' :
0 commit comments