-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalignment_test.py
More file actions
362 lines (281 loc) · 11.6 KB
/
alignment_test.py
File metadata and controls
362 lines (281 loc) · 11.6 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#
# This file is part of libdestruct (https://github.com/mrindeciso/libdestruct).
# Copyright (c) 2026 Roberto Alessandro Bertolini. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#
import struct as pystruct
import unittest
from libdestruct import c_char, c_int, c_long, c_short, c_uchar, inflater, offset, size_of, struct
from libdestruct.common.utils import alignment_of
class AlignmentOfTest(unittest.TestCase):
def test_alignment_of_c_char(self):
self.assertEqual(alignment_of(c_char), 1)
def test_alignment_of_c_short(self):
self.assertEqual(alignment_of(c_short), 2)
def test_alignment_of_c_int(self):
self.assertEqual(alignment_of(c_int), 4)
def test_alignment_of_c_long(self):
self.assertEqual(alignment_of(c_long), 8)
class AlignedStructTest(unittest.TestCase):
def test_packed_struct_no_padding(self):
"""Default structs are packed with no alignment padding."""
class packed_t(struct):
a: c_char
b: c_int
# packed: 1 + 4 = 5
self.assertEqual(size_of(packed_t), 5)
def test_aligned_struct_padding(self):
"""Aligned struct inserts padding for field alignment."""
class aligned_t(struct):
_aligned_ = True
a: c_char
b: c_int
# aligned: 1 + 3 padding + 4 = 8
self.assertEqual(size_of(aligned_t), 8)
def test_aligned_struct_tail_padding(self):
"""Aligned struct pads total size to max alignment."""
class aligned_t(struct):
_aligned_ = True
a: c_int
b: c_char
# aligned: 4 + 1 + 3 tail padding = 8 (aligned to 4-byte boundary)
self.assertEqual(size_of(aligned_t), 8)
def test_aligned_struct_read_values(self):
"""Values are read correctly from aligned positions."""
class aligned_t(struct):
_aligned_ = True
a: c_char
b: c_int
# a at offset 0, padding 3 bytes, b at offset 4
memory = pystruct.pack("<b", 0x41) + b"\x00" * 3 + pystruct.pack("<i", 42)
s = aligned_t.from_bytes(memory)
self.assertEqual(s.a.value, 0x41)
self.assertEqual(s.b.value, 42)
def test_aligned_struct_mixed_types(self):
"""Correct alignment for mixed type sizes."""
class mixed_t(struct):
_aligned_ = True
a: c_char # offset 0, size 1
b: c_short # offset 2 (aligned to 2), size 2
c: c_char # offset 4, size 1
d: c_int # offset 8 (aligned to 4), size 4
e: c_char # offset 12, size 1
f: c_long # offset 16 (aligned to 8), size 8
# total: 24 (padded to 8-byte boundary)
self.assertEqual(size_of(mixed_t), 24)
def test_aligned_struct_write(self):
"""Writing to aligned struct fields works correctly."""
class aligned_t(struct):
_aligned_ = True
a: c_char
b: c_int
memory = bytearray(8)
lib = inflater(memory)
s = lib.inflate(aligned_t, 0)
s.a.value = 0x41
s.b.value = 42
self.assertEqual(s.a.value, 0x41)
self.assertEqual(s.b.value, 42)
def test_aligned_nested_struct(self):
"""Nested aligned struct respects inner struct alignment."""
class inner_t(struct):
_aligned_ = True
a: c_char
b: c_int
class outer_t(struct):
_aligned_ = True
x: c_char
inner: inner_t
# inner_t: size 8, alignment 4
# outer_t: x at 0 (1 byte), inner at 4 (aligned to 4), inner is 8 bytes
# total: 12, padded to 4-byte boundary = 12
self.assertEqual(size_of(outer_t), 12)
def test_aligned_struct_all_same_size(self):
"""Aligned struct with uniform field sizes has no extra padding."""
class uniform_t(struct):
_aligned_ = True
a: c_int
b: c_int
c: c_int
# No padding needed: 4 + 4 + 4 = 12, tail padded to 4 = 12
self.assertEqual(size_of(uniform_t), 12)
def test_alignment_of_aligned_struct(self):
"""alignment_of returns the struct's computed alignment."""
class aligned_t(struct):
_aligned_ = True
a: c_char
b: c_int
self.assertEqual(alignment_of(aligned_t), 4)
def test_alignment_of_packed_struct(self):
"""Packed struct has alignment 1."""
class packed_t(struct):
a: c_char
b: c_int
self.assertEqual(alignment_of(packed_t), 1)
class CustomAlignmentTest(unittest.TestCase):
def test_aligned_integer_width(self):
"""_aligned_ = N uses N as the struct's minimum alignment."""
class wide_t(struct):
_aligned_ = 16
a: c_int
# 4 bytes data, padded to 16-byte boundary
self.assertEqual(size_of(wide_t), 16)
self.assertEqual(alignment_of(wide_t), 16)
def test_aligned_32(self):
"""_aligned_ = 32 pads to 32-byte boundary."""
class wide_t(struct):
_aligned_ = 32
a: c_char
b: c_int
# 1 + 3 padding + 4 = 8 data, padded to 32
self.assertEqual(size_of(wide_t), 32)
def test_aligned_true_same_as_natural(self):
"""_aligned_ = True uses natural alignment (max 8)."""
class a_t(struct):
_aligned_ = True
x: c_char
y: c_long
class b_t(struct):
_aligned_ = 8
x: c_char
y: c_long
self.assertEqual(size_of(a_t), size_of(b_t))
self.assertEqual(alignment_of(a_t), alignment_of(b_t))
class AlignmentWithOffsetTest(unittest.TestCase):
def test_explicit_offset_overrides_alignment(self):
"""Explicit offset is respected even if it's not naturally aligned."""
class s_t(struct):
_aligned_ = True
a: c_char
b: c_int = offset(3)
# b should be at offset 3, not rounded up to 4
self.assertEqual(size_of(s_t), 7) # 3 + 4
def test_explicit_offset_read_values(self):
"""Values at explicit offsets in aligned structs are read correctly."""
class s_t(struct):
_aligned_ = True
a: c_char
b: c_int = offset(3)
memory = pystruct.pack("<b", 0x41) + b"\x00\x00" + pystruct.pack("<i", 42)
s = s_t.from_bytes(memory)
self.assertEqual(s.a.value, 0x41)
self.assertEqual(s.b.value, 42)
def test_alignment_resumes_after_explicit_offset(self):
"""Alignment padding resumes for fields after an explicitly-offset field."""
class s_t(struct):
_aligned_ = True
a: c_int = offset(0)
b: c_char = offset(4)
c: c_int # should be at offset 8 (aligned to 4)
self.assertEqual(size_of(s_t), 12) # 4 + 1 + 3 padding + 4
class AlignedStructSerializationTest(unittest.TestCase):
def test_aligned_struct_to_bytes_includes_padding(self):
"""to_bytes on aligned struct includes padding bytes."""
class aligned_t(struct):
_aligned_ = True
a: c_char
b: c_int
memory = pystruct.pack("<b", 0x41) + b"\x00" * 3 + pystruct.pack("<i", 42)
s = aligned_t.from_bytes(memory)
# to_bytes should be 8 bytes (including padding), not 5
self.assertEqual(len(s.to_bytes()), 8)
def test_aligned_struct_to_bytes_round_trip(self):
"""from_bytes(to_bytes()) preserves values for aligned structs."""
class aligned_t(struct):
_aligned_ = True
a: c_char
b: c_int
memory = pystruct.pack("<b", 0x41) + b"\x00" * 3 + pystruct.pack("<i", 42)
s1 = aligned_t.from_bytes(memory)
s2 = aligned_t.from_bytes(s1.to_bytes())
self.assertEqual(s2.a.value, 0x41)
self.assertEqual(s2.b.value, 42)
class ArrayAlignmentTest(unittest.TestCase):
def test_alignment_of_array_field(self):
"""alignment_of(array_of(c_int, N)) should be 4 (element alignment), not 1."""
from libdestruct import array_of
arr = array_of(c_int, 3)
self.assertEqual(alignment_of(arr), 4)
def test_aligned_struct_with_array(self):
"""Array field in aligned struct should be aligned to element alignment."""
from libdestruct import array_of
class s_t(struct):
_aligned_ = True
a: c_char
arr: list[c_int] = array_of(c_int, 3)
# a at 0 (1 byte), padding 3, arr at 4 (12 bytes) = 16, tail padded to 4 = 16
self.assertEqual(size_of(s_t), 16)
def test_aligned_struct_with_array_read(self):
"""Values read correctly from aligned array in struct."""
from libdestruct import array_of
class s_t(struct):
_aligned_ = True
a: c_char
arr: list[c_int] = array_of(c_int, 3)
memory = pystruct.pack("<b", 0x41) + b"\x00" * 3
for v in [10, 20, 30]:
memory += pystruct.pack("<i", v)
s = s_t.from_bytes(memory)
self.assertEqual(s.a.value, 0x41)
self.assertEqual(s.arr[0].value, 10)
self.assertEqual(s.arr[1].value, 20)
self.assertEqual(s.arr[2].value, 30)
class BitfieldAlignmentTest(unittest.TestCase):
def test_bitfield_alignment_padding(self):
"""Bitfield backing type is aligned in aligned structs."""
from libdestruct import bitfield_of
class s_t(struct):
_aligned_ = True
a: c_char
flags: c_int = bitfield_of(c_int, 3)
# c_char (1) + 3 padding + c_int backing (4) = 8
self.assertEqual(size_of(s_t), 8)
def test_bitfield_alignment_read(self):
"""Bitfield values read correctly in aligned structs."""
from libdestruct import bitfield_of
from libdestruct import c_uint
class s_t(struct):
_aligned_ = True
a: c_char
flags: c_uint = bitfield_of(c_uint, 3)
memory = pystruct.pack("<b", 0x41) + b"\x00" * 3 + pystruct.pack("<I", 5)
s = s_t.from_bytes(memory)
self.assertEqual(s.a.value, 0x41)
self.assertEqual(s.flags.value, 5)
class AlignedStructTailPaddingInstanceTest(unittest.TestCase):
"""Instance size must include tail padding for aligned structs."""
def test_instance_size_matches_class_size(self):
"""size_of(instance) should equal size_of(class) for aligned structs."""
class aligned_t(struct):
_aligned_ = True
a: c_int
b: c_char
self.assertEqual(size_of(aligned_t), 8)
memory = pystruct.pack("<ib", 42, 0x41) + b"\x00" * 3
s = aligned_t.from_bytes(memory)
self.assertEqual(size_of(s), 8)
def test_to_bytes_includes_tail_padding(self):
"""to_bytes() should return 8 bytes (including tail padding), not 5."""
class aligned_t(struct):
_aligned_ = True
a: c_int
b: c_char
memory = pystruct.pack("<ib", 42, 0x41) + b"\x00" * 3
s = aligned_t.from_bytes(memory)
self.assertEqual(len(s.to_bytes()), 8)
def test_nested_aligned_struct_tail_padding(self):
"""Nested aligned structs should also have correct tail padding."""
class inner_t(struct):
_aligned_ = True
x: c_int
y: c_char
class outer_t(struct):
_aligned_ = True
a: inner_t
b: c_int
self.assertEqual(size_of(inner_t), 8)
memory = b"\x00" * 16
s = outer_t.from_bytes(memory)
self.assertEqual(size_of(s), size_of(outer_t))
if __name__ == "__main__":
unittest.main()