Skip to content

Commit 381f77b

Browse files
committed
Add Message Capture Test
Add a functional test for CaptureMessage. This connects and then disconnects a peer so that the handshake can be used to check if capture is being done correctly. Included in a docstring in the test is the following: From the data file we'll only check the structure. We won't care about things like: - Deserializing the payload of the message - This is managed by the deserialize methods in test_framework.messages - The order of the messages - There's no reason why we can't, say, change the order of the messages in the handshake - Message Type - We can add new message types We're ignoring these because they're simply too brittle to test here.
1 parent e4f378a commit 381f77b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test per-peer message capture capability.
6+
7+
Additionally, the output of contrib/message-capture/message-capture-parser.py should be verified manually.
8+
"""
9+
10+
import glob
11+
from io import BytesIO
12+
import os
13+
14+
from test_framework.p2p import P2PDataStore, MESSAGEMAP
15+
from test_framework.test_framework import BitcoinTestFramework
16+
from test_framework.util import assert_equal
17+
18+
TIME_SIZE = 8
19+
LENGTH_SIZE = 4
20+
MSGTYPE_SIZE = 12
21+
22+
def mini_parser(dat_file):
23+
"""Parse a data file created by CaptureMessage.
24+
25+
From the data file we'll only check the structure.
26+
27+
We won't care about things like:
28+
- Deserializing the payload of the message
29+
- This is managed by the deserialize methods in test_framework.messages
30+
- The order of the messages
31+
- There's no reason why we can't, say, change the order of the messages in the handshake
32+
- Message Type
33+
- We can add new message types
34+
35+
We're ignoring these because they're simply too brittle to test here.
36+
"""
37+
with open(dat_file, 'rb') as f_in:
38+
# This should have at least one message in it
39+
assert(os.fstat(f_in.fileno()).st_size >= TIME_SIZE + LENGTH_SIZE + MSGTYPE_SIZE)
40+
while True:
41+
tmp_header_raw = f_in.read(TIME_SIZE + LENGTH_SIZE + MSGTYPE_SIZE)
42+
if not tmp_header_raw:
43+
break
44+
tmp_header = BytesIO(tmp_header_raw)
45+
int.from_bytes(tmp_header.read(TIME_SIZE), "little") # type: int
46+
raw_msgtype = tmp_header.read(MSGTYPE_SIZE)
47+
msgtype = raw_msgtype.split(b'\x00', 1)[0] # type: bytes
48+
remainder = raw_msgtype.split(b'\x00', 1)[1]
49+
assert(len(msgtype) > 0)
50+
assert(msgtype in MESSAGEMAP)
51+
assert(len(remainder) == 0 or not remainder.decode().isprintable())
52+
length = int.from_bytes(tmp_header.read(LENGTH_SIZE), "little") # type: int
53+
data = f_in.read(length)
54+
assert_equal(len(data), length)
55+
56+
57+
58+
class MessageCaptureTest(BitcoinTestFramework):
59+
def set_test_params(self):
60+
self.num_nodes = 1
61+
self.extra_args = [["-capturemessages"]]
62+
self.setup_clean_chain = True
63+
64+
def run_test(self):
65+
capturedir = os.path.join(self.nodes[0].datadir, "regtest/message_capture")
66+
# Connect a node so that the handshake occurs
67+
self.nodes[0].add_p2p_connection(P2PDataStore())
68+
self.nodes[0].disconnect_p2ps()
69+
recv_file = glob.glob(os.path.join(capturedir, "*/msgs_recv.dat"))[0]
70+
mini_parser(recv_file)
71+
sent_file = glob.glob(os.path.join(capturedir, "*/msgs_sent.dat"))[0]
72+
mini_parser(sent_file)
73+
74+
75+
if __name__ == '__main__':
76+
MessageCaptureTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
'p2p_add_connections.py',
265265
'p2p_unrequested_blocks.py',
266266
'p2p_blockfilters.py',
267+
'p2p_message_capture.py',
267268
'feature_includeconf.py',
268269
'feature_asmap.py',
269270
'mempool_unbroadcast.py',

0 commit comments

Comments
 (0)