-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
55 lines (42 loc) · 1.65 KB
/
Copy pathtest.py
File metadata and controls
55 lines (42 loc) · 1.65 KB
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
54
55
import smoltlv
def hexdump(data: bytes):
def to_printable_ascii(byte):
return chr(byte) if 32 <= byte <= 126 else "."
res = ""
offset = 0
while offset < len(data):
chunk = data[offset : offset + 16]
hex_values = " ".join(f"{byte:02x}" for byte in chunk)
ascii_values = "".join(to_printable_ascii(byte) for byte in chunk)
res += (f"{offset:08x} {hex_values:<48} |{ascii_values}|\n")
offset += 16
return res
def try_roundtrip(value, allow_unknown_types=False):
print("Testing value:", repr(value))
encoded = smoltlv.dumps(value)
print("Encoded data:")
print(hexdump(encoded))
decoded = smoltlv.loads(encoded, allow_unknown_types=allow_unknown_types)
assert decoded == value, f"Roundtrip failed: {decoded} != {value}"
try_roundtrip(None)
try_roundtrip(True)
try_roundtrip(False)
try_roundtrip(42)
try_roundtrip(-1)
try_roundtrip(b"hello world")
try_roundtrip("hello world")
try_roundtrip([1, 2, 3, "four", True, None])
try_roundtrip({"key1": "value1", "key2": 12345, "key3": False})
try_roundtrip({})
try_roundtrip([])
try_roundtrip({"nested": {"list": [1, 2, 3], "bool": True}, "empty_dict": {}, "empty_list": []})
try_roundtrip(smoltlv.UnknownTLV(0x99, b"\x01\x02\x03\x04"), allow_unknown_types=True)
try_roundtrip([{"known": 123, "unknown": smoltlv.UnknownTLV(0xAB, b"\x05\x06")}], allow_unknown_types=True)
try_roundtrip([
{"level1_key1": "level1_value1",
"level1_key2": {
"level2_key1": [1, 2, 3, {"level3_key": smoltlv.UnknownTLV(0xCD, b"\x07\x08")}],
"level2_key2": False
}},
smoltlv.UnknownTLV(0xEF, b"\x09\x0A\x0B")
], allow_unknown_types=True)