-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Expand file tree
/
Copy pathtest_edtlib.py
More file actions
1242 lines (1037 loc) · 50.1 KB
/
test_edtlib.py
File metadata and controls
1242 lines (1037 loc) · 50.1 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
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
# Copyright (c) 2019 Nordic Semiconductor ASA
# SPDX-License-Identifier: BSD-3-Clause
import contextlib
from copy import deepcopy
import io
from logging import WARNING
import os
from pathlib import Path
import textwrap
import pytest
from devicetree import edtlib
# Test suite for edtlib.py.
#
# Run it using pytest (https://docs.pytest.org/en/stable/usage.html):
#
# $ pytest testedtlib.py
#
# See the comment near the top of testdtlib.py for additional pytest advice.
#
# test.dts is the main test file. test-bindings/ and test-bindings-2/ has
# bindings. The tests mostly use string comparisons via the various __repr__()
# methods.
HERE = os.path.dirname(__file__)
@contextlib.contextmanager
def from_here():
# Convenience hack to minimize diff from zephyr.
cwd = os.getcwd()
try:
os.chdir(HERE)
yield
finally:
os.chdir(cwd)
def hpath(filename):
'''Convert 'filename' to the host path syntax.'''
return os.fspath(Path(filename))
def test_warnings(caplog):
'''Tests for situations that should cause warnings.'''
with from_here(): edtlib.EDT("test.dts", ["test-bindings"])
enums_hpath = hpath('test-bindings/enums.yaml')
expected_warnings = [
f"'oldprop' is marked as deprecated in 'properties:' in '{hpath('test-bindings/deprecated.yaml')}' for node /test-deprecated.",
"unit address and first address in 'reg' (0x1) don't match for /reg-zero-size-cells/node",
"unit address and first address in 'reg' (0x5) don't match for /reg-ranges/parent/node",
"unit address and first address in 'reg' (0x30000000200000001) don't match for /reg-nested-ranges/grandparent/parent/node",
f"compatible 'enums' in binding '{enums_hpath}' has non-tokenizable enum for property 'string-enum': 'foo bar', 'foo_bar'",
f"compatible 'enums' in binding '{enums_hpath}' has enum for property 'tokenizable-lower-enum' that is only tokenizable in lowercase: 'bar', 'BAR'",
]
assert caplog.record_tuples == [('devicetree.edtlib', WARNING, warning_message)
for warning_message in expected_warnings]
def test_interrupts():
'''Tests for the interrupts property.'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
node = edt.get_node("/interrupt-parent-test/node")
controller = edt.get_node('/interrupt-parent-test/controller')
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=controller, data={'one': 1, 'two': 2, 'three': 3}, name='foo', basename=None),
edtlib.ControllerAndData(node=node, controller=controller, data={'one': 4, 'two': 5, 'three': 6}, name='bar', basename=None)
]
node = edt.get_node("/interrupts-extended-test/node")
controller_0 = edt.get_node('/interrupts-extended-test/controller-0')
controller_1 = edt.get_node('/interrupts-extended-test/controller-1')
controller_2 = edt.get_node('/interrupts-extended-test/controller-2')
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=controller_0, data={'one': 1}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_1, data={'one': 2, 'two': 3}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_2, data={'one': 4, 'two': 5, 'three': 6}, name=None, basename=None)
]
node = edt.get_node("/interrupt-map-test/node@0")
controller_0 = edt.get_node('/interrupt-map-test/controller-0')
controller_1 = edt.get_node('/interrupt-map-test/controller-1')
controller_2 = edt.get_node('/interrupt-map-test/controller-2')
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=controller_0, data={'one': 0}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_1, data={'one': 0, 'two': 1}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_2, data={'one': 0, 'two': 0, 'three': 2}, name=None, basename=None)
]
node = edt.get_node("/interrupt-map-test/node@1")
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=controller_0, data={'one': 3}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_1, data={'one': 0, 'two': 4}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_2, data={'one': 0, 'two': 0, 'three': 5}, name=None, basename=None)
]
node = edt.get_node("/interrupt-map-test/node@2")
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=controller_0, data={'one': 0}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_1, data={'one': 0, 'two': 1}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_2, data={'one': 0, 'two': 0, 'three': 2}, name=None, basename=None)
]
node = edt.get_node("/interrupt-map-test/node@3")
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=controller_0, data={'one': 0}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_1, data={'one': 0, 'two': 1}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_2, data={'one': 0, 'two': 0, 'three': 2}, name=None, basename=None)
]
node = edt.get_node("/interrupt-map-test/node@4")
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=controller_0, data={'one': 3}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_1, data={'one': 0, 'two': 4}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_2, data={'one': 0, 'two': 0, 'three': 5}, name=None, basename=None)
]
node = edt.get_node("/interrupt-map-test/node@100000004")
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=controller_0, data={'one': 3}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_1, data={'one': 0, 'two': 4}, name=None, basename=None),
edtlib.ControllerAndData(node=node, controller=controller_2, data={'one': 0, 'two': 0, 'three': 5}, name=None, basename=None)
]
node = edt.get_node("/interrupt-map-bitops-test/node@70000000e")
assert node.interrupts == [
edtlib.ControllerAndData(node=node, controller=edt.get_node('/interrupt-map-bitops-test/controller'), data={'one': 3, 'two': 2}, name=None, basename=None)
]
def test_maps():
'''Tests for the maps property.'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
nexus = edt.get_node("/interrupt-map-test/nexus")
controller_0 = edt.get_node("/interrupt-map-test/controller-0")
controller_1 = edt.get_node("/interrupt-map-test/controller-1")
controller_2 = edt.get_node("/interrupt-map-test/controller-2")
controller_no_addr = edt.get_node("/interrupt-map-no-address/controller")
assert len(nexus.maps.keys()) == 1
assert "interrupt" in nexus.maps
entries = nexus.maps["interrupt"]
assert len(entries) == 7
assert entries[0] == edtlib.MapEntry(
node=nexus,
child_addresses=[0, 0],
child_specifiers=[0, 0],
parent=controller_0,
parent_addresses=[0],
parent_specifiers=[0],
basename="interrupt",
)
assert entries[1] == edtlib.MapEntry(
node=nexus,
child_addresses=[0, 0],
child_specifiers=[0, 1],
parent=controller_1,
parent_addresses=[0, 0],
parent_specifiers=[0, 1],
basename="interrupt",
)
assert entries[2] == edtlib.MapEntry(
node=nexus,
child_addresses=[0, 0],
child_specifiers=[0, 2],
parent=controller_2,
parent_addresses=[0, 0, 0],
parent_specifiers=[0, 0, 2],
basename="interrupt",
)
assert entries[3] == edtlib.MapEntry(
node=nexus,
child_addresses=[0, 1],
child_specifiers=[0, 0],
parent=controller_0,
parent_addresses=[0],
parent_specifiers=[3],
basename="interrupt",
)
assert entries[4] == edtlib.MapEntry(
node=nexus,
child_addresses=[0, 1],
child_specifiers=[0, 1],
parent=controller_1,
parent_addresses=[0, 0],
parent_specifiers=[0, 4],
basename="interrupt",
)
assert entries[5] == edtlib.MapEntry(
node=nexus,
child_addresses=[0, 1],
child_specifiers=[0, 2],
parent=controller_2,
parent_addresses=[0, 0, 0],
parent_specifiers=[0, 0, 5],
basename="interrupt",
)
assert entries[6] == edtlib.MapEntry(
node=nexus,
child_addresses=[0, 1],
child_specifiers=[1, 0],
parent=controller_no_addr,
parent_addresses=[],
parent_specifiers=[6],
basename="interrupt",
)
empty = edt.get_node("/interrupt-map-test/empty")
assert len(empty.maps) == 1
assert "interrupt" in empty.maps
assert len(empty.maps["interrupt"]) == 0
def test_ranges():
'''Tests for the ranges property'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
node = edt.get_node("/reg-ranges/parent")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x1, parent_bus_cells=0x2, parent_bus_addr=0xa0000000b, length_cells=0x1, length=0x1),
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x2, parent_bus_cells=0x2, parent_bus_addr=0xc0000000d, length_cells=0x1, length=0x2),
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x4, parent_bus_cells=0x2, parent_bus_addr=0xe0000000f, length_cells=0x1, length=0x1)
]
node = edt.get_node("/reg-nested-ranges/grandparent")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x2, child_bus_addr=0x0, parent_bus_cells=0x3, parent_bus_addr=0x30000000000000000, length_cells=0x2, length=0x200000002)
]
node = edt.get_node("/reg-nested-ranges/grandparent/parent")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x0, parent_bus_cells=0x2, parent_bus_addr=0x200000000, length_cells=0x1, length=0x2)
]
assert edt.get_node("/ranges-zero-cells/node").ranges == []
node = edt.get_node("/ranges-zero-parent-cells/node")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0xa, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x0, length=None),
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x1a, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x0, length=None),
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x2a, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x0, length=None)
]
node = edt.get_node("/ranges-one-address-cells/node")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0xa, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x1, length=0xb),
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x1a, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x1, length=0x1b),
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x2a, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x1, length=0x2b)
]
node = edt.get_node("/ranges-one-address-two-size-cells/node")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0xa, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x2, length=0xb0000000c),
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x1a, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x2, length=0x1b0000001c),
edtlib.Range(node=node, child_bus_cells=0x1, child_bus_addr=0x2a, parent_bus_cells=0x0, parent_bus_addr=None, length_cells=0x2, length=0x2b0000002c)
]
node = edt.get_node("/ranges-two-address-cells/node@1")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x2, child_bus_addr=0xa0000000b, parent_bus_cells=0x1, parent_bus_addr=0xc, length_cells=0x1, length=0xd),
edtlib.Range(node=node, child_bus_cells=0x2, child_bus_addr=0x1a0000001b, parent_bus_cells=0x1, parent_bus_addr=0x1c, length_cells=0x1, length=0x1d),
edtlib.Range(node=node, child_bus_cells=0x2, child_bus_addr=0x2a0000002b, parent_bus_cells=0x1, parent_bus_addr=0x2c, length_cells=0x1, length=0x2d)
]
node = edt.get_node("/ranges-two-address-two-size-cells/node@1")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x2, child_bus_addr=0xa0000000b, parent_bus_cells=0x1, parent_bus_addr=0xc, length_cells=0x2, length=0xd0000000e),
edtlib.Range(node=node, child_bus_cells=0x2, child_bus_addr=0x1a0000001b, parent_bus_cells=0x1, parent_bus_addr=0x1c, length_cells=0x2, length=0x1d0000001e),
edtlib.Range(node=node, child_bus_cells=0x2, child_bus_addr=0x2a0000002b, parent_bus_cells=0x1, parent_bus_addr=0x2c, length_cells=0x2, length=0x2d0000001d)
]
node = edt.get_node("/ranges-three-address-cells/node@1")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x3, child_bus_addr=0xa0000000b0000000c, parent_bus_cells=0x2, parent_bus_addr=0xd0000000e, length_cells=0x1, length=0xf),
edtlib.Range(node=node, child_bus_cells=0x3, child_bus_addr=0x1a0000001b0000001c, parent_bus_cells=0x2, parent_bus_addr=0x1d0000001e, length_cells=0x1, length=0x1f),
edtlib.Range(node=node, child_bus_cells=0x3, child_bus_addr=0x2a0000002b0000002c, parent_bus_cells=0x2, parent_bus_addr=0x2d0000002e, length_cells=0x1, length=0x2f)
]
node = edt.get_node("/ranges-three-address-two-size-cells/node@1")
assert node.ranges == [
edtlib.Range(node=node, child_bus_cells=0x3, child_bus_addr=0xa0000000b0000000c, parent_bus_cells=0x2, parent_bus_addr=0xd0000000e, length_cells=0x2, length=0xf00000010),
edtlib.Range(node=node, child_bus_cells=0x3, child_bus_addr=0x1a0000001b0000001c, parent_bus_cells=0x2, parent_bus_addr=0x1d0000001e, length_cells=0x2, length=0x1f00000110),
edtlib.Range(node=node, child_bus_cells=0x3, child_bus_addr=0x2a0000002b0000002c, parent_bus_cells=0x2, parent_bus_addr=0x2d0000002e, length_cells=0x2, length=0x2f00000210)
]
def test_reg():
'''Tests for the regs property'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
def verify_regs(node, expected_tuples):
regs = node.regs
assert len(regs) == len(expected_tuples)
for reg, expected_tuple in zip(regs, expected_tuples):
name, addr, size = expected_tuple
assert reg.node is node
assert reg.name == name
assert reg.addr == addr
assert reg.size == size
verify_regs(edt.get_node("/reg-zero-address-cells/node"),
[('foo', None, 0x1),
('bar', None, 0x2)])
verify_regs(edt.get_node("/reg-zero-size-cells/node"),
[(None, 0x1, None),
(None, 0x2, None)])
verify_regs(edt.get_node("/reg-ranges/parent/node"),
[(None, 0x5, 0x1),
(None, 0xe0000000f, 0x1),
(None, 0xc0000000e, 0x1),
(None, 0xc0000000d, 0x1),
(None, 0xa0000000b, 0x1),
(None, 0x0, 0x1)])
verify_regs(edt.get_node("/reg-nested-ranges/grandparent/parent/node"),
[(None, 0x30000000200000001, 0x1)])
def test_pinctrl():
'''Test 'pinctrl-<index>'.'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
node = edt.get_node("/pinctrl/dev")
state_1 = edt.get_node('/pinctrl/pincontroller/state-1')
state_2 = edt.get_node('/pinctrl/pincontroller/state-2')
assert node.pinctrls == [
edtlib.PinCtrl(node=node, name='zero', conf_nodes=[]),
edtlib.PinCtrl(node=node, name='one', conf_nodes=[state_1]),
edtlib.PinCtrl(node=node, name='two', conf_nodes=[state_1, state_2])
]
def test_hierarchy():
'''Test Node.parent and Node.children'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
assert edt.get_node("/").parent is None
assert str(edt.get_node("/parent/child-1").parent) == \
"<Node /parent in 'test.dts', no binding>"
assert str(edt.get_node("/parent/child-2/grandchild").parent) == \
"<Node /parent/child-2 in 'test.dts', no binding>"
assert str(edt.get_node("/parent").children) == \
"{'child-1': <Node /parent/child-1 in 'test.dts', no binding>, 'child-2': <Node /parent/child-2 in 'test.dts', no binding>}"
assert edt.get_node("/parent/child-1").children == {}
def test_child_index():
'''Test Node.child_index.'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
parent, child_1, child_2 = [edt.get_node(path) for path in
("/parent",
"/parent/child-1",
"/parent/child-2")]
assert parent.child_index(child_1) == 0
assert parent.child_index(child_2) == 1
with pytest.raises(KeyError):
parent.child_index(parent)
def test_include():
'''Test 'include:' and the legacy 'inherits: !include ...' in bindings'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
binding_include = edt.get_node("/binding-include")
assert binding_include.description == "Parent binding"
verify_props(binding_include,
['foo', 'bar', 'baz', 'qaz'],
['int', 'int', 'int', 'int'],
[0, 1, 2, 3])
verify_props(edt.get_node("/binding-include/child"),
['foo', 'bar', 'baz', 'qaz'],
['int', 'int', 'int', 'int'],
[0, 1, 2, 3])
def test_include_filters():
'''Test property-allowlist and property-blocklist in an include.'''
fname2path = {'include.yaml': 'test-bindings-include/include.yaml',
'include-2.yaml': 'test-bindings-include/include-2.yaml'}
with pytest.raises(edtlib.EDTError) as e:
with from_here():
edtlib.Binding("test-bindings-include/allow-and-blocklist.yaml", fname2path)
assert ("should not specify both 'property-allowlist:' and 'property-blocklist:'"
in str(e.value))
with pytest.raises(edtlib.EDTError) as e:
with from_here():
edtlib.Binding("test-bindings-include/allow-and-blocklist-child.yaml", fname2path)
assert ("should not specify both 'property-allowlist:' and 'property-blocklist:'"
in str(e.value))
with pytest.raises(edtlib.EDTError) as e:
with from_here():
edtlib.Binding("test-bindings-include/allow-not-list.yaml", fname2path)
value_str = str(e.value)
assert value_str.startswith("'property-allowlist' value")
assert value_str.endswith("should be a list")
with pytest.raises(edtlib.EDTError) as e:
with from_here():
edtlib.Binding("test-bindings-include/block-not-list.yaml", fname2path)
value_str = str(e.value)
assert value_str.startswith("'property-blocklist' value")
assert value_str.endswith("should be a list")
with pytest.raises(edtlib.EDTError) as e:
with from_here():
binding = edtlib.Binding("test-bindings-include/include-invalid-keys.yaml", fname2path)
value_str = str(e.value)
assert value_str.startswith(
"'include:' in 'test-bindings-include/include-invalid-keys.yaml' should not have these "
"unexpected contents: ")
assert 'bad-key-1' in value_str
assert 'bad-key-2' in value_str
with pytest.raises(edtlib.EDTError) as e:
with from_here():
binding = edtlib.Binding("test-bindings-include/include-invalid-type.yaml", fname2path)
value_str = str(e.value)
assert value_str.startswith(
"'include:' in 'test-bindings-include/include-invalid-type.yaml' "
"should be a string or list, but has type ")
with pytest.raises(edtlib.EDTError) as e:
with from_here():
binding = edtlib.Binding("test-bindings-include/include-no-name.yaml", fname2path)
value_str = str(e.value)
assert value_str.startswith("'include:' element")
assert value_str.endswith(
"in 'test-bindings-include/include-no-name.yaml' should have a 'name' key")
with from_here():
binding = edtlib.Binding("test-bindings-include/allowlist.yaml", fname2path)
assert set(binding.prop2specs.keys()) == {'x'} # 'x' is allowed
binding = edtlib.Binding("test-bindings-include/empty-allowlist.yaml", fname2path)
assert set(binding.prop2specs.keys()) == set() # nothing is allowed
binding = edtlib.Binding("test-bindings-include/blocklist.yaml", fname2path)
assert set(binding.prop2specs.keys()) == {'y', 'z'} # 'x' is blocked
binding = edtlib.Binding("test-bindings-include/empty-blocklist.yaml", fname2path)
assert set(binding.prop2specs.keys()) == {'x', 'y', 'z'} # nothing is blocked
binding = edtlib.Binding("test-bindings-include/intermixed.yaml", fname2path)
assert set(binding.prop2specs.keys()) == {'x', 'a'}
binding = edtlib.Binding("test-bindings-include/include-no-list.yaml", fname2path)
assert set(binding.prop2specs.keys()) == {'x', 'y', 'z'}
binding = edtlib.Binding("test-bindings-include/filter-child-bindings.yaml", fname2path)
child = binding.child_binding
grandchild = child.child_binding
assert set(binding.prop2specs.keys()) == {'x'}
assert set(child.prop2specs.keys()) == {'child-prop-2'}
assert set(grandchild.prop2specs.keys()) == {'grandchild-prop-1'}
binding = edtlib.Binding("test-bindings-include/allow-and-blocklist-multilevel.yaml",
fname2path)
assert set(binding.prop2specs.keys()) == {'x'} # 'x' is allowed
child = binding.child_binding
assert set(child.prop2specs.keys()) == {'child-prop-1', 'child-prop-2',
'x', 'z'} # root level 'y' is blocked
def test_include_filters_inherited_bindings() -> None:
'''Test the basics of filtering properties inherited via an intermediary binding file.
Use-case "B includes I includes X":
- X is a base binding file, specifying common properties
- I is an intermediary binding file, which includes X without modification
nor filter
- B includes I, filtering the properties it chooses to inherit
with an allowlist or a blocklist
Checks that the properties inherited from X via I are actually filtered
as B intends to.
'''
fname2path = {
# Base binding file, specifies a few properties up to the grandchild-binding level.
"simple.yaml": "test-bindings-include/simple.yaml",
# 'include:'s the base file above, without modification nor filter
"simple_inherit.yaml": "test-bindings-include/simple_inherit.yaml",
}
with from_here():
binding = edtlib.Binding(
# Filters inherited specifications with an allowlist.
"test-bindings-include/simple_filter_allowlist.yaml",
fname2path,
require_compatible=False,
require_description=False,
)
# Only property allowed.
assert {"prop-1"} == set(binding.prop2specs.keys())
with from_here():
binding = edtlib.Binding(
# Filters inherited specifications with a blocklist.
"test-bindings-include/simple_filter_blocklist.yaml",
fname2path,
require_compatible=False,
require_description=False,
)
# Only non blocked property.
assert {"prop-1"} == set(binding.prop2specs.keys())
def test_include_filters_inherited_child_bindings() -> None:
'''Test the basics of filtering properties inherited via an intermediary binding file
(child-binding level).
See also: test_include_filters_inherited_bindings()
'''
fname2path = {
"simple.yaml": "test-bindings-include/simple.yaml",
"simple_inherit.yaml": "test-bindings-include/simple_inherit.yaml",
}
with from_here():
binding = edtlib.Binding(
"test-bindings-include/simple_filter_allowlist.yaml",
fname2path,
require_compatible=False,
require_description=False,
)
assert binding.child_binding
child_binding = binding.child_binding
# Only property allowed.
assert {"child-prop-1"} == set(child_binding.prop2specs.keys())
with from_here():
binding = edtlib.Binding(
"test-bindings-include/simple_filter_blocklist.yaml",
fname2path,
require_compatible=False,
require_description=False,
)
# Only non blocked property.
assert binding.child_binding
child_binding = binding.child_binding
assert {"child-prop-1"} == set(child_binding.prop2specs.keys())
def test_include_filters_inherited_grandchild_bindings() -> None:
'''Test the basics of filtering properties inherited via an intermediary binding file
(grandchild-binding level).
See also: test_include_filters_inherited_bindings()
'''
fname2path = {
"simple.yaml": "test-bindings-include/simple.yaml",
"simple_inherit.yaml": "test-bindings-include/simple_inherit.yaml",
}
with from_here():
binding = edtlib.Binding(
"test-bindings-include/simple_filter_allowlist.yaml",
fname2path,
require_compatible=False,
require_description=False,
)
assert binding.child_binding
child_binding = binding.child_binding
assert child_binding.child_binding
grandchild_binding = child_binding.child_binding
# Only property allowed.
assert {"grandchild-prop-1"} == set(grandchild_binding.prop2specs.keys())
with from_here():
binding = edtlib.Binding(
"test-bindings-include/simple_filter_blocklist.yaml",
fname2path,
require_compatible=False,
require_description=False,
)
assert binding.child_binding
child_binding = binding.child_binding
assert child_binding.child_binding
grandchild_binding = child_binding.child_binding
# Only non blocked property.
assert {"grandchild-prop-1"} == set(grandchild_binding.prop2specs.keys())
def test_bus():
'''Test 'bus:' and 'on-bus:' in bindings'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
assert isinstance(edt.get_node("/buses/foo-bus").buses, list)
assert "foo" in edt.get_node("/buses/foo-bus").buses
# foo-bus does not itself appear on a bus
assert isinstance(edt.get_node("/buses/foo-bus").on_buses, list)
assert not edt.get_node("/buses/foo-bus").on_buses
assert edt.get_node("/buses/foo-bus").bus_node is None
# foo-bus/node1 is not a bus node...
assert isinstance(edt.get_node("/buses/foo-bus/node1").buses, list)
assert not edt.get_node("/buses/foo-bus/node1").buses
# ...but is on a bus
assert isinstance(edt.get_node("/buses/foo-bus/node1").on_buses, list)
assert "foo" in edt.get_node("/buses/foo-bus/node1").on_buses
assert edt.get_node("/buses/foo-bus/node1").bus_node.path == \
"/buses/foo-bus"
# foo-bus/node2 is not a bus node...
assert isinstance(edt.get_node("/buses/foo-bus/node2").buses, list)
assert not edt.get_node("/buses/foo-bus/node2").buses
# ...but is on a bus
assert isinstance(edt.get_node("/buses/foo-bus/node2").on_buses, list)
assert "foo" in edt.get_node("/buses/foo-bus/node2").on_buses
# no-bus-node is not a bus node...
assert isinstance(edt.get_node("/buses/no-bus-node").buses, list)
assert not edt.get_node("/buses/no-bus-node").buses
# ... and is not on a bus
assert isinstance(edt.get_node("/buses/no-bus-node").on_buses, list)
assert not edt.get_node("/buses/no-bus-node").on_buses
# Same compatible string, but different bindings from being on different
# buses
assert str(edt.get_node("/buses/foo-bus/node1").binding_path) == \
hpath("test-bindings/device-on-foo-bus.yaml")
assert str(edt.get_node("/buses/foo-bus/node2").binding_path) == \
hpath("test-bindings/device-on-any-bus.yaml")
assert str(edt.get_node("/buses/bar-bus/node").binding_path) == \
hpath("test-bindings/device-on-bar-bus.yaml")
assert str(edt.get_node("/buses/no-bus-node").binding_path) == \
hpath("test-bindings/device-on-any-bus.yaml")
# foo-bus/node/nested also appears on the foo-bus bus
assert isinstance(edt.get_node("/buses/foo-bus/node1/nested").on_buses, list)
assert "foo" in edt.get_node("/buses/foo-bus/node1/nested").on_buses
assert str(edt.get_node("/buses/foo-bus/node1/nested").binding_path) == \
hpath("test-bindings/device-on-foo-bus.yaml")
def test_binding_top_key():
fname2path = {'include.yaml': 'test-bindings-include/include.yaml',
'include-2.yaml': 'test-bindings-include/include-2.yaml'}
with from_here():
binding = edtlib.Binding("test-bindings/defaults.yaml", fname2path)
title = binding.title
description = binding.description
compatible = binding.compatible
examples = binding.examples[0]
assert title == "Test binding"
assert description == "Property default value test"
assert compatible == "defaults"
assert examples == textwrap.dedent("""\
/ {
leds {
compatible = "gpio-leds";
uled: led {
gpios = <&gpioe 12 GPIO_ACTIVE_HIGH>;
};
};
aliases {
led0 = &uled;
};
};
""")
def test_child_binding():
'''Test 'child-binding:' in bindings'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
child1 = edt.get_node("/child-binding/child-1")
child2 = edt.get_node("/child-binding/child-2")
grandchild = edt.get_node("/child-binding/child-1/grandchild")
assert str(child1.binding_path) == hpath("test-bindings/child-binding.yaml")
assert str(child1.description) == "child node"
verify_props(child1, ['child-prop'], ['int'], [1])
assert str(child2.binding_path) == hpath("test-bindings/child-binding.yaml")
assert str(child2.description) == "child node"
verify_props(child2, ['child-prop'], ['int'], [3])
assert str(grandchild.binding_path) == hpath("test-bindings/child-binding.yaml")
assert str(grandchild.description) == "grandchild node"
verify_props(grandchild, ['grandchild-prop'], ['int'], [2])
with from_here():
binding_file = Path("test-bindings/child-binding.yaml").resolve()
top = edtlib.Binding(binding_file, {})
child = top.child_binding
assert Path(top.path) == binding_file
assert Path(child.path) == binding_file
assert top.compatible == 'top-binding'
assert child.compatible is None
with from_here():
binding_file = Path("test-bindings/child-binding-with-compat.yaml").resolve()
top = edtlib.Binding(binding_file, {})
child = top.child_binding
assert Path(top.path) == binding_file
assert Path(child.path) == binding_file
assert top.compatible == 'top-binding-with-compat'
assert child.compatible == 'child-compat'
def test_props():
'''Test Node.props (derived from DT and 'properties:' in the binding)'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
props_node = edt.get_node('/props')
ctrl_1, ctrl_2 = [edt.get_node(path) for path in ['/ctrl-1', '/ctrl-2']]
verify_props(props_node,
['int',
'existent-boolean', 'nonexistent-boolean',
'array', 'uint8-array',
'string', 'string-array',
'phandle-ref', 'phandle-refs',
'path'],
['int',
'boolean', 'boolean',
'array', 'uint8-array',
'string', 'string-array',
'phandle', 'phandles',
'path'],
[1,
True, False,
[1,2,3], b'\x124',
'foo', ['foo','bar','baz'],
ctrl_1, [ctrl_1,ctrl_2],
ctrl_1])
verify_phandle_array_prop(props_node,
'phandle-array-foos',
[(ctrl_1, {'one': 1}),
(ctrl_2, {'one': 2, 'two': 3})])
verify_phandle_array_prop(edt.get_node("/props-2"),
"phandle-array-foos",
[(edt.get_node('/ctrl-0-1'), {}),
None,
(edt.get_node('/ctrl-0-2'), {})])
verify_phandle_array_prop(props_node,
'foo-gpios',
[(ctrl_1, {'gpio-one': 1})])
verify_phandle_array_prop(props_node,
'bar-io-channels',
[(ctrl_2, {'io-channel-one': 2})])
def test_nexus():
'''Test <prefix>-map via gpio-map (the most common case).'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
source = edt.get_node("/gpio-map/source")
destination = edt.get_node('/gpio-map/destination')
verify_phandle_array_prop(source,
'foo-gpios',
[(destination, {'val': 6}),
(destination, {'val': 5})])
assert source.props["foo-gpios"].val[0].basename == f"gpio"
def test_prop_defaults():
'''Test property default values given in bindings'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
node = edt.get_node("/defaults")
verify_props(node,
['int',
'array', 'uint8-array',
'string', 'string-array',
'default-not-used'],
['int',
'array', 'uint8-array',
'string', 'string-array',
'int'],
[123,
[1,2,3], b'\x89\xab\xcd',
'hello', ['hello','there'],
234])
# Verify HexInt preservation in PropertySpec.default (raw binding values)
# uint8-array default [0x89, 0xAB, 0xCD] should have HexInt elements
assert all(isinstance(v, edtlib.HexInt) for v in node.props["uint8-array"].spec.default)
# int/array defaults (decimal in binding) should NOT be HexInt
assert not isinstance(node.props["int"].spec.default, edtlib.HexInt)
assert not any(isinstance(v, edtlib.HexInt) for v in node.props["array"].spec.default)
def test_prop_enums():
'''test properties with enum: in the binding'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
props = edt.get_node('/enums').props
int_enum = props['int-enum']
string_enum = props['string-enum']
tokenizable_enum = props['tokenizable-enum']
tokenizable_lower_enum = props['tokenizable-lower-enum']
array_enum = props['array-enum']
string_array_enum = props['string-array-enum']
no_enum = props['no-enum']
assert int_enum.val == 1
assert int_enum.enum_indices[0] == 0
assert not int_enum.spec.enum_tokenizable
assert not int_enum.spec.enum_upper_tokenizable
assert string_enum.val == 'foo_bar'
assert string_enum.enum_indices[0] == 1
assert not string_enum.spec.enum_tokenizable
assert not string_enum.spec.enum_upper_tokenizable
assert tokenizable_enum.val == '123 is ok'
assert tokenizable_enum.val_as_tokens[0] == '123_is_ok'
assert tokenizable_enum.enum_indices[0] == 2
assert tokenizable_enum.spec.enum_tokenizable
assert tokenizable_enum.spec.enum_upper_tokenizable
assert tokenizable_lower_enum.val == 'bar'
assert tokenizable_lower_enum.val_as_tokens[0] == 'bar'
assert tokenizable_lower_enum.enum_indices[0] == 0
assert tokenizable_lower_enum.spec.enum_tokenizable
assert not tokenizable_lower_enum.spec.enum_upper_tokenizable
assert array_enum.val == [0, 40, 40, 10]
assert array_enum.enum_indices == [0, 4, 4, 1]
assert not array_enum.spec.enum_tokenizable
assert not array_enum.spec.enum_upper_tokenizable
assert string_array_enum.val == ["foo", "bar"]
assert string_array_enum.val_as_tokens == ["foo", "bar"]
assert string_array_enum.enum_indices == [1, 0]
assert string_array_enum.spec.enum_tokenizable
assert string_array_enum.spec.enum_upper_tokenizable
assert no_enum.enum_indices is None
assert not no_enum.spec.enum_tokenizable
assert not no_enum.spec.enum_upper_tokenizable
def test_binding_inference():
'''Test inferred bindings for special zephyr-specific nodes.'''
warnings = io.StringIO()
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"], warnings)
assert str(edt.get_node("/zephyr,user").props) == '{}'
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"], warnings,
infer_binding_for_paths=["/zephyr,user"])
ctrl_1 = edt.get_node('/ctrl-1')
ctrl_2 = edt.get_node('/ctrl-2')
zephyr_user = edt.get_node("/zephyr,user")
verify_props(zephyr_user,
['boolean', 'bytes', 'number',
'numbers', 'string', 'strings'],
['boolean', 'uint8-array', 'int',
'array', 'string', 'string-array'],
[True, b'\x81\x82\x83', 23,
[1,2,3], 'text', ['a','b','c']])
assert zephyr_user.props['handle'].val is ctrl_1
phandles = zephyr_user.props['phandles']
val = phandles.val
assert len(val) == 2
assert val[0] is ctrl_1
assert val[1] is ctrl_2
verify_phandle_array_prop(zephyr_user,
'phandle-array-foos',
[(edt.get_node('/ctrl-2'), {'one': 1, 'two': 2})])
verify_phandle_array_prop(zephyr_user,
'foo-gpios',
[(ctrl_1, {'gpio-one': 1})])
verify_phandle_array_prop(zephyr_user,
'bar-io-channels',
[(ctrl_2, {'io-channel-one': 2})])
def test_multi_bindings():
'''Test having multiple directories with bindings'''
with from_here():
edt = edtlib.EDT("test-multidir.dts", ["test-bindings", "test-bindings-2"])
assert str(edt.get_node("/in-dir-1").binding_path) == \
hpath("test-bindings/multidir.yaml")
assert str(edt.get_node("/in-dir-2").binding_path) == \
hpath("test-bindings-2/multidir.yaml")
def test_dependencies():
''''Test dependency relations'''
with from_here():
edt = edtlib.EDT("test-multidir.dts", ["test-bindings", "test-bindings-2"])
assert edt.get_node("/").dep_ordinal == 0
assert edt.get_node("/in-dir-1").dep_ordinal == 1
assert edt.get_node("/") in edt.get_node("/in-dir-1").depends_on
assert edt.get_node("/in-dir-1") in edt.get_node("/").required_by
def test_child_dependencies():
'''Test dependencies relashionship with child nodes propagated to parent'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
dep_node = edt.get_node("/child-binding-dep")
assert dep_node in edt.get_node("/child-binding").depends_on
assert dep_node in edt.get_node("/child-binding/child-1/grandchild").depends_on
assert dep_node in edt.get_node("/child-binding/child-2").depends_on
assert edt.get_node("/child-binding") in dep_node.required_by
assert edt.get_node("/child-binding/child-1/grandchild") in dep_node.required_by
assert edt.get_node("/child-binding/child-2") in dep_node.required_by
def test_child_phandle_circular_dependency(tmp_path):
'''Test parent phandles to child states do not create dependency cycles.'''
binding_dir = tmp_path / "bindings"
binding_dir.mkdir()
binding_file = binding_dir / "test-stm.yaml"
binding_file.write_text("""
description: Generic child state dependency test
compatible: "test,stm"
properties:
states:
type: phandles
child-binding:
description: state node
properties:
next-state:
type: int
""", encoding="utf-8")
dts_file = tmp_path / "child-descendant-ref.dts"
dts_file.write_text("""
/dts-v1/;
/ {
test_stm {
compatible = "test,stm";
states = <&state1 &state2 &state3>;
state1: state1 {
next-state = <2>;
};
state2: state2 {
next-state = <3>;
};
state3: state3 {
next-state = <1>;
};
};
};
""", encoding="utf-8")
edt = edtlib.EDT(os.fspath(dts_file), [os.fspath(binding_dir)])
parent = edt.get_node("/test_stm")
states = [
edt.get_node("/test_stm/state1"),
edt.get_node("/test_stm/state2"),
edt.get_node("/test_stm/state3"),
]
assert parent.props["states"].val == states
for state in states:
assert parent not in state.required_by