Пример #1
0
def test_rlp_decode_to_nested_sequence() -> None:
    encoded_data = (
        b"\xdf\x85hello\x81\xff\xd6\x83how\xd1\x83are\x83you\xc8\x85doing\xc1#"
    )
    expected_raw_data = [
        b"hello",
        Uint(255).to_be_bytes(),
        [
            b"how",
            [b"are", b"you", [b"doing", [Uint(35).to_be_bytes()]]],
        ],
    ]
    assert rlp.decode_to_sequence(encoded_data) == expected_raw_data
Пример #2
0
def test_roundtrip_encoding_and_decoding() -> None:
    test_cases = [
        b"",
        b"h",
        b"hello how are you doing today?",
        Uint(35).to_be_bytes(),
        Uint(255).to_be_bytes(),
        [],
        [
            b"hello",
            [b"how", [b"are", b"you", [b"doing", [Uint(255).to_be_bytes()]]]],
        ],
    ]
    for raw_data in test_cases:
        assert rlp.decode(rlp.encode(cast(RLP, raw_data))) == raw_data
Пример #3
0
def test_rlp_decode_to_20_elem_sequence_of_bytes_and_uints() -> None:
    encoded_data = (
        bytearray([0xF8]) + b"F" +
        b"\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello##########"
    )
    expected_raw_data = [b"hello"] * 10 + [Uint(35).to_be_bytes()] * 10
    assert rlp.decode_to_sequence(encoded_data) == expected_raw_data
Пример #4
0
def test_rlp_encode_20_elem_byte_uint_combo() -> None:
    raw_data = [Uint(35)] * 10 + [b"hello"] * 10  # type: ignore
    expected = (
        bytearray([0xF8]) + b"F" +
        b"##########\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello\x85hello"
    )
    assert rlp.encode_sequence(raw_data) == expected
Пример #5
0
def test_rlp_encode_fails() -> None:
    test_cases = [
        123,
        [b"hello", Uint(255), [b"how", [b"are", [b"you", [123]]]]],
    ]
    for raw_data in test_cases:
        with pytest.raises(TypeError):
            rlp.encode(cast(RLP, raw_data))
Пример #6
0
def convert_to_rlp_native(
        obj: Union[str, int, Sequence[Union[str, int]]]) -> RLP:
    if isinstance(obj, str):
        return bytes(obj, "utf-8")
    elif isinstance(obj, int):
        return Uint(obj)

    # It's a sequence
    return [convert_to_rlp_native(element) for element in obj]
Пример #7
0
def test_rlp_encode_nested_sequence() -> None:
    nested_sequence: Sequence["RLP"] = [
        b"hello",
        Uint(255),
        [b"how", [b"are", b"you", [b"doing"]]],
    ]
    expected: Bytes = (
        b"\xdd\x85hello\x81\xff\xd4\x83how\xcf\x83are\x83you\xc6\x85doing")
    assert rlp.encode_sequence(nested_sequence) == expected
Пример #8
0
def test_rlp_encode_successfully() -> None:
    test_cases = [
        (b"", bytearray([0x80])),
        (b"\x83" * 55, bytearray([0xB7]) + bytearray(b"\x83" * 55)),
        (Uint(0), b"\x80"),
        (Uint(255), b"\x81\xff"),
        ([], bytearray([0xC0])),
        (
            [b"hello"] * 5 + [Uint(35)] * 5,  # type: ignore
            bytearray([0xE3]) +
            bytearray(b"\x85hello\x85hello\x85hello\x85hello\x85hello#####"),
        ),
        (
            [b"hello",
             Uint(255), [b"how", [b"are", b"you", [b"doing"]]]],
            bytearray(
                b"\xdd\x85hello\x81\xff\xd4\x83how\xcf\x83are\x83you\xc6\x85doing"
            ),
        ),
    ]
    for (raw_data, expected_encoding) in test_cases:
        assert rlp.encode(cast(RLP, raw_data)) == expected_encoding
Пример #9
0
def test_rlp_encode_uint_byte_max() -> None:
    assert rlp.encode(Uint(255)) == b"\x81\xff"
Пример #10
0
def test_rlp_encode_uint_0() -> None:
    assert rlp.encode(Uint(0)) == b"\x80"
Пример #11
0
def test_rlp_decode_to_1_elem_sequence_of_uint() -> None:
    assert rlp.decode_to_sequence(bytearray([0xC2]) +
                                  b"\x81\xff") == [Uint(255).to_be_bytes()]
Пример #12
0
def test_rlp_decode_to_255_uint() -> None:
    assert rlp.decode(b"\x81\xff") == Uint(255).to_be_bytes()
Пример #13
0
def test_rlp_decode_to_zero_uint() -> None:
    assert rlp.decode(b"\x80") == Uint(0).to_be_bytes()
Пример #14
0
def test_rlp_encode_10_elem_byte_uint_combo() -> None:
    raw_data = [b"hello"] * 5 + [Uint(35)] * 5  # type: ignore
    expected = (bytearray([0xE3]) +
                b"\x85hello\x85hello\x85hello\x85hello\x85hello#####")
    assert rlp.encode_sequence(raw_data) == expected
Пример #15
0
def test_rlp_encode_single_elem_list_uint() -> None:
    assert rlp.encode_sequence([Uint(255)]) == bytearray([0xC2]) + b"\x81\xff"