aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/addons/test_core_option_validation.py1
-rw-r--r--test/mitmproxy/addons/test_proxyauth.py421
-rw-r--r--test/mitmproxy/addons/test_script.py27
-rw-r--r--test/mitmproxy/data/addonscripts/load_error.py2
-rw-r--r--test/mitmproxy/proxy/protocol/test_http2.py3
-rw-r--r--test/mitmproxy/proxy/protocol/test_tls.py3
-rw-r--r--test/mitmproxy/test_log.py5
-rw-r--r--test/mitmproxy/test_optmanager.py6
-rw-r--r--test/mitmproxy/utils/test_arg_check.py36
-rw-r--r--test/mitmproxy/utils/test_human.py1
10 files changed, 305 insertions, 200 deletions
diff --git a/test/mitmproxy/addons/test_core_option_validation.py b/test/mitmproxy/addons/test_core_option_validation.py
index 6d6d5ba4..cd5d4dfa 100644
--- a/test/mitmproxy/addons/test_core_option_validation.py
+++ b/test/mitmproxy/addons/test_core_option_validation.py
@@ -11,7 +11,6 @@ def test_simple():
with pytest.raises(exceptions.OptionsError):
tctx.configure(sa, body_size_limit = "invalid")
tctx.configure(sa, body_size_limit = "1m")
- assert tctx.master.options._processed["body_size_limit"]
with pytest.raises(exceptions.OptionsError, match="mutually exclusive"):
tctx.configure(
diff --git a/test/mitmproxy/addons/test_proxyauth.py b/test/mitmproxy/addons/test_proxyauth.py
index 40044bf0..1d05e137 100644
--- a/test/mitmproxy/addons/test_proxyauth.py
+++ b/test/mitmproxy/addons/test_proxyauth.py
@@ -10,197 +10,242 @@ from mitmproxy.test import tflow
from mitmproxy.test import tutils
-def test_parse_http_basic_auth():
- assert proxyauth.parse_http_basic_auth(
- proxyauth.mkauth("test", "test")
- ) == ("basic", "test", "test")
- with pytest.raises(ValueError):
- proxyauth.parse_http_basic_auth("")
- with pytest.raises(ValueError):
- proxyauth.parse_http_basic_auth("foo bar")
- with pytest.raises(ValueError):
- proxyauth.parse_http_basic_auth("basic abc")
- with pytest.raises(ValueError):
- v = "basic " + binascii.b2a_base64(b"foo").decode("ascii")
- proxyauth.parse_http_basic_auth(v)
-
-
-def test_configure():
- up = proxyauth.ProxyAuth()
- with taddons.context() as ctx:
- with pytest.raises(exceptions.OptionsError):
- ctx.configure(up, proxyauth="foo")
-
- ctx.configure(up, proxyauth="foo:bar")
- assert up.singleuser == ["foo", "bar"]
-
- ctx.configure(up, proxyauth=None)
- assert up.singleuser is None
-
- ctx.configure(up, proxyauth="any")
- assert up.nonanonymous
- ctx.configure(up, proxyauth=None)
- assert not up.nonanonymous
-
- with mock.patch('ldap3.Server', return_value="ldap://fake_server:389 - cleartext"):
- with mock.patch('ldap3.Connection', return_value="test"):
- ctx.configure(up, proxyauth="ldap:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com")
- assert up.ldapserver
- ctx.configure(up, proxyauth="ldaps:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com")
- assert up.ldapserver
-
- with pytest.raises(exceptions.OptionsError):
- ctx.configure(up, proxyauth="ldap:test:test:test")
-
- with pytest.raises(IndexError):
- ctx.configure(up, proxyauth="ldap:fake_serveruid=?dc=example,dc=com:person")
-
- with pytest.raises(exceptions.OptionsError):
- ctx.configure(up, proxyauth="ldapssssssss:fake_server:dn:password:tree")
-
- with pytest.raises(exceptions.OptionsError):
+class TestMkauth:
+ def test_mkauth_scheme(self):
+ assert proxyauth.mkauth('username', 'password') == 'basic dXNlcm5hbWU6cGFzc3dvcmQ=\n'
+
+ @pytest.mark.parametrize('scheme, expected', [
+ ('', ' dXNlcm5hbWU6cGFzc3dvcmQ=\n'),
+ ('basic', 'basic dXNlcm5hbWU6cGFzc3dvcmQ=\n'),
+ ('foobar', 'foobar dXNlcm5hbWU6cGFzc3dvcmQ=\n'),
+ ])
+ def test_mkauth(self, scheme, expected):
+ assert proxyauth.mkauth('username', 'password', scheme) == expected
+
+
+class TestParseHttpBasicAuth:
+ @pytest.mark.parametrize('input', [
+ '',
+ 'foo bar',
+ 'basic abc',
+ 'basic ' + binascii.b2a_base64(b"foo").decode("ascii"),
+ ])
+ def test_parse_http_basic_auth_error(self, input):
+ with pytest.raises(ValueError):
+ proxyauth.parse_http_basic_auth(input)
+
+ def test_parse_http_basic_auth(self):
+ input = proxyauth.mkauth("test", "test")
+ assert proxyauth.parse_http_basic_auth(input) == ("basic", "test", "test")
+
+
+class TestProxyAuth:
+ @pytest.mark.parametrize('mode, expected', [
+ ('', False),
+ ('foobar', False),
+ ('regular', True),
+ ('upstream:', True),
+ ('upstream:foobar', True),
+ ])
+ def test_is_proxy_auth(self, mode, expected):
+ up = proxyauth.ProxyAuth()
+ with taddons.context() as ctx:
+ ctx.options.mode = mode
+ assert up.is_proxy_auth() is expected
+
+ @pytest.mark.parametrize('is_proxy_auth, expected', [
+ (True, 'Proxy-Authorization'),
+ (False, 'Authorization'),
+ ])
+ def test_which_auth_header(self, is_proxy_auth, expected):
+ up = proxyauth.ProxyAuth()
+ with mock.patch('mitmproxy.addons.proxyauth.ProxyAuth.is_proxy_auth', return_value=is_proxy_auth):
+ assert up.which_auth_header() == expected
+
+ @pytest.mark.parametrize('is_proxy_auth, expected_status_code, expected_header', [
+ (True, 407, 'Proxy-Authenticate'),
+ (False, 401, 'WWW-Authenticate'),
+ ])
+ def test_auth_required_response(self, is_proxy_auth, expected_status_code, expected_header):
+ up = proxyauth.ProxyAuth()
+ with mock.patch('mitmproxy.addons.proxyauth.ProxyAuth.is_proxy_auth', return_value=is_proxy_auth):
+ resp = up.auth_required_response()
+ assert resp.status_code == expected_status_code
+ assert expected_header in resp.headers.keys()
+
+ def test_check(self):
+ up = proxyauth.ProxyAuth()
+ with taddons.context() as ctx:
+ ctx.configure(up, proxyauth="any", mode="regular")
+ f = tflow.tflow()
+ assert not up.check(f)
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "test", "test"
+ )
+ assert up.check(f)
+
+ f.request.headers["Proxy-Authorization"] = "invalid"
+ assert not up.check(f)
+
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "test", "test", scheme="unknown"
+ )
+ assert not up.check(f)
+
+ ctx.configure(up, proxyauth="test:test")
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "test", "test"
+ )
+ assert up.check(f)
+ ctx.configure(up, proxyauth="test:foo")
+ assert not up.check(f)
+
ctx.configure(
up,
- proxyauth= "@" + tutils.test_data.path("mitmproxy/net/data/server.crt")
+ proxyauth="@" + tutils.test_data.path(
+ "mitmproxy/net/data/htpasswd"
+ )
)
- with pytest.raises(exceptions.OptionsError):
- ctx.configure(up, proxyauth="@nonexistent")
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "test", "test"
+ )
+ assert up.check(f)
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "test", "foo"
+ )
+ assert not up.check(f)
+
+ with mock.patch('ldap3.Server', return_value="ldap://fake_server:389 - cleartext"):
+ with mock.patch('ldap3.Connection', search="test"):
+ with mock.patch('ldap3.Connection.search', return_value="test"):
+ ctx.configure(
+ up,
+ proxyauth="ldap:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com"
+ )
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "test", "test"
+ )
+ assert up.check(f)
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "", ""
+ )
+ assert not up.check(f)
+
+ def test_authenticate(self):
+ up = proxyauth.ProxyAuth()
+ with taddons.context() as ctx:
+ ctx.configure(up, proxyauth="any", mode="regular")
+
+ f = tflow.tflow()
+ assert not f.response
+ up.authenticate(f)
+ assert f.response.status_code == 407
+
+ f = tflow.tflow()
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "test", "test"
+ )
+ up.authenticate(f)
+ assert not f.response
+ assert not f.request.headers.get("Proxy-Authorization")
+
+ f = tflow.tflow()
+ ctx.configure(up, mode="reverse")
+ assert not f.response
+ up.authenticate(f)
+ assert f.response.status_code == 401
+
+ f = tflow.tflow()
+ f.request.headers["Authorization"] = proxyauth.mkauth(
+ "test", "test"
+ )
+ up.authenticate(f)
+ assert not f.response
+ assert not f.request.headers.get("Authorization")
+
+ def test_configure(self):
+ up = proxyauth.ProxyAuth()
+ with taddons.context() as ctx:
+ with pytest.raises(exceptions.OptionsError):
+ ctx.configure(up, proxyauth="foo")
+
+ ctx.configure(up, proxyauth="foo:bar")
+ assert up.singleuser == ["foo", "bar"]
+
+ ctx.configure(up, proxyauth=None)
+ assert up.singleuser is None
+
+ ctx.configure(up, proxyauth="any")
+ assert up.nonanonymous
+ ctx.configure(up, proxyauth=None)
+ assert not up.nonanonymous
+
+ with mock.patch('ldap3.Server', return_value="ldap://fake_server:389 - cleartext"):
+ with mock.patch('ldap3.Connection', return_value="test"):
+ ctx.configure(up, proxyauth="ldap:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com")
+ assert up.ldapserver
+ ctx.configure(up, proxyauth="ldaps:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com")
+ assert up.ldapserver
+
+ with pytest.raises(exceptions.OptionsError):
+ ctx.configure(up, proxyauth="ldap:test:test:test")
+
+ with pytest.raises(IndexError):
+ ctx.configure(up, proxyauth="ldap:fake_serveruid=?dc=example,dc=com:person")
+
+ with pytest.raises(exceptions.OptionsError):
+ ctx.configure(up, proxyauth="ldapssssssss:fake_server:dn:password:tree")
+
+ with pytest.raises(exceptions.OptionsError):
+ ctx.configure(
+ up,
+ proxyauth= "@" + tutils.test_data.path("mitmproxy/net/data/server.crt")
+ )
+ with pytest.raises(exceptions.OptionsError):
+ ctx.configure(up, proxyauth="@nonexistent")
- ctx.configure(
- up,
- proxyauth= "@" + tutils.test_data.path(
- "mitmproxy/net/data/htpasswd"
+ ctx.configure(
+ up,
+ proxyauth= "@" + tutils.test_data.path(
+ "mitmproxy/net/data/htpasswd"
+ )
)
- )
- assert up.htpasswd
- assert up.htpasswd.check_password("test", "test")
- assert not up.htpasswd.check_password("test", "foo")
- ctx.configure(up, proxyauth=None)
- assert not up.htpasswd
-
- with pytest.raises(exceptions.OptionsError):
- ctx.configure(up, proxyauth="any", mode="transparent")
- with pytest.raises(exceptions.OptionsError):
- ctx.configure(up, proxyauth="any", mode="socks5")
-
-
-def test_check(monkeypatch):
- up = proxyauth.ProxyAuth()
- with taddons.context() as ctx:
- ctx.configure(up, proxyauth="any", mode="regular")
- f = tflow.tflow()
- assert not up.check(f)
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "test", "test"
- )
- assert up.check(f)
-
- f.request.headers["Proxy-Authorization"] = "invalid"
- assert not up.check(f)
-
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "test", "test", scheme="unknown"
- )
- assert not up.check(f)
-
- ctx.configure(up, proxyauth="test:test")
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "test", "test"
- )
- assert up.check(f)
- ctx.configure(up, proxyauth="test:foo")
- assert not up.check(f)
-
- ctx.configure(
- up,
- proxyauth="@" + tutils.test_data.path(
- "mitmproxy/net/data/htpasswd"
+ assert up.htpasswd
+ assert up.htpasswd.check_password("test", "test")
+ assert not up.htpasswd.check_password("test", "foo")
+ ctx.configure(up, proxyauth=None)
+ assert not up.htpasswd
+
+ with pytest.raises(exceptions.OptionsError):
+ ctx.configure(up, proxyauth="any", mode="transparent")
+ with pytest.raises(exceptions.OptionsError):
+ ctx.configure(up, proxyauth="any", mode="socks5")
+
+ def test_handlers(self):
+ up = proxyauth.ProxyAuth()
+ with taddons.context() as ctx:
+ ctx.configure(up, proxyauth="any", mode="regular")
+
+ f = tflow.tflow()
+ assert not f.response
+ up.requestheaders(f)
+ assert f.response.status_code == 407
+
+ f = tflow.tflow()
+ f.request.method = "CONNECT"
+ assert not f.response
+ up.http_connect(f)
+ assert f.response.status_code == 407
+
+ f = tflow.tflow()
+ f.request.method = "CONNECT"
+ f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
+ "test", "test"
)
- )
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "test", "test"
- )
- assert up.check(f)
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "test", "foo"
- )
- assert not up.check(f)
-
- with mock.patch('ldap3.Server', return_value="ldap://fake_server:389 - cleartext"):
- with mock.patch('ldap3.Connection', search="test"):
- with mock.patch('ldap3.Connection.search', return_value="test"):
- ctx.configure(
- up,
- proxyauth="ldap:localhost:cn=default,dc=cdhdt,dc=com:password:ou=application,dc=cdhdt,dc=com"
- )
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "test", "test"
- )
- assert up.check(f)
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "", ""
- )
- assert not up.check(f)
-
-
-def test_authenticate():
- up = proxyauth.ProxyAuth()
- with taddons.context() as ctx:
- ctx.configure(up, proxyauth="any", mode="regular")
-
- f = tflow.tflow()
- assert not f.response
- up.authenticate(f)
- assert f.response.status_code == 407
-
- f = tflow.tflow()
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "test", "test"
- )
- up.authenticate(f)
- assert not f.response
- assert not f.request.headers.get("Proxy-Authorization")
-
- f = tflow.tflow()
- ctx.configure(up, mode="reverse")
- assert not f.response
- up.authenticate(f)
- assert f.response.status_code == 401
-
- f = tflow.tflow()
- f.request.headers["Authorization"] = proxyauth.mkauth(
- "test", "test"
- )
- up.authenticate(f)
- assert not f.response
- assert not f.request.headers.get("Authorization")
-
-
-def test_handlers():
- up = proxyauth.ProxyAuth()
- with taddons.context() as ctx:
- ctx.configure(up, proxyauth="any", mode="regular")
-
- f = tflow.tflow()
- assert not f.response
- up.requestheaders(f)
- assert f.response.status_code == 407
-
- f = tflow.tflow()
- f.request.method = "CONNECT"
- assert not f.response
- up.http_connect(f)
- assert f.response.status_code == 407
-
- f = tflow.tflow()
- f.request.method = "CONNECT"
- f.request.headers["Proxy-Authorization"] = proxyauth.mkauth(
- "test", "test"
- )
- up.http_connect(f)
- assert not f.response
-
- f2 = tflow.tflow(client_conn=f.client_conn)
- up.requestheaders(f2)
- assert not f2.response
- assert f2.metadata["proxyauth"] == ('test', 'test')
+ up.http_connect(f)
+ assert not f.response
+
+ f2 = tflow.tflow(client_conn=f.client_conn)
+ up.requestheaders(f2)
+ assert not f2.response
+ assert f2.metadata["proxyauth"] == ('test', 'test')
diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py
index 64fd9505..aa7ca68e 100644
--- a/test/mitmproxy/addons/test_script.py
+++ b/test/mitmproxy/addons/test_script.py
@@ -1,15 +1,16 @@
-import traceback
-import sys
import os
+import sys
+import traceback
+from unittest import mock
+
import pytest
-from unittest import mock
-from mitmproxy.test import tflow
-from mitmproxy.test import tutils
-from mitmproxy.test import taddons
from mitmproxy import addonmanager
from mitmproxy import exceptions
from mitmproxy.addons import script
+from mitmproxy.test import taddons
+from mitmproxy.test import tflow
+from mitmproxy.test import tutils
def test_load_script():
@@ -216,6 +217,20 @@ class TestScriptLoader:
assert not tctx.options.scripts
assert not sl.addons
+ def test_load_err(self):
+ sc = script.ScriptLoader()
+ with taddons.context() as tctx:
+ tctx.configure(sc, scripts=[
+ tutils.test_data.path("mitmproxy/data/addonscripts/load_error.py")
+ ])
+ try:
+ tctx.invoke(sc, "tick")
+ except ValueError:
+ pass # this is expected and normally guarded.
+ # on the next tick we should not fail however.
+ tctx.invoke(sc, "tick")
+ assert len(tctx.master.addons) == 0
+
def test_order(self):
rec = tutils.test_data.path("mitmproxy/data/addonscripts/recorder")
sc = script.ScriptLoader()
diff --git a/test/mitmproxy/data/addonscripts/load_error.py b/test/mitmproxy/data/addonscripts/load_error.py
new file mode 100644
index 00000000..4c05e9ed
--- /dev/null
+++ b/test/mitmproxy/data/addonscripts/load_error.py
@@ -0,0 +1,2 @@
+def load(_):
+ raise ValueError()
diff --git a/test/mitmproxy/proxy/protocol/test_http2.py b/test/mitmproxy/proxy/protocol/test_http2.py
index 5e6fa701..4f161ef5 100644
--- a/test/mitmproxy/proxy/protocol/test_http2.py
+++ b/test/mitmproxy/proxy/protocol/test_http2.py
@@ -507,9 +507,6 @@ class TestBodySizeLimit(_Http2Test):
def test_body_size_limit(self):
self.options.body_size_limit = "20"
- # FIXME: This should not be required?
- self.options._processed["body_size_limit"] = 20
-
h2_conn = self.setup_connection()
self._send_request(
diff --git a/test/mitmproxy/proxy/protocol/test_tls.py b/test/mitmproxy/proxy/protocol/test_tls.py
index 980ba7bd..e17ee46f 100644
--- a/test/mitmproxy/proxy/protocol/test_tls.py
+++ b/test/mitmproxy/proxy/protocol/test_tls.py
@@ -23,5 +23,4 @@ class TestClientHello:
)
c = TlsClientHello(data)
assert c.sni == 'example.com'
- assert c.alpn_protocols[0].name == b'h2'
- assert c.alpn_protocols[1].name == b'http/1.1'
+ assert c.alpn_protocols == [b'h2', b'http/1.1']
diff --git a/test/mitmproxy/test_log.py b/test/mitmproxy/test_log.py
index cde679ed..349e3ac8 100644
--- a/test/mitmproxy/test_log.py
+++ b/test/mitmproxy/test_log.py
@@ -4,3 +4,8 @@ from mitmproxy import log
def test_logentry():
e = log.LogEntry("foo", "info")
assert repr(e) == "LogEntry(foo, info)"
+
+ f = log.LogEntry("foo", "warning")
+ assert e == e
+ assert e != f
+ assert e != 42
diff --git a/test/mitmproxy/test_optmanager.py b/test/mitmproxy/test_optmanager.py
index fe72e6bb..d9b93227 100644
--- a/test/mitmproxy/test_optmanager.py
+++ b/test/mitmproxy/test_optmanager.py
@@ -73,6 +73,11 @@ def test_required_int():
o.parse_setval("required_int", None)
+def test_deepcopy():
+ o = TD()
+ copy.deepcopy(o)
+
+
def test_options():
o = TO()
assert o.keys() == {"bool", "one", "two", "required_int"}
@@ -244,6 +249,7 @@ def test_serialize():
o2 = TD2()
optmanager.load(o2, data)
assert o2 == o
+ assert not o == 42
t = """
unknown: foo
diff --git a/test/mitmproxy/utils/test_arg_check.py b/test/mitmproxy/utils/test_arg_check.py
new file mode 100644
index 00000000..72913955
--- /dev/null
+++ b/test/mitmproxy/utils/test_arg_check.py
@@ -0,0 +1,36 @@
+import io
+import contextlib
+from unittest import mock
+
+import pytest
+
+from mitmproxy.utils import arg_check
+
+
+@pytest.mark.parametrize('arg, output', [
+ (["-T"], "-T is deprecated, please use --mode transparent instead"),
+ (["-U"], "-U is deprecated, please use --mode upstream:SPEC instead"),
+ (["--cadir"], "--cadir is deprecated.\n"
+ "Please use `--set cadir=value` instead.\n"
+ "To show all options and their default values use --options"),
+ (["--palette"], "--palette is deprecated.\n"
+ "Please use `--set console_palette=value` instead.\n"
+ "To show all options and their default values use --options"),
+ (["--wfile"], "--wfile is deprecated.\n"
+ "Please use `--save-stream-file` instead."),
+ (["--eventlog"], "--eventlog has been removed."),
+ (["--nonanonymous"], '--nonanonymous is deprecated.\n'
+ 'Please use `--proxyauth SPEC` instead.\n'
+ 'SPEC Format: "username:pass", "any" to accept any user/pass combination,\n'
+ '"@path" to use an Apache htpasswd file, or\n'
+ '"ldap[s]:url_server_ldap:dn_auth:password:dn_subtree" '
+ 'for LDAP authentication.')
+
+])
+def test_check_args(arg, output):
+ f = io.StringIO()
+ with contextlib.redirect_stdout(f):
+ with mock.patch('sys.argv') as m:
+ m.__getitem__.return_value = arg
+ arg_check.check()
+ assert f.getvalue().strip() == output
diff --git a/test/mitmproxy/utils/test_human.py b/test/mitmproxy/utils/test_human.py
index 76dc2f88..e8ffaad4 100644
--- a/test/mitmproxy/utils/test_human.py
+++ b/test/mitmproxy/utils/test_human.py
@@ -22,6 +22,7 @@ def test_parse_size():
human.parse_size("1f")
with pytest.raises(ValueError):
human.parse_size("ak")
+ assert human.parse_size(None) is None
def test_pretty_size():