Example #1
0
    def test_parse_partial(self):
        tests = [
            [
                "foo bar",
                [
                    command.ParseResult(value="foo",
                                        type=mitmproxy.types.Cmd,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="bar",
                                        type=mitmproxy.types.Unknown,
                                        valid=False)
                ],
                [],
            ],
            [
                "cmd1 'bar",
                [
                    command.ParseResult(value="cmd1",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="'bar", type=str, valid=True)
                ],
                [],
            ],
            [
                "a",
                [
                    command.ParseResult(value="a",
                                        type=mitmproxy.types.Cmd,
                                        valid=False)
                ],
                [],
            ],
            [
                "", [],
                [
                    command.CommandParameter("", mitmproxy.types.Cmd),
                    command.CommandParameter("", mitmproxy.types.CmdArgs)
                ]
            ],
            [
                "cmd3 1",
                [
                    command.ParseResult(value="cmd3",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="1", type=int, valid=True),
                ], []
            ],
            [
                "cmd3 ",
                [
                    command.ParseResult(value="cmd3",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                ], [command.CommandParameter('foo', int)]
            ],
            [
                "subcommand ",
                [
                    command.ParseResult(
                        value="subcommand",
                        type=mitmproxy.types.Cmd,
                        valid=True,
                    ),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                ],
                [
                    command.CommandParameter('cmd', mitmproxy.types.Cmd),
                    command.CommandParameter(
                        'args',
                        mitmproxy.types.CmdArgs,
                        kind=inspect.Parameter.VAR_POSITIONAL),
                ],
            ],
            [
                "varargs one",
                [
                    command.ParseResult(value="varargs",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="one", type=str, valid=True),
                ],
                [
                    command.CommandParameter(
                        'var', str, kind=inspect.Parameter.VAR_POSITIONAL)
                ]
            ],
            [
                "varargs one two three",
                [
                    command.ParseResult(value="varargs",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="one", type=str, valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="two", type=str, valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="three", type=str, valid=True),
                ],
                [],
            ],
            [
                "subcommand cmd3 ",
                [
                    command.ParseResult(value="subcommand",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="cmd3",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                ], [command.CommandParameter('foo', int)]
            ],
            [
                "cmd4",
                [
                    command.ParseResult(value="cmd4",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                ],
                [
                    command.CommandParameter('a', int),
                    command.CommandParameter('b', str),
                    command.CommandParameter('c', mitmproxy.types.Path),
                ]
            ],
            [
                "cmd4 ",
                [
                    command.ParseResult(value="cmd4",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                ],
                [
                    command.CommandParameter('a', int),
                    command.CommandParameter('b', str),
                    command.CommandParameter('c', mitmproxy.types.Path),
                ]
            ],
            [
                "cmd4 1",
                [
                    command.ParseResult(value="cmd4",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="1", type=int, valid=True),
                ],
                [
                    command.CommandParameter('b', str),
                    command.CommandParameter('c', mitmproxy.types.Path),
                ]
            ],
            [
                "flow",
                [
                    command.ParseResult(value="flow",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                ],
                [
                    command.CommandParameter('f', flow.Flow),
                    command.CommandParameter('s', str),
                ]
            ],
            [
                "flow ",
                [
                    command.ParseResult(value="flow",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                ],
                [
                    command.CommandParameter('f', flow.Flow),
                    command.CommandParameter('s', str),
                ]
            ],
            [
                "flow x",
                [
                    command.ParseResult(value="flow",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="x", type=flow.Flow,
                                        valid=False),
                ], [
                    command.CommandParameter('s', str),
                ]
            ],
            [
                "flow x ",
                [
                    command.ParseResult(value="flow",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="x", type=flow.Flow,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                ], [
                    command.CommandParameter('s', str),
                ]
            ],
            [
                "flow \"one two",
                [
                    command.ParseResult(value="flow",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="\"one two",
                                        type=flow.Flow,
                                        valid=False),
                ], [
                    command.CommandParameter('s', str),
                ]
            ],
            [
                "flow \"three four\"",
                [
                    command.ParseResult(value="flow",
                                        type=mitmproxy.types.Cmd,
                                        valid=True),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value='"three four"',
                                        type=flow.Flow,
                                        valid=False),
                ], [
                    command.CommandParameter('s', str),
                ]
            ],
            [
                "spaces '    '",
                [
                    command.ParseResult(value="spaces",
                                        type=mitmproxy.types.Cmd,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="'    '",
                                        type=mitmproxy.types.Unknown,
                                        valid=False)
                ],
                [],
            ],
            [
                'spaces2 "    "',
                [
                    command.ParseResult(value="spaces2",
                                        type=mitmproxy.types.Cmd,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value='"    "',
                                        type=mitmproxy.types.Unknown,
                                        valid=False)
                ],
                [],
            ],
            [
                '"abc"',
                [
                    command.ParseResult(value='"abc"',
                                        type=mitmproxy.types.Cmd,
                                        valid=False),
                ],
                [],
            ],
            [
                "'def'",
                [
                    command.ParseResult(value="'def'",
                                        type=mitmproxy.types.Cmd,
                                        valid=False),
                ],
                [],
            ],
            [
                "cmd10 'a' \"b\" c",
                [
                    command.ParseResult(value="cmd10",
                                        type=mitmproxy.types.Cmd,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="'a'",
                                        type=mitmproxy.types.Unknown,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value='"b"',
                                        type=mitmproxy.types.Unknown,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="c",
                                        type=mitmproxy.types.Unknown,
                                        valid=False),
                ],
                [],
            ],
            [
                "cmd11 'a \"b\" c'",
                [
                    command.ParseResult(value="cmd11",
                                        type=mitmproxy.types.Cmd,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="'a \"b\" c'",
                                        type=mitmproxy.types.Unknown,
                                        valid=False),
                ],
                [],
            ],
            [
                'cmd12 "a \'b\' c"',
                [
                    command.ParseResult(value="cmd12",
                                        type=mitmproxy.types.Cmd,
                                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value='"a \'b\' c"',
                                        type=mitmproxy.types.Unknown,
                                        valid=False),
                ],
                [],
            ],
            [
                "    spaces_at_the_begining_are_not_stripped",
                [
                    command.ParseResult(value="    ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(
                        value="spaces_at_the_begining_are_not_stripped",
                        type=mitmproxy.types.Cmd,
                        valid=False),
                ],
                [],
            ],
            [
                "    spaces_at_the_begining_are_not_stripped neither_at_the_end      ",
                [
                    command.ParseResult(value="    ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(
                        value="spaces_at_the_begining_are_not_stripped",
                        type=mitmproxy.types.Cmd,
                        valid=False),
                    command.ParseResult(value=" ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                    command.ParseResult(value="neither_at_the_end",
                                        type=mitmproxy.types.Unknown,
                                        valid=False),
                    command.ParseResult(value="      ",
                                        type=mitmproxy.types.Space,
                                        valid=True),
                ],
                [],
            ],
        ]

        with taddons.context() as tctx:
            tctx.master.addons.add(TAddon())
            for s, expected, expectedremain in tests:
                current, remain = tctx.master.commands.parse_partial(s)
                assert (s, current, expectedremain) == (s, expected, remain)
Example #2
0
 def test_parse_partial(self):
     tests = [
         [
             "foo bar",
             [
                 command.ParseResult(value="foo",
                                     type=mitmproxy.types.Cmd,
                                     valid=False),
                 command.ParseResult(value="bar",
                                     type=mitmproxy.types.Unknown,
                                     valid=False)
             ],
             [],
         ],
         [
             "cmd1 'bar",
             [
                 command.ParseResult(value="cmd1",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="'bar", type=str, valid=True)
             ],
             [],
         ],
         [
             "a",
             [
                 command.ParseResult(value="a",
                                     type=mitmproxy.types.Cmd,
                                     valid=False)
             ],
             [],
         ],
         [
             "",
             [
                 command.ParseResult(value="",
                                     type=mitmproxy.types.Cmd,
                                     valid=False)
             ], []
         ],
         [
             "cmd3 1",
             [
                 command.ParseResult(value="cmd3",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="1", type=int, valid=True),
             ], []
         ],
         [
             "cmd3 ",
             [
                 command.ParseResult(value="cmd3",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="", type=int, valid=False),
             ], []
         ],
         [
             "subcommand ",
             [
                 command.ParseResult(
                     value="subcommand",
                     type=mitmproxy.types.Cmd,
                     valid=True,
                 ),
                 command.ParseResult(value="",
                                     type=mitmproxy.types.Cmd,
                                     valid=False),
             ],
             ["arg"],
         ],
         [
             "subcommand cmd3 ",
             [
                 command.ParseResult(value="subcommand",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="cmd3",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="", type=int, valid=False),
             ], []
         ],
         [
             "cmd4",
             [
                 command.ParseResult(value="cmd4",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
             ], ["int", "str", "path"]
         ],
         [
             "cmd4 ",
             [
                 command.ParseResult(value="cmd4",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="", type=int, valid=False),
             ], ["str", "path"]
         ],
         [
             "cmd4 1",
             [
                 command.ParseResult(value="cmd4",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="1", type=int, valid=True),
             ], ["str", "path"]
         ],
         [
             "cmd4 1",
             [
                 command.ParseResult(value="cmd4",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="1", type=int, valid=True),
             ], ["str", "path"]
         ],
         [
             "flow",
             [
                 command.ParseResult(value="flow",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
             ], ["flow", "str"]
         ],
         [
             "flow ",
             [
                 command.ParseResult(value="flow",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="", type=flow.Flow, valid=False),
             ], ["str"]
         ],
         [
             "flow x",
             [
                 command.ParseResult(value="flow",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="x", type=flow.Flow,
                                     valid=False),
             ], ["str"]
         ],
         [
             "flow x ",
             [
                 command.ParseResult(value="flow",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="x", type=flow.Flow,
                                     valid=False),
                 command.ParseResult(value="", type=str, valid=True),
             ], []
         ],
         [
             "flow \"one two",
             [
                 command.ParseResult(value="flow",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="\"one two",
                                     type=flow.Flow,
                                     valid=False),
             ], ["str"]
         ],
         [
             "flow \"one two\"",
             [
                 command.ParseResult(value="flow",
                                     type=mitmproxy.types.Cmd,
                                     valid=True),
                 command.ParseResult(value="one two",
                                     type=flow.Flow,
                                     valid=False),
             ], ["str"]
         ],
     ]
     with taddons.context() as tctx:
         tctx.master.addons.add(TAddon())
         for s, expected, expectedremain in tests:
             current, remain = tctx.master.commands.parse_partial(s)
             assert current == expected
             assert expectedremain == remain
Example #3
0
 def test_parse_partial(self):
     tests = [
         [
             "foo bar",
             [
                 command.ParseResult(value="foo", type=command.Cmd),
                 command.ParseResult(value="bar", type=str)
             ],
         ],
         [
             "foo 'bar",
             [
                 command.ParseResult(value="foo", type=command.Cmd),
                 command.ParseResult(value="'bar", type=str)
             ]
         ],
         ["a", [command.ParseResult(value="a", type=command.Cmd)]],
         ["", [command.ParseResult(value="", type=command.Cmd)]],
         [
             "cmd3 1",
             [
                 command.ParseResult(value="cmd3", type=command.Cmd),
                 command.ParseResult(value="1", type=int),
             ]
         ],
         [
             "cmd3 ",
             [
                 command.ParseResult(value="cmd3", type=command.Cmd),
                 command.ParseResult(value="", type=int),
             ]
         ],
         [
             "subcommand ",
             [
                 command.ParseResult(value="subcommand", type=command.Cmd),
                 command.ParseResult(value="", type=command.Cmd),
             ]
         ],
         [
             "subcommand cmd3 ",
             [
                 command.ParseResult(value="subcommand", type=command.Cmd),
                 command.ParseResult(value="cmd3", type=command.Cmd),
                 command.ParseResult(value="", type=int),
             ]
         ],
     ]
     with taddons.context() as tctx:
         tctx.master.addons.add(TAddon())
         for s, expected in tests:
             assert tctx.master.commands.parse_partial(s) == expected