Skip to content

Commit

Permalink
scripted-diff: test: replace command with msgtype
Browse files Browse the repository at this point in the history
This is the functional test framework pendant for
7777e36, which renamed "strCommand" with
"msg_type" in the network processing code.

-BEGIN VERIFY SCRIPT-
 # Rename in test framework
 sed -i 's/command/msgtype/g' ./test/functional/test_framework/messages.py ./test/functional/test_framework/mininode.py
 # Rename in individual tests
 sed -i 's/command/msgtype/g' ./test/functional/p2p_invalid_messages.py ./test/functional/p2p_leak.py
-END VERIFY SCRIPT-
  • Loading branch information
theStack committed Apr 15, 2020
1 parent 20c0e2e commit 9df32e8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
16 changes: 8 additions & 8 deletions test/functional/p2p_invalid_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class msg_unrecognized:
"""Nonsensical message. Modeled after similar types in test_framework.messages."""

command = b'badmsg'
msgtype = b'badmsg'

def __init__(self, *, str_data):
self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data
Expand All @@ -28,7 +28,7 @@ def serialize(self):
return messages.ser_string(self.str_data)

def __repr__(self):
return "{}(data={})".format(self.command, self.str_data)
return "{}(data={})".format(self.msgtype, self.str_data)


class InvalidMessagesTest(BitcoinTestFramework):
Expand All @@ -50,7 +50,7 @@ def run_test(self):
self.test_magic_bytes()
self.test_checksum()
self.test_size()
self.test_command()
self.test_msgtype()
self.test_large_inv()

node = self.nodes[0]
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_checksum(self):
msg = conn.build_message(msg_unrecognized(str_data="d"))
cut_len = (
4 + # magic
12 + # command
12 + # msgtype
4 #len
)
# modify checksum
Expand All @@ -183,21 +183,21 @@ def test_size(self):
msg = conn.build_message(msg_unrecognized(str_data="d"))
cut_len = (
4 + # magic
12 # command
12 # msgtype
)
# modify len to MAX_SIZE + 1
msg = msg[:cut_len] + struct.pack("<I", 0x02000000 + 1) + msg[cut_len + 4:]
self.nodes[0].p2p.send_raw_message(msg)
conn.wait_for_disconnect(timeout=1)
self.nodes[0].disconnect_p2ps()

def test_command(self):
def test_msgtype(self):
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: ERRORS IN HEADER']):
msg = msg_unrecognized(str_data="d")
msg.command = b'\xff' * 12
msg.msgtype = b'\xff' * 12
msg = conn.build_message(msg)
# Modify command
# Modify msgtype
msg = msg[:7] + b'\x00' + msg[7 + 1:]
self.nodes[0].p2p.send_raw_message(msg)
conn.sync_with_ping(timeout=1)
Expand Down
2 changes: 1 addition & 1 deletion test/functional/p2p_leak.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self):

def bad_message(self, message):
self.unexpected_msg = True
self.log.info("should not have received message: %s" % message.command)
self.log.info("should not have received message: %s" % message.msgtype)

def on_open(self):
self.ever_connected = True
Expand Down
58 changes: 29 additions & 29 deletions test/functional/test_framework/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ def __repr__(self):
class msg_version:
__slots__ = ("addrFrom", "addrTo", "nNonce", "nRelay", "nServices",
"nStartingHeight", "nTime", "nVersion", "strSubVer")
command = b"version"
msgtype = b"version"

def __init__(self):
self.nVersion = MY_VERSION
Expand Down Expand Up @@ -1004,7 +1004,7 @@ def __repr__(self):

class msg_verack:
__slots__ = ()
command = b"verack"
msgtype = b"verack"

def __init__(self):
pass
Expand All @@ -1021,7 +1021,7 @@ def __repr__(self):

class msg_addr:
__slots__ = ("addrs",)
command = b"addr"
msgtype = b"addr"

def __init__(self):
self.addrs = []
Expand All @@ -1038,7 +1038,7 @@ def __repr__(self):

class msg_inv:
__slots__ = ("inv",)
command = b"inv"
msgtype = b"inv"

def __init__(self, inv=None):
if inv is None:
Expand All @@ -1058,7 +1058,7 @@ def __repr__(self):

class msg_getdata:
__slots__ = ("inv",)
command = b"getdata"
msgtype = b"getdata"

def __init__(self, inv=None):
self.inv = inv if inv is not None else []
Expand All @@ -1075,7 +1075,7 @@ def __repr__(self):

class msg_getblocks:
__slots__ = ("locator", "hashstop")
command = b"getblocks"
msgtype = b"getblocks"

def __init__(self):
self.locator = CBlockLocator()
Expand All @@ -1099,7 +1099,7 @@ def __repr__(self):

class msg_tx:
__slots__ = ("tx",)
command = b"tx"
msgtype = b"tx"

def __init__(self, tx=CTransaction()):
self.tx = tx
Expand All @@ -1123,7 +1123,7 @@ def serialize(self):

class msg_block:
__slots__ = ("block",)
command = b"block"
msgtype = b"block"

def __init__(self, block=None):
if block is None:
Expand All @@ -1142,12 +1142,12 @@ def __repr__(self):


# for cases where a user needs tighter control over what is sent over the wire
# note that the user must supply the name of the command, and the data
# note that the user must supply the name of the msgtype, and the data
class msg_generic:
__slots__ = ("command", "data")
__slots__ = ("msgtype", "data")

def __init__(self, command, data=None):
self.command = command
def __init__(self, msgtype, data=None):
self.msgtype = msgtype
self.data = data

def serialize(self):
Expand All @@ -1165,7 +1165,7 @@ def serialize(self):

class msg_getaddr:
__slots__ = ()
command = b"getaddr"
msgtype = b"getaddr"

def __init__(self):
pass
Expand All @@ -1182,7 +1182,7 @@ def __repr__(self):

class msg_ping:
__slots__ = ("nonce",)
command = b"ping"
msgtype = b"ping"

def __init__(self, nonce=0):
self.nonce = nonce
Expand All @@ -1201,7 +1201,7 @@ def __repr__(self):

class msg_pong:
__slots__ = ("nonce",)
command = b"pong"
msgtype = b"pong"

def __init__(self, nonce=0):
self.nonce = nonce
Expand All @@ -1220,7 +1220,7 @@ def __repr__(self):

class msg_mempool:
__slots__ = ()
command = b"mempool"
msgtype = b"mempool"

def __init__(self):
pass
Expand All @@ -1237,7 +1237,7 @@ def __repr__(self):

class msg_notfound:
__slots__ = ("vec", )
command = b"notfound"
msgtype = b"notfound"

def __init__(self, vec=None):
self.vec = vec or []
Expand All @@ -1254,7 +1254,7 @@ def __repr__(self):

class msg_sendheaders:
__slots__ = ()
command = b"sendheaders"
msgtype = b"sendheaders"

def __init__(self):
pass
Expand All @@ -1275,7 +1275,7 @@ def __repr__(self):
# hash_stop (hash of last desired block header, 0 to get as many as possible)
class msg_getheaders:
__slots__ = ("hashstop", "locator",)
command = b"getheaders"
msgtype = b"getheaders"

def __init__(self):
self.locator = CBlockLocator()
Expand All @@ -1301,7 +1301,7 @@ def __repr__(self):
# <count> <vector of block headers>
class msg_headers:
__slots__ = ("headers",)
command = b"headers"
msgtype = b"headers"

def __init__(self, headers=None):
self.headers = headers if headers is not None else []
Expand All @@ -1322,7 +1322,7 @@ def __repr__(self):

class msg_merkleblock:
__slots__ = ("merkleblock",)
command = b"merkleblock"
msgtype = b"merkleblock"

def __init__(self, merkleblock=None):
if merkleblock is None:
Expand All @@ -1342,7 +1342,7 @@ def __repr__(self):

class msg_filterload:
__slots__ = ("data", "nHashFuncs", "nTweak", "nFlags")
command = b"filterload"
msgtype = b"filterload"

def __init__(self, data=b'00', nHashFuncs=0, nTweak=0, nFlags=0):
self.data = data
Expand Down Expand Up @@ -1371,7 +1371,7 @@ def __repr__(self):

class msg_filteradd:
__slots__ = ("data")
command = b"filteradd"
msgtype = b"filteradd"

def __init__(self, data):
self.data = data
Expand All @@ -1390,7 +1390,7 @@ def __repr__(self):

class msg_filterclear:
__slots__ = ()
command = b"filterclear"
msgtype = b"filterclear"

def __init__(self):
pass
Expand All @@ -1407,7 +1407,7 @@ def __repr__(self):

class msg_feefilter:
__slots__ = ("feerate",)
command = b"feefilter"
msgtype = b"feefilter"

def __init__(self, feerate=0):
self.feerate = feerate
Expand All @@ -1426,7 +1426,7 @@ def __repr__(self):

class msg_sendcmpct:
__slots__ = ("announce", "version")
command = b"sendcmpct"
msgtype = b"sendcmpct"

def __init__(self):
self.announce = False
Expand All @@ -1448,7 +1448,7 @@ def __repr__(self):

class msg_cmpctblock:
__slots__ = ("header_and_shortids",)
command = b"cmpctblock"
msgtype = b"cmpctblock"

def __init__(self, header_and_shortids = None):
self.header_and_shortids = header_and_shortids
Expand All @@ -1468,7 +1468,7 @@ def __repr__(self):

class msg_getblocktxn:
__slots__ = ("block_txn_request",)
command = b"getblocktxn"
msgtype = b"getblocktxn"

def __init__(self):
self.block_txn_request = None
Expand All @@ -1488,7 +1488,7 @@ def __repr__(self):

class msg_blocktxn:
__slots__ = ("block_transactions",)
command = b"blocktxn"
msgtype = b"blocktxn"

def __init__(self):
self.block_transactions = BlockTransactions()
Expand Down
22 changes: 11 additions & 11 deletions test/functional/test_framework/mininode.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _on_data(self):
raise ValueError("magic bytes mismatch: {} != {}".format(repr(self.magic_bytes), repr(self.recvbuf)))
if len(self.recvbuf) < 4 + 12 + 4 + 4:
return
command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
msgtype = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
checksum = self.recvbuf[4+12+4:4+12+4+4]
if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen:
Expand All @@ -191,10 +191,10 @@ def _on_data(self):
if checksum != h[:4]:
raise ValueError("got bad checksum " + repr(self.recvbuf))
self.recvbuf = self.recvbuf[4+12+4+4+msglen:]
if command not in MESSAGEMAP:
raise ValueError("Received unknown command from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, command, repr(msg)))
if msgtype not in MESSAGEMAP:
raise ValueError("Received unknown msgtype from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, msgtype, repr(msg)))
f = BytesIO(msg)
t = MESSAGEMAP[command]()
t = MESSAGEMAP[msgtype]()
t.deserialize(f)
self._log_message("receive", t)
self.on_message(t)
Expand Down Expand Up @@ -233,11 +233,11 @@ def maybe_write():

def build_message(self, message):
"""Build a serialized P2P message"""
command = message.command
msgtype = message.msgtype
data = message.serialize()
tmsg = self.magic_bytes
tmsg += command
tmsg += b"\x00" * (12 - len(command))
tmsg += msgtype
tmsg += b"\x00" * (12 - len(msgtype))
tmsg += struct.pack("<I", len(data))
th = sha256(data)
h = sha256(th)
Expand Down Expand Up @@ -304,10 +304,10 @@ def on_message(self, message):
and the most recent message of each type."""
with mininode_lock:
try:
command = message.command.decode('ascii')
self.message_count[command] += 1
self.last_message[command] = message
getattr(self, 'on_' + command)(message)
msgtype = message.msgtype.decode('ascii')
self.message_count[msgtype] += 1
self.last_message[msgtype] = message
getattr(self, 'on_' + msgtype)(message)
except:
print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0]))
raise
Expand Down

0 comments on commit 9df32e8

Please sign in to comment.