aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/test_command_lexer.py
blob: ec9940874a5dc110ce206b038de456dbbafaa905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pyparsing
import pytest
from hypothesis import given, example
from hypothesis.strategies import text

from mitmproxy import command_lexer


@pytest.mark.parametrize(
    "test_input,valid", [
        ("'foo'", True),
        ('"foo"', True),
        ("'foo' bar'", False),
        ("'foo\\' bar'", True),
        ("'foo' 'bar'", False),
        ("'foo'x", False),
        ('''"foo    ''', True),
        ('''"foo 'bar'   ''', True),
        ('"foo\\', True),
    ]
)
def test_partial_quoted_string(test_input, valid):
    if valid:
        assert command_lexer.PartialQuotedString.parseString(test_input, parseAll=True)[0] == test_input
    else:
        with pytest.raises(pyparsing.ParseException):
            command_lexer.PartialQuotedString.parseString(test_input, parseAll=True)


@pytest.mark.parametrize(
    "test_input,expected", [
        ("'foo'", ["'foo'"]),
        ('"foo"', ['"foo"']),
        ("'foo' 'bar'", ["'foo'", ' ', "'bar'"]),
        ("'foo'x", ["'foo'", 'x']),
        ('''"foo''', ['"foo']),
        ('''"foo 'bar' ''', ['''"foo 'bar' ''']),
        ('"foo\\', ['"foo\\']),
    ]
)
def test_expr(test_input, expected):
    assert list(command_lexer.expr.parseString(test_input, parseAll=True)) == expected


@given(text())
def test_quote_unquote_cycle(s):
    assert command_lexer.unquote(command_lexer.quote(s)) == s


@given(text())
@example("'foo\\'")
def test_unquote_never_fails(s):
    command_lexer.unquote(s)