mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 03:47:29 -03:00
test: Check that disconnection happens when >4095 garbage bytes is sent
This test type is represented using EXCESS_GARBAGE.
This commit is contained in:
parent
e075fd131d
commit
e351576862
2 changed files with 35 additions and 3 deletions
|
@ -3,12 +3,16 @@
|
|||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
import random
|
||||
from enum import Enum
|
||||
|
||||
from test_framework.messages import MAGIC_BYTES
|
||||
from test_framework.p2p import P2PInterface
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.v2_p2p import EncryptedP2PState
|
||||
from test_framework.v2_p2p import (
|
||||
EncryptedP2PState,
|
||||
MAX_GARBAGE_LEN,
|
||||
)
|
||||
|
||||
|
||||
class TestType(Enum):
|
||||
|
@ -16,8 +20,10 @@ class TestType(Enum):
|
|||
|
||||
1. EARLY_KEY_RESPONSE - The responder needs to wait until one byte is received which does not match the 16 bytes
|
||||
consisting of network magic followed by "version\x00\x00\x00\x00\x00" before sending out its ellswift + garbage bytes
|
||||
2. EXCESS_GARBAGE - Disconnection happens when > MAX_GARBAGE_LEN bytes garbage is sent
|
||||
"""
|
||||
EARLY_KEY_RESPONSE = 0
|
||||
EXCESS_GARBAGE = 1
|
||||
|
||||
|
||||
class EarlyKeyResponseState(EncryptedP2PState):
|
||||
|
@ -32,6 +38,13 @@ class EarlyKeyResponseState(EncryptedP2PState):
|
|||
return b""
|
||||
|
||||
|
||||
class ExcessGarbageState(EncryptedP2PState):
|
||||
"""Generate > MAX_GARBAGE_LEN garbage bytes"""
|
||||
def generate_keypair_and_garbage(self):
|
||||
garbage_len = MAX_GARBAGE_LEN + random.randrange(1, MAX_GARBAGE_LEN + 1)
|
||||
return super().generate_keypair_and_garbage(garbage_len)
|
||||
|
||||
|
||||
class MisbehavingV2Peer(P2PInterface):
|
||||
"""Custom implementation of P2PInterface which uses modified v2 P2P protocol functions for testing purposes."""
|
||||
def __init__(self, test_type):
|
||||
|
@ -41,6 +54,8 @@ class MisbehavingV2Peer(P2PInterface):
|
|||
def connection_made(self, transport):
|
||||
if self.test_type == TestType.EARLY_KEY_RESPONSE:
|
||||
self.v2_state = EarlyKeyResponseState(initiating=True, net='regtest')
|
||||
elif self.test_type == TestType.EXCESS_GARBAGE:
|
||||
self.v2_state = ExcessGarbageState(initiating=True, net='regtest')
|
||||
super().connection_made(transport)
|
||||
|
||||
def data_received(self, t):
|
||||
|
@ -58,6 +73,7 @@ class EncryptedP2PMisbehaving(BitcoinTestFramework):
|
|||
|
||||
def run_test(self):
|
||||
self.test_earlykeyresponse()
|
||||
self.test_v2disconnection()
|
||||
|
||||
def test_earlykeyresponse(self):
|
||||
self.log.info('Sending ellswift bytes in parts to ensure that response from responder is received only when')
|
||||
|
@ -75,6 +91,21 @@ class EncryptedP2PMisbehaving(BitcoinTestFramework):
|
|||
peer1.wait_for_disconnect(timeout=5)
|
||||
self.log.info('successful disconnection since modified ellswift was sent as response')
|
||||
|
||||
def test_v2disconnection(self):
|
||||
# test v2 disconnection scenarios
|
||||
node0 = self.nodes[0]
|
||||
expected_debug_message = [
|
||||
[], # EARLY_KEY_RESPONSE
|
||||
["V2 transport error: missing garbage terminator, peer=1"], # EXCESS_GARBAGE
|
||||
]
|
||||
for test_type in TestType:
|
||||
if test_type == TestType.EARLY_KEY_RESPONSE:
|
||||
continue
|
||||
with node0.assert_debug_log(expected_debug_message[test_type.value], timeout=5):
|
||||
peer = node0.add_p2p_connection(MisbehavingV2Peer(test_type), wait_for_verack=False, send_version=False, supports_v2_p2p=True, expect_success=False)
|
||||
peer.wait_for_disconnect()
|
||||
self.log.info(f"Expected disconnection for {test_type.name}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
EncryptedP2PMisbehaving().main()
|
||||
|
|
|
@ -109,10 +109,11 @@ class EncryptedP2PState:
|
|||
# Responding, place their public key encoding first.
|
||||
return TaggedHash("bip324_ellswift_xonly_ecdh", ellswift_theirs + ellswift_ours + ecdh_point_x32)
|
||||
|
||||
def generate_keypair_and_garbage(self):
|
||||
def generate_keypair_and_garbage(self, garbage_len=None):
|
||||
"""Generates ellswift keypair and 4095 bytes garbage at max"""
|
||||
self.privkey_ours, self.ellswift_ours = ellswift_create()
|
||||
garbage_len = random.randrange(MAX_GARBAGE_LEN + 1)
|
||||
if garbage_len is None:
|
||||
garbage_len = random.randrange(MAX_GARBAGE_LEN + 1)
|
||||
self.sent_garbage = random.randbytes(garbage_len)
|
||||
return self.ellswift_ours + self.sent_garbage
|
||||
|
||||
|
|
Loading…
Reference in a new issue