Exemple #1
0
def test_decode_nested_flat() -> None:
    r = tlv.decode(b"\x9f\x02\x01\xff\xe0\x03\x9f\x03\x00\x9f\x04\x00",
                   flatten=True)
    assert r == {"9F02": b"\xff", "9F03": b"", "9F04": b""}

    r = tlv.decode(b"\x9f\x02\x01\xff\xe0\x04\x9f\x02\x01\xaa\x9f\x04\x00",
                   flatten=True)
    assert r == {"9F02": b"\xaa", "9F04": b""}
Exemple #2
0
def test_decode_exception(tags: str, tag: str, offset: int, value: Any,
                          error: str) -> None:
    tags_b = bytes.fromhex(tags)
    with pytest.raises(tlv.DecodeError) as e:
        _ = tlv.decode(tags_b)
    assert e.value.args[0] == error
    assert e.value.tag == tag
    assert e.value.offset == offset
    assert e.value.tlv == value
Exemple #3
0
def test_decode_tag(tags: str, value: Any) -> None:
    r = tlv.decode(bytes.fromhex(tags))
    assert r == value
Exemple #4
0
def test_decode_simple() -> None:
    r = tlv.decode(b"\x9f\x02\xff" + b"\xee" * 255 + b"\x9f\x03\x00",
                   simple=True)
    assert r == {"9F02": b"\xee" * 255, "9F03": b""}
Exemple #5
0
def test_decode_nested() -> None:
    r = tlv.decode(b"\x9f\x02\x01\xff\xe0\x03\x9f\x03\x00\x9f\x04\x00")
    assert r == {"9F02": b"\xff", "E0": {"9F03": b""}, "9F04": b""}
Exemple #6
0
def test_decode_tag_coerce(tags: str, value: Any) -> None:
    r = tlv.decode(bytes.fromhex(tags),
                   convert=lambda t, v: v.hex().upper(),
                   flatten=True)
    assert r == value