forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestConformanceSupport.py
957 lines (869 loc) · 42.9 KB
/
TestConformanceSupport.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
#
# Copyright (c) 2023 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import xml.etree.ElementTree as ElementTree
from typing import Callable
from chip.tlv import uint
from conformance_support import (Choice, Conformance, ConformanceDecision, ConformanceException, ConformanceParseParameters,
deprecated, disallowed, mandatory, optional, parse_basic_callable_from_xml,
parse_callable_from_xml, parse_device_type_callable_from_xml, provisional, zigbee)
from matter_testing_support import MatterBaseTest, default_matter_test_main
from mobly import asserts
def basic_test(xml: str, cls: Callable) -> None:
et = ElementTree.fromstring(xml)
xml_callable = parse_basic_callable_from_xml(et)
asserts.assert_true(isinstance(xml_callable, cls), "Unexpected class parsed from basic conformance")
class TestConformanceSupport(MatterBaseTest):
def setup_class(self):
super().setup_class()
# a small feature map
self.feature_names_to_bits = {'AB': 0x01, 'CD': 0x02}
# none, AB, CD, AB&CD
self.feature_maps = [0x00, 0x01, 0x02, 0x03]
self.has_ab = [False, True, False, True]
self.has_cd = [False, False, True, True]
self.attribute_names_to_values = {'attr1': 0x00, 'attr2': 0x01}
self.attribute_lists = [[], [0x00], [0x01], [0x00, 0x01]]
self.has_attr1 = [False, True, False, True]
self.has_attr2 = [False, False, True, True]
self.command_names_to_values = {'cmd1': 0x00, 'cmd2': 0x01}
self.cmd_lists = [[], [0x00], [0x01], [0x00, 0x01]]
self.has_cmd1 = [False, True, False, True]
self.has_cmd2 = [False, False, True, True]
self.params = ConformanceParseParameters(
feature_map=self.feature_names_to_bits, attribute_map=self.attribute_names_to_values, command_map=self.command_names_to_values)
def test_conformance_mandatory(self):
xml = '<mandatoryConform />'
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for f in self.feature_maps:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
asserts.assert_equal(str(xml_callable), 'M')
def test_conformance_optional(self):
xml = '<optionalConform />'
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for f in self.feature_maps:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
asserts.assert_equal(str(xml_callable), 'O')
def test_conformance_disallowed(self):
xml = '<disallowConform />'
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for f in self.feature_maps:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.DISALLOWED)
asserts.assert_equal(str(xml_callable), 'X')
xml = '<deprecateConform />'
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for f in self.feature_maps:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.DISALLOWED)
asserts.assert_equal(str(xml_callable), 'D')
def test_conformance_provisional(self):
xml = '<provisionalConform />'
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for f in self.feature_maps:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.PROVISIONAL)
asserts.assert_equal(str(xml_callable), 'P')
def test_conformance_zigbee(self):
xml = '<condition name="Zigbee"/>'
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for f in self.feature_maps:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'Zigbee')
def test_conformance_mandatory_on_condition(self):
xml = ('<mandatoryConform>'
'<feature name="AB" />'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_ab[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'AB')
xml = ('<mandatoryConform>'
'<feature name="CD" />'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'CD')
# single attribute mandatory
xml = ('<mandatoryConform>'
'<attribute name="attr1" />'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, a in enumerate(self.attribute_lists):
if self.has_attr1[i]:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'attr1')
xml = ('<mandatoryConform>'
'<attribute name="attr2" />'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, a in enumerate(self.attribute_lists):
if self.has_attr2[i]:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'attr2')
# test command in optional and in boolean - this is the same as attribute essentially, so testing every permutation is overkill
def test_conformance_optional_on_condition(self):
# single feature optional
xml = ('<optionalConform>'
'<feature name="AB" />'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_ab[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[AB]')
xml = ('<optionalConform>'
'<feature name="CD" />'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[CD]')
# single attribute optional
xml = ('<optionalConform>'
'<attribute name="attr1" />'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, a in enumerate(self.attribute_lists):
if self.has_attr1[i]:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[attr1]')
xml = ('<optionalConform>'
'<attribute name="attr2" />'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, a in enumerate(self.attribute_lists):
if self.has_attr2[i]:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[attr2]')
# single command optional
xml = ('<optionalConform>'
'<command name="cmd1" />'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, c in enumerate(self.cmd_lists):
if self.has_cmd1[i]:
asserts.assert_equal(xml_callable(0x00, [], c), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(0x00, [], c), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[cmd1]')
xml = ('<optionalConform>'
'<command name="cmd2" />'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, c in enumerate(self.cmd_lists):
if self.has_cmd2[i]:
asserts.assert_equal(xml_callable(0x00, [], c), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(0x00, [], c), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[cmd2]')
def test_conformance_not_term_mandatory(self):
# single feature not mandatory
xml = ('<mandatoryConform>'
'<notTerm>'
'<feature name="AB" />'
'</notTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if not self.has_ab[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '!AB')
xml = ('<mandatoryConform>'
'<notTerm>'
'<feature name="CD" />'
'</notTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if not self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '!CD')
# single attribute not mandatory
xml = ('<mandatoryConform>'
'<notTerm>'
'<attribute name="attr1" />'
'</notTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, a in enumerate(self.attribute_lists):
if not self.has_attr1[i]:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '!attr1')
xml = ('<mandatoryConform>'
'<notTerm>'
'<attribute name="attr2" />'
'</notTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, a in enumerate(self.attribute_lists):
if not self.has_attr2[i]:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '!attr2')
def test_conformance_not_term_optional(self):
# single feature not optional
xml = ('<optionalConform>'
'<notTerm>'
'<feature name="AB" />'
'</notTerm>'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if not self.has_ab[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[!AB]')
xml = ('<optionalConform>'
'<notTerm>'
'<feature name="CD" />'
'</notTerm>'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if not self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[!CD]')
def test_conformance_and_term(self):
# and term for features only
xml = ('<mandatoryConform>'
'<andTerm>'
'<feature name="AB" />'
'<feature name="CD" />'
'</andTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_ab[i] and self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'AB & CD')
# and term for attributes only
xml = ('<mandatoryConform>'
'<andTerm>'
'<attribute name="attr1" />'
'<attribute name="attr2" />'
'</andTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, a in enumerate(self.attribute_lists):
if self.has_attr1[i] and self.has_attr2[i]:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'attr1 & attr2')
# and term for feature and attribute
xml = ('<mandatoryConform>'
'<andTerm>'
'<feature name="AB" />'
'<attribute name="attr2" />'
'</andTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
for j, a in enumerate(self.attribute_lists):
if self.has_ab[i] and self.has_attr2[j]:
asserts.assert_equal(xml_callable(f, a, []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'AB & attr2')
def test_conformance_or_term(self):
# or term feature only
xml = ('<mandatoryConform>'
'<orTerm>'
'<feature name="AB" />'
'<feature name="CD" />'
'</orTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_ab[i] or self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'AB | CD')
# or term attribute only
xml = ('<mandatoryConform>'
'<orTerm>'
'<attribute name="attr1" />'
'<attribute name="attr2" />'
'</orTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, a in enumerate(self.attribute_lists):
if self.has_attr1[i] or self.has_attr2[i]:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(0x00, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'attr1 | attr2')
# or term feature and attribute
xml = ('<mandatoryConform>'
'<orTerm>'
'<feature name="AB" />'
'<attribute name="attr2" />'
'</orTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
for j, a in enumerate(self.attribute_lists):
if self.has_ab[i] or self.has_attr2[j]:
asserts.assert_equal(xml_callable(f, a, []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, a, []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'AB | attr2')
def test_conformance_and_term_with_not(self):
# and term with not
xml = ('<optionalConform>'
'<andTerm>'
'<notTerm>'
'<feature name="AB" />'
'</notTerm>'
'<feature name="CD" />'
'</andTerm>'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if not self.has_ab[i] and self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[!AB & CD]')
def test_conformance_or_term_with_not(self):
# or term with not on second feature
xml = ('<mandatoryConform>'
'<orTerm>'
'<feature name="AB" />'
'<notTerm>'
'<feature name="CD" />'
'</notTerm>'
'</orTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_ab[i] or not self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'AB | !CD')
# not around or term with
xml = ('<optionalConform>'
'<notTerm>'
'<orTerm>'
'<feature name="AB" />'
'<feature name="CD" />'
'</orTerm>'
'</notTerm>'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if not (self.has_ab[i] or self.has_cd[i]):
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[!(AB | CD)]')
def test_conformance_and_term_with_three_terms(self):
# and term with three features
xml = ('<optionalConform>'
'<andTerm>'
'<feature name="AB" />'
'<feature name="CD" />'
'<feature name="EF" />'
'</andTerm>'
'</optionalConform>')
self.feature_names_to_bits['EF'] = 0x04
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
# no features
asserts.assert_equal(xml_callable(0x00, [], []), ConformanceDecision.NOT_APPLICABLE)
# one feature
asserts.assert_equal(xml_callable(0x01, [], []), ConformanceDecision.NOT_APPLICABLE)
# all features
asserts.assert_equal(xml_callable(0x07, [], []), ConformanceDecision.OPTIONAL)
asserts.assert_equal(str(xml_callable), '[AB & CD & EF]')
# and term with one of each
xml = ('<optionalConform>'
'<andTerm>'
'<feature name="AB" />'
'<attribute name="attr1" />'
'<command name="cmd1" />'
'</andTerm>'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
for j, a in enumerate(self.attribute_lists):
for k, c in enumerate(self.cmd_lists):
if self.has_ab[i] and self.has_attr1[j] and self.has_cmd1[k]:
asserts.assert_equal(xml_callable(f, a, c), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, a, c), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[AB & attr1 & cmd1]')
def test_conformance_or_term_with_three_terms(self):
# or term with three features
xml = ('<optionalConform>'
'<orTerm>'
'<feature name="AB" />'
'<feature name="CD" />'
'<feature name="EF" />'
'</orTerm>'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
# no features
asserts.assert_equal(xml_callable(0x00, [], []), ConformanceDecision.NOT_APPLICABLE)
# one feature
asserts.assert_equal(xml_callable(0x01, [], []), ConformanceDecision.OPTIONAL)
# all features
asserts.assert_equal(xml_callable(0x07, [], []), ConformanceDecision.OPTIONAL)
asserts.assert_equal(str(xml_callable), '[AB | CD | EF]')
# or term with one of each
xml = ('<optionalConform>'
'<orTerm>'
'<feature name="AB" />'
'<attribute name="attr1" />'
'<command name="cmd1" />'
'</orTerm>'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
for j, a in enumerate(self.attribute_lists):
for k, c in enumerate(self.cmd_lists):
if self.has_ab[i] or self.has_attr1[j] or self.has_cmd1[k]:
asserts.assert_equal(xml_callable(f, a, c), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, a, c), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), '[AB | attr1 | cmd1]')
def test_conformance_otherwise(self):
# AB, O
xml = ('<otherwiseConform>'
'<mandatoryConform>'
'<feature name="AB" />'
'</mandatoryConform>'
'<optionalConform />'
'</otherwiseConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_ab[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
asserts.assert_equal(str(xml_callable), 'AB, O')
# AB, [CD]
xml = ('<otherwiseConform>'
'<mandatoryConform>'
'<feature name="AB" />'
'</mandatoryConform>'
'<optionalConform>'
'<feature name="CD" />'
'</optionalConform>'
'</otherwiseConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_ab[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
elif self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.OPTIONAL)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.NOT_APPLICABLE)
asserts.assert_equal(str(xml_callable), 'AB, [CD]')
# AB & !CD, P
xml = ('<otherwiseConform>'
'<mandatoryConform>'
'<andTerm>'
'<feature name="AB" />'
'<notTerm>'
'<feature name="CD" />'
'</notTerm>'
'</andTerm>'
'</mandatoryConform>'
'<provisionalConform />'
'</otherwiseConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
for i, f in enumerate(self.feature_maps):
if self.has_ab[i] and not self.has_cd[i]:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.MANDATORY)
else:
asserts.assert_equal(xml_callable(f, [], []), ConformanceDecision.PROVISIONAL)
asserts.assert_equal(str(xml_callable), 'AB & !CD, P')
def test_conformance_greater(self):
# AB, [CD]
xml = ('<mandatoryConform>'
'<greaterTerm>'
'<attribute name="attr1" />'
'<literal value="1" />'
'</greaterTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
# TODO: switch this to check greater than once the update to the base is done (#33422)
asserts.assert_equal(xml_callable(0x00, [], []), ConformanceDecision.OPTIONAL)
asserts.assert_equal(str(xml_callable), 'attr1 > 1')
# Ensure that we can only have greater terms with exactly 2 value
xml = ('<mandatoryConform>'
'<greaterTerm>'
'<attribute name="attr1" />'
'<attribute name="attr2" />'
'<literal value="1" />'
'</greaterTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
try:
xml_callable = parse_callable_from_xml(et, self.params)
asserts.fail("Incorrectly parsed bad greaterTerm XML with > 2 values")
except ConformanceException:
pass
xml = ('<mandatoryConform>'
'<greaterTerm>'
'<attribute name="attr1" />'
'</greaterTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
try:
xml_callable = parse_callable_from_xml(et, self.params)
asserts.fail("Incorrectly parsed bad greaterTerm XML with < 2 values")
except ConformanceException:
pass
# Only attributes and literals allowed because arithmetic operations require values
xml = ('<mandatoryConform>'
'<greaterTerm>'
'<feature name="AB" />'
'<literal value="1" />'
'</greaterTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
try:
xml_callable = parse_callable_from_xml(et, self.params)
asserts.fail("Incorrectly parsed greater term with feature value")
except ConformanceException:
pass
def test_basic_conformance(self):
basic_test('<mandatoryConform />', mandatory)
basic_test('<optionalConform />', optional)
basic_test('<disallowConform />', disallowed)
basic_test('<deprecateConform />', deprecated)
basic_test('<provisionalConform />', provisional)
basic_test('<condition name="zigbee" />', zigbee)
# feature is not basic so we should get an exception
xml = '<feature name="CD" />'
et = ElementTree.fromstring(xml)
try:
parse_basic_callable_from_xml(et)
asserts.fail("Unexpected success parsing non-basic conformance")
except ConformanceException:
pass
# mandatory tag is basic, but this one is a wrapper, so we should get a TypeError
xml = ('<mandatoryConform>'
'<andTerm>'
'<feature name="AB" />'
'<notTerm>'
'<feature name="CD" />'
'</notTerm>'
'</andTerm>'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
try:
parse_basic_callable_from_xml(et)
asserts.fail("Unexpected success parsing mandatory wrapper")
except ConformanceException:
pass
def test_device_type_conformance(self):
msg = "Unexpected conformance returned for device type"
xml = ('<mandatoryConform>'
'<condition name="zigbee" />'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_device_type_callable_from_xml(et)
asserts.assert_equal(str(xml_callable), 'Zigbee', msg)
asserts.assert_equal(xml_callable(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg)
xml = ('<optionalConform>'
'<condition name="zigbee" />'
'</optionalConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_device_type_callable_from_xml(et)
# expect no exception here
asserts.assert_equal(str(xml_callable), '[Zigbee]', msg)
asserts.assert_equal(xml_callable(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg)
# otherwise conforms are allowed
xml = ('<otherwiseConform>'
'<condition name="zigbee" />'
'<provisionalConform />'
'</otherwiseConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_device_type_callable_from_xml(et)
# expect no exception here
asserts.assert_equal(str(xml_callable), 'Zigbee, P', msg)
asserts.assert_equal(xml_callable(0, [], []), ConformanceDecision.PROVISIONAL, msg)
# Device type conditions or features don't correspond to anything in the spec, so the XML takes a best
# guess as to what they are. We should be able to parse features, conditions, attributes as the same
# thing.
# TODO: allow querying conformance for conditional device features
# TODO: adjust conformance call function to accept a list of features and evaluate based on that
xml = ('<mandatoryConform>'
'<feature name="CD" />'
'</mandatoryConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_device_type_callable_from_xml(et)
asserts.assert_equal(str(xml_callable), 'CD', msg)
# Device features are always optional (at least for now), even though we didn't pass this feature in
asserts.assert_equal(xml_callable(0, [], []), ConformanceDecision.OPTIONAL)
xml = ('<otherwiseConform>'
'<feature name="CD" />'
'<condition name="testy" />'
'</otherwiseConform>')
et = ElementTree.fromstring(xml)
xml_callable = parse_device_type_callable_from_xml(et)
asserts.assert_equal(str(xml_callable), 'CD, testy', msg)
asserts.assert_equal(xml_callable(0, [], []), ConformanceDecision.OPTIONAL)
def test_choice_conformance(self):
# Choice conformances can appear on:
# - base optional O.a
# - base optional feature [AB].a
# - base optional attribute [attr1].a
# - base optional command [cmd1].a
# - optional wrapper of complex feature [AB | CD].a, [!attr1].a
# - otherwise conformance attr1, [AB], O.a / attr1, [AB].a, O
# - multiple in otherwise [AB].a, [CD].b
#
# Choice conformances are disallowed on:
# - mandatory M.a
# - mandatory feature AB.a
# - mandatory attribute attr1.a
# - mandatory command cmd1.a
# - AND expressions (attr1 & O.a)
# - OR expressions (attr1 | O.a)
# - NOT expressions (!O.a)
# - internal expressions [AB.a], [attr1.a], [cmd1.a]
# - provisional P.a
# - disallowed X.a
# - deprecated D.a
choices = [('a+', 'choice="a" more="true"', True), ('a', 'choice="a"', False)]
for suffix, xml_attrs, more in choices:
def check_good_choice(xml: str, conformance_str: str) -> Conformance:
msg = 'Bad choice conformance string'
et = ElementTree.fromstring(xml)
xml_callable = parse_callable_from_xml(et, self.params)
asserts.assert_equal(str(xml_callable), conformance_str, msg)
return xml_callable
def check_decision(conformance: Conformance, feature_map: uint, attr_list: list[uint], cmd_list: list[uint]):
decision = conformance(feature_map, attr_list, cmd_list)
asserts.assert_true(decision.get_choice(), 'Expected choice conformance on decision, but did not get one')
asserts.assert_equal(decision.get_choice().choice, 'a', 'Unexpected conformance string returned')
asserts.assert_equal(decision.get_choice().more, more, "Unexpected more on choice")
AB = self.feature_names_to_bits['AB']
attr1 = [self.attribute_names_to_values['attr1']]
cmd1 = [self.command_names_to_values['cmd1']]
msg_not_applicable = "Expected NOT_APPLICABLE conformance"
xml = f'<optionalConform {xml_attrs} />'
conformance = check_good_choice(xml, f'O.{suffix}')
check_decision(conformance, 0, [], [])
xml = (f'<optionalConform {xml_attrs}>'
'<feature name="AB" />'
'</optionalConform>')
conformance = check_good_choice(xml, f'[AB].{suffix}')
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
check_decision(conformance, AB, [], [])
xml = (f'<optionalConform {xml_attrs}>'
'<attribute name="attr1" />'
'</optionalConform>')
conformance = check_good_choice(xml, f'[attr1].{suffix}')
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
check_decision(conformance, 0, attr1, [])
xml = (f'<optionalConform {xml_attrs}>'
'<command name="cmd1" />'
'</optionalConform>')
conformance = check_good_choice(xml, f'[cmd1].{suffix}')
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
check_decision(conformance, 0, [], cmd1)
xml = (f'<optionalConform {xml_attrs}>'
'<orTerm>'
'<feature name="AB" />'
'<feature name="CD" />'
'</orTerm>'
'</optionalConform>')
conformance = check_good_choice(xml, f'[AB | CD].{suffix}')
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
check_decision(conformance, AB, [], [])
xml = (f'<optionalConform {xml_attrs}>'
'<notTerm>'
'<attribute name="attr1" />'
'</notTerm>'
'</optionalConform>')
conformance = check_good_choice(xml, f'[!attr1].{suffix}')
asserts.assert_equal(conformance(0, attr1, []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
check_decision(conformance, 0, [], [])
xml = ('<otherwiseConform>'
'<attribute name="attr1" />'
f'<optionalConform>'
'<feature name="AB" />'
'</optionalConform>'
f'<optionalConform {xml_attrs} />'
'</otherwiseConform>')
conformance = check_good_choice(xml, f'attr1, [AB], O.{suffix}')
# with no features or attributes, this should end up as O.a, so there should be a choice
check_decision(conformance, 0, [], [])
# when we have this attribute, we should not have a choice
asserts.assert_equal(conformance(0, attr1, []), ConformanceDecision.MANDATORY, 'Unexpected conformance')
asserts.assert_equal(conformance(0, attr1, []).get_choice(), None, 'Unexpected choice in conformance')
# when we have only this feature, we should not have a choice
asserts.assert_equal(conformance(AB, [], []), ConformanceDecision.OPTIONAL, 'Unexpected conformance')
asserts.assert_equal(conformance(AB, [], []).get_choice(), None, 'Unexpected choice in conformance')
# - multiple in otherwise [AB].a, [CD].b
xml = ('<otherwiseConform>'
'<attribute name="attr1" />'
f'<optionalConform {xml_attrs}>'
'<feature name="AB" />'
'</optionalConform>'
f'<optionalConform choice="b">'
'<feature name="CD" />'
'</optionalConform>'
'</otherwiseConform>')
conformance = check_good_choice(xml, f'attr1, [AB].{suffix}, [CD].b')
asserts.assert_equal(conformance(0, [], []), ConformanceDecision.NOT_APPLICABLE, msg_not_applicable)
# when we have this attribute, we should not have a choice
asserts.assert_equal(conformance(0, attr1, []), ConformanceDecision.MANDATORY, 'Unexpected conformance')
asserts.assert_equal(conformance(0, attr1, []).get_choice(), None, 'Unexpected choice in conformance')
# When it's just AB, we should have a choice
check_decision(conformance, AB, [], [])
# When we have both the attribute and AB, we should not have a choice
asserts.assert_equal(conformance(0, attr1, []), ConformanceDecision.MANDATORY, 'Unexpected conformance')
asserts.assert_equal(conformance(0, attr1, []).get_choice(), None, 'Unexpected choice in conformance')
# When we have AB and CD, we should be using the AB choice
CD = self.feature_names_to_bits['CD']
ABCD = AB | CD
check_decision(conformance, ABCD, [], [])
# When we just have CD, we still have a choice, but the string should be b
asserts.assert_equal(conformance(CD, [], []), ConformanceDecision.OPTIONAL, 'Unexpected conformance')
asserts.assert_equal(conformance(CD, [], []).get_choice(), Choice('b', False), 'Unexpected choice in conformance')
# Ones that should throw exceptions
def check_bad_choice(xml: str):
msg = f'Choice conformance string should cause exception, but did not: {xml}'
et = ElementTree.fromstring(xml)
try:
parse_callable_from_xml(et, self.params)
asserts.fail(msg)
except ConformanceException:
pass
xml = f'<mandatoryConform {xml_attrs} />'
check_bad_choice(xml)
xml = f'<feature name="AB" {xml_attrs} />'
check_bad_choice(xml)
xml = f'<attribute name="attr1" {xml_attrs} />'
check_bad_choice(xml)
xml = f'<command name="cmd1" {xml_attrs} />'
check_bad_choice(xml)
xml = ('<mandatoryConform>'
'<andTerm>'
'<attribute name="attr1"/>'
f'<optionalConform {xml_attrs} />'
'</andTerm>'
'</mandatoryConform>')
check_bad_choice(xml)
xml = ('<mandatoryConform>'
'<orTerm>'
'<attribute name="attr1"/>'
f'<optionalConform {xml_attrs} />'
'</orTerm>'
'</mandatoryConform>')
check_bad_choice(xml)
xml = ('<mandatoryConform>'
'<notTerm>'
f'<optionalConform {xml_attrs} />'
'</notTerm>'
'</mandatoryConform>')
check_bad_choice(xml)
xml = ('<optionalConform>'
f'<feature name="AB" {xml_attrs}/>'
'</optionalConform>')
check_bad_choice(xml)
xml = ('<optionalConform>'
f'<attribute name="attr1" {xml_attrs}/>'
'</optionalConform>')
check_bad_choice(xml)
xml = ('<optionalConform>'
f'<command name="cmd1" {xml_attrs}/>'
'</optionalConform>')
check_bad_choice(xml)
xml = (f'<provisionalConform {xml_attrs}/>')
check_bad_choice(xml)
xml = (f'<disallowConform {xml_attrs}/>')
check_bad_choice(xml)
xml = (f'<deprecateConform {xml_attrs}/>')
check_bad_choice(xml)
if __name__ == "__main__":
default_matter_test_main()