def test_varargs(self): with taddons.context() as tctx: cm = command.CommandManager(tctx.master) a = TAddon() c = command.Command(cm, "varargs", a.varargs) assert c.signature_help() == "varargs one *var -> str[]" assert c.call(["one", "two", "three"]) == ["two", "three"]
def test_simple(): with taddons.context() as tctx: c = command.CommandManager(tctx.master) a = TAddon() c.add("one.two", a.cmd1) assert (c.commands["one.two"].help == "cmd1 help") assert (c.execute("one.two foo") == "ret foo") assert (c.execute("one.two \"foo\"") == "ret foo") assert (c.execute("one.two 'foo bar'") == "ret foo bar") assert (c.call("one.two", "foo") == "ret foo") with pytest.raises(exceptions.CommandError, match="Unknown"): c.execute("nonexistent") with pytest.raises(exceptions.CommandError, match="Invalid"): c.execute("") with pytest.raises(exceptions.CommandError, match="argument mismatch"): c.execute("one.two too many args") with pytest.raises(exceptions.CommandError, match="Unknown"): c.call("nonexistent") with pytest.raises(exceptions.CommandError, match="Unknown"): c.execute("\\") c.add("empty", a.empty) c.execute("empty") fp = io.StringIO() c.dump(fp) assert fp.getvalue()
def __init__(self, opts): self.options = opts or options.Options() # type: options.Options self.commands = command.CommandManager(self) self.addons = addonmanager.AddonManager(self) self.event_queue = queue.Queue() self.should_exit = threading.Event() self._server = None self.first_tick = True
def test_collect_commands(): """ This tests for the error thrown by hasattr() """ with taddons.context() as tctx: c = command.CommandManager(tctx.master) a = TCmds() c.collect_commands(a) assert "empty" in c.commands
def test_varargs(self): with taddons.context() as tctx: cm = command.CommandManager(tctx.master) a = TAddon() c = command.Command(cm, "varargs", a.varargs) assert c.signature_help() == "varargs str *str -> [str]" assert c.call(["one", "two", "three"]) == ["two", "three"] with pytest.raises(exceptions.CommandError): c.call(["one", "two", 3])
def test_typecheck(self): with taddons.context(loadcore=False) as tctx: cm = command.CommandManager(tctx.master) a = TypeErrAddon() command.Command(cm, "noret", a.noret) with pytest.raises(exceptions.CommandError): command.Command(cm, "invalidret", a.invalidret) with pytest.raises(exceptions.CommandError): assert command.Command(cm, "invalidarg", a.invalidarg)
def __init__(self, opts, server): self.options = opts or options.Options() self.commands = command.CommandManager(self) self.addons = addonmanager.AddonManager(self) self.event_queue = queue.Queue() self.should_exit = threading.Event() self.server = server self.first_tick = True channel = controller.Channel(self.event_queue, self.should_exit) server.set_channel(channel)
def __init__(self, opts): self.should_exit = threading.Event() self.event_loop = asyncio.get_event_loop() self.options: options.Options = opts or options.Options() self.commands = command.CommandManager(self) self.addons = addonmanager.AddonManager(self) self._server = None self.log = log.Log(self) mitmproxy_ctx.master = self mitmproxy_ctx.log = self.log mitmproxy_ctx.options = self.options
def test_decorator(): with taddons.context() as tctx: c = command.CommandManager(tctx.master) a = TDec() c.collect_commands(a) assert "cmd1" in c.commands assert c.execute("cmd1 bar") == "ret bar" assert "empty" in c.commands assert c.execute("empty") is None with taddons.context() as tctx: tctx.master.addons.add(a) assert tctx.master.commands.execute("cmd1 bar") == "ret bar"
async def test_collect_commands(): """ This tests for the error thrown by hasattr() """ with taddons.context() as tctx: c = command.CommandManager(tctx.master) a = TCmds() c.collect_commands(a) assert "empty" in c.commands a = TypeErrAddon() c.collect_commands(a) await tctx.master.await_log("Could not load")
def test_call(self): o = options.Options() m = master.Master(o, proxy.DummyServer(o)) cm = command.CommandManager(m) a = TAddon() c = command.Command(cm, "cmd.path", a.cmd1) assert c.call(["foo"]) == "ret foo" assert c.signature_help() == "cmd.path str -> str" c = command.Command(cm, "cmd.two", a.cmd2) with pytest.raises(exceptions.CommandError): c.call(["foo"])
def test_call(self): with taddons.context() as tctx: cm = command.CommandManager(tctx.master) a = TAddon() c = command.Command(cm, "cmd.path", a.cmd1) assert c.call(["foo"]) == "ret foo" assert c.signature_help() == "cmd.path str -> str" c = command.Command(cm, "cmd.two", a.cmd2) with pytest.raises(exceptions.CommandError): c.call(["foo"]) c = command.Command(cm, "cmd.three", a.cmd3) assert c.call(["1"]) == 1
async def test_collect_commands(): """ This tests for errors thrown by getattr() or __getattr__ implementations that return an object for .command_name. """ with taddons.context() as tctx: c = command.CommandManager(tctx.master) a = TCmds() c.collect_commands(a) assert "empty" in c.commands a = TypeErrAddon() c.collect_commands(a) await tctx.master.await_log("Could not load")
def test_simple(): with taddons.context() as tctx: c = command.CommandManager(tctx.master) a = TAddon() c.add("one.two", a.cmd1) assert c.commands["one.two"].help == "cmd1 help" assert (c.call("one.two foo") == "ret foo") with pytest.raises(exceptions.CommandError, match="Unknown"): c.call("nonexistent") with pytest.raises(exceptions.CommandError, match="Invalid"): c.call("") with pytest.raises(exceptions.CommandError, match="Usage"): c.call("one.two too many args") c.add("empty", a.empty) c.call("empty")
def __init__(self, opts, event_loop: Optional[asyncio.AbstractEventLoop] = None): self.options: options.Options = opts or options.Options() self.commands = command.CommandManager(self) self.addons = addonmanager.AddonManager(self) self.log = log.Log(self) # We expect an active event loop here already because some addons # may want to spawn tasks during the initial configuration phase, # which happens before run(). self.event_loop = event_loop or asyncio.get_running_loop() try: self.should_exit = asyncio.Event() except RuntimeError: self.should_exit = asyncio.Event(loop=self.event_loop) mitmproxy_ctx.master = self mitmproxy_ctx.log = self.log mitmproxy_ctx.options = self.options
def __init__(self, opts): self.event_queue = asyncio.Queue() self.should_exit = threading.Event() self.channel = controller.Channel( asyncio.get_event_loop(), self.event_queue, self.should_exit, ) asyncio.ensure_future(self.main()) asyncio.ensure_future(self.tick()) self.options = opts or options.Options() # type: options.Options self.commands = command.CommandManager(self) self.addons = addonmanager.AddonManager(self) self._server = None self.first_tick = True self.waiting_flows = []
def __init__(self, opts): self.should_exit = threading.Event() self.channel = controller.Channel( self, asyncio.get_event_loop(), self.should_exit, ) self.options: options.Options = opts or options.Options() self.commands = command.CommandManager(self) self.addons = addonmanager.AddonManager(self) self._server = None self.waiting_flows = [] self.log = log.Log(self) mitmproxy_ctx.master = self mitmproxy_ctx.log = self.log mitmproxy_ctx.options = self.options
async def test_commands_exist(): command_manager = command.CommandManager(ctx) km = keymap.Keymap(None) defaultkeys.map(km) assert km.bindings m = master.ConsoleMaster(None) await m.load_flow(tflow()) for binding in km.bindings: parsed, _ = command_manager.parse_partial(binding.command.strip()) cmd = parsed[0].value args = [a.value for a in parsed[1:] if a.type != mitmproxy.types.Space] assert cmd in m.commands.commands cmd_obj = m.commands.commands[cmd] try: cmd_obj.prepare_args(args) except Exception as e: raise ValueError(f"Invalid command: {binding.command}") from e