2017-02-14 14:37:14 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2017 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test message sending before handshake completion.
|
2017-02-14 14:37:14 +01:00
|
|
|
|
|
|
|
A node should never send anything other than VERSION/VERACK/REJECT until it's
|
|
|
|
received a VERACK.
|
|
|
|
|
2018-03-21 16:16:28 +01:00
|
|
|
This test connects to a node and sends it a few messages, trying to entice it
|
2017-02-14 14:37:14 +01:00
|
|
|
into sending us something it shouldn't.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""
|
|
|
|
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
from test_framework.messages import msg_getaddr, msg_ping, msg_verack
|
|
|
|
from test_framework.mininode import mininode_lock, P2PInterface
|
2019-01-07 10:55:35 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from test_framework.util import wait_until
|
2017-02-14 14:37:14 +01:00
|
|
|
|
|
|
|
banscore = 10
|
|
|
|
|
2017-11-30 23:58:58 +01:00
|
|
|
class CLazyNode(P2PInterface):
|
2017-02-14 14:37:14 +01:00
|
|
|
def __init__(self):
|
2017-03-30 09:22:59 +02:00
|
|
|
super().__init__()
|
2017-02-14 14:37:14 +01:00
|
|
|
self.unexpected_msg = False
|
2017-05-02 19:10:23 +02:00
|
|
|
self.ever_connected = False
|
2017-02-14 14:37:14 +01:00
|
|
|
|
|
|
|
def bad_message(self, message):
|
|
|
|
self.unexpected_msg = True
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("should not have received message: %s" % message.command)
|
2017-02-14 14:37:14 +01:00
|
|
|
|
2020-04-05 13:12:45 +02:00
|
|
|
def on_open(self):
|
2017-05-02 19:10:23 +02:00
|
|
|
self.ever_connected = True
|
2017-02-14 14:37:14 +01:00
|
|
|
|
2020-04-05 13:12:45 +02:00
|
|
|
def on_version(self, message): self.bad_message(message)
|
|
|
|
def on_verack(self, message): self.bad_message(message)
|
|
|
|
def on_reject(self, message): self.bad_message(message)
|
|
|
|
def on_inv(self, message): self.bad_message(message)
|
|
|
|
def on_addr(self, message): self.bad_message(message)
|
|
|
|
def on_getdata(self, message): self.bad_message(message)
|
|
|
|
def on_getblocks(self, message): self.bad_message(message)
|
|
|
|
def on_tx(self, message): self.bad_message(message)
|
|
|
|
def on_block(self, message): self.bad_message(message)
|
|
|
|
def on_getaddr(self, message): self.bad_message(message)
|
|
|
|
def on_headers(self, message): self.bad_message(message)
|
|
|
|
def on_getheaders(self, message): self.bad_message(message)
|
|
|
|
def on_ping(self, message): self.bad_message(message)
|
|
|
|
def on_mempool(self, message): self.bad_message(message)
|
|
|
|
def on_pong(self, message): self.bad_message(message)
|
|
|
|
def on_sendheaders(self, message): self.bad_message(message)
|
|
|
|
def on_sendcmpct(self, message): self.bad_message(message)
|
|
|
|
def on_cmpctblock(self, message): self.bad_message(message)
|
|
|
|
def on_getblocktxn(self, message): self.bad_message(message)
|
|
|
|
def on_blocktxn(self, message): self.bad_message(message)
|
2017-02-14 14:37:14 +01:00
|
|
|
|
|
|
|
# Node that never sends a version. We'll use this to send a bunch of messages
|
|
|
|
# anyway, and eventually get disconnected.
|
|
|
|
class CNodeNoVersionBan(CLazyNode):
|
|
|
|
# send a bunch of veracks without sending a message. This should get us disconnected.
|
2020-06-11 10:39:04 +02:00
|
|
|
# NOTE: implementation-specific check here. Remove if dashd ban behavior changes
|
2020-04-05 13:12:45 +02:00
|
|
|
def on_open(self):
|
|
|
|
super().on_open()
|
2017-02-14 14:37:14 +01:00
|
|
|
for i in range(banscore):
|
|
|
|
self.send_message(msg_verack())
|
|
|
|
|
2020-04-05 13:12:45 +02:00
|
|
|
def on_reject(self, message): pass
|
2017-02-14 14:37:14 +01:00
|
|
|
|
|
|
|
# Node that never sends a version. This one just sits idle and hopes to receive
|
|
|
|
# any message (it shouldn't!)
|
|
|
|
class CNodeNoVersionIdle(CLazyNode):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
# Node that sends a version but not a verack.
|
|
|
|
class CNodeNoVerackIdle(CLazyNode):
|
|
|
|
def __init__(self):
|
|
|
|
self.version_received = False
|
|
|
|
super().__init__()
|
|
|
|
|
2020-04-05 13:12:45 +02:00
|
|
|
def on_reject(self, message): pass
|
|
|
|
def on_verack(self, message): pass
|
2017-02-14 14:37:14 +01:00
|
|
|
# When version is received, don't reply with a verack. Instead, see if the
|
|
|
|
# node will give us a message that it shouldn't. This is not an exhaustive
|
|
|
|
# list!
|
2020-04-05 13:12:45 +02:00
|
|
|
def on_version(self, message):
|
2017-02-14 14:37:14 +01:00
|
|
|
self.version_received = True
|
2020-04-05 13:12:45 +02:00
|
|
|
self.send_message(msg_ping())
|
|
|
|
self.send_message(msg_getaddr())
|
2017-02-14 14:37:14 +01:00
|
|
|
|
|
|
|
class P2PLeakTest(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2017-02-14 14:37:14 +01:00
|
|
|
self.num_nodes = 1
|
2018-08-09 14:07:16 +02:00
|
|
|
self.extra_args = [['-banscore=' + str(banscore)]]
|
2017-02-14 14:37:14 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
2018-08-09 14:07:16 +02:00
|
|
|
no_version_bannode = self.nodes[0].add_p2p_connection(CNodeNoVersionBan(), send_version=False, wait_for_verack=False)
|
|
|
|
no_version_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVersionIdle(), send_version=False, wait_for_verack=False)
|
2017-11-08 19:10:43 +01:00
|
|
|
no_verack_idlenode = self.nodes[0].add_p2p_connection(CNodeNoVerackIdle())
|
2017-02-14 14:37:14 +01:00
|
|
|
|
2017-08-23 23:11:33 +02:00
|
|
|
wait_until(lambda: no_version_bannode.ever_connected, timeout=10, lock=mininode_lock)
|
|
|
|
wait_until(lambda: no_version_idlenode.ever_connected, timeout=10, lock=mininode_lock)
|
|
|
|
wait_until(lambda: no_verack_idlenode.version_received, timeout=10, lock=mininode_lock)
|
2017-02-14 14:37:14 +01:00
|
|
|
|
|
|
|
# Mine a block and make sure that it's not sent to the connected nodes
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
|
|
|
#Give the node enough time to possibly leak out a message
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
#This node should have been banned
|
2018-06-24 01:28:13 +02:00
|
|
|
assert not no_version_bannode.is_connected
|
2017-02-14 14:37:14 +01:00
|
|
|
|
2017-11-14 08:56:04 +01:00
|
|
|
self.nodes[0].disconnect_p2ps()
|
2017-02-14 14:37:14 +01:00
|
|
|
|
2017-09-13 18:54:09 +02:00
|
|
|
# Wait until all connections are closed
|
|
|
|
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 0)
|
|
|
|
|
2017-02-14 14:37:14 +01:00
|
|
|
# Make sure no unexpected messages came in
|
|
|
|
assert(no_version_bannode.unexpected_msg == False)
|
|
|
|
assert(no_version_idlenode.unexpected_msg == False)
|
|
|
|
assert(no_verack_idlenode.unexpected_msg == False)
|
|
|
|
|
2018-06-29 18:04:25 +02:00
|
|
|
|
2017-02-14 14:37:14 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
P2PLeakTest().main()
|