Merge #14522: tests: add invalid P2P message tests

d20a9fa13d tests: add tests for invalid P2P messages (James O'Beirne)
62f94d39f8 tests: add P2PConnection.send_raw_message (James O'Beirne)
5aa31f6ef2 tests: add utility to assert node memory usage hasn't increased (James O'Beirne)

Pull request description:

  - Adds `p2p_invalid_messages.py`: tests based on behavior for dealing with invalid and malformed P2P messages. Includes a test verifying that we can't DoS a node by spamming it with large invalid messages.
  - Adds `TestNode.assert_memory_usage_stable`: a context manager that allows us to ensure memory usage doesn't significantly increase on a node during some test.
  - Adds `P2PConnection.send_raw_message`: which allows us to construct and send messages with tweaked headers.

Tree-SHA512: 720a4894c1e6d8f1551b2ae710e5b06c9e4f281524623957cb01599be9afea82671dc26d6152281de0acb87720f0c53b61e2b27d40434d30e525dd9e31fa671f
This commit is contained in:
Wladimir J. van der Laan
2018-11-06 11:08:40 +01:00
4 changed files with 230 additions and 6 deletions

View File

@ -207,10 +207,13 @@ class P2PConnection(asyncio.Protocol):
This method takes a P2P payload, builds the P2P header and adds
the message to the send buffer to be sent over the socket."""
tmsg = self.build_message(message)
self._log_message("send", message)
return self.send_raw_message(tmsg)
def send_raw_message(self, raw_message_bytes):
if not self.is_connected:
raise IOError('Not connected')
self._log_message("send", message)
tmsg = self._build_message(message)
def maybe_write():
if not self._transport:
@ -220,12 +223,12 @@ class P2PConnection(asyncio.Protocol):
# Python 3.4 versions.
if hasattr(self._transport, 'is_closing') and self._transport.is_closing():
return
self._transport.write(tmsg)
self._transport.write(raw_message_bytes)
NetworkThread.network_event_loop.call_soon_threadsafe(maybe_write)
# Class utility methods
def _build_message(self, message):
def build_message(self, message):
"""Build a serialized P2P message"""
command = message.command
data = message.serialize()
@ -409,9 +412,9 @@ class P2PInterface(P2PConnection):
# Message sending helper functions
def send_and_ping(self, message):
def send_and_ping(self, message, timeout=60):
self.send_message(message)
self.sync_with_ping()
self.sync_with_ping(timeout=timeout)
# Sync up with the node
def sync_with_ping(self, timeout=60):

View File

@ -115,6 +115,28 @@ class TestNode():
]
return PRIV_KEYS[self.index]
def get_mem_rss(self):
"""Get the memory usage (RSS) per `ps`.
If process is stopped or `ps` is unavailable, return None.
"""
if not (self.running and self.process):
self.log.warning("Couldn't get memory usage; process isn't running.")
return None
try:
return int(subprocess.check_output(
"ps h -o rss {}".format(self.process.pid),
shell=True, stderr=subprocess.DEVNULL).strip())
# Catching `Exception` broadly to avoid failing on platforms where ps
# isn't installed or doesn't work as expected, e.g. OpenBSD.
#
# We could later use something like `psutils` to work across platforms.
except Exception:
self.log.exception("Unable to get memory usage")
return None
def _node_msg(self, msg: str) -> str:
"""Return a modified msg that identifies this node by its index as a debugging aid."""
return "[node %d] %s" % (self.index, msg)
@ -271,6 +293,29 @@ class TestNode():
if re.search(re.escape(expected_msg), log, flags=re.MULTILINE) is None:
self._raise_assertion_error('Expected message "{}" does not partially match log:\n\n{}\n\n'.format(expected_msg, print_log))
@contextlib.contextmanager
def assert_memory_usage_stable(self, perc_increase_allowed=0.03):
"""Context manager that allows the user to assert that a node's memory usage (RSS)
hasn't increased beyond some threshold percentage.
"""
before_memory_usage = self.get_mem_rss()
yield
after_memory_usage = self.get_mem_rss()
if not (before_memory_usage and after_memory_usage):
self.log.warning("Unable to detect memory usage (RSS) - skipping memory check.")
return
perc_increase_memory_usage = 1 - (float(before_memory_usage) / after_memory_usage)
if perc_increase_memory_usage > perc_increase_allowed:
self._raise_assertion_error(
"Memory usage increased over threshold of {:.3f}% from {} to {} ({:.3f}%)".format(
perc_increase_allowed * 100, before_memory_usage, after_memory_usage,
perc_increase_memory_usage * 100))
def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, match=ErrorMatch.FULL_TEXT, *args, **kwargs):
"""Attempt to start the node and expect it to raise an error.