Skip to content

Commit 9bbf839

Browse files
committed
test: add test about struct deserialization
1 parent e51254b commit 9bbf839

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

libdestruct/common/struct/struct.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,4 @@ def from_bytes(cls: type[struct], data: bytes) -> struct_impl:
2727
"""Create a struct from a serialized representation."""
2828
type_inflater = inflater(data)
2929

30-
result = type_inflater.inflate(cls, 0)
31-
32-
if result.size != len(data):
33-
raise ValueError("The length of the serialized struct does not match the struct size.")
34-
35-
return result
30+
return type_inflater.inflate(cls, 0)

test/scripts/basic_struct_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,26 @@ class test8(struct):
265265

266266
d.kill()
267267
d.terminate()
268+
269+
def test_struct_from_bytes(self):
270+
class test_t(struct):
271+
a: c_int
272+
b: c_long
273+
c: ptr = ptr_to_self()
274+
275+
memory = b""
276+
memory += (1337).to_bytes(4, "little")
277+
memory += (13371337).to_bytes(8, "little")
278+
memory += (4 + 8 + 8).to_bytes(8, "little")
279+
memory += (1234).to_bytes(4, "little")
280+
memory += (12345678).to_bytes(8, "little")
281+
memory += (0).to_bytes(8, "little")
282+
283+
test = test_t.from_bytes(memory)
284+
285+
assert test.a.value == 1337
286+
assert test.b.value == 13371337
287+
assert test.c.unwrap().a.value == 1234
288+
assert test.c.unwrap().b.value == 12345678
289+
assert test.address == 0x0
290+
assert test.c.unwrap().address == (4 + 8 + 8)

0 commit comments

Comments
 (0)