aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/conftest.py20
-rw-r--r--test/mitmproxy/addons/test_clientplayback.py7
-rw-r--r--test/mitmproxy/addons/test_save.py5
-rw-r--r--test/mitmproxy/addons/test_serverplayback.py7
-rw-r--r--test/mitmproxy/addons/test_view.py7
-rw-r--r--test/mitmproxy/contentviews/test_protobuf.py4
-rw-r--r--test/mitmproxy/contentviews/test_wbxml.py22
-rw-r--r--test/mitmproxy/contentviews/test_wbxml_data/data-formatted.wbxml10
-rw-r--r--test/mitmproxy/contentviews/test_wbxml_data/data.wbxmlbin0 -> 34 bytes
-rw-r--r--test/mitmproxy/contrib/test_tls_parser.py38
-rw-r--r--test/mitmproxy/net/test_tcp.py8
-rw-r--r--test/mitmproxy/net/tservers.py14
-rw-r--r--test/mitmproxy/platform/test_pf.py5
-rw-r--r--test/mitmproxy/proxy/protocol/test_http1.py1
-rw-r--r--test/mitmproxy/proxy/protocol/test_http2.py172
-rw-r--r--test/mitmproxy/proxy/protocol/test_tls.py3
-rw-r--r--test/mitmproxy/proxy/protocol/test_websocket.py122
-rw-r--r--test/mitmproxy/test_connections.py13
-rw-r--r--test/mitmproxy/test_flowfilter.py3
-rw-r--r--test/mitmproxy/test_proxy.py3
-rw-r--r--test/mitmproxy/tservers.py2
-rw-r--r--test/pathod/language/test_base.py6
-rw-r--r--test/pathod/language/test_generators.py14
-rw-r--r--test/pathod/protocols/test_http2.py2
-rw-r--r--test/pathod/test_test.py40
-rw-r--r--test/pathod/tservers.py4
26 files changed, 253 insertions, 279 deletions
diff --git a/test/conftest.py b/test/conftest.py
index b4e1da93..bb913548 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -1,8 +1,6 @@
import os
import pytest
import OpenSSL
-import functools
-from contextlib import contextmanager
import mitmproxy.net.tcp
@@ -32,21 +30,3 @@ skip_appveyor = pytest.mark.skipif(
def disable_alpn(monkeypatch):
monkeypatch.setattr(mitmproxy.net.tcp, 'HAS_ALPN', False)
monkeypatch.setattr(OpenSSL.SSL._lib, 'Cryptography_HAS_ALPN', False)
-
-
-################################################################################
-# TODO: remove this wrapper when pytest 3.1.0 is released
-original_pytest_raises = pytest.raises
-
-
-@contextmanager
-@functools.wraps(original_pytest_raises)
-def raises(exc, *args, **kwargs):
- with original_pytest_raises(exc, *args, **kwargs) as exc_info:
- yield
- if 'match' in kwargs:
- assert exc_info.match(kwargs['match'])
-
-
-pytest.raises = raises
-################################################################################
diff --git a/test/mitmproxy/addons/test_clientplayback.py b/test/mitmproxy/addons/test_clientplayback.py
index 7ffda317..6089b2d5 100644
--- a/test/mitmproxy/addons/test_clientplayback.py
+++ b/test/mitmproxy/addons/test_clientplayback.py
@@ -10,9 +10,10 @@ from mitmproxy.test import taddons
def tdump(path, flows):
- w = io.FlowWriter(open(path, "wb"))
- for i in flows:
- w.add(i)
+ with open(path, "wb") as f:
+ w = io.FlowWriter(f)
+ for i in flows:
+ w.add(i)
class MockThread():
diff --git a/test/mitmproxy/addons/test_save.py b/test/mitmproxy/addons/test_save.py
index 85c2a398..a4e425cd 100644
--- a/test/mitmproxy/addons/test_save.py
+++ b/test/mitmproxy/addons/test_save.py
@@ -26,8 +26,9 @@ def test_configure(tmpdir):
def rd(p):
- x = io.FlowReader(open(p, "rb"))
- return list(x.stream())
+ with open(p, "rb") as f:
+ x = io.FlowReader(f)
+ return list(x.stream())
def test_tcp(tmpdir):
diff --git a/test/mitmproxy/addons/test_serverplayback.py b/test/mitmproxy/addons/test_serverplayback.py
index 3ceab3fa..7605a5d9 100644
--- a/test/mitmproxy/addons/test_serverplayback.py
+++ b/test/mitmproxy/addons/test_serverplayback.py
@@ -11,9 +11,10 @@ from mitmproxy import io
def tdump(path, flows):
- w = io.FlowWriter(open(path, "wb"))
- for i in flows:
- w.add(i)
+ with open(path, "wb") as f:
+ w = io.FlowWriter(f)
+ for i in flows:
+ w.add(i)
def test_load_file(tmpdir):
diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py
index 6da13650..d5a3a456 100644
--- a/test/mitmproxy/addons/test_view.py
+++ b/test/mitmproxy/addons/test_view.py
@@ -132,9 +132,10 @@ def test_filter():
def tdump(path, flows):
- w = io.FlowWriter(open(path, "wb"))
- for i in flows:
- w.add(i)
+ with open(path, "wb") as f:
+ w = io.FlowWriter(f)
+ for i in flows:
+ w.add(i)
def test_create():
diff --git a/test/mitmproxy/contentviews/test_protobuf.py b/test/mitmproxy/contentviews/test_protobuf.py
index 31e382ec..71e51576 100644
--- a/test/mitmproxy/contentviews/test_protobuf.py
+++ b/test/mitmproxy/contentviews/test_protobuf.py
@@ -17,7 +17,9 @@ def test_view_protobuf_request():
m.configure_mock(**attrs)
n.return_value = m
- content_type, output = v(open(p, "rb").read())
+ with open(p, "rb") as f:
+ data = f.read()
+ content_type, output = v(data)
assert content_type == "Protobuf"
assert output[0] == [('text', b'1: "3bbc333c-e61c-433b-819a-0b9a8cc103b8"')]
diff --git a/test/mitmproxy/contentviews/test_wbxml.py b/test/mitmproxy/contentviews/test_wbxml.py
index 777ab4dd..09c770e7 100644
--- a/test/mitmproxy/contentviews/test_wbxml.py
+++ b/test/mitmproxy/contentviews/test_wbxml.py
@@ -1 +1,21 @@
-# TODO: write tests
+from mitmproxy.contentviews import wbxml
+from mitmproxy.test import tutils
+from . import full_eval
+
+data = tutils.test_data.push("mitmproxy/contentviews/test_wbxml_data/")
+
+
+def test_wbxml():
+ v = full_eval(wbxml.ViewWBXML())
+
+ assert v(b'\x03\x01\x6A\x00') == ('WBXML', [[('text', '<?xml version="1.0" ?>')]])
+ assert v(b'foo') is None
+
+ path = data.path("data.wbxml") # File taken from https://github.com/davidpshaw/PyWBXMLDecoder/tree/master/wbxml_samples
+ with open(path, 'rb') as f:
+ input = f.read()
+ with open("-formatted.".join(path.rsplit(".", 1))) as f:
+ expected = f.read()
+
+ p = wbxml.ASCommandResponse.ASCommandResponse(input)
+ assert p.xmlString == expected
diff --git a/test/mitmproxy/contentviews/test_wbxml_data/data-formatted.wbxml b/test/mitmproxy/contentviews/test_wbxml_data/data-formatted.wbxml
new file mode 100644
index 00000000..fed293bd
--- /dev/null
+++ b/test/mitmproxy/contentviews/test_wbxml_data/data-formatted.wbxml
@@ -0,0 +1,10 @@
+<?xml version="1.0" ?>
+<Sync>
+ <Collections>
+ <Collection>
+ <SyncKey>1509029063</SyncKey>
+ <CollectionId>7</CollectionId>
+ <Status>1</Status>
+ </Collection>
+ </Collections>
+</Sync>
diff --git a/test/mitmproxy/contentviews/test_wbxml_data/data.wbxml b/test/mitmproxy/contentviews/test_wbxml_data/data.wbxml
new file mode 100644
index 00000000..7c7a2004
--- /dev/null
+++ b/test/mitmproxy/contentviews/test_wbxml_data/data.wbxml
Binary files differ
diff --git a/test/mitmproxy/contrib/test_tls_parser.py b/test/mitmproxy/contrib/test_tls_parser.py
deleted file mode 100644
index 66972b62..00000000
--- a/test/mitmproxy/contrib/test_tls_parser.py
+++ /dev/null
@@ -1,38 +0,0 @@
-from mitmproxy.contrib import tls_parser
-
-
-def test_parse_chrome():
- """
- Test if we properly parse a ClientHello sent by Chrome 54.
- """
- data = bytes.fromhex(
- "03033b70638d2523e1cba15f8364868295305e9c52aceabda4b5147210abc783e6e1000022c02bc02fc02cc030"
- "cca9cca8cc14cc13c009c013c00ac014009c009d002f0035000a0100006cff0100010000000010000e00000b65"
- "78616d706c652e636f6d0017000000230000000d00120010060106030501050304010403020102030005000501"
- "00000000001200000010000e000c02683208687474702f312e3175500000000b00020100000a00080006001d00"
- "170018"
- )
- c = tls_parser.ClientHello.parse(data)
- assert c.version.major == 3
- assert c.version.minor == 3
-
- alpn = [a for a in c.extensions.extensions if a.type == 16]
- assert len(alpn) == 1
- assert alpn[0].alpn_protocols == [b"h2", b"http/1.1"]
-
- sni = [a for a in c.extensions.extensions if a.type == 0]
- assert len(sni) == 1
- assert sni[0].server_names[0].name_type == 0
- assert sni[0].server_names[0].host_name == b"example.com"
-
-
-def test_parse_no_extensions():
- data = bytes.fromhex(
- "03015658a756ab2c2bff55f636814deac086b7ca56b65058c7893ffc6074f5245f70205658a75475103a152637"
- "78e1bb6d22e8bbd5b6b0a3a59760ad354e91ba20d353001a0035002f000a000500040009000300060008006000"
- "61006200640100"
- )
- c = tls_parser.ClientHello.parse(data)
- assert c.version.major == 3
- assert c.version.minor == 1
- assert c.extensions is None
diff --git a/test/mitmproxy/net/test_tcp.py b/test/mitmproxy/net/test_tcp.py
index 81d51888..adf8701a 100644
--- a/test/mitmproxy/net/test_tcp.py
+++ b/test/mitmproxy/net/test_tcp.py
@@ -34,7 +34,7 @@ class ClientCipherListHandler(tcp.BaseHandler):
sni = None
def handle(self):
- self.wfile.write("%s" % self.connection.get_cipher_list())
+ self.wfile.write(str(self.connection.get_cipher_list()).encode())
self.wfile.flush()
@@ -398,7 +398,8 @@ class TestServerCipherList(tservers.ServerTestBase):
c = tcp.TCPClient(("127.0.0.1", self.port))
with c.connect():
c.convert_to_ssl(sni="foo.com")
- assert c.rfile.readline() == b"['AES256-GCM-SHA384']"
+ expected = b"['AES256-GCM-SHA384']"
+ assert c.rfile.read(len(expected) + 2) == expected
class TestServerCurrentCipher(tservers.ServerTestBase):
@@ -424,7 +425,7 @@ class TestServerCurrentCipher(tservers.ServerTestBase):
class TestServerCipherListError(tservers.ServerTestBase):
handler = ClientCipherListHandler
ssl = dict(
- cipher_list='bogus'
+ cipher_list=b'bogus'
)
def test_echo(self):
@@ -632,6 +633,7 @@ class TestTCPServer:
with s.handler_counter:
with pytest.raises(exceptions.Timeout):
s.wait_for_silence()
+ s.shutdown()
class TestFileLike:
diff --git a/test/mitmproxy/net/tservers.py b/test/mitmproxy/net/tservers.py
index ebe6d3eb..44701aa5 100644
--- a/test/mitmproxy/net/tservers.py
+++ b/test/mitmproxy/net/tservers.py
@@ -16,9 +16,6 @@ class _ServerThread(threading.Thread):
def run(self):
self.server.serve_forever()
- def shutdown(self):
- self.server.shutdown()
-
class _TServer(tcp.TCPServer):
@@ -54,9 +51,9 @@ class _TServer(tcp.TCPServer):
raw_key = self.ssl.get(
"key",
tutils.test_data.path("mitmproxy/net/data/server.key"))
- key = OpenSSL.crypto.load_privatekey(
- OpenSSL.crypto.FILETYPE_PEM,
- open(raw_key, "rb").read())
+ with open(raw_key) as f:
+ raw_key = f.read()
+ key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, raw_key)
if self.ssl.get("v3_only", False):
method = OpenSSL.SSL.SSLv3_METHOD
options = OpenSSL.SSL.OP_NO_SSLv2 | OpenSSL.SSL.OP_NO_TLSv1
@@ -64,7 +61,8 @@ class _TServer(tcp.TCPServer):
method = OpenSSL.SSL.SSLv23_METHOD
options = None
h.convert_to_ssl(
- cert, key,
+ cert,
+ key,
method=method,
options=options,
handle_sni=getattr(h, "handle_sni", None),
@@ -103,7 +101,7 @@ class ServerTestBase:
@classmethod
def teardown_class(cls):
- cls.server.shutdown()
+ cls.server.server.shutdown()
def teardown(self):
self.server.server.wait_for_silence()
diff --git a/test/mitmproxy/platform/test_pf.py b/test/mitmproxy/platform/test_pf.py
index f644bcc5..3292d345 100644
--- a/test/mitmproxy/platform/test_pf.py
+++ b/test/mitmproxy/platform/test_pf.py
@@ -9,10 +9,11 @@ class TestLookup:
def test_simple(self):
if sys.platform == "freebsd10":
p = tutils.test_data.path("mitmproxy/data/pf02")
- d = open(p, "rb").read()
else:
p = tutils.test_data.path("mitmproxy/data/pf01")
- d = open(p, "rb").read()
+ with open(p, "rb") as f:
+ d = f.read()
+
assert pf.lookup("192.168.1.111", 40000, d) == ("5.5.5.5", 80)
with pytest.raises(Exception, match="Could not resolve original destination"):
pf.lookup("192.168.1.112", 40000, d)
diff --git a/test/mitmproxy/proxy/protocol/test_http1.py b/test/mitmproxy/proxy/protocol/test_http1.py
index 07cd7dcc..b642afb3 100644
--- a/test/mitmproxy/proxy/protocol/test_http1.py
+++ b/test/mitmproxy/proxy/protocol/test_http1.py
@@ -65,6 +65,7 @@ class TestExpectHeader(tservers.HTTPProxyTest):
assert resp.status_code == 200
client.finish()
+ client.close()
class TestHeadContentLength(tservers.HTTPProxyTest):
diff --git a/test/mitmproxy/proxy/protocol/test_http2.py b/test/mitmproxy/proxy/protocol/test_http2.py
index b07257b3..261f8415 100644
--- a/test/mitmproxy/proxy/protocol/test_http2.py
+++ b/test/mitmproxy/proxy/protocol/test_http2.py
@@ -118,12 +118,16 @@ class _Http2TestBase:
self.master.reset([])
self.server.server.handle_server_event = self.handle_server_event
- def _setup_connection(self):
- client = mitmproxy.net.tcp.TCPClient(("127.0.0.1", self.proxy.port))
- client.connect()
+ def teardown(self):
+ if self.client:
+ self.client.close()
+
+ def setup_connection(self):
+ self.client = mitmproxy.net.tcp.TCPClient(("127.0.0.1", self.proxy.port))
+ self.client.connect()
# send CONNECT request
- client.wfile.write(http1.assemble_request(mitmproxy.net.http.Request(
+ self.client.wfile.write(http1.assemble_request(mitmproxy.net.http.Request(
'authority',
b'CONNECT',
b'',
@@ -134,13 +138,13 @@ class _Http2TestBase:
[(b'host', b'localhost:%d' % self.server.server.address[1])],
b'',
)))
- client.wfile.flush()
+ self.client.wfile.flush()
# read CONNECT response
- while client.rfile.readline() != b"\r\n":
+ while self.client.rfile.readline() != b"\r\n":
pass
- client.convert_to_ssl(alpn_protos=[b'h2'])
+ self.client.convert_to_ssl(alpn_protos=[b'h2'])
config = h2.config.H2Configuration(
client_side=True,
@@ -148,10 +152,10 @@ class _Http2TestBase:
validate_inbound_headers=False)
h2_conn = h2.connection.H2Connection(config)
h2_conn.initiate_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
- return client, h2_conn
+ return h2_conn
def _send_request(self,
wfile,
@@ -205,8 +209,8 @@ class TestSimple(_Http2Test):
if isinstance(event, h2.events.ConnectionTerminated):
return False
elif isinstance(event, h2.events.RequestReceived):
- assert (b'client-foo', b'client-bar-1') in event.headers
- assert (b'client-foo', b'client-bar-2') in event.headers
+ assert (b'self.client-foo', b'self.client-bar-1') in event.headers
+ assert (b'self.client-foo', b'self.client-bar-2') in event.headers
elif isinstance(event, h2.events.StreamEnded):
import warnings
with warnings.catch_warnings():
@@ -233,32 +237,32 @@ class TestSimple(_Http2Test):
def test_simple(self):
response_body_buffer = b''
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
self._send_request(
- client.wfile,
+ self.client.wfile,
h2_conn,
headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
(':method', 'GET'),
(':scheme', 'https'),
(':path', '/'),
- ('ClIeNt-FoO', 'client-bar-1'),
- ('ClIeNt-FoO', 'client-bar-2'),
+ ('self.client-FoO', 'self.client-bar-1'),
+ ('self.client-FoO', 'self.client-bar-2'),
],
body=b'request body')
done = False
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
assert False
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.DataReceived):
@@ -267,8 +271,8 @@ class TestSimple(_Http2Test):
done = True
h2_conn.close_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
assert len(self.master.state.flows) == 1
assert self.master.state.flows[0].response.status_code == 200
@@ -317,10 +321,10 @@ class TestRequestWithPriority(_Http2Test):
def test_request_with_priority(self, http2_priority_enabled, priority, expected_priority):
self.config.options.http2_priority = http2_priority_enabled
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
self._send_request(
- client.wfile,
+ self.client.wfile,
h2_conn,
headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
@@ -336,22 +340,22 @@ class TestRequestWithPriority(_Http2Test):
done = False
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
assert False
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamEnded):
done = True
h2_conn.close_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
assert len(self.master.state.flows) == 1
@@ -397,15 +401,15 @@ class TestPriority(_Http2Test):
self.config.options.http2_priority = http2_priority_enabled
self.__class__.priority_data = []
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
if prioritize_before:
h2_conn.prioritize(1, exclusive=priority[0], depends_on=priority[1], weight=priority[2])
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
self._send_request(
- client.wfile,
+ self.client.wfile,
h2_conn,
headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
@@ -419,28 +423,28 @@ class TestPriority(_Http2Test):
if not prioritize_before:
h2_conn.prioritize(1, exclusive=priority[0], depends_on=priority[1], weight=priority[2])
h2_conn.end_stream(1)
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
done = False
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
assert False
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamEnded):
done = True
h2_conn.close_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
assert len(self.master.state.flows) == 1
assert self.priority_data == expected_priority
@@ -460,10 +464,10 @@ class TestStreamResetFromServer(_Http2Test):
return True
def test_request_with_priority(self):
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
self._send_request(
- client.wfile,
+ self.client.wfile,
h2_conn,
headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
@@ -476,22 +480,22 @@ class TestStreamResetFromServer(_Http2Test):
done = False
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
assert False
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamReset):
done = True
h2_conn.close_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
assert len(self.master.state.flows) == 1
assert self.master.state.flows[0].response is None
@@ -510,10 +514,10 @@ class TestBodySizeLimit(_Http2Test):
self.config.options.body_size_limit = "20"
self.config.options._processed["body_size_limit"] = 20
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
self._send_request(
- client.wfile,
+ self.client.wfile,
h2_conn,
headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
@@ -527,22 +531,22 @@ class TestBodySizeLimit(_Http2Test):
done = False
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
assert False
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamReset):
done = True
h2_conn.close_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
assert len(self.master.state.flows) == 0
@@ -609,9 +613,9 @@ class TestPushPromise(_Http2Test):
return True
def test_push_promise(self):
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
- self._send_request(client.wfile, h2_conn, stream_id=1, headers=[
+ self._send_request(self.client.wfile, h2_conn, stream_id=1, headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
(':method', 'GET'),
(':scheme', 'https'),
@@ -625,15 +629,15 @@ class TestPushPromise(_Http2Test):
responses = 0
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
assert False
except:
break
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamEnded):
@@ -649,8 +653,8 @@ class TestPushPromise(_Http2Test):
done = True
h2_conn.close_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
assert ended_streams == 3
assert pushed_streams == 2
@@ -665,9 +669,9 @@ class TestPushPromise(_Http2Test):
assert len(pushed_flows) == 2
def test_push_promise_reset(self):
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
- self._send_request(client.wfile, h2_conn, stream_id=1, headers=[
+ self._send_request(self.client.wfile, h2_conn, stream_id=1, headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
(':method', 'GET'),
(':scheme', 'https'),
@@ -681,14 +685,14 @@ class TestPushPromise(_Http2Test):
responses = 0
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
assert False
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamEnded) and event.stream_id == 1:
@@ -696,8 +700,8 @@ class TestPushPromise(_Http2Test):
elif isinstance(event, h2.events.PushedStreamReceived):
pushed_streams += 1
h2_conn.reset_stream(event.pushed_stream_id, error_code=0x8)
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
elif isinstance(event, h2.events.ResponseReceived):
responses += 1
if isinstance(event, h2.events.ConnectionTerminated):
@@ -707,8 +711,8 @@ class TestPushPromise(_Http2Test):
done = True
h2_conn.close_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
bodies = [flow.response.content for flow in self.master.state.flows if flow.response]
assert len(bodies) >= 1
@@ -728,9 +732,9 @@ class TestConnectionLost(_Http2Test):
return False
def test_connection_lost(self):
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
- self._send_request(client.wfile, h2_conn, stream_id=1, headers=[
+ self._send_request(self.client.wfile, h2_conn, stream_id=1, headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
(':method', 'GET'),
(':scheme', 'https'),
@@ -741,7 +745,7 @@ class TestConnectionLost(_Http2Test):
done = False
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
h2_conn.receive_data(raw)
except exceptions.HttpException:
print(traceback.format_exc())
@@ -749,8 +753,8 @@ class TestConnectionLost(_Http2Test):
except:
break
try:
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
except:
break
@@ -782,12 +786,12 @@ class TestMaxConcurrentStreams(_Http2Test):
return True
def test_max_concurrent_streams(self):
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
new_streams = [1, 3, 5, 7, 9, 11]
for stream_id in new_streams:
# this will exceed MAX_CONCURRENT_STREAMS on the server connection
# and cause mitmproxy to throttle stream creation to the server
- self._send_request(client.wfile, h2_conn, stream_id=stream_id, headers=[
+ self._send_request(self.client.wfile, h2_conn, stream_id=stream_id, headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
(':method', 'GET'),
(':scheme', 'https'),
@@ -798,20 +802,20 @@ class TestMaxConcurrentStreams(_Http2Test):
ended_streams = 0
while ended_streams != len(new_streams):
try:
- header, body = http2.read_raw_frame(client.rfile)
+ header, body = http2.read_raw_frame(self.client.rfile)
events = h2_conn.receive_data(b''.join([header, body]))
except:
break
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
for event in events:
if isinstance(event, h2.events.StreamEnded):
ended_streams += 1
h2_conn.close_connection()
- client.wfile.write(h2_conn.data_to_send())
- client.wfile.flush()
+ self.client.wfile.write(h2_conn.data_to_send())
+ self.client.wfile.flush()
assert len(self.master.state.flows) == len(new_streams)
for flow in self.master.state.flows:
@@ -831,9 +835,9 @@ class TestConnectionTerminated(_Http2Test):
return True
def test_connection_terminated(self):
- client, h2_conn = self._setup_connection()
+ h2_conn = self.setup_connection()
- self._send_request(client.wfile, h2_conn, headers=[
+ self._send_request(self.client.wfile, h2_conn, headers=[
(':authority', "127.0.0.1:{}".format(self.server.server.address[1])),
(':method', 'GET'),
(':scheme', 'https'),
@@ -844,7 +848,7 @@ class TestConnectionTerminated(_Http2Test):
connection_terminated_event = None
while not done:
try:
- raw = b''.join(http2.read_raw_frame(client.rfile))
+ raw = b''.join(http2.read_raw_frame(self.client.rfile))
events = h2_conn.receive_data(raw)
for event in events:
if isinstance(event, h2.events.ConnectionTerminated):
diff --git a/test/mitmproxy/proxy/protocol/test_tls.py b/test/mitmproxy/proxy/protocol/test_tls.py
index e17ee46f..980ba7bd 100644
--- a/test/mitmproxy/proxy/protocol/test_tls.py
+++ b/test/mitmproxy/proxy/protocol/test_tls.py
@@ -23,4 +23,5 @@ class TestClientHello:
)
c = TlsClientHello(data)
assert c.sni == 'example.com'
- assert c.alpn_protocols == [b'h2', b'http/1.1']
+ assert c.alpn_protocols[0].name == b'h2'
+ assert c.alpn_protocols[1].name == b'http/1.1'
diff --git a/test/mitmproxy/proxy/protocol/test_websocket.py b/test/mitmproxy/proxy/protocol/test_websocket.py
index 8dfc4f2b..f78e173f 100644
--- a/test/mitmproxy/proxy/protocol/test_websocket.py
+++ b/test/mitmproxy/proxy/protocol/test_websocket.py
@@ -79,9 +79,13 @@ class _WebSocketTestBase:
self.master.reset([])
self.server.server.handle_websockets = self.handle_websockets
- def _setup_connection(self):
- client = tcp.TCPClient(("127.0.0.1", self.proxy.port))
- client.connect()
+ def teardown(self):
+ if self.client:
+ self.client.close()
+
+ def setup_connection(self):
+ self.client = tcp.TCPClient(("127.0.0.1", self.proxy.port))
+ self.client.connect()
request = http.Request(
"authority",
@@ -92,14 +96,14 @@ class _WebSocketTestBase:
"",
"HTTP/1.1",
content=b'')
- client.wfile.write(http.http1.assemble_request(request))
- client.wfile.flush()
+ self.client.wfile.write(http.http1.assemble_request(request))
+ self.client.wfile.flush()
- response = http.http1.read_response(client.rfile, request)
+ response = http.http1.read_response(self.client.rfile, request)
if self.ssl:
- client.convert_to_ssl()
- assert client.ssl_established
+ self.client.convert_to_ssl()
+ assert self.client.ssl_established
request = http.Request(
"relative",
@@ -116,14 +120,12 @@ class _WebSocketTestBase:
sec_websocket_key="1234",
),
content=b'')
- client.wfile.write(http.http1.assemble_request(request))
- client.wfile.flush()
+ self.client.wfile.write(http.http1.assemble_request(request))
+ self.client.wfile.flush()
- response = http.http1.read_response(client.rfile, request)
+ response = http.http1.read_response(self.client.rfile, request)
assert websockets.check_handshake(response.headers)
- return client
-
class _WebSocketTest(_WebSocketTestBase, _WebSocketServerBase):
@@ -154,25 +156,25 @@ class TestSimple(_WebSocketTest):
wfile.flush()
def test_simple(self):
- client = self._setup_connection()
+ self.setup_connection()
- frame = websockets.Frame.from_file(client.rfile)
+ frame = websockets.Frame.from_file(self.client.rfile)
assert frame.payload == b'server-foobar'
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.TEXT, payload=b'client-foobar')))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.TEXT, payload=b'self.client-foobar')))
+ self.client.wfile.flush()
- frame = websockets.Frame.from_file(client.rfile)
- assert frame.payload == b'client-foobar'
+ frame = websockets.Frame.from_file(self.client.rfile)
+ assert frame.payload == b'self.client-foobar'
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.BINARY, payload=b'\xde\xad\xbe\xef')))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.BINARY, payload=b'\xde\xad\xbe\xef')))
+ self.client.wfile.flush()
- frame = websockets.Frame.from_file(client.rfile)
+ frame = websockets.Frame.from_file(self.client.rfile)
assert frame.payload == b'\xde\xad\xbe\xef'
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE)))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE)))
+ self.client.wfile.flush()
assert len(self.master.state.flows) == 2
assert isinstance(self.master.state.flows[0], HTTPFlow)
@@ -180,9 +182,9 @@ class TestSimple(_WebSocketTest):
assert len(self.master.state.flows[1].messages) == 5
assert self.master.state.flows[1].messages[0].content == b'server-foobar'
assert self.master.state.flows[1].messages[0].type == websockets.OPCODE.TEXT
- assert self.master.state.flows[1].messages[1].content == b'client-foobar'
+ assert self.master.state.flows[1].messages[1].content == b'self.client-foobar'
assert self.master.state.flows[1].messages[1].type == websockets.OPCODE.TEXT
- assert self.master.state.flows[1].messages[2].content == b'client-foobar'
+ assert self.master.state.flows[1].messages[2].content == b'self.client-foobar'
assert self.master.state.flows[1].messages[2].type == websockets.OPCODE.TEXT
assert self.master.state.flows[1].messages[3].content == b'\xde\xad\xbe\xef'
assert self.master.state.flows[1].messages[3].type == websockets.OPCODE.BINARY
@@ -203,19 +205,19 @@ class TestSimpleTLS(_WebSocketTest):
wfile.flush()
def test_simple_tls(self):
- client = self._setup_connection()
+ self.setup_connection()
- frame = websockets.Frame.from_file(client.rfile)
+ frame = websockets.Frame.from_file(self.client.rfile)
assert frame.payload == b'server-foobar'
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.TEXT, payload=b'client-foobar')))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.TEXT, payload=b'self.client-foobar')))
+ self.client.wfile.flush()
- frame = websockets.Frame.from_file(client.rfile)
- assert frame.payload == b'client-foobar'
+ frame = websockets.Frame.from_file(self.client.rfile)
+ assert frame.payload == b'self.client-foobar'
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE)))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE)))
+ self.client.wfile.flush()
class TestPing(_WebSocketTest):
@@ -233,16 +235,16 @@ class TestPing(_WebSocketTest):
wfile.flush()
def test_ping(self):
- client = self._setup_connection()
+ self.setup_connection()
- frame = websockets.Frame.from_file(client.rfile)
+ frame = websockets.Frame.from_file(self.client.rfile)
assert frame.header.opcode == websockets.OPCODE.PING
assert frame.payload == b'foobar'
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.PONG, payload=frame.payload)))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.PONG, payload=frame.payload)))
+ self.client.wfile.flush()
- frame = websockets.Frame.from_file(client.rfile)
+ frame = websockets.Frame.from_file(self.client.rfile)
assert frame.header.opcode == websockets.OPCODE.TEXT
assert frame.payload == b'pong-received'
@@ -259,12 +261,12 @@ class TestPong(_WebSocketTest):
wfile.flush()
def test_pong(self):
- client = self._setup_connection()
+ self.setup_connection()
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.PING, payload=b'foobar')))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.PING, payload=b'foobar')))
+ self.client.wfile.flush()
- frame = websockets.Frame.from_file(client.rfile)
+ frame = websockets.Frame.from_file(self.client.rfile)
assert frame.header.opcode == websockets.OPCODE.PONG
assert frame.payload == b'foobar'
@@ -282,34 +284,34 @@ class TestClose(_WebSocketTest):
websockets.Frame.from_file(rfile)
def test_close(self):
- client = self._setup_connection()
+ self.setup_connection()
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE)))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE)))
+ self.client.wfile.flush()
- websockets.Frame.from_file(client.rfile)
+ websockets.Frame.from_file(self.client.rfile)
with pytest.raises(exceptions.TcpDisconnect):
- websockets.Frame.from_file(client.rfile)
+ websockets.Frame.from_file(self.client.rfile)
def test_close_payload_1(self):
- client = self._setup_connection()
+ self.setup_connection()
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE, payload=b'\00\42')))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE, payload=b'\00\42')))
+ self.client.wfile.flush()
- websockets.Frame.from_file(client.rfile)
+ websockets.Frame.from_file(self.client.rfile)
with pytest.raises(exceptions.TcpDisconnect):
- websockets.Frame.from_file(client.rfile)
+ websockets.Frame.from_file(self.client.rfile)
def test_close_payload_2(self):
- client = self._setup_connection()
+ self.setup_connection()
- client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE, payload=b'\00\42foobar')))
- client.wfile.flush()
+ self.client.wfile.write(bytes(websockets.Frame(fin=1, opcode=websockets.OPCODE.CLOSE, payload=b'\00\42foobar')))
+ self.client.wfile.flush()
- websockets.Frame.from_file(client.rfile)
+ websockets.Frame.from_file(self.client.rfile)
with pytest.raises(exceptions.TcpDisconnect):
- websockets.Frame.from_file(client.rfile)
+ websockets.Frame.from_file(self.client.rfile)
class TestInvalidFrame(_WebSocketTest):
@@ -320,9 +322,9 @@ class TestInvalidFrame(_WebSocketTest):
wfile.flush()
def test_invalid_frame(self):
- client = self._setup_connection()
+ self.setup_connection()
# with pytest.raises(exceptions.TcpDisconnect):
- frame = websockets.Frame.from_file(client.rfile)
+ frame = websockets.Frame.from_file(self.client.rfile)
assert frame.header.opcode == 15
assert frame.payload == b'foobar'
diff --git a/test/mitmproxy/test_connections.py b/test/mitmproxy/test_connections.py
index e320885d..99367bb6 100644
--- a/test/mitmproxy/test_connections.py
+++ b/test/mitmproxy/test_connections.py
@@ -1,5 +1,4 @@
import socket
-import os
import threading
import ssl
import OpenSSL
@@ -140,16 +139,17 @@ class TestServerConnection:
assert d.last_log()
c.finish()
+ c.close()
d.shutdown()
def test_terminate_error(self):
d = test.Daemon()
c = connections.ServerConnection((d.IFACE, d.port))
c.connect()
+ c.close()
c.connection = mock.Mock()
c.connection.recv = mock.Mock(return_value=False)
c.connection.flush = mock.Mock(side_effect=exceptions.TcpDisconnect)
- c.finish()
d.shutdown()
def test_sni(self):
@@ -194,22 +194,25 @@ class TestClientConnectionTLS:
s = socket.create_connection(address)
s = ctx.wrap_socket(s, server_hostname=sni)
s.send(b'foobar')
- s.shutdown(socket.SHUT_RDWR)
+ s.close()
threading.Thread(target=client_run).start()
connection, client_address = sock.accept()
c = connections.ClientConnection(connection, client_address, None)
cert = tutils.test_data.path("mitmproxy/net/data/server.crt")
+ with open(tutils.test_data.path("mitmproxy/net/data/server.key")) as f:
+ raw_key = f.read()
key = OpenSSL.crypto.load_privatekey(
OpenSSL.crypto.FILETYPE_PEM,
- open(tutils.test_data.path("mitmproxy/net/data/server.key"), "rb").read())
+ raw_key)
c.convert_to_ssl(cert, key)
assert c.connected()
assert c.sni == sni
assert c.tls_established
assert c.rfile.read(6) == b'foobar'
c.finish()
+ sock.close()
class TestServerConnectionTLS(tservers.ServerTestBase):
@@ -222,7 +225,7 @@ class TestServerConnectionTLS(tservers.ServerTestBase):
@pytest.mark.parametrize("clientcert", [
None,
tutils.test_data.path("mitmproxy/data/clientcert"),
- os.path.join(tutils.test_data.path("mitmproxy/data/clientcert"), "client.pem"),
+ tutils.test_data.path("mitmproxy/data/clientcert/client.pem"),
])
def test_tls(self, clientcert):
c = connections.ServerConnection(("127.0.0.1", self.port))
diff --git a/test/mitmproxy/test_flowfilter.py b/test/mitmproxy/test_flowfilter.py
index 46fff477..fe9b2408 100644
--- a/test/mitmproxy/test_flowfilter.py
+++ b/test/mitmproxy/test_flowfilter.py
@@ -209,6 +209,9 @@ class TestMatchingHTTPFlow:
assert self.q("~u address:22/path", q)
assert not self.q("~u moo/path", q)
+ q.request = None
+ assert not self.q("~u address", q)
+
assert self.q("~u address", s)
assert self.q("~u address:22/path", s)
assert not self.q("~u moo/path", s)
diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py
index e1d0da00..299abab3 100644
--- a/test/mitmproxy/test_proxy.py
+++ b/test/mitmproxy/test_proxy.py
@@ -32,8 +32,7 @@ class TestProcessProxyOptions:
opts = options.Options()
cmdline.common_options(parser, opts)
args = parser.parse_args(args=args)
- main.process_options(parser, opts, args)
- pconf = config.ProxyConfig(opts)
+ pconf = main.process_options(parser, opts, args)
return parser, pconf
def assert_noerr(self, *args):
diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py
index b8005529..3a2050e1 100644
--- a/test/mitmproxy/tservers.py
+++ b/test/mitmproxy/tservers.py
@@ -133,7 +133,7 @@ class ProxyTestBase:
@classmethod
def teardown_class(cls):
- # perf: we want to run tests in parallell
+ # perf: we want to run tests in parallel
# should this ever cause an error, travis should catch it.
# shutil.rmtree(cls.cadir)
cls.proxy.shutdown()
diff --git a/test/pathod/language/test_base.py b/test/pathod/language/test_base.py
index ec460b07..910d298a 100644
--- a/test/pathod/language/test_base.py
+++ b/test/pathod/language/test_base.py
@@ -202,12 +202,14 @@ class TestMisc:
e.parseString("m@1")
s = base.Settings(staticdir=str(tmpdir))
- tmpdir.join("path").write_binary(b"a" * 20, ensure=True)
+ with open(str(tmpdir.join("path")), 'wb') as f:
+ f.write(b"a" * 20)
v = e.parseString("m<path")[0]
with pytest.raises(Exception, match="Invalid value length"):
v.values(s)
- tmpdir.join("path2").write_binary(b"a" * 4, ensure=True)
+ with open(str(tmpdir.join("path2")), 'wb') as f:
+ f.write(b"a" * 4)
v = e.parseString("m<path2")[0]
assert v.values(s)
diff --git a/test/pathod/language/test_generators.py b/test/pathod/language/test_generators.py
index dc15aaa1..5e64c726 100644
--- a/test/pathod/language/test_generators.py
+++ b/test/pathod/language/test_generators.py
@@ -14,18 +14,14 @@ def test_randomgenerator():
def test_filegenerator(tmpdir):
f = tmpdir.join("foo")
- f.write(b"x" * 10000)
+ f.write(b"abcdefghijklmnopqrstuvwxyz" * 1000)
g = generators.FileGenerator(str(f))
- assert len(g) == 10000
- assert g[0] == b"x"
- assert g[-1] == b"x"
- assert g[0:5] == b"xxxxx"
+ assert len(g) == 26000
+ assert g[0] == b"a"
+ assert g[2:7] == b"cdefg"
assert len(g[1:10]) == 9
- assert len(g[10000:10001]) == 0
+ assert len(g[26000:26001]) == 0
assert repr(g)
- # remove all references to FileGenerator instance to close the file
- # handle.
- del g
def test_transform_generator():
diff --git a/test/pathod/protocols/test_http2.py b/test/pathod/protocols/test_http2.py
index 1c074197..c16a6d40 100644
--- a/test/pathod/protocols/test_http2.py
+++ b/test/pathod/protocols/test_http2.py
@@ -202,7 +202,7 @@ class TestApplySettings(net_tservers.ServerTestBase):
def handle(self):
# check settings acknowledgement
assert self.rfile.read(9) == codecs.decode('000000040100000000', 'hex_codec')
- self.wfile.write("OK")
+ self.wfile.write(b"OK")
self.wfile.flush()
self.rfile.safe_read(9) # just to keep the connection alive a bit longer
diff --git a/test/pathod/test_test.py b/test/pathod/test_test.py
index 40f45f53..d51a2c7a 100644
--- a/test/pathod/test_test.py
+++ b/test/pathod/test_test.py
@@ -1,15 +1,9 @@
-import logging
+import os
import requests
import pytest
from pathod import test
-
-from mitmproxy.test import tutils
-
-import requests.packages.urllib3
-
-requests.packages.urllib3.disable_warnings()
-logging.disable(logging.CRITICAL)
+from pathod.pathod import SSLOptions, CA_CERT_NAME
class TestDaemonManual:
@@ -22,29 +16,17 @@ class TestDaemonManual:
with pytest.raises(requests.ConnectionError):
requests.get("http://localhost:%s/p/202:da" % d.port)
- def test_startstop_ssl(self):
- d = test.Daemon(ssl=True)
- rsp = requests.get(
- "https://localhost:%s/p/202:da" %
- d.port,
- verify=False)
- assert rsp.ok
- assert rsp.status_code == 202
- d.shutdown()
- with pytest.raises(requests.ConnectionError):
- requests.get("http://localhost:%s/p/202:da" % d.port)
-
- def test_startstop_ssl_explicit(self):
- ssloptions = dict(
- certfile=tutils.test_data.path("pathod/data/testkey.pem"),
- cacert=tutils.test_data.path("pathod/data/testkey.pem"),
- ssl_after_connect=False
+ @pytest.mark.parametrize('not_after_connect', [True, False])
+ def test_startstop_ssl(self, not_after_connect):
+ ssloptions = SSLOptions(
+ cn=b'localhost',
+ sans=[b'localhost', b'127.0.0.1'],
+ not_after_connect=not_after_connect,
)
- d = test.Daemon(ssl=ssloptions)
+ d = test.Daemon(ssl=True, ssloptions=ssloptions)
rsp = requests.get(
- "https://localhost:%s/p/202:da" %
- d.port,
- verify=False)
+ "https://localhost:%s/p/202:da" % d.port,
+ verify=os.path.expanduser(os.path.join(d.thread.server.ssloptions.confdir, CA_CERT_NAME)))
assert rsp.ok
assert rsp.status_code == 202
d.shutdown()
diff --git a/test/pathod/tservers.py b/test/pathod/tservers.py
index fab09288..a7c92964 100644
--- a/test/pathod/tservers.py
+++ b/test/pathod/tservers.py
@@ -1,3 +1,4 @@
+import os
import tempfile
import re
import shutil
@@ -13,6 +14,7 @@ from pathod import language
from pathod import pathoc
from pathod import pathod
from pathod import test
+from pathod.pathod import CA_CERT_NAME
def treader(bytes):
@@ -72,7 +74,7 @@ class DaemonTests:
self.d.port,
path
),
- verify=False,
+ verify=os.path.join(self.d.thread.server.ssloptions.confdir, CA_CERT_NAME),
params=params
)
return resp