diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/full_coverage_plugin.py | 7 | ||||
-rw-r--r-- | test/mitmproxy/contentviews/test_api.py | 15 | ||||
-rw-r--r-- | test/mitmproxy/contentviews/test_xml_html.py | 8 | ||||
-rw-r--r-- | test/mitmproxy/contentviews/test_xml_html_data/test-formatted.html | 44 | ||||
-rw-r--r-- | test/mitmproxy/contentviews/test_xml_html_data/test.html | 14 | ||||
-rw-r--r-- | test/mitmproxy/net/test_tcp.py | 43 | ||||
-rw-r--r-- | test/mitmproxy/test_controller.py | 2 |
7 files changed, 124 insertions, 9 deletions
diff --git a/test/full_coverage_plugin.py b/test/full_coverage_plugin.py index d98c29d6..ec30a9f9 100644 --- a/test/full_coverage_plugin.py +++ b/test/full_coverage_plugin.py @@ -1,6 +1,7 @@ import os import configparser import pytest +import sys here = os.path.abspath(os.path.dirname(__file__)) @@ -59,6 +60,12 @@ def pytest_runtestloop(session): if os.name == 'nt': cov.exclude('pragma: windows no cover') + if sys.platform == 'darwin': + cov.exclude('pragma: osx no cover') + + if os.environ.get("OPENSSL") == "old": + cov.exclude('pragma: openssl-old no cover') + yield coverage_values = dict([(name, 0) for name in pytest.config.option.full_cov]) diff --git a/test/mitmproxy/contentviews/test_api.py b/test/mitmproxy/contentviews/test_api.py index 95d83af9..c072c86f 100644 --- a/test/mitmproxy/contentviews/test_api.py +++ b/test/mitmproxy/contentviews/test_api.py @@ -9,23 +9,28 @@ from mitmproxy.test import tutils class TestContentView(contentviews.View): name = "test" - prompt = ("t", "test") + prompt = ("test", "t") content_types = ["test/123"] def test_add_remove(): tcv = TestContentView() contentviews.add(tcv) + assert tcv in contentviews.views # repeated addition causes exception - with pytest.raises(ContentViewException): + with pytest.raises(ContentViewException, match="Duplicate view"): contentviews.add(tcv) + tcv2 = TestContentView() + tcv2.name = "test2" + tcv2.prompt = ("test2", "t") # Same shortcut doesn't work either. - with pytest.raises(ContentViewException): - contentviews.add(TestContentView()) + with pytest.raises(ContentViewException, match="Duplicate view shortcut"): + contentviews.add(tcv2) contentviews.remove(tcv) + assert tcv not in contentviews.views def test_get_content_view(): @@ -43,6 +48,7 @@ def test_get_content_view(): headers=Headers(content_type="application/json") ) assert desc == "JSON" + assert list(lines) desc, lines, err = contentviews.get_content_view( contentviews.get("JSON"), @@ -84,3 +90,4 @@ def test_get_message_content_view(): def test_get_by_shortcut(): assert contentviews.get_by_shortcut("s") + assert not contentviews.get_by_shortcut("b") diff --git a/test/mitmproxy/contentviews/test_xml_html.py b/test/mitmproxy/contentviews/test_xml_html.py index 2b0aee4d..8148fd4c 100644 --- a/test/mitmproxy/contentviews/test_xml_html.py +++ b/test/mitmproxy/contentviews/test_xml_html.py @@ -11,6 +11,13 @@ def test_simple(): v = full_eval(xml_html.ViewXmlHtml()) assert v(b"foo") == ('XML', [[('text', 'foo')]]) assert v(b"<html></html>") == ('HTML', [[('text', '<html></html>')]]) + assert v(b"<>") == ('XML', [[('text', '<>')]]) + assert v(b"<p") == ('XML', [[('text', '<p')]]) + + with open(data.path("simple.html")) as f: + input = f.read() + tokens = xml_html.tokenize(input) + assert str(next(tokens)) == "Tag(<!DOCTYPE html>)" @pytest.mark.parametrize("filename", [ @@ -18,6 +25,7 @@ def test_simple(): "cdata.xml", "comment.xml", "inline.html", + "test.html" ]) def test_format_xml(filename): path = data.path(filename) diff --git a/test/mitmproxy/contentviews/test_xml_html_data/test-formatted.html b/test/mitmproxy/contentviews/test_xml_html_data/test-formatted.html new file mode 100644 index 00000000..0eb60004 --- /dev/null +++ b/test/mitmproxy/contentviews/test_xml_html_data/test-formatted.html @@ -0,0 +1,44 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Title</title> +</head> +<body> + <p> + Lorem ipsum dolor + <p> + sit amet, consectetur + <p> + adipiscing elit, sed + <p> + do eiusmod tempor + <p> + incididunt ut + <p> + labore et dolore + <p> + magna aliqua. + <p> + Ut enim ad minim + <p> + veniam, quis nostrud + <p> + exercitation + <p> + ullamco laboris + <p> + nisi ut aliquip ex ea + <p> + commodo consequat. + <p> + Duis aute irure + <p> + dolor in reprehenderit + <p> + in voluptate velit + <p> + esse cillum dolore + <p>eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> +</body> +</html> diff --git a/test/mitmproxy/contentviews/test_xml_html_data/test.html b/test/mitmproxy/contentviews/test_xml_html_data/test.html new file mode 100644 index 00000000..e74ac314 --- /dev/null +++ b/test/mitmproxy/contentviews/test_xml_html_data/test.html @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Title</title> +</head> +<body> +<p>Lorem ipsum dolor<p>sit amet, consectetur <p>adipiscing elit, sed<p>do eiusmod tempor<p> incididunt ut<p> labore et dolore<p> magna aliqua. + <p>Ut enim ad minim <p>veniam, quis nostrud <p>exercitation <p>ullamco laboris <p> + nisi ut aliquip ex ea <p>commodo consequat.<p>Duis aute irure <p>dolor in reprehenderit <p>in voluptate velit<p> esse cillum dolore <p>eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> + + +</body> +</html>
\ No newline at end of file diff --git a/test/mitmproxy/net/test_tcp.py b/test/mitmproxy/net/test_tcp.py index 8b26784a..81d51888 100644 --- a/test/mitmproxy/net/test_tcp.py +++ b/test/mitmproxy/net/test_tcp.py @@ -529,10 +529,10 @@ class TestTimeOut(tservers.ServerTestBase): class TestCryptographyALPN: def test_has_alpn(self): - if 'OPENSSL_ALPN' in os.environ: + if os.environ.get("OPENSSL") == "with-alpn": assert tcp.HAS_ALPN assert SSL._lib.Cryptography_HAS_ALPN - elif 'OPENSSL_OLD' in os.environ: + elif os.environ.get("OPENSSL") == "old": assert not tcp.HAS_ALPN assert not SSL._lib.Cryptography_HAS_ALPN @@ -603,13 +603,36 @@ class TestDHParams(tservers.ServerTestBase): assert ret[0] == "DHE-RSA-AES256-SHA" -class TestTCPClient: +class TestTCPClient(tservers.ServerTestBase): def test_conerr(self): c = tcp.TCPClient(("127.0.0.1", 0)) - with pytest.raises(exceptions.TcpException): + with pytest.raises(exceptions.TcpException, match="Error connecting"): c.connect() + def test_timeout(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + with c.create_connection(timeout=20) as conn: + assert conn.gettimeout() == 20 + + def test_spoof_address(self): + c = tcp.TCPClient(("127.0.0.1", self.port), spoof_source_address=("127.0.0.1", 0)) + with pytest.raises(exceptions.TcpException, match="Failed to spoof"): + c.connect() + + +class TestTCPServer: + + def test_binderr(self): + with pytest.raises(socket.error, match="prohibited"): + tcp.TCPServer(("localhost", 8080)) + + def test_wait_for_silence(self): + s = tcp.TCPServer(("127.0.0.1", 0)) + with s.handler_counter: + with pytest.raises(exceptions.Timeout): + s.wait_for_silence() + class TestFileLike: @@ -811,7 +834,7 @@ class TestSSLKeyLogger(tservers.ServerTestBase): assert not tcp.SSLKeyLogger.create_logfun(False) -class TestSSLInvalidMethod(tservers.ServerTestBase): +class TestSSLInvalid(tservers.ServerTestBase): handler = EchoHandler ssl = True @@ -821,3 +844,13 @@ class TestSSLInvalidMethod(tservers.ServerTestBase): with c.connect(): with pytest.raises(exceptions.TlsException): c.convert_to_ssl(method=fake_ssl_method) + + def test_alpn_error(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + with c.connect(): + if tcp.HAS_ALPN: + with pytest.raises(exceptions.TlsException, match="must be a function"): + c.create_ssl_context(alpn_select_callback="foo") + + with pytest.raises(exceptions.TlsException, match="ALPN error"): + c.create_ssl_context(alpn_select="foo", alpn_select_callback="bar") diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index ccc8bf35..2e13d298 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -176,6 +176,8 @@ class TestDummyReply: reply = controller.DummyReply() reply.ack() reply.take() + with pytest.raises(ControlException): + reply.mark_reset() reply.commit() reply.mark_reset() assert reply.state == "committed" |