|
3 | 3 | from dissect.cstruct.cstruct import cstruct
|
4 | 4 | from dissect.cstruct.exceptions import ArraySizeError
|
5 | 5 |
|
| 6 | +from .utils import verify_compiled |
| 7 | + |
6 | 8 |
|
7 | 9 | def test_array_size_mismatch(cs: cstruct):
|
8 | 10 | with pytest.raises(ArraySizeError):
|
9 | 11 | cs.uint8[2]([1, 2, 3]).dumps()
|
10 | 12 |
|
11 | 13 | assert cs.uint8[2]([1, 2]).dumps()
|
| 14 | + |
| 15 | + |
| 16 | +def test_eof(cs: cstruct, compiled: bool): |
| 17 | + cdef = """ |
| 18 | + struct test_char { |
| 19 | + char data[EOF]; |
| 20 | + }; |
| 21 | +
|
| 22 | + struct test_wchar { |
| 23 | + wchar data[EOF]; |
| 24 | + }; |
| 25 | +
|
| 26 | + struct test_packed { |
| 27 | + uint16 data[EOF]; |
| 28 | + }; |
| 29 | +
|
| 30 | + struct test_int { |
| 31 | + uint24 data[EOF]; |
| 32 | + }; |
| 33 | +
|
| 34 | + enum Test : uint16 { |
| 35 | + A = 1 |
| 36 | + }; |
| 37 | +
|
| 38 | + struct test_enum { |
| 39 | + Test data[EOF]; |
| 40 | + }; |
| 41 | +
|
| 42 | + struct test_eof_field { |
| 43 | + uint8 EOF; |
| 44 | + char data[EOF]; |
| 45 | + uint8 remainder; |
| 46 | + }; |
| 47 | + """ |
| 48 | + cs.load(cdef, compiled=compiled) |
| 49 | + |
| 50 | + assert verify_compiled(cs.test_char, compiled) |
| 51 | + assert verify_compiled(cs.test_wchar, compiled) |
| 52 | + assert verify_compiled(cs.test_packed, compiled) |
| 53 | + assert verify_compiled(cs.test_int, compiled) |
| 54 | + assert verify_compiled(cs.test_enum, compiled) |
| 55 | + assert verify_compiled(cs.test_eof_field, compiled) |
| 56 | + |
| 57 | + test_char = cs.test_char(b"abc") |
| 58 | + assert test_char.data == b"abc" |
| 59 | + assert test_char.dumps() == b"abc" |
| 60 | + |
| 61 | + test_wchar = cs.test_wchar("abc".encode("utf-16-le")) |
| 62 | + assert test_wchar.data == "abc" |
| 63 | + assert test_wchar.dumps() == "abc".encode("utf-16-le") |
| 64 | + |
| 65 | + test_packed = cs.test_packed(b"\x01\x00\x02\x00") |
| 66 | + assert test_packed.data == [1, 2] |
| 67 | + assert test_packed.dumps() == b"\x01\x00\x02\x00" |
| 68 | + |
| 69 | + test_int = cs.test_int(b"\x01\x00\x00\x02\x00\x00") |
| 70 | + assert test_int.data == [1, 2] |
| 71 | + assert test_int.dumps() == b"\x01\x00\x00\x02\x00\x00" |
| 72 | + |
| 73 | + test_enum = cs.test_enum(b"\x01\x00") |
| 74 | + assert test_enum.data == [cs.Test.A] |
| 75 | + assert test_enum.dumps() == b"\x01\x00" |
| 76 | + |
| 77 | + test_eof_field = cs.test_eof_field(b"\x01a\x02") |
| 78 | + assert test_eof_field.data == b"a" |
| 79 | + assert test_eof_field.remainder == 2 |
| 80 | + assert test_eof_field.dumps() == b"\x01a\x02" |
0 commit comments