This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
executable file
·1127 lines (792 loc) · 27.9 KB
/
test.py
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import itertools
import json
import os
import struct
import tempfile
import bitstring
import bread as b
import pytest
# Shared structs for bread struct test
test_struct = [
{
"endianness": b.BIG_ENDIAN
},
("flag_one", b.boolean),
("flag_two", b.boolean),
("flag_three", b.boolean),
("flag_four", b.boolean),
("first", b.uint8),
(b.padding(2),),
b.padding(2),
("blah", b.uint16),
("second", b.int64),
("third", b.uint64),
("fourth", b.int8)
]
test_array_struct = [
{
"endianness": b.BIG_ENDIAN
},
("first", b.uint8),
("flags", b.array(8, b.boolean)),
("last", b.uint8)]
nested_array_struct = [
{
"endianness": b.BIG_ENDIAN
},
("first", b.uint8),
("matrix", b.array(3, b.array(3, b.uint8))),
("last", b.uint8)
]
simple_struct = [
("length", b.uint8),
("ok", b.boolean)
]
offset_struct = [
("length", b.uint8, {"offset": 1})
]
deeply_nested_struct = [
{
"endianness": b.BIG_ENDIAN
},
("ubermatrix", b.array(3, nested_array_struct)),
("dummy", simple_struct)
]
conditional_test = [
("qux", b.boolean),
(b.CONDITIONAL, "qux", {
False: [("fooz", b.byte), ("barz", b.byte)],
True: [("frooz", b.nibble), ("quxz", b.byte)]
})
]
as_native_struct = [
{
"endianness": b.BIG_ENDIAN
},
("ubermatrix", b.array(3, nested_array_struct)),
("dummy", simple_struct),
b.padding(7)
]
def test_simple_struct():
data = struct.pack(">IqQb", 0xafb0dddd, -57, 90, 0)
test = b.parse(data, spec=test_struct)
assert test.__offsets__.flag_one == 0
assert test.__offsets__.flag_two == 1
assert test.__offsets__.flag_three == 2
assert test.__offsets__.flag_four == 3
assert test.__offsets__.first == 4
assert test.__offsets__.blah == 16
assert test.__offsets__.second == 32
assert test.__offsets__.third == 96
assert test.__offsets__.fourth == 160
assert len(test) == 168
assert test.flag_one
assert not test.flag_two
assert test.flag_three
assert not test.flag_four
assert test.first == 0xfb
assert test.blah == 0xdddd
assert test.second == -57
assert test.third == 90
assert test.fourth == 0
output_data = b.write(test, test_struct)
assert output_data == data
expected_json_struct = {
"flag_one": True,
"flag_two": False,
"flag_three": True,
"flag_four": False,
"first": 0xfb,
"blah": 0xdddd,
"second": -57,
"third": 90,
"fourth": 0
}
assert json.loads(test.as_json()) == expected_json_struct
def test_write_intX():
ints_struct = [
("off_by_one", b.uint8, {"offset": 1}),
("unsigned_int", b.uint16),
("signed_int", b.int8)
]
data = bytearray([5, 0xba, 0xbd, 0xed])
parsed = b.parse(data, ints_struct)
assert parsed.off_by_one == 6
assert parsed.unsigned_int == 0xbdba
assert parsed.signed_int == -19
parsed.off_by_one = 9
parsed.unsigned_int = 0xcbab
parsed.signed_int = -7
output = b.write(parsed)
assert output == bytearray([8, 0xab, 0xcb, 0xf9])
def test_updates_do_not_leak():
data = struct.pack(">IqQb", 0xafb3dddd, -57, 90, 0)
data2 = struct.pack(">IqQb", 0x1de0fafe, 24, 999999, 1)
test = b.parse(data, test_struct)
test2 = b.parse(data2, test_struct)
# test2's offsets should be the same as test's
assert test2.__offsets__.flag_one == 0
assert test2.__offsets__.flag_two == 1
assert test2.__offsets__.flag_three == 2
assert test2.__offsets__.flag_four == 3
assert test2.__offsets__.first == 4
assert test2.__offsets__.blah == 16
assert test2.__offsets__.second == 32
assert test2.__offsets__.third == 96
assert test2.__offsets__.fourth == 160
assert len(test2) == 168
assert not test2.flag_one
assert not test2.flag_two
assert not test2.flag_three
assert test2.flag_four
assert test2.first == 0xde
assert test2.blah == 0xfafe
assert test2.second == 24
assert test2.third == 999999
assert test2.fourth == 1
# Updating test2 shouldn't impact test
assert test.flag_one
assert not test.flag_two
assert test.flag_three
assert not test.flag_four
assert test.first == 0xfb
assert test.blah == 0xdddd
assert test.second == -57
assert test.third == 90
assert test.fourth == 0
def test_array():
data = bytearray([0b11111111, 0b10010101, 0b00010001])
array_test = b.parse(data, test_array_struct)
assert array_test.__offsets__.first == 0
assert array_test.__offsets__.flags == 8
assert array_test.__offsets__.last == 16
assert len(array_test) == 24
expected_flags = [True, False, False, True, False, True, False, True]
assert array_test.flags == expected_flags
assert b.write(array_test, test_array_struct) == data
def test_nested_array():
data = bytearray([42, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0xdb])
nested_test = b.parse(data, nested_array_struct)
assert nested_test.__offsets__.first == 0
assert nested_test.__offsets__.matrix == 8
assert nested_test.__offsets__.last == 80
assert len(nested_test) == 88
assert nested_test.first == 42
for i in range(9):
assert nested_test.matrix[int(i / 3)][int(i % 3)] == i
assert nested_test.last == 0xdb
assert b.write(nested_test, nested_array_struct) == data
expected_json_struct = {
"first": 42,
"matrix": [[0, 1, 2], [3, 4, 5], [6, 7, 8]],
"last": 0xdb
}
assert json.loads(nested_test.as_json()) == expected_json_struct
def test_nested_struct():
data = bitstring.BitArray(bytearray(range(34)))
data.append('0b0')
supernested_test = b.parse(data, deeply_nested_struct)
assert supernested_test.__offsets__.ubermatrix == 0
assert supernested_test.__offsets__.dummy == 264
assert len(supernested_test) == 273
assert len(supernested_test.ubermatrix) == 3
assert sum(map(len, supernested_test.ubermatrix)) == 264
current_byte = 0
for substruct in supernested_test.ubermatrix:
assert substruct.first == current_byte
current_byte += 1
for i, j in itertools.product(range(3), range(3)):
assert substruct.matrix[i][j] == current_byte + i * 3 + j
current_byte += 9
assert substruct.last == current_byte
current_byte += 1
assert supernested_test.__offsets__.dummy == current_byte * 8
current_byte += 1
assert not supernested_test.dummy.ok
assert b.write(supernested_test, deeply_nested_struct) == bytearray(list(range(34)) + [0b0])
expected_json_struct = {
"dummy": {
"length": 33,
"ok": False
},
"ubermatrix": [
{
"first": 0,
"matrix": [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
"last": 10
},
{
"first": 11,
"matrix": [[12, 13, 14], [15, 16, 17], [18, 19, 20]],
"last": 21
},
{
"first": 22,
"matrix": [[23, 24, 25], [26, 27, 28], [29, 30, 31]],
"last": 32
}
]
}
assert json.loads(supernested_test.as_json()) == expected_json_struct
def test_single_byte_fields():
single_byte_fields_struct = [
("bit_0", b.bit),
("bit_1", b.bit),
("semi_nibble", b.semi_nibble),
("nibble", b.nibble)]
data = bytearray([0b10110010])
test = b.parse(data, single_byte_fields_struct)
assert test.bit_0 == 1
assert test.bit_1 == 0
assert test.semi_nibble == 0b11
assert test.nibble == 0b0010
assert b.write(test, single_byte_fields_struct) == data
def test_endianness():
endianness_test = [
("big_endian", b.uint32, {"endianness": b.BIG_ENDIAN}),
("little_endian", b.uint32, {"endianness": b.LITTLE_ENDIAN}),
("default_endian", b.uint32)]
data = bytearray([0x01, 0x02, 0x03, 0x04] * 3)
test = b.parse(data, endianness_test)
assert test.big_endian == 0x01020304
assert test.little_endian == 0x04030201
assert hex(test.default_endian) == hex(test.little_endian)
assert b.write(test, endianness_test) == data
def test_conditional():
true_data = bitstring.BitArray(bytearray([0b11001010, 0b11101000]))
true_data.append('0b0')
true_test = b.parse(true_data, conditional_test)
assert true_test._length == 13
assert true_test.qux
assert hasattr(true_test, "frooz")
assert not hasattr(true_test, "fooz")
assert true_test.frooz == 0b1001
assert true_test.quxz == 0b01011101
written_bytes = b.write(true_test, conditional_test)
expected_bytes = bytearray([0b11001010, 0b11101000])
assert written_bytes == expected_bytes
false_data = bitstring.BitArray(
bytearray([0b01001000, 0b10000000]))
false_data.append('0b1')
false_test = b.parse(false_data, conditional_test)
assert false_test._length == 17
assert not false_test.qux
assert hasattr(false_test, "fooz")
assert not hasattr(false_test, "frooz")
assert false_test.fooz == 0b10010001
assert false_test.barz == 1
written_bytes = b.write(false_test, conditional_test)
expected_bytes = bytearray([0b01001000, 0b10000000, 0b10000000])
assert written_bytes == expected_bytes
def test_conditional_as_native():
true_data = bitstring.BitArray(bytearray([0b11001010, 0b11101000]))
true_data.append('0b0')
test_struct = b.parse(true_data, conditional_test)
assert test_struct.as_native() == {
'qux': True,
'frooz': 0b1001,
'quxz': 0b01011101
}
test_struct.qux = False
assert test_struct.as_native() == {
'qux': False,
'fooz': 0b10010101,
'barz': 0b11010000
}
def test_conditional_set():
true_data = bitstring.BitArray(bytearray([0b11001010, 0b11101000]))
true_data.append('0b0')
test_struct = b.parse(true_data, conditional_test)
assert test_struct.frooz == 0b1001
assert test_struct.quxz == 0b01011101
assert test_struct.qux
assert not hasattr(test_struct, "fooz")
assert not hasattr(test_struct, "barz")
test_struct.qux = False
assert not test_struct.qux
assert test_struct.fooz == 0b10010101
assert test_struct.barz == 0b11010000
assert not hasattr(test_struct, "frooz")
assert not hasattr(test_struct, "quxz")
test_struct.barz = 0b11101010
written_bytes = b.write(test_struct)
expected_bytes = bytearray([0b01001010, 0b11110101, 0b0])
assert written_bytes == expected_bytes
def test_conditional_bad_switch():
with pytest.raises(b.BadConditionalCaseError):
test_struct = [
("cond", b.uint8),
(b.CONDITIONAL, "cond", {
1: [("foo", b.uint8)],
2: [("foo", b.uint8)],
4: [("foo", b.uint8)]
})
]
test_data = bytearray([3, 9])
test_parsed = b.parse(test_data, test_struct)
test_parsed.foo = 12
def test_as_native():
data = bitstring.BitArray(bytearray(range(35)))
supernested_test = b.parse(data, as_native_struct)
supernested_test.as_native() == {
'ubermatrix': [
{
'first': 0,
'matrix': [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
'last': 10
},
{
'first': 11,
'matrix': [[12, 13, 14], [15, 16, 17], [18, 19, 20]],
'last': 21
},
{
'first': 22,
'matrix': [[23, 24, 25], [26, 27, 28], [29, 30, 31]],
'last': 32
}
],
'dummy': {
'length': 33,
'ok': False
}
}
def test_array_of_conditionals():
test_struct = [
("cond", b.uint8),
("foos", b.array(3, (b.CONDITIONAL, "cond", {
1: [("foo", b.nibble), b.padding(4)],
2: [("bar", b.bit), b.padding(7)],
4: [("baz", b.semi_nibble), b.padding(6)]
})))
]
test_data = bytearray([1, 0b10000101, 0b01010110, 0b11010101])
test_parsed = b.parse(test_data, test_struct)
assert test_parsed.cond == 1
assert test_parsed.foos[0].foo == 0b1000
assert test_parsed.foos[1].foo == 0b0101
assert test_parsed.foos[2].foo == 0b1101
assert test_parsed._length == 32
test_parsed.cond = 4
assert test_parsed.cond == 4
assert test_parsed.foos[0].baz == 0b10
assert test_parsed.foos[1].baz == 0b01
assert test_parsed.foos[2].baz == 0b11
assert test_parsed._length == 32
def test_modifying_conditional_with_structs_that_have_different_lengths():
true_data = bitstring.BitArray(bytearray([0b11001010, 0b11101000]))
true_data.append('0b0')
true_test = b.parse(true_data, conditional_test)
true_test.qux = False
assert true_test._length == 17
assert true_test.fooz == 0b10010101
def test_field_properties_in_array():
array_endian_test = [
("little_arr", b.array(3, b.uint16), {"endianness": b.LITTLE_ENDIAN}),
("big_arr", b.array(3, b.uint16), {"endianness": b.BIG_ENDIAN})
]
data = bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06])
test = b.parse(data, array_endian_test)
assert len(test.little_arr) == 3
assert test.little_arr[0] == 0x0201
assert test.little_arr[1] == 0x0403
assert test.little_arr[2] == 0x0605
assert len(test.big_arr) == 3
assert test.big_arr[0] == 0x0102
assert test.big_arr[1] == 0x0304
assert test.big_arr[2] == 0x0506
def test_set_non_leaf_value_fails():
with pytest.raises(ValueError):
struct_in_a_struct = [
("simple", [
("fooz", b.uint8),
("mooz", b.uint8),
("shooz", b.uint8)
])
]
data = bitstring.BitArray(bytearray([1, 2, 3]))
nested_set_test = b.parse(data, struct_in_a_struct)
nested_set_test.simple = 5
def test_multiple_conditionals():
test_struct = [
("cond", b.uint8),
(b.CONDITIONAL, "cond", {
1: [("foo", b.uint8)],
2: [("foo", b.uint8)],
4: [("foo", b.uint8)]
}),
(b.CONDITIONAL, "cond", {
1: [("qux", b.uint8)],
2: [("buzz", b.uint8)],
4: [("fuzz", b.uint8)]
})
]
test_data = bytearray([1, 2, 4])
test_parsed = b.parse(test_data, test_struct)
assert test_parsed.foo == 2
assert test_parsed.qux == 4
def test_set_sub_byte_intX():
test_struct = [
("signed_nibble", b.intX(4, signed=True)),
("bit1", b.bit),
("bit2", b.bit),
("seminibble", b.semi_nibble)
]
test_data = bytearray([0xdb])
test_parsed = b.parse(test_data, test_struct)
assert test_parsed.signed_nibble == -3
test_parsed.signed_nibble = -6
test_parsed.bit1 = 0
test_parsed.seminibble = 2
assert bytearray([0xa2]) == b.write(test_parsed)
def test_parse_str():
test_struct = [
("str", b.string(13))
]
test_str = str("gabbagabbahey")
test_parsed = b.parse(test_str, test_struct)
assert test_parsed.str == "gabbagabbahey"
def test_str():
str_test = [("msg", b.string(5))]
data = bytearray([0x68, 0x65, 0x6c, 0x6c, 0x6f])
result = b.parse(data, str_test)
assert result.msg == "hello"
assert b.write(result, str_test) == data
assert result.as_json() == json.dumps({'msg': 'hello'})
def test_str_unicode():
str_test = [("msg", b.string(5))]
data = bytearray([104, 101, 108, 108, 111])
result = b.parse(data, str_test)
assert result.msg == "hello"
assert b.write(result, str_test) == data
result.msg = "abate"
output_data = b.write(result, str_test)
edited_result = b.parse(output_data, str_test)
assert edited_result.msg == "abate"
def test_enum():
enum_test = [
("suit", b.enum(8, {
0: "diamonds",
1: "hearts",
2: "spades",
3: "clubs"
}))]
for value, suit in zip(
list(range(4)), ["diamonds", "hearts", "spades", "clubs"]):
data = bytearray([value])
result = b.parse(data, enum_test)
assert result.suit == suit
assert b.write(result, enum_test) == data
spades_test = b.parse([2], enum_test)
spades_test.suit = "clubs"
assert bytearray([3]) == b.write(spades_test)
def get_data_field():
data = bytearray([42])
result = b.parse(data, enum_test)
result.suit
with pytest.raises(ValueError):
get_data_field()
def test_enum_default():
enum_test = [
("suit", b.enum(8, {
0: "diamonds",
1: "hearts",
2: "spades",
3: "clubs"
}, default="joker"))]
data = bytearray([42])
result = b.parse(data, enum_test)
assert result.suit == "joker"
data = bytearray([2])
result = b.parse(data, enum_test)
assert result.suit == "spades"
def test_enum_multiple_values_same_key():
enum_test = [
('waveform', b.enum(8, {
0: 'triangle',
1: 'saw down',
2: 'saw up',
3: 'square',
4: 'sine',
(5, 7): 'sample and hold'
}))
]
data = bytearray([7])
result = b.parse(data, enum_test)
assert result.waveform == 'sample and hold'
dumped = b.write(result, enum_test)
assert dumped == bytearray([7])
data = bytearray([5])
result = b.parse(data, enum_test)
assert result.waveform == 'sample and hold'
data = bytearray([2])
result = b.parse(data, enum_test)
assert result.waveform == 'saw up'
test_struct = b.new(enum_test)
test_struct.waveform = 'sample and hold'
dumped = b.write(test_struct, enum_test)
assert dumped == bytearray([5])
def test_enum_set_invalid_value():
with pytest.raises(ValueError):
enum_test = [
("suit", b.enum(8, {
0: "diamonds",
1: "hearts",
2: "spades",
3: "clubs"
}, default="joker"))]
data = bytearray([1])
parsed = b.parse(data, enum_test)
assert "hearts" == parsed.suit
parsed.suit = "skulls"
def test_conditional_on_non_integer_enum():
enum_test = [
("instrument_type", b.enum(8, {
0: "pulse",
1: "wave",
2: "kit",
3: "noise"
})),
(b.CONDITIONAL, "instrument_type", {
"pulse": [("pulse_foo", b.uint8)],
"wave": [("wave_foo", b.uint8)],
"kit": [("kit_foo", b.uint8)],
"noise": [("noise_foo", b.uint8)]
})]
pulse_test = bytearray([0, 19])
pulse = b.parse(pulse_test, enum_test)
assert pulse.instrument_type == "pulse"
assert pulse.pulse_foo == 19
assert b.write(pulse, enum_test) == pulse_test
wave_test = bytearray([1, 65])
wave = b.parse(wave_test, enum_test)
assert wave.instrument_type == "wave"
assert wave.wave_foo == 65
assert b.write(wave, enum_test) == wave_test
kit_test = bytearray([2, 9])
kit = b.parse(kit_test, enum_test)
assert kit.instrument_type == "kit"
assert kit.kit_foo == 9
assert b.write(kit, enum_test) == kit_test
noise_test = bytearray([3, 17])
noise = b.parse(noise_test, enum_test)
assert noise.instrument_type == "noise"
assert noise.noise_foo == 17
assert b.write(noise, enum_test) == noise_test
def test_non_powers_of_eight_intX():
intX_test = [
("unsigned_10b", b.intX(10, False)),
("unsigned_14b", b.intX(14, False)),
("signed_20b", b.intX(20, True)),
("signed_4b", b.intX(4, True)),
]
in_bytes = bytearray([
0b11010101, 0b11101010, 0b00110101, 0b11010101, 0b11101010, 0b00110101])
result = b.parse(in_bytes, intX_test)
assert result.unsigned_10b == 0b1101010111
assert result.unsigned_14b == 0b10101000110101
assert result.signed_20b == - 0b101010000101011101
assert result.signed_4b == 0b0101
assert b.write(result, intX_test) == in_bytes
def test_read_modify_write():
data = bitstring.BitArray(bytearray(range(34)))
data.append('0b0')
supernested_test = b.parse(data, deeply_nested_struct)
assert supernested_test.ubermatrix[1].matrix[2][1] == 19
supernested_test.ubermatrix[1].matrix[2][1] = 42
assert supernested_test.ubermatrix[1].matrix[2][1] == 42
written_data = b.write(supernested_test, deeply_nested_struct)
re_read_data = b.parse(written_data, deeply_nested_struct)
assert re_read_data.ubermatrix[1].matrix[2][1] == 42
def test_read_modify_write_with_offset():
data = bytearray([4])
parsed = b.parse(data, offset_struct)
assert parsed.length == 5
output = b.write(parsed, offset_struct)
assert output == data
parsed.length = 10
output = b.write(parsed, offset_struct)
assert output[0] == 9
def test_file_io():
data = bytearray(list(range(36)))
supernested_test = b.parse(data, deeply_nested_struct)
(handle, file_path) = tempfile.mkstemp()
try:
b.write(supernested_test, deeply_nested_struct, filename=file_path)
with open(file_path, 'rb') as fp:
supernested_test_from_file = b.parse(fp, deeply_nested_struct)
for i, j, k in itertools.product(range(3), range(3), range(3)):
assert supernested_test_from_file.ubermatrix[i].matrix[j][k] == supernested_test.ubermatrix[i].matrix[j][k]
finally:
os.close(handle)
os.unlink(file_path)
def test_comparison():
data = struct.pack(">IqQb", 0xafb0dddd, -57, 90, 0)
obj_1 = b.parse(data, spec=test_struct)
obj_2 = b.parse(data, spec=test_struct)
assert obj_1 == obj_2
obj_2.flag_four = not obj_1.flag_four
assert obj_1 != obj_2
obj_2.flag_four = obj_1.flag_four
assert obj_1 == obj_2
def test_invalid_field_get_raises():
with pytest.raises(AttributeError):
data = struct.pack(">IqQb", 0xafb0dddd, -57, 90, 0)
test = b.parse(data, spec=test_struct)
test.missingfield
def test_invalid_field_set_raises():
with pytest.raises(AttributeError):
data = struct.pack(">IqQb", 0xafb0dddd, -57, 90, 0)
test = b.parse(data, spec=test_struct)
test.missingfield = 12
def test_too_small_struct_fails():
with pytest.raises(ValueError):
data = "X".encode('utf-8')
b.parse(data, spec=simple_struct)
def test_bad_type_fails():
with pytest.raises(ValueError):
data = struct.pack(">IqQb", 0xafb0dddd, -57, 90, 0)
test = b.parse(data, spec=test_struct)
test.flag_four = 50
def test_compare_struct_to_nonstruct_returns_false():
data = struct.pack(">IqQb", 0xafb0dddd, -57, 90, 0)
test = b.parse(data, spec=test_struct)
assert test != 75
def test_set_array_to_nonarray_fails():
with pytest.raises(ValueError):
data = bytearray([42, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0xdb])
nested_test = b.parse(data, nested_array_struct)
nested_test.matrix = 46
def test_set_array_to_list():
data = bytearray([42, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0xdb])
nested_test = b.parse(data, nested_array_struct)
nested_test.matrix = [[2, 4, 6], [8, 10, 12], [14, 16, 18]]
output_bytes = b.write(nested_test)
assert output_bytes == bytearray([42, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0xdb])
nested_test.matrix[1] = [9, 8, 7]
output_bytes = b.write(nested_test)
assert output_bytes == bytearray([42, 2, 4, 6, 9, 8, 7, 14, 16, 18, 0xdb])
def assign_wrong_length_array():
nested_test.matrix[1] = [9, 8, 7, 6]
with pytest.raises(ValueError):
assign_wrong_length_array()
def test_array_eq():
first_test_struct = [("nums", b.array(3, b.uint8))]
first_test_data = bytearray([2, 4, 6])
second_test_struct = [("nums", b.array(4, b.uint8))]
second_test_data = bytearray([2, 4, 6, 8])
first_test_parsed = b.parse(first_test_data, first_test_struct)
second_test_parsed = b.parse(second_test_data, second_test_struct)
assert first_test_parsed == first_test_parsed
assert first_test_parsed != second_test_parsed
first_test_parsed_copy = b.parse(first_test_data, first_test_struct)
assert first_test_parsed.nums == first_test_parsed_copy.nums
first_test_parsed_copy.nums[2] = 100
assert first_test_parsed != first_test_parsed_copy
assert first_test_parsed.nums != first_test_parsed_copy.nums
def test_printable_str():
data = bytearray([42, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0xdb])
nested_test = b.parse(data, nested_array_struct)
assert str(nested_test) == """{
first: 42
matrix: [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
last: 219
}"""
def test_nested_struct_str():
data = bitstring.BitArray(bytearray(range(35)))
supernested_test = b.parse(data, as_native_struct)
expected = '\n'.join([
'{',
' ubermatrix: [',
' {',
' first: 0',
' matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]',
' last: 10', ' }, ',