[quic] improve and fix tests

This commit is contained in:
Manuel Meitinger 2022-11-08 07:51:25 +01:00
parent e1d5f4b838
commit 2bda324d94
4 changed files with 33 additions and 38 deletions

View File

@ -181,15 +181,15 @@ class TestNextLayer:
assert isinstance(nl._next_layer(ctx, b"", b"hello"), layers.TCPLayer)
@pytest.mark.parametrize(
("client_hello", "client_layer", "server_layer"),
("protocol", "client_layer", "server_layer"),
[
(dtls_client_hello_with_extensions, layers.ClientTLSLayer, layers.ServerTLSLayer),
(quic_client_hello, layers.ClientQuicLayer, layers.ServerQuicLayer),
("dtls", layers.ClientTLSLayer, layers.ServerTLSLayer),
("quic", layers.ClientQuicLayer, layers.ServerQuicLayer),
]
)
def test_next_layer_udp(
self,
client_hello: bytes,
protocol: str,
client_layer: layer.Layer,
server_layer: layer.Layer,
):
@ -205,6 +205,10 @@ class TestNextLayer:
and layer.mode is mode
)
client_hello = {
"dtls": dtls_client_hello_with_extensions,
"quic": quic_client_hello,
}[protocol]
nl = NextLayer()
ctx = MagicMock()
ctx.client.alpn = None

View File

@ -757,15 +757,15 @@ async def test_reverse_http3_and_quic_stream(
await caplog_async.await_log(f"Stopped reverse proxy to {scheme}")
async def test_reverse_quic_datagram(caplog_async) -> None:
@pytest.mark.parametrize("connection_strategy", ["lazy", "eager"])
async def test_reverse_quic_datagram(caplog_async, connection_strategy: str) -> None:
caplog_async.set_level("INFO")
ps = Proxyserver()
nl = NextLayer()
ta = TlsConfig()
with taddons.context(ps, nl, ta) as tctx:
tctx.options.keep_host_header = True
# eager is not (yet) support for non-H3
tctx.options.connection_strategy = "lazy"
tctx.options.connection_strategy = connection_strategy
ta.configure(["confdir"])
async with quic_server(QuicDatagramEchoServer, alpn=["dgram"]) as server_addr:
mode = f"reverse:quic://{server_addr[0]}:{server_addr[1]}@127.0.0.1:0"

View File

@ -26,7 +26,7 @@ from mitmproxy.proxy.layers.tls import (
)
from mitmproxy.proxy.mode_specs import ProxyMode
from mitmproxy.tcp import TCPFlow
from mitmproxy.test import tflow
from mitmproxy.test import taddons, tflow
from mitmproxy.udp import UDPFlow
from test.mitmproxy.proxy.layers.test_tls import (
reply_tls_start_client,
@ -174,27 +174,28 @@ def test_reverse_dns(tctx):
@pytest.mark.parametrize("keep_host_header", [True, False])
def test_quic(tctx: Context, keep_host_header: bool):
tctx.options.keep_host_header = keep_host_header
tctx.server.sni = "other"
tctx.client.proxy_mode = ProxyMode.parse("reverse:quic://1.2.3.4:5")
client_hello = Placeholder(bytes)
with taddons.context():
tctx.options.keep_host_header = keep_host_header
tctx.server.sni = "other"
tctx.client.proxy_mode = ProxyMode.parse("reverse:quic://1.2.3.4:5")
client_hello = Placeholder(bytes)
def set_settings(data: quic.QuicTlsData):
data.settings = quic.QuicTlsSettings()
def set_settings(data: quic.QuicTlsData):
data.settings = quic.QuicTlsSettings()
assert (
Playbook(modes.ReverseProxy(tctx))
<< OpenConnection(tctx.server)
>> reply(None)
<< quic.QuicStartServerHook(Placeholder(quic.QuicTlsData))
>> reply(side_effect=set_settings)
<< SendData(tctx.server, client_hello)
<< RequestWakeup(Placeholder(float))
)
assert tctx.server.address == ("1.2.3.4", 5)
assert quic.quic_parse_client_hello(client_hello()).sni == (
"other" if keep_host_header else "1.2.3.4"
)
assert (
Playbook(modes.ReverseProxy(tctx))
<< OpenConnection(tctx.server)
>> reply(None)
<< quic.QuicStartServerHook(Placeholder(quic.QuicTlsData))
>> reply(side_effect=set_settings)
<< SendData(tctx.server, client_hello)
<< RequestWakeup(Placeholder(float))
)
assert tctx.server.address == ("1.2.3.4", 5)
assert quic.quic_parse_client_hello(client_hello()).sni == (
"other" if keep_host_header else "1.2.3.4"
)
def test_udp(tctx: Context):

View File

@ -8,8 +8,7 @@ from aioquic.quic.connection import QuicConnection, pull_quic_header
from typing import Literal, Optional, TypeVar
from unittest.mock import MagicMock
import pytest
from mitmproxy import connection, options
from mitmproxy.addons.proxyserver import Proxyserver
from mitmproxy import connection
from mitmproxy.proxy import commands, context, events, layer, tunnel
from mitmproxy.proxy import layers
from mitmproxy.proxy.layers import quic, tcp, tls, udp
@ -26,15 +25,6 @@ tlsdata = data.Data(__name__)
T = TypeVar('T', bound=layer.Layer)
@pytest.fixture
def tctx() -> context.Context:
opts = options.Options()
Proxyserver().load(opts)
return context.Context(
connection.Client(("client", 1234), ("127.0.0.1", 8080), 1605699329), opts
)
class DummyLayer(layer.Layer):
child_layer: Optional[layer.Layer]