Beispiel #1
0
def test_invalid_input_random_corruption(test_case):
    json_chars = list(json.dumps(test_case))

    while True:
        try:
            test_case_str = "".join(json_chars)
            json.loads(test_case_str)
            json_chars[random.randint(0,
                                      len(json_chars) - 1)] = chr(
                                          random.randint(0, 128))

        except ValueError:
            break

    # XXX Ideally, it should follow json.loads() with value error if there's an error.
    # for now, though, we are happy if it doesn't overflow the string buffer or segfaults
    parser = json_parser.Parser(test_case_str.encode("utf-8"), True, 1)

    try:
        parser.parse()

    except ValueError:
        pass

    assert parser.consistent()
Beispiel #2
0
def test_invalid_input_non_json_token_fuzzying(s):
    try:
        json.loads(s)
        # valid json, skip test
        return

    except:
        pass

    # XXX Ideally, it should follow json.loads() with value error if there's an error.
    # for now, though, we are happy if it doesn't overflow the string buffer or segfaults
    parser = json_parser.Parser(s.encode("utf-8"), True, 1)
    try:
        parser.parse()

    except ValueError:
        pass

    assert parser.consistent()
Beispiel #3
0
def test_obj(obj):
    parser = json_parser.Parser(json.dumps(obj).encode("utf-8"), True, 1)
    parsed = parser.parse()

    assert parsed == obj
Beispiel #4
0
def test_array(a):
    parser = json_parser.Parser(json.dumps(a).encode("utf-8"), True, 1)
    parsed = parser.parse()

    assert parsed == a
Beispiel #5
0
def test_bool(b):
    parser = json_parser.Parser(json.dumps(b).encode("utf-8"), True, 1)
    parsed = parser.parse()

    assert parsed == b
Beispiel #6
0
def test_null():
    parser = json_parser.Parser(json.dumps(None).encode("utf-8"), True, 1)
    parsed = parser.parse()

    assert parsed == None
Beispiel #7
0
def test_num_floats(i):
    parser = json_parser.Parser(json.dumps(i).encode("utf-8"), True, 1)
    parsed = parser.parse()

    assert (parsed == i or math.isnan(parsed) and math.isnan(i))
    assert type(parsed) == type(i)
Beispiel #8
0
def test_num_integers(i):
    parser = json_parser.Parser(json.dumps(i).encode("utf-8"), True, 1)
    parsed = parser.parse()

    assert parsed == i
    assert type(parsed) == type(i)
Beispiel #9
0
def test_str_escaped_quotes_str(s):
    parser = json_parser.Parser(json.dumps(s).encode("utf-8"), True, 1)
    parsed = parser.parse()
    assert parsed == s
Beispiel #10
0
def test_str_simple(s):
    parser = json_parser.Parser(json.dumps(s).encode("utf-8"), True, 1)
    parsed = parser.parse()
    assert parsed == s
Beispiel #11
0
def test_init():
    parser = json_parser.Parser(b'{}', True, 1)
Beispiel #12
0
def test_invalid_input():
    for test_case_str in TEST_CASES:
        parser = json_parser.Parser(test_case_str.encode("utf-8"), True, 1)
        with pytest.raises(ValueError):
            parser.parse()
        assert parser.consistent()