|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Project CHIP Authors |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +import chip.clusters as Clusters |
| 18 | +from chip.testing.conformance import ConformanceDecision, ConformanceException |
| 19 | +from chip.testing.global_attribute_ids import is_standard_attribute_id |
| 20 | +from chip.testing.matter_testing import MatterBaseTest, default_matter_test_main |
| 21 | +from chip.testing.spec_parsing import PrebuiltDataModelDirectory, build_xml_clusters, dm_from_spec_version |
| 22 | +from chip.tlv import uint |
| 23 | +from mobly import asserts, signals |
| 24 | +from TC_DeviceConformance import DeviceConformanceTests |
| 25 | + |
| 26 | + |
| 27 | +class TestSpecParsingSelection(MatterBaseTest, DeviceConformanceTests): |
| 28 | + def setup_class(self): |
| 29 | + # Overriding the DeviceConformanceTest setup_class so we don't go out to a real device |
| 30 | + pass |
| 31 | + |
| 32 | + def test_dm_from_spec_version(self): |
| 33 | + asserts.assert_equal(dm_from_spec_version(0x01030000), PrebuiltDataModelDirectory.k1_3, |
| 34 | + "Incorrect directory selected for 1.3 with patch 0") |
| 35 | + asserts.assert_equal(dm_from_spec_version(0x01030100), PrebuiltDataModelDirectory.k1_3, |
| 36 | + "Incorrect directory selected for 1.3 with patch 1") |
| 37 | + asserts.assert_equal(dm_from_spec_version(0x01040100), PrebuiltDataModelDirectory.k1_4_1, |
| 38 | + "Incorrect directory selected for 1.4.1") |
| 39 | + asserts.assert_equal(dm_from_spec_version(0x01040100), PrebuiltDataModelDirectory.k1_4_1, |
| 40 | + "Incorrect directory selected for 1.4.1") |
| 41 | + asserts.assert_equal(dm_from_spec_version(0x01050000), PrebuiltDataModelDirectory.kMaster, |
| 42 | + "Incorrect directory selected for 1.5") |
| 43 | + |
| 44 | + # We don't have data model files for 1.2, so these should error |
| 45 | + with asserts.assert_raises(ConformanceException, "Expected assertion was not raised for spec version 1.2"): |
| 46 | + dm_from_spec_version(0x01020000) |
| 47 | + |
| 48 | + # Any dot release besides 0 and 1 for 1.4 should error |
| 49 | + with asserts.assert_raises(ConformanceException, "Data model incorrectly identified for 1.4.2"): |
| 50 | + dm_from_spec_version(0x01040200) |
| 51 | + |
| 52 | + with asserts.assert_raises(ConformanceException, "Data model incorrectly identified for 1.4.FF"): |
| 53 | + dm_from_spec_version(0x0104FF00) |
| 54 | + |
| 55 | + # Any dot release besides 0 for 1.5 should error |
| 56 | + with asserts.assert_raises(ConformanceException, "Data model incorrectly identified for 1.5.1"): |
| 57 | + dm_from_spec_version(0x01050100) |
| 58 | + with asserts.assert_raises(ConformanceException, "Data model incorrectly identified for 1.5.FF"): |
| 59 | + dm_from_spec_version(0x0105FF00) |
| 60 | + |
| 61 | + # Any value with stuff in reserved should error |
| 62 | + with asserts.assert_raises(ConformanceException, "Error not returned for specification revision with non-zero reserved values"): |
| 63 | + dm_from_spec_version(0x01030001) |
| 64 | + with asserts.assert_raises(ConformanceException, "Error not returned for specification revision with non-zero reserved values"): |
| 65 | + dm_from_spec_version(0x01040001) |
| 66 | + with asserts.assert_raises(ConformanceException, "Error not returned for specification revision with non-zero reserved values"): |
| 67 | + dm_from_spec_version(0x01040101) |
| 68 | + with asserts.assert_raises(ConformanceException, "Error not returned for specification revision with non-zero reserved values"): |
| 69 | + dm_from_spec_version(0x01050001) |
| 70 | + |
| 71 | + def _create_device(self, spec_version: uint, tc_enabled: bool): |
| 72 | + # Build at 1.4.1 so we can have TC info |
| 73 | + xml_clusters, _ = build_xml_clusters(PrebuiltDataModelDirectory.k1_4_1) |
| 74 | + |
| 75 | + gc_feature_map = Clusters.GeneralCommissioning.Bitmaps.Feature.kTermsAndConditions if tc_enabled else 0 |
| 76 | + |
| 77 | + def create_cluster_globals(cluster, feature_map): |
| 78 | + spec_attributes = xml_clusters[cluster.id].attributes |
| 79 | + spec_accepted_commands = xml_clusters[cluster.id].accepted_commands |
| 80 | + spec_generated_commands = xml_clusters[cluster.id].generated_commands |
| 81 | + # Build just the lists - basic composition checks the wildcard against the lists, conformance just uses lists |
| 82 | + attributes = [id for id, a in spec_attributes.items() if a.conformance( |
| 83 | + feature_map, [], []).decision == ConformanceDecision.MANDATORY] |
| 84 | + accepted_commands = [id for id, c in spec_accepted_commands.items() if c.conformance( |
| 85 | + feature_map, [], []).decision == ConformanceDecision.MANDATORY] |
| 86 | + generated_commands = [id for id, c in spec_generated_commands.items() if c.conformance( |
| 87 | + feature_map, [], []).decision == ConformanceDecision.MANDATORY] |
| 88 | + attr = cluster.Attributes |
| 89 | + |
| 90 | + resp = {} |
| 91 | + non_global_attrs = [a for a in attributes if is_standard_attribute_id(a)] |
| 92 | + for attribute_id in non_global_attrs: |
| 93 | + # We don't use the values in these tests, set them all to 0. The types are wrong, but it shouldn't matter |
| 94 | + resp[Clusters.ClusterObjects.ALL_ATTRIBUTES[cluster.id][attribute_id]] = 0 |
| 95 | + |
| 96 | + resp[attr.AttributeList] = attributes |
| 97 | + resp[attr.AcceptedCommandList] = accepted_commands |
| 98 | + resp[attr.GeneratedCommandList] = generated_commands |
| 99 | + resp[attr.FeatureMap] = feature_map |
| 100 | + resp[attr.ClusterRevision] = xml_clusters[cluster.id].revision |
| 101 | + |
| 102 | + return resp |
| 103 | + |
| 104 | + def get_tlv(resp): |
| 105 | + # This only works because there are no structs in here. |
| 106 | + # structs need special handling. Beware. |
| 107 | + return {k.attribute_id: v for k, v in resp.items()} |
| 108 | + |
| 109 | + gc_resp = create_cluster_globals(Clusters.GeneralCommissioning, gc_feature_map) |
| 110 | + bi_resp = create_cluster_globals(Clusters.BasicInformation, 0) |
| 111 | + bi_resp[Clusters.BasicInformation.Attributes.SpecificationVersion] = spec_version |
| 112 | + |
| 113 | + self.endpoints = {0: {Clusters.GeneralCommissioning: gc_resp, Clusters.BasicInformation: bi_resp}} |
| 114 | + self.endpoints_tlv = {0: {Clusters.GeneralCommissioning.id: get_tlv( |
| 115 | + gc_resp), Clusters.BasicInformation.id: get_tlv(bi_resp)}} |
| 116 | + |
| 117 | + def _run_conformance_against_device(self, spec_version: uint, tc_enabled: bool, expect_success_conformance: bool, expect_success_revisions: bool): |
| 118 | + self._create_device(spec_version, tc_enabled) |
| 119 | + # build the spec XMLs for the stated version |
| 120 | + self.build_spec_xmls() |
| 121 | + success, problems = self.check_conformance(ignore_in_progress=False, is_ci=False, allow_provisional=False) |
| 122 | + problem_strs = [str(p) for p in problems] |
| 123 | + problem_str = "\n".join(problem_strs) |
| 124 | + asserts.assert_equal(success, expect_success_conformance, |
| 125 | + f"Improper conformance result for spec version {spec_version:08X}, TC: {tc_enabled} problems: {problem_str}") |
| 126 | + |
| 127 | + success, problems = self.check_revisions(ignore_in_progress=False) |
| 128 | + asserts.assert_equal(success, expect_success_revisions, |
| 129 | + f"Improper revision result for spec version {spec_version:08X}, TC: {tc_enabled} problems: {problems}") |
| 130 | + |
| 131 | + def test_conformance(self): |
| 132 | + |
| 133 | + # 1.4 is OK if TC is off |
| 134 | + self._run_conformance_against_device(0x01040000, False, expect_success_conformance=True, expect_success_revisions=True) |
| 135 | + # 1.4.1 is OK if TC is off |
| 136 | + self._run_conformance_against_device(0x01040100, False, expect_success_conformance=True, expect_success_revisions=True) |
| 137 | + # 1.4.1 is OK if TC is on |
| 138 | + self._run_conformance_against_device(0x01040100, True, expect_success_conformance=True, expect_success_revisions=True) |
| 139 | + # 1.4 is NOT OK if TC is on |
| 140 | + self._run_conformance_against_device(0x01040000, True, expect_success_conformance=False, expect_success_revisions=True) |
| 141 | + |
| 142 | + # Check that we get a test failure on a bad spec revision |
| 143 | + self._create_device(0xFFFFFFFF, False) |
| 144 | + with asserts.assert_raises(signals.TestFailure, "Exception not properly raised for bad spec type"): |
| 145 | + self.build_spec_xmls() |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + default_matter_test_main() |
0 commit comments