From c842abff2da096144a68d5cb8d0ccae30d571cc7 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sat, 13 Mar 2021 23:17:07 +0100 Subject: [PATCH] coverage++ --- mitmproxy/proxy/layer.py | 8 ++++++-- mitmproxy/proxy/tunnel.py | 2 -- test/mitmproxy/proxy/test_layer.py | 29 +++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/mitmproxy/proxy/layer.py b/mitmproxy/proxy/layer.py index a39051e36..d446ec0e5 100644 --- a/mitmproxy/proxy/layer.py +++ b/mitmproxy/proxy/layer.py @@ -91,8 +91,12 @@ class Layer: @property def stack_pos(self) -> str: """repr() for this layer and all its parent layers, only useful for debugging.""" - idx = self.context.layers.index(self) - return " >> ".join(repr(x) for x in self.context.layers[:idx + 1]) + try: + idx = self.context.layers.index(self) + except ValueError: + return repr(self) + else: + return " >> ".join(repr(x) for x in self.context.layers[:idx + 1]) @abstractmethod def _handle_event(self, event: events.Event) -> CommandGenerator[None]: diff --git a/mitmproxy/proxy/tunnel.py b/mitmproxy/proxy/tunnel.py index 0d6daee03..14c54c6f5 100644 --- a/mitmproxy/proxy/tunnel.py +++ b/mitmproxy/proxy/tunnel.py @@ -79,8 +79,6 @@ class TunnelLayer(layer.Layer): yield from self.on_handshake_error(err) yield from self._handshake_finished(err) self.tunnel_state = TunnelState.CLOSED - elif isinstance(event, events.MessageInjected): - yield from self.event_to_child(event) else: # pragma: no cover raise AssertionError(f"Unexpected event: {event}") else: diff --git a/test/mitmproxy/proxy/test_layer.py b/test/mitmproxy/proxy/test_layer.py index a590ecfaf..c9dbbfe90 100644 --- a/test/mitmproxy/proxy/test_layer.py +++ b/test/mitmproxy/proxy/test_layer.py @@ -1,11 +1,26 @@ import pytest from mitmproxy.proxy import commands, events, layer +from mitmproxy.proxy.context import Context from test.mitmproxy.proxy import tutils class TestLayer: - def test_debug_messages(self, tctx): + def test_continue(self, tctx: Context): + class TLayer(layer.Layer): + def _handle_event(self, event: events.Event) -> layer.CommandGenerator[None]: + yield commands.OpenConnection(self.context.server) + yield commands.OpenConnection(self.context.server) + + assert ( + tutils.Playbook(TLayer(tctx)) + << commands.OpenConnection(tctx.server) + >> tutils.reply(None) + << commands.OpenConnection(tctx.server) + >> tutils.reply(None) + ) + + def test_debug_messages(self, tctx: Context): tctx.server.id = "serverid" class TLayer(layer.Layer): @@ -53,7 +68,7 @@ class TestLayer: class TestNextLayer: - def test_simple(self, tctx): + def test_simple(self, tctx: Context): nl = layer.NextLayer(tctx, ask_on_start=True) nl.debug = " " playbook = tutils.Playbook(nl, hooks=True) @@ -79,7 +94,7 @@ class TestNextLayer: << commands.SendData(tctx.client, b"bar") ) - def test_late_hook_reply(self, tctx): + def test_late_hook_reply(self, tctx: Context): """ Properly handle case where we receive an additional event while we are waiting for a reply from the proxy core. @@ -104,7 +119,7 @@ class TestNextLayer: ) @pytest.mark.parametrize("layer_found", [True, False]) - def test_receive_close(self, tctx, layer_found): + def test_receive_close(self, tctx: Context, layer_found: bool): """Test that we abort a client connection which has disconnected without any layer being found.""" nl = layer.NextLayer(tctx) playbook = tutils.Playbook(nl) @@ -128,7 +143,7 @@ class TestNextLayer: << commands.CloseConnection(tctx.client) ) - def test_func_references(self, tctx): + def test_func_references(self, tctx: Context): nl = layer.NextLayer(tctx) playbook = tutils.Playbook(nl) @@ -147,7 +162,9 @@ class TestNextLayer: sd, = handle(events.DataReceived(tctx.client, b"bar")) assert isinstance(sd, commands.SendData) - def test_repr(self, tctx): + def test_repr(self, tctx: Context): nl = layer.NextLayer(tctx) nl.layer = tutils.EchoLayer(tctx) assert repr(nl) + assert nl.stack_pos + assert nl.layer.stack_pos