diff options
author | Aldo Cortesi <aldo@corte.si> | 2016-07-17 12:53:14 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-17 12:53:14 +1200 |
commit | 08b3af98c27aa70a13662541211a22ed134497fb (patch) | |
tree | 96bac80dc4bd9a10e6d2e52e64e71549caefca37 /test | |
parent | b5bbeb0ff3d13abec23d3884cb1449bfdb1b2047 (diff) | |
parent | 4bbab72cb214d4fea0aeb921a7eb36741599ea3f (diff) | |
download | mitmproxy-08b3af98c27aa70a13662541211a22ed134497fb.tar.gz mitmproxy-08b3af98c27aa70a13662541211a22ed134497fb.tar.bz2 mitmproxy-08b3af98c27aa70a13662541211a22ed134497fb.zip |
Merge branch 'master' into dumper
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/test_filt.py | 191 | ||||
-rw-r--r-- | test/mitmproxy/test_flow.py | 16 | ||||
-rw-r--r-- | test/mitmproxy/tutils.py | 28 |
3 files changed, 229 insertions, 6 deletions
diff --git a/test/mitmproxy/test_filt.py b/test/mitmproxy/test_filt.py index 9fe36b2a..69f042bb 100644 --- a/test/mitmproxy/test_filt.py +++ b/test/mitmproxy/test_filt.py @@ -1,6 +1,8 @@ from six.moves import cStringIO as StringIO -from mitmproxy import filt from mock import patch + +from mitmproxy import filt + from . import tutils @@ -73,7 +75,7 @@ class TestParsing: self._dump(a) -class TestMatching: +class TestMatchingHTTPFlow: def req(self): return tutils.tflow() @@ -87,6 +89,11 @@ class TestMatching: def q(self, q, o): return filt.parse(q)(o) + def test_http(self): + s = self.req() + assert self.q("~http", s) + assert not self.q("~tcp", s) + def test_asset(self): s = self.resp() assert not self.q("~a", s) @@ -247,6 +254,186 @@ class TestMatching: assert not self.q("!~c 201 !~c 200", s) +class TestMatchingTCPFlow: + + def flow(self): + return tutils.ttcpflow() + + def err(self): + return tutils.ttcpflow(err=True) + + def q(self, q, o): + return filt.parse(q)(o) + + def test_tcp(self): + f = self.flow() + assert self.q("~tcp", f) + assert not self.q("~http", f) + + def test_ferr(self): + e = self.err() + assert self.q("~e", e) + + def test_body(self): + f = self.flow() + + # Messages sent by client or server + assert self.q("~b hello", f) + assert self.q("~b me", f) + assert not self.q("~b nonexistent", f) + + # Messages sent by client + assert self.q("~bq hello", f) + assert not self.q("~bq me", f) + assert not self.q("~bq nonexistent", f) + + # Messages sent by server + assert self.q("~bs me", f) + assert not self.q("~bs hello", f) + assert not self.q("~bs nonexistent", f) + + def test_src(self): + f = self.flow() + assert self.q("~src address", f) + assert not self.q("~src foobar", f) + assert self.q("~src :22", f) + assert not self.q("~src :99", f) + assert self.q("~src address:22", f) + + def test_dst(self): + f = self.flow() + f.server_conn = tutils.tserver_conn() + assert self.q("~dst address", f) + assert not self.q("~dst foobar", f) + assert self.q("~dst :22", f) + assert not self.q("~dst :99", f) + assert self.q("~dst address:22", f) + + def test_and(self): + f = self.flow() + f.server_conn = tutils.tserver_conn() + assert self.q("~b hello & ~b me", f) + assert not self.q("~src wrongaddress & ~b hello", f) + assert self.q("(~src :22 & ~dst :22) & ~b hello", f) + assert not self.q("(~src address:22 & ~dst :22) & ~b nonexistent", f) + assert not self.q("(~src address:22 & ~dst :99) & ~b hello", f) + + def test_or(self): + f = self.flow() + f.server_conn = tutils.tserver_conn() + assert self.q("~b hello | ~b me", f) + assert self.q("~src :22 | ~b me", f) + assert not self.q("~src :99 | ~dst :99", f) + assert self.q("(~src :22 | ~dst :22) | ~b me", f) + + def test_not(self): + f = self.flow() + assert not self.q("! ~src :22", f) + assert self.q("! ~src :99", f) + assert self.q("!~src :99 !~src :99", f) + assert not self.q("!~src :99 !~src :22", f) + + def test_request(self): + f = self.flow() + assert not self.q("~q", f) + + def test_response(self): + f = self.flow() + assert not self.q("~s", f) + + def test_headers(self): + f = self.flow() + assert not self.q("~h whatever", f) + + # Request headers + assert not self.q("~hq whatever", f) + + # Response headers + assert not self.q("~hs whatever", f) + + def test_content_type(self): + f = self.flow() + assert not self.q("~t whatever", f) + + # Request content-type + assert not self.q("~tq whatever", f) + + # Response content-type + assert not self.q("~ts whatever", f) + + def test_code(self): + f = self.flow() + assert not self.q("~c 200", f) + + def test_domain(self): + f = self.flow() + assert not self.q("~d whatever", f) + + def test_method(self): + f = self.flow() + assert not self.q("~m whatever", f) + + def test_url(self): + f = self.flow() + assert not self.q("~u whatever", f) + + +class TestMatchingDummyFlow: + + def flow(self): + return tutils.tdummyflow() + + def err(self): + return tutils.tdummyflow(err=True) + + def q(self, q, o): + return filt.parse(q)(o) + + def test_filters(self): + e = self.err() + f = self.flow() + f.server_conn = tutils.tserver_conn() + + assert not self.q("~a", f) + + assert not self.q("~b whatever", f) + assert not self.q("~bq whatever", f) + assert not self.q("~bs whatever", f) + + assert not self.q("~c 0", f) + + assert not self.q("~d whatever", f) + + assert self.q("~dst address", f) + assert not self.q("~dst nonexistent", f) + + assert self.q("~e", e) + assert not self.q("~e", f) + + assert not self.q("~http", f) + + assert not self.q("~h whatever", f) + assert not self.q("~hq whatever", f) + assert not self.q("~hs whatever", f) + + assert not self.q("~m whatever", f) + + assert not self.q("~s", f) + + assert self.q("~src address", f) + assert not self.q("~src nonexistent", f) + + assert not self.q("~tcp", f) + + assert not self.q("~t whatever", f) + assert not self.q("~tq whatever", f) + assert not self.q("~ts whatever", f) + + assert not self.q("~u whatever", f) + + assert not self.q("~q", f) + + @patch('traceback.extract_tb') def test_pyparsing_bug(extract_tb): """https://github.com/mitmproxy/mitmproxy/issues/1087""" diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index f73616d1..10163401 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -293,7 +293,7 @@ class TestServerPlaybackState: assert s._hash(r) == s._hash(r2) -class TestFlow(object): +class TestHTTPFlow(object): def test_copy(self): f = tutils.tflow(resp=True) @@ -443,6 +443,20 @@ class TestFlow(object): assert f.response.raw_content == b"abarb" +class TestTCPFlow: + + def test_match(self): + f = tutils.ttcpflow() + assert not f.match("~b nonexistent") + assert f.match(None) + assert not f.match("~b nonexistent") + + f = tutils.ttcpflow(err=True) + assert f.match("~e") + + tutils.raises(ValueError, f.match, "~") + + class TestState: def test_backup(self): diff --git a/test/mitmproxy/tutils.py b/test/mitmproxy/tutils.py index d0a09035..d743a9e6 100644 --- a/test/mitmproxy/tutils.py +++ b/test/mitmproxy/tutils.py @@ -4,18 +4,19 @@ import tempfile import argparse import sys -from mitmproxy.models.tcp import TCPMessage -from six.moves import cStringIO as StringIO from contextlib import contextmanager - from unittest.case import SkipTest +from six.moves import cStringIO as StringIO + import netlib.utils import netlib.tutils from mitmproxy import controller from mitmproxy.models import ( ClientConnection, ServerConnection, Error, HTTPRequest, HTTPResponse, HTTPFlow, TCPFlow ) +from mitmproxy.models.tcp import TCPMessage +from mitmproxy.models.flow import Flow def _skip_windows(*args): @@ -47,6 +48,27 @@ def skip_appveyor(fn): return fn +class DummyFlow(Flow): + """A flow that is neither HTTP nor TCP.""" + + def __init__(self, client_conn, server_conn, live=None): + super(DummyFlow, self).__init__("dummy", client_conn, server_conn, live) + + +def tdummyflow(client_conn=True, server_conn=True, err=None): + if client_conn is True: + client_conn = tclient_conn() + if server_conn is True: + server_conn = tserver_conn() + if err is True: + err = terr() + + f = DummyFlow(client_conn, server_conn) + f.error = err + f.reply = controller.DummyReply() + return f + + def ttcpflow(client_conn=True, server_conn=True, messages=True, err=None): if client_conn is True: client_conn = tclient_conn() |