def tscript(cmd, args=""): o = options.Options() cmd = example_dir.path(cmd) + " " + args m = RaiseMaster(o, proxy.DummyServer()) sc = script.Script(cmd) m.addons.add(sc) return m, sc
def test_concurrent_err(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path( "mitmproxy/data/addonscripts/concurrent_decorator_err.py")) sc.start() assert "decorator not supported" in tctx.master.event_log[0][1]
def test_stream_modify(self): s = script.Script( tutils.test_data.path( "mitmproxy/data/addonscripts/stream_modify.py")) self.set_addons(s) d = self.pathod('200:b"foo"') assert d.content == b"bar"
def test_concurrent_err(self): m = mastertest.RecordingMaster(options.Options(), proxy.DummyServer()) sc = script.Script( tutils.test_data.path( "data/addonscripts/concurrent_decorator_err.py")) with m.handlecontext(): sc.start() assert "decorator not supported" in m.event_log[0][1]
def test_concurrent_err(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path( "mitmproxy/data/addonscripts/concurrent_decorator_err.py")) l = addonmanager.Loader(tctx.master) sc.load(l) assert tctx.master.has_log("decorator not supported")
def test_addon(self): o = options.Options() m = master.Master(o, proxy.DummyServer()) sc = script.Script(tutils.test_data.path("data/addonscripts/addon.py")) m.addons.add(sc) assert sc.ns.event_log == [ 'scriptstart', 'addonstart', 'addonconfigure' ]
async def test_addon(self, tdata): with taddons.context() as tctx: sc = script.Script( tdata.path("mitmproxy/data/addonscripts/addon.py"), True) tctx.master.addons.add(sc) await tctx.master.await_log("addon running") assert sc.ns.event_log == [ 'scriptload', 'addonload', 'scriptconfigure', 'addonconfigure' ]
def test_quotes_around_filename(self, tdata): """ Test that a script specified as '"foo.py"' works to support the calling convention of mitmproxy 2.0, as e.g. used by Cuckoo Sandbox. """ path = tdata.path("mitmproxy/data/addonscripts/recorder/recorder.py") s = script.Script(f'"{path}"', False) assert '"' not in s.fullpath
def test_addon(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path("mitmproxy/data/addonscripts/addon.py")) sc.start() tctx.configure(sc) assert sc.ns.event_log == [ 'scriptstart', 'addonstart', 'addonconfigure' ]
async def test_optionexceptions(self, tdata): with taddons.context() as tctx: sc = script.Script( tdata.path("mitmproxy/data/addonscripts/configure.py"), True, ) tctx.master.addons.add(sc) tctx.configure(sc) await tctx.master.await_log("Options Error")
def test_tcp_stream_modify(self): s = script.Script( tutils.test_data.path("mitmproxy/data/addonscripts/tcp_stream_modify.py") ) self.master.addons.add(s) self._tcpproxy_on() d = self.pathod('200:b"foo"') self._tcpproxy_off() assert d.content == b"bar" self.master.addons.remove(s)
async def test_stream_modify(self, tdata): s = script.Script( tdata.path("mitmproxy/data/addonscripts/stream_modify.py"), False, ) self.set_addons(s) await self.master.await_log("stream_modify running") d = self.pathod('200:b"foo"') assert d.content == b"bar"
def test_tcp_stream_modify(self, tdata): s = script.Script( tdata.path("mitmproxy/data/addonscripts/tcp_stream_modify.py"), False, ) self.set_addons(s) self._tcpproxy_on() d = self.pathod('200:b"foo"') self._tcpproxy_off() assert d.content == b"bar"
def test_addon(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path("mitmproxy/data/addonscripts/addon.py")) tctx.master.addons.add(sc) tctx.configure(sc) sc.tick() assert sc.ns.event_log == [ 'scriptload', 'addonload', 'scriptconfigure', 'addonconfigure' ]
def test_addon(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path("mitmproxy/data/addonscripts/addon.py")) l = addonmanager.Loader(tctx.master) sc.load(l) tctx.configure(sc) assert sc.ns.event_log == [ 'scriptload', 'addonload', 'addonconfigure' ]
def script(self, path): """ Loads a script from path, and returns the enclosed addon. """ sc = script.Script(path) loader = addonmanager.Loader(self.master) self.master.addons.invoke_addon(sc, "load", loader) self.configure(sc) self.master.addons.invoke_addon(sc, "tick") return sc.addons[0] if sc.addons else None
def test_exception(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path("mitmproxy/data/addonscripts/error.py") ) sc.start() f = tflow.tflow(resp=True) sc.request(f) assert tctx.master.event_log[0][0] == "error" assert len(tctx.master.event_log[0][1].splitlines()) == 6 assert re.search(r'addonscripts[\\/]error.py", line \d+, in request', tctx.master.event_log[0][1]) assert re.search(r'addonscripts[\\/]error.py", line \d+, in mkerr', tctx.master.event_log[0][1]) assert tctx.master.event_log[0][1].endswith("ValueError: Error!\n")
def test_concurrent(self): m = master.Master(options.Options(), proxy.DummyServer()) sc = script.Script( tutils.test_data.path("data/addonscripts/concurrent_decorator.py")) m.addons.add(sc) f1, f2 = tutils.tflow(), tutils.tflow() m.request(f1) m.request(f2) start = time.time() while time.time() - start < 5: if f1.reply.state == f2.reply.state == "committed": return raise ValueError("Script never acked")
def test_reload(self, tmpdir): with taddons.context() as tctx: f = tmpdir.join("foo.py") f.ensure(file=True) f.write("\n") sc = script.Script(str(f)) tctx.configure(sc) sc.tick() for _ in range(3): sc.last_load, sc.last_mtime = 0, 0 sc.tick() time.sleep(0.1) tctx.master.has_log("Loading")
def test_exception(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path("mitmproxy/data/addonscripts/error.py")) tctx.master.addons.add(sc) tctx.configure(sc) sc.tick() f = tflow.tflow(resp=True) tctx.master.addons.trigger("request", f) assert tctx.master.has_log("ValueError: Error!") assert tctx.master.has_log("error.py")
def test_reload(self, tmpdir): with taddons.context() as tctx: f = tmpdir.join("foo.py") f.ensure(file=True) sc = script.Script(str(f)) tctx.configure(sc) for _ in range(100): f.write(".") sc.tick() time.sleep(0.1) if tctx.master.logs: return raise AssertionError("Change event not detected.")
def test_simple(self): with taddons.context(): sc = script.Script( tutils.test_data.path( "mitmproxy/data/addonscripts/recorder.py")) sc.load_script() assert sc.ns.call_log[0][0:2] == ("solo", "load") sc.ns.call_log = [] f = tflow.tflow(resp=True) sc.request(f) recf = sc.ns.call_log[0] assert recf[1] == "request"
async def test_reload(self, tmpdir): with taddons.context() as tctx: f = tmpdir.join("foo.py") f.ensure(file=True) f.write("\n") sc = script.Script(str(f)) tctx.configure(sc) sc.tick() assert await tctx.master.await_log("Loading") tctx.master.clear() sc.last_load, sc.last_mtime = 0, 0 sc.tick() assert await tctx.master.await_log("Loading")
def test_exception(self): o = options.Options() m = mastertest.RecordingMaster(o, proxy.DummyServer()) sc = script.Script(tutils.test_data.path("data/addonscripts/error.py")) m.addons.add(sc) f = tflow.tflow(resp=True) m.request(f) assert m.event_log[0][0] == "error" assert len(m.event_log[0][1].splitlines()) == 6 assert re.search('addonscripts/error.py", line \d+, in request', m.event_log[0][1]) assert re.search('addonscripts/error.py", line \d+, in mkerr', m.event_log[0][1]) assert m.event_log[0][1].endswith("ValueError: Error!\n")
async def test_exception(self, tdata): with taddons.context() as tctx: sc = script.Script( tdata.path("mitmproxy/data/addonscripts/error.py"), True, ) tctx.master.addons.add(sc) await tctx.master.await_log("error running") tctx.configure(sc) f = tflow.tflow(resp=True) tctx.master.addons.trigger("request", f) assert await tctx.master.await_log("ValueError: Error!") assert await tctx.master.await_log("error.py")
def test_simple(self): o = options.Options() m = master.Master(o, proxy.DummyServer()) sc = script.Script( tutils.test_data.path("data/addonscripts/recorder.py")) m.addons.add(sc) assert sc.ns.call_log == [("solo", "start", (), {}), ("solo", "configure", (o, o.keys()), {})] sc.ns.call_log = [] f = tflow.tflow(resp=True) m.request(f) recf = sc.ns.call_log[0] assert recf[1] == "request"
def test_reload(self): with taddons.context() as tctx: with tutils.tmpdir(): with open("foo.py", "w"): pass sc = script.Script("foo.py") tctx.configure(sc) for _ in range(100): with open("foo.py", "a") as f: f.write(".") sc.tick() time.sleep(0.1) if tctx.master.event_log: return raise AssertionError("Change event not detected.")
def test_concurrent(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path( "mitmproxy/data/addonscripts/concurrent_decorator.py")) sc.start() f1, f2 = tflow.tflow(), tflow.tflow() tctx.cycle(sc, f1) tctx.cycle(sc, f2) start = time.time() while time.time() - start < 5: if f1.reply.state == f2.reply.state == "committed": return raise ValueError("Script never acked")
def test_exception(self): with taddons.context() as tctx: sc = script.Script( tutils.test_data.path("mitmproxy/data/addonscripts/error.py")) l = addonmanager.Loader(tctx.master) sc.load(l) f = tflow.tflow(resp=True) sc.request(f) assert tctx.master.logs[0].level == "error" assert len(tctx.master.logs[0].msg.splitlines()) == 6 assert re.search( r'addonscripts[\\/]error.py", line \d+, in request', tctx.master.logs[0].msg) assert re.search(r'addonscripts[\\/]error.py", line \d+, in mkerr', tctx.master.logs[0].msg) assert tctx.master.logs[0].msg.endswith("ValueError: Error!\n")
async def test_reload(self, tmpdir): with taddons.context() as tctx: f = tmpdir.join("foo.py") f.ensure(file=True) f.write("\n") sc = script.Script(str(f), True) tctx.configure(sc) assert await tctx.master.await_log("Loading") tctx.master.clear() for i in range(20): f.write("\n") if tctx.master.has_log("Loading"): break await asyncio.sleep(0.1) else: raise AssertionError("No reload seen")