|
3 | 3 | from ..tutils import Placeholder |
4 | 4 | from ..tutils import Playbook |
5 | 5 | from ..tutils import reply |
| 6 | +from mitmproxy import connection |
| 7 | +from mitmproxy import options |
6 | 8 | from mitmproxy.proxy.commands import CloseConnection |
7 | 9 | from mitmproxy.proxy.commands import CloseTcpConnection |
8 | 10 | from mitmproxy.proxy.commands import OpenConnection |
|
11 | 13 | from mitmproxy.proxy.events import DataReceived |
12 | 14 | from mitmproxy.proxy.layers import tcp |
13 | 15 | from mitmproxy.proxy.layers.tcp import TcpMessageInjected |
| 16 | +from mitmproxy.proxy import context as proxy_context |
14 | 17 | from mitmproxy.tcp import TCPFlow |
15 | 18 | from mitmproxy.tcp import TCPMessage |
16 | 19 |
|
@@ -149,3 +152,42 @@ def test_inject(tctx): |
149 | 152 | << None |
150 | 153 | ) |
151 | 154 | assert len(f().messages) == 2 |
| 155 | + |
| 156 | + |
| 157 | +def _build_tctx() -> proxy_context.Context: |
| 158 | + opts = options.Options() |
| 159 | + client = connection.Client( |
| 160 | + peername=("client", 1234), |
| 161 | + sockname=("127.0.0.1", 8080), |
| 162 | + state=connection.ConnectionState.OPEN, |
| 163 | + timestamp_start=1605699329, |
| 164 | + ) |
| 165 | + return proxy_context.Context(client, opts) |
| 166 | + |
| 167 | + |
| 168 | +def test_kill_closes_connection_when_flow_not_live(): |
| 169 | + """If a TCP flow is marked not live, the layer should close rather than relay data.""" |
| 170 | + |
| 171 | + def mark_flow_dead(flow: TCPFlow) -> None: |
| 172 | + flow.live = False |
| 173 | + |
| 174 | + f = Placeholder(TCPFlow) |
| 175 | + tctx = _build_tctx() |
| 176 | + |
| 177 | + assert ( |
| 178 | + Playbook(tcp.TCPLayer(tctx)) |
| 179 | + << tcp.TcpStartHook(f) |
| 180 | + >> reply() |
| 181 | + << OpenConnection(tctx.server) |
| 182 | + >> reply(None) |
| 183 | + >> DataReceived(tctx.client, b"terminate") |
| 184 | + << tcp.TcpMessageHook(f) |
| 185 | + >> reply(side_effect=mark_flow_dead) |
| 186 | + << CloseTcpConnection(tctx.server, half_close=True) |
| 187 | + << None |
| 188 | + ) |
| 189 | + |
| 190 | + flow = f() |
| 191 | + assert flow is not None |
| 192 | + assert flow.live is False |
| 193 | + assert flow.messages[-1].content == b"terminate" |
0 commit comments