From 297493801ded90ee5b9717115fb2fa25bac43d22 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 3 Dec 2016 09:00:55 +1300 Subject: [PATCH] Clean up dump tests - Remove tests that redundantly test addon funtionality that we've already tested more comprehensively elsewhere. - Extend to 100% coverage for tools/dump.py --- mitmproxy/tools/dump.py | 11 -- test/mitmproxy/test_dump.py | 163 ------------------------------ test/mitmproxy/test_tools_dump.py | 38 +++++++ 3 files changed, 38 insertions(+), 174 deletions(-) delete mode 100644 test/mitmproxy/test_dump.py create mode 100644 test/mitmproxy/test_tools_dump.py diff --git a/mitmproxy/tools/dump.py b/mitmproxy/tools/dump.py index 3cd94c30f..69258ae2d 100644 --- a/mitmproxy/tools/dump.py +++ b/mitmproxy/tools/dump.py @@ -3,7 +3,6 @@ from typing import Optional, IO from mitmproxy import controller from mitmproxy import exceptions from mitmproxy import addons -from mitmproxy import io from mitmproxy import options from mitmproxy import master from mitmproxy.addons import dumper, termlog @@ -62,16 +61,6 @@ class DumpMaster(master.Master): self.add_log("Flow file corrupted.", "error") raise DumpError(v) - def _readflow(self, paths): - """ - Utitility function that reads a list of flows - or raises a DumpError if that fails. - """ - try: - return io.read_flows_from_paths(paths) - except exceptions.FlowReadException as e: - raise DumpError(str(e)) - @controller.handler def log(self, e): if e.level == "error": diff --git a/test/mitmproxy/test_dump.py b/test/mitmproxy/test_dump.py deleted file mode 100644 index c6b15c845..000000000 --- a/test/mitmproxy/test_dump.py +++ /dev/null @@ -1,163 +0,0 @@ -from mitmproxy.test import tflow -import os -import io - -from mitmproxy.tools import dump -from mitmproxy import exceptions -from mitmproxy import proxy -from mitmproxy.test import tutils -from . import mastertest - - -class TestDumpMaster(mastertest.MasterTest): - def dummy_cycle(self, master, n, content): - mastertest.MasterTest.dummy_cycle(self, master, n, content) - return master.options.tfile.getvalue() - - def mkmaster(self, flt, **options): - if "verbosity" not in options: - options["verbosity"] = 0 - if "flow_detail" not in options: - options["flow_detail"] = 0 - o = dump.Options(filtstr=flt, tfile=io.StringIO(), **options) - return dump.DumpMaster(o, proxy.DummyServer()) - - def test_basic(self): - for i in (1, 2, 3): - assert "GET" in self.dummy_cycle( - self.mkmaster("~s", flow_detail=i), - 1, - b"" - ) - assert "GET" in self.dummy_cycle( - self.mkmaster("~s", flow_detail=i), - 1, - b"\x00\x00\x00" - ) - assert "GET" in self.dummy_cycle( - self.mkmaster("~s", flow_detail=i), - 1, - b"ascii" - ) - - def test_error(self): - o = dump.Options( - tfile=io.StringIO(), - flow_detail=1 - ) - m = dump.DumpMaster(o, proxy.DummyServer()) - f = tflow.tflow(err=True) - m.error(f) - assert "error" in o.tfile.getvalue() - - def test_replay(self): - o = dump.Options(http2=False, server_replay=["nonexistent"], replay_kill_extra=True) - tutils.raises(exceptions.OptionsError, dump.DumpMaster, o, proxy.DummyServer()) - - with tutils.tmpdir() as t: - p = os.path.join(t, "rep") - self.flowfile(p) - - o = dump.Options(http2=False, server_replay=[p], replay_kill_extra=True) - o.verbosity = 0 - o.flow_detail = 0 - m = dump.DumpMaster(o, proxy.DummyServer()) - - self.cycle(m, b"content") - self.cycle(m, b"content") - - o = dump.Options(http2=False, server_replay=[p], replay_kill_extra=False) - o.verbosity = 0 - o.flow_detail = 0 - m = dump.DumpMaster(o, proxy.DummyServer()) - self.cycle(m, b"nonexistent") - - o = dump.Options(http2=False, client_replay=[p], replay_kill_extra=False) - o.verbosity = 0 - o.flow_detail = 0 - m = dump.DumpMaster(o, proxy.DummyServer()) - - def test_read(self): - with tutils.tmpdir() as t: - p = os.path.join(t, "read") - self.flowfile(p) - assert "GET" in self.dummy_cycle( - self.mkmaster(None, flow_detail=1, rfile=p), - 1, b"", - ) - tutils.raises( - dump.DumpError, - self.mkmaster, None, verbosity=1, rfile="/nonexistent" - ) - tutils.raises( - dump.DumpError, - self.mkmaster, None, verbosity=1, rfile="test_dump.py" - ) - - def test_options(self): - o = dump.Options(verbosity = 2) - assert o.verbosity == 2 - - def test_filter(self): - assert "GET" not in self.dummy_cycle( - self.mkmaster("~u foo", verbosity=1), 1, b"" - ) - - def test_replacements(self): - o = dump.Options( - replacements=[(".*", "content", "foo")], - tfile = io.StringIO(), - ) - o.verbosity = 0 - o.flow_detail = 0 - m = dump.DumpMaster(o, proxy.DummyServer()) - f = self.cycle(m, b"content") - assert f.request.content == b"foo" - - def test_setheader(self): - o = dump.Options( - setheaders=[(".*", "one", "two")], - tfile=io.StringIO() - ) - o.verbosity = 0 - o.flow_detail = 0 - m = dump.DumpMaster(o, proxy.DummyServer()) - f = self.cycle(m, b"content") - assert f.request.headers["one"] == "two" - - def test_script(self): - ret = self.dummy_cycle( - self.mkmaster( - None, - scripts=[tutils.test_data.path("mitmproxy/data/scripts/all.py")], - verbosity=2 - ), - 1, b"", - ) - assert "XCLIENTCONNECT" in ret - assert "XSERVERCONNECT" in ret - assert "XREQUEST" in ret - assert "XRESPONSE" in ret - assert "XCLIENTDISCONNECT" in ret - tutils.raises( - exceptions.AddonError, - self.mkmaster, - None, scripts=["nonexistent"] - ) - tutils.raises( - exceptions.AddonError, - self.mkmaster, - None, scripts=["starterr.py"] - ) - - def test_stickycookie(self): - self.dummy_cycle( - self.mkmaster(None, stickycookie = ".*"), - 1, b"" - ) - - def test_stickyauth(self): - self.dummy_cycle( - self.mkmaster(None, stickyauth = ".*"), - 1, b"" - ) diff --git a/test/mitmproxy/test_tools_dump.py b/test/mitmproxy/test_tools_dump.py new file mode 100644 index 000000000..1488f33b6 --- /dev/null +++ b/test/mitmproxy/test_tools_dump.py @@ -0,0 +1,38 @@ +import os + +from mitmproxy.tools import dump +from mitmproxy import proxy +from mitmproxy.test import tutils +from mitmproxy import log +from mitmproxy import controller +from . import mastertest + + +class TestDumpMaster(mastertest.MasterTest): + def mkmaster(self, flt, **options): + o = dump.Options(filtstr=flt, verbosity=-1, flow_detail=0, **options) + return dump.DumpMaster(o, proxy.DummyServer()) + + def test_read(self): + with tutils.tmpdir() as t: + p = os.path.join(t, "read") + self.flowfile(p) + self.dummy_cycle( + self.mkmaster(None, rfile=p), + 1, b"", + ) + tutils.raises( + dump.DumpError, + self.mkmaster, None, rfile="/nonexistent" + ) + tutils.raises( + dump.DumpError, + self.mkmaster, None, rfile="test_dump.py" + ) + + def test_has_error(self): + m = self.mkmaster(None) + ent = log.LogEntry("foo", "error") + ent.reply = controller.DummyReply() + m.log(ent) + assert m.has_errored