diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/builtins/test_anticache.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/builtins/test_anticomp.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/builtins/test_dumper.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/builtins/test_filestreamer.py | 6 | ||||
-rw-r--r-- | test/mitmproxy/builtins/test_replace.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/builtins/test_script.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/builtins/test_setheaders.py | 8 | ||||
-rw-r--r-- | test/mitmproxy/builtins/test_stickyauth.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/builtins/test_stickycookie.py | 18 | ||||
-rw-r--r-- | test/mitmproxy/mastertest.py | 21 | ||||
-rw-r--r-- | test/mitmproxy/script/test_concurrent.py | 6 | ||||
-rw-r--r-- | test/mitmproxy/test_controller.py | 134 | ||||
-rw-r--r-- | test/mitmproxy/test_examples.py | 22 | ||||
-rw-r--r-- | test/mitmproxy/test_flow.py | 29 |
14 files changed, 179 insertions, 87 deletions
diff --git a/test/mitmproxy/builtins/test_anticache.py b/test/mitmproxy/builtins/test_anticache.py index ac321e26..8897de52 100644 --- a/test/mitmproxy/builtins/test_anticache.py +++ b/test/mitmproxy/builtins/test_anticache.py @@ -14,11 +14,11 @@ class TestAntiCache(mastertest.MasterTest): m.addons.add(o, sa) f = tutils.tflow(resp=True) - self.invoke(m, "request", f) + m.request(f) f = tutils.tflow(resp=True) f.request.headers["if-modified-since"] = "test" f.request.headers["if-none-match"] = "test" - self.invoke(m, "request", f) + m.request(f) assert "if-modified-since" not in f.request.headers assert "if-none-match" not in f.request.headers diff --git a/test/mitmproxy/builtins/test_anticomp.py b/test/mitmproxy/builtins/test_anticomp.py index a5f5a270..af9e4a6a 100644 --- a/test/mitmproxy/builtins/test_anticomp.py +++ b/test/mitmproxy/builtins/test_anticomp.py @@ -14,10 +14,10 @@ class TestAntiComp(mastertest.MasterTest): m.addons.add(o, sa) f = tutils.tflow(resp=True) - self.invoke(m, "request", f) + m.request(f) f = tutils.tflow(resp=True) f.request.headers["Accept-Encoding"] = "foobar" - self.invoke(m, "request", f) + m.request(f) assert f.request.headers["Accept-Encoding"] == "identity" diff --git a/test/mitmproxy/builtins/test_dumper.py b/test/mitmproxy/builtins/test_dumper.py index 1c7173e0..9b518b53 100644 --- a/test/mitmproxy/builtins/test_dumper.py +++ b/test/mitmproxy/builtins/test_dumper.py @@ -80,5 +80,5 @@ class TestContentView(mastertest.MasterTest): m = mastertest.RecordingMaster(o, None, s) d = dumper.Dumper() m.addons.add(o, d) - self.invoke(m, "response", tutils.tflow()) + m.response(tutils.tflow()) assert "Content viewer failed" in m.event_log[0][1] diff --git a/test/mitmproxy/builtins/test_filestreamer.py b/test/mitmproxy/builtins/test_filestreamer.py index 0e69b340..94d68813 100644 --- a/test/mitmproxy/builtins/test_filestreamer.py +++ b/test/mitmproxy/builtins/test_filestreamer.py @@ -28,8 +28,8 @@ class TestStream(mastertest.MasterTest): m.addons.add(o, sa) f = tutils.tflow(resp=True) - self.invoke(m, "request", f) - self.invoke(m, "response", f) + m.request(f) + m.response(f) m.addons.remove(sa) assert r()[0].response @@ -38,6 +38,6 @@ class TestStream(mastertest.MasterTest): m.addons.add(o, sa) f = tutils.tflow() - self.invoke(m, "request", f) + m.request(f) m.addons.remove(sa) assert not r()[1].response diff --git a/test/mitmproxy/builtins/test_replace.py b/test/mitmproxy/builtins/test_replace.py index 5e70ce56..07abcda4 100644 --- a/test/mitmproxy/builtins/test_replace.py +++ b/test/mitmproxy/builtins/test_replace.py @@ -43,10 +43,10 @@ class TestReplace(mastertest.MasterTest): f = tutils.tflow() f.request.content = b"foo" - self.invoke(m, "request", f) + m.request(f) assert f.request.content == b"bar" f = tutils.tflow(resp=True) f.response.content = b"foo" - self.invoke(m, "response", f) + m.response(f) assert f.response.content == b"bar" diff --git a/test/mitmproxy/builtins/test_script.py b/test/mitmproxy/builtins/test_script.py index 2870fd17..0bac6ca0 100644 --- a/test/mitmproxy/builtins/test_script.py +++ b/test/mitmproxy/builtins/test_script.py @@ -69,7 +69,7 @@ class TestScript(mastertest.MasterTest): sc.ns.call_log = [] f = tutils.tflow(resp=True) - self.invoke(m, "request", f) + m.request(f) recf = sc.ns.call_log[0] assert recf[1] == "request" @@ -102,7 +102,7 @@ class TestScript(mastertest.MasterTest): ) m.addons.add(o, sc) f = tutils.tflow(resp=True) - self.invoke(m, "request", f) + m.request(f) assert m.event_log[0][0] == "error" def test_duplicate_flow(self): diff --git a/test/mitmproxy/builtins/test_setheaders.py b/test/mitmproxy/builtins/test_setheaders.py index 41c18360..63685177 100644 --- a/test/mitmproxy/builtins/test_setheaders.py +++ b/test/mitmproxy/builtins/test_setheaders.py @@ -33,12 +33,12 @@ class TestSetHeaders(mastertest.MasterTest): ) f = tutils.tflow() f.request.headers["one"] = "xxx" - self.invoke(m, "request", f) + m.request(f) assert f.request.headers["one"] == "two" f = tutils.tflow(resp=True) f.response.headers["one"] = "xxx" - self.invoke(m, "response", f) + m.response(f) assert f.response.headers["one"] == "three" m, sh = self.mkmaster( @@ -50,7 +50,7 @@ class TestSetHeaders(mastertest.MasterTest): f = tutils.tflow(resp=True) f.request.headers["one"] = "xxx" f.response.headers["one"] = "xxx" - self.invoke(m, "response", f) + m.response(f) assert f.response.headers.get_all("one") == ["two", "three"] m, sh = self.mkmaster( @@ -61,5 +61,5 @@ class TestSetHeaders(mastertest.MasterTest): ) f = tutils.tflow() f.request.headers["one"] = "xxx" - self.invoke(m, "request", f) + m.request(f) assert f.request.headers.get_all("one") == ["two", "three"] diff --git a/test/mitmproxy/builtins/test_stickyauth.py b/test/mitmproxy/builtins/test_stickyauth.py index 5757fb2d..00b12072 100644 --- a/test/mitmproxy/builtins/test_stickyauth.py +++ b/test/mitmproxy/builtins/test_stickyauth.py @@ -15,10 +15,10 @@ class TestStickyAuth(mastertest.MasterTest): f = tutils.tflow(resp=True) f.request.headers["authorization"] = "foo" - self.invoke(m, "request", f) + m.request(f) assert "address" in sa.hosts f = tutils.tflow(resp=True) - self.invoke(m, "request", f) + m.request(f) assert f.request.headers["authorization"] == "foo" diff --git a/test/mitmproxy/builtins/test_stickycookie.py b/test/mitmproxy/builtins/test_stickycookie.py index e9d92c83..13cea6a2 100644 --- a/test/mitmproxy/builtins/test_stickycookie.py +++ b/test/mitmproxy/builtins/test_stickycookie.py @@ -34,23 +34,23 @@ class TestStickyCookie(mastertest.MasterTest): f = tutils.tflow(resp=True) f.response.headers["set-cookie"] = "foo=bar" - self.invoke(m, "request", f) + m.request(f) f.reply.acked = False - self.invoke(m, "response", f) + m.response(f) assert sc.jar assert "cookie" not in f.request.headers f = f.copy() f.reply.acked = False - self.invoke(m, "request", f) + m.request(f) assert f.request.headers["cookie"] == "foo=bar" def _response(self, s, m, sc, cookie, host): f = tutils.tflow(req=ntutils.treq(host=host, port=80), resp=True) f.response.headers["Set-Cookie"] = cookie - self.invoke(m, "response", f) + m.response(f) return f def test_response(self): @@ -79,7 +79,7 @@ class TestStickyCookie(mastertest.MasterTest): c2 = "othercookie=helloworld; Path=/" f = self._response(s, m, sc, c1, "www.google.com") f.response.headers["Set-Cookie"] = c2 - self.invoke(m, "response", f) + m.response(f) googlekey = list(sc.jar.keys())[0] assert len(sc.jar[googlekey].keys()) == 2 @@ -96,7 +96,7 @@ class TestStickyCookie(mastertest.MasterTest): ] for c in cs: f.response.headers["Set-Cookie"] = c - self.invoke(m, "response", f) + m.response(f) googlekey = list(sc.jar.keys())[0] assert len(sc.jar[googlekey].keys()) == len(cs) @@ -108,7 +108,7 @@ class TestStickyCookie(mastertest.MasterTest): c2 = "somecookie=newvalue; Path=/" f = self._response(s, m, sc, c1, "www.google.com") f.response.headers["Set-Cookie"] = c2 - self.invoke(m, "response", f) + m.response(f) googlekey = list(sc.jar.keys())[0] assert len(sc.jar[googlekey].keys()) == 1 assert list(sc.jar[googlekey]["somecookie"].items())[0][1] == "newvalue" @@ -120,7 +120,7 @@ class TestStickyCookie(mastertest.MasterTest): # by setting the expire time in the past f = self._response(s, m, sc, "duffer=zafar; Path=/", "www.google.com") f.response.headers["Set-Cookie"] = "duffer=; Expires=Thu, 01-Jan-1970 00:00:00 GMT" - self.invoke(m, "response", f) + m.response(f) assert not sc.jar.keys() def test_request(self): @@ -128,5 +128,5 @@ class TestStickyCookie(mastertest.MasterTest): f = self._response(s, m, sc, "SSID=mooo", "www.google.com") assert "cookie" not in f.request.headers - self.invoke(m, "request", f) + m.request(f) assert "cookie" in f.request.headers diff --git a/test/mitmproxy/mastertest.py b/test/mitmproxy/mastertest.py index dcc0dc48..08659d19 100644 --- a/test/mitmproxy/mastertest.py +++ b/test/mitmproxy/mastertest.py @@ -1,5 +1,3 @@ -import mock - from . import tutils import netlib.tutils @@ -8,26 +6,19 @@ from mitmproxy import flow, proxy, models, controller class MasterTest: - def invoke(self, master, handler, *message): - with master.handlecontext(): - func = getattr(master, handler) - func(*message) - if message: - message[0].reply = controller.DummyReply() def cycle(self, master, content): f = tutils.tflow(req=netlib.tutils.treq(content=content)) l = proxy.Log("connect") - l.reply = mock.MagicMock() + l.reply = controller.DummyReply() master.log(l) - self.invoke(master, "clientconnect", f.client_conn) - self.invoke(master, "clientconnect", f.client_conn) - self.invoke(master, "serverconnect", f.server_conn) - self.invoke(master, "request", f) + master.clientconnect(f.client_conn) + master.serverconnect(f.server_conn) + master.request(f) if not f.error: f.response = models.HTTPResponse.wrap(netlib.tutils.tresp(content=content)) - self.invoke(master, "response", f) - self.invoke(master, "clientdisconnect", f) + master.response(f) + master.clientdisconnect(f) return f def dummy_cycle(self, master, n, content): diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py index a5f76994..c4f1e9ae 100644 --- a/test/mitmproxy/script/test_concurrent.py +++ b/test/mitmproxy/script/test_concurrent.py @@ -25,11 +25,11 @@ class TestConcurrent(mastertest.MasterTest): ) m.addons.add(m.options, sc) f1, f2 = tutils.tflow(), tutils.tflow() - self.invoke(m, "request", f1) - self.invoke(m, "request", f2) + m.request(f1) + m.request(f2) start = time.time() while time.time() - start < 5: - if f1.reply.acked and f2.reply.acked: + if f1.reply.state == f2.reply.state == "committed": return raise ValueError("Script never acked") diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index 6d4b8fe6..abf66b6c 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -1,3 +1,4 @@ +from test.mitmproxy import tutils from threading import Thread, Event from mock import Mock @@ -5,7 +6,7 @@ from mock import Mock from mitmproxy import controller from six.moves import queue -from mitmproxy.exceptions import Kill +from mitmproxy.exceptions import Kill, ControlException from mitmproxy.proxy import DummyServer from netlib.tutils import raises @@ -55,7 +56,7 @@ class TestChannel(object): def test_tell(self): q = queue.Queue() channel = controller.Channel(q, Event()) - m = Mock() + m = Mock(name="test_tell") channel.tell("test", m) assert q.get() == ("test", m) assert m.reply @@ -66,12 +67,15 @@ class TestChannel(object): def reply(): m, obj = q.get() assert m == "test" + obj.reply.handle() obj.reply.send(42) + obj.reply.take() + obj.reply.commit() Thread(target=reply).start() channel = controller.Channel(q, Event()) - assert channel.ask("test", Mock()) == 42 + assert channel.ask("test", Mock(name="test_ask_simple")) == 42 def test_ask_shutdown(self): q = queue.Queue() @@ -79,31 +83,125 @@ class TestChannel(object): done.set() channel = controller.Channel(q, done) with raises(Kill): - channel.ask("test", Mock()) - - -class TestDummyReply(object): - def test_simple(self): - reply = controller.DummyReply() - assert not reply.acked - reply.ack() - assert reply.acked + channel.ask("test", Mock(name="test_ask_shutdown")) class TestReply(object): def test_simple(self): reply = controller.Reply(42) - assert not reply.acked + assert reply.state == "unhandled" + + reply.handle() + assert reply.state == "handled" + reply.send("foo") - assert reply.acked + assert reply.value == "foo" + + reply.take() + assert reply.state == "taken" + + with tutils.raises(queue.Empty): + reply.q.get_nowait() + reply.commit() + assert reply.state == "committed" assert reply.q.get() == "foo" - def test_default(self): - reply = controller.Reply(42) + def test_kill(self): + reply = controller.Reply(43) + reply.handle() + reply.kill() + reply.take() + reply.commit() + assert reply.q.get() == Kill + + def test_ack(self): + reply = controller.Reply(44) + reply.handle() reply.ack() - assert reply.q.get() == 42 + reply.take() + reply.commit() + assert reply.q.get() == 44 def test_reply_none(self): - reply = controller.Reply(42) + reply = controller.Reply(45) + reply.handle() reply.send(None) + reply.take() + reply.commit() assert reply.q.get() is None + + def test_commit_no_reply(self): + reply = controller.Reply(46) + reply.handle() + reply.take() + with tutils.raises(ControlException): + reply.commit() + reply.ack() + reply.commit() + + def test_double_send(self): + reply = controller.Reply(47) + reply.handle() + reply.send(1) + with tutils.raises(ControlException): + reply.send(2) + reply.take() + reply.commit() + + def test_state_transitions(self): + states = {"unhandled", "handled", "taken", "committed"} + accept = { + "handle": {"unhandled"}, + "take": {"handled"}, + "commit": {"taken"}, + "ack": {"handled", "taken"}, + } + for fn, ok in accept.items(): + for state in states: + r = controller.Reply(48) + r._state = state + if fn == "commit": + r.value = 49 + if state in ok: + getattr(r, fn)() + else: + with tutils.raises(ControlException): + getattr(r, fn)() + r._state = "committed" # hide warnings on deletion + + def test_del(self): + reply = controller.Reply(47) + with tutils.raises(ControlException): + reply.__del__() + reply.handle() + reply.ack() + reply.take() + reply.commit() + + +class TestDummyReply(object): + def test_simple(self): + reply = controller.DummyReply() + for _ in range(2): + reply.handle() + reply.ack() + reply.take() + reply.commit() + reply.mark_reset() + reply.reset() + assert reply.state == "unhandled" + + def test_reset(self): + reply = controller.DummyReply() + reply.handle() + reply.ack() + reply.take() + reply.commit() + reply.mark_reset() + assert reply.state == "committed" + reply.reset() + assert reply.state == "unhandled" + + def test_del(self): + reply = controller.DummyReply() + reply.__del__() diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 34fcc261..6c24ace5 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -39,7 +39,7 @@ class TestScripts(mastertest.MasterTest): def test_add_header(self): m, _ = tscript("add_header.py") f = tutils.tflow(resp=netutils.tresp()) - self.invoke(m, "response", f) + m.response(f) assert f.response.headers["newheader"] == "foo" def test_custom_contentviews(self): @@ -54,9 +54,9 @@ class TestScripts(mastertest.MasterTest): tscript("iframe_injector.py") m, sc = tscript("iframe_injector.py", "http://example.org/evil_iframe") - flow = tutils.tflow(resp=netutils.tresp(content=b"<html>mitmproxy</html>")) - self.invoke(m, "response", flow) - content = flow.response.content + f = tutils.tflow(resp=netutils.tresp(content=b"<html>mitmproxy</html>")) + m.response(f) + content = f.response.content assert b'iframe' in content and b'evil_iframe' in content def test_modify_form(self): @@ -64,23 +64,23 @@ class TestScripts(mastertest.MasterTest): form_header = Headers(content_type="application/x-www-form-urlencoded") f = tutils.tflow(req=netutils.treq(headers=form_header)) - self.invoke(m, "request", f) + m.request(f) assert f.request.urlencoded_form[b"mitmproxy"] == b"rocks" f.request.headers["content-type"] = "" - self.invoke(m, "request", f) + m.request(f) assert list(f.request.urlencoded_form.items()) == [(b"foo", b"bar")] def test_modify_querystring(self): m, sc = tscript("modify_querystring.py") f = tutils.tflow(req=netutils.treq(path="/search?q=term")) - self.invoke(m, "request", f) + m.request(f) assert f.request.query["mitmproxy"] == "rocks" f.request.path = "/" - self.invoke(m, "request", f) + m.request(f) assert f.request.query["mitmproxy"] == "rocks" def test_modify_response_body(self): @@ -89,13 +89,13 @@ class TestScripts(mastertest.MasterTest): m, sc = tscript("modify_response_body.py", "mitmproxy rocks") f = tutils.tflow(resp=netutils.tresp(content=b"I <3 mitmproxy")) - self.invoke(m, "response", f) + m.response(f) assert f.response.content == b"I <3 rocks" def test_redirect_requests(self): m, sc = tscript("redirect_requests.py") f = tutils.tflow(req=netutils.treq(host="example.org")) - self.invoke(m, "request", f) + m.request(f) assert f.request.host == "mitmproxy.org" def test_har_extractor(self): @@ -119,7 +119,7 @@ class TestScripts(mastertest.MasterTest): req=netutils.treq(**times), resp=netutils.tresp(**times) ) - self.invoke(m, "response", f) + m.response(f) m.addons.remove(sc) with open(path, "rb") as f: diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index d4bf764c..1caeb100 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -3,9 +3,9 @@ import io import netlib.utils from netlib.http import Headers -from mitmproxy import filt, controller, flow, options +from mitmproxy import filt, flow, options from mitmproxy.contrib import tnetstring -from mitmproxy.exceptions import FlowReadException +from mitmproxy.exceptions import FlowReadException, Kill from mitmproxy.models import Error from mitmproxy.models import Flow from mitmproxy.models import HTTPFlow @@ -372,19 +372,23 @@ class TestHTTPFlow(object): assert f.get_state() == f2.get_state() def test_kill(self): - s = flow.State() - fm = flow.FlowMaster(None, None, s) + fm = mock.Mock() f = tutils.tflow() - f.intercept(mock.Mock()) + f.reply.handle() + f.intercept(fm) + assert fm.handle_intercept.called + assert f.killable f.kill(fm) - for i in s.view: - assert "killed" in str(i.error) + assert not f.killable + assert fm.error.called + assert f.reply.value == Kill def test_killall(self): s = flow.State() fm = flow.FlowMaster(None, None, s) f = tutils.tflow() + f.reply.handle() f.intercept(fm) s.killall(fm) @@ -393,11 +397,11 @@ class TestHTTPFlow(object): def test_accept_intercept(self): f = tutils.tflow() - + f.reply.handle() f.intercept(mock.Mock()) - assert not f.reply.acked + assert f.reply.state == "taken" f.accept_intercept(mock.Mock()) - assert f.reply.acked + assert f.reply.state == "committed" def test_replace_unicode(self): f = tutils.tflow(resp=True) @@ -735,7 +739,6 @@ class TestFlowMaster: fm.clientdisconnect(f.client_conn) f.error = Error("msg") - f.error.reply = controller.DummyReply() fm.error(f) fm.shutdown() @@ -834,8 +837,8 @@ class TestFlowMaster: f = tutils.tflow() f.request.host = "nonexistent" - fm.process_new_request(f) - assert "killed" in f.error.msg + fm.request(f) + assert f.reply.value == Kill class TestRequest: |