forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: unit tests. Note this has not been tested against the example app as it is still under development.
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# | ||
# Copyright (c) 2024 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. | ||
# | ||
|
||
from typing import Any | ||
|
||
import chip.clusters as Clusters | ||
from conformance_support import ConformanceDecision | ||
from global_attribute_ids import GlobalAttributeIds | ||
from matter_testing_support import MatterBaseTest, async_test_body, default_matter_test_main | ||
from mobly import asserts | ||
from spec_parsing_support import build_xml_clusters, build_xml_device_types | ||
from TC_DeviceConformance import DeviceConformanceTests | ||
|
||
|
||
def is_mandatory(conformance): | ||
return conformance(0, [], []).decision == ConformanceDecision.MANDATORY | ||
|
||
|
||
class TestConformanceSupport(MatterBaseTest, DeviceConformanceTests): | ||
def setup_class(self): | ||
self.xml_clusters, self.problems = build_xml_clusters() | ||
self.xml_device_types, problems = build_xml_device_types() | ||
self.problems.extend(problems) | ||
|
||
def _create_minimal_cluster(self, cluster_id: int) -> dict[int, Any]: | ||
attrs = {} | ||
attrs[GlobalAttributeIds.FEATURE_MAP_ID] = 0 | ||
|
||
mandatory_attributes = [id for id, a in self.xml_clusters[cluster_id].attributes.items() if is_mandatory(a.conformance)] | ||
for m in mandatory_attributes: | ||
# dummy versions - we're not using the values in this test | ||
attrs[m] = 0 | ||
attrs[GlobalAttributeIds.ATTRIBUTE_LIST_ID] = mandatory_attributes | ||
mandatory_accepted_commands = [id for id, a in self.xml_clusters[cluster_id].accepted_commands.items() | ||
if is_mandatory(a.conformance)] | ||
attrs[GlobalAttributeIds.ACCEPTED_COMMAND_LIST_ID] = mandatory_accepted_commands | ||
mandatory_generated_commands = [id for id, a in self.xml_clusters[cluster_id].generated_commands.items() | ||
if is_mandatory(a.conformance)] | ||
attrs[GlobalAttributeIds.GENERATED_COMMAND_LIST_ID] = mandatory_generated_commands | ||
attrs[GlobalAttributeIds.CLUSTER_REVISION_ID] = self.xml_clusters[cluster_id].revision | ||
return attrs | ||
|
||
def _create_minimal_dt(self, device_type_id: int) -> dict[int, dict[int, Any]]: | ||
''' Creates the internals of an endpoint_tlv with the minimal set of clusters, with the minimal set of attributes and commands. Global attributes only. | ||
Does NOT take into account overrides yet. | ||
''' | ||
endpoint_tlv = {} | ||
required_servers = [id for id, c in self.xml_device_types[device_type_id].server_clusters.items() | ||
if is_mandatory(c.conformance)] | ||
required_clients = [id for id, c in self.xml_device_types[device_type_id].client_clusters.items() | ||
if is_mandatory(c.conformance)] | ||
device_type_revision = self.xml_device_types[device_type_id].revision | ||
|
||
for s in required_servers: | ||
endpoint_tlv[s] = self._create_minimal_cluster(s) | ||
|
||
# Descriptor | ||
attr = Clusters.Descriptor.Attributes | ||
attrs = {} | ||
attrs[attr.FeatureMap.attribute_id] = 0 | ||
attrs[attr.AcceptedCommandList.attribute_id] = [] | ||
attrs[attr.GeneratedCommandList.attribute_id] = [] | ||
attrs[attr.ClusterRevision.attribute_id] = self.xml_clusters[Clusters.Descriptor.id].revision | ||
attrs[attr.DeviceTypeList.attribute_id] = [ | ||
Clusters.Descriptor.Structs.DeviceTypeStruct(deviceType=device_type_id, revision=device_type_revision)] | ||
attrs[attr.ServerList.attribute_id] = required_servers | ||
attrs[attr.ClientList.attribute_id] = required_clients | ||
attrs[attr.PartsList.attribute_id] = [] | ||
attrs[attr.AttributeList.attribute_id] = [] | ||
attrs[attr.AttributeList.attribute_id] = list(attrs.keys()) | ||
|
||
endpoint_tlv[Clusters.Descriptor.id] = attrs | ||
return endpoint_tlv | ||
|
||
def add_macl(self, root_endpoint: dict[int, dict[int, Any]]): | ||
ac = Clusters.AccessControl | ||
root_endpoint[ac.id][ac.Attributes.FeatureMap.attribute_id] = ac.Bitmaps.Feature.kManagedDevice | ||
root_endpoint[ac.id][ac.Attributes.Arl.attribute_id] = [] | ||
root_endpoint[ac.id][ac.Attributes.CommissioningARL.attribute_id] = [] | ||
root_endpoint[ac.id][ac.Attributes.AttributeList.attribute_id].extend([ | ||
ac.Attributes.Arl.attribute_id, ac.Attributes.CommissioningARL.attribute_id]) | ||
root_endpoint[ac.id][ac.Attributes.AcceptedCommandList.attribute_id].append(ac.Commands.ReviewFabricRestrictions.command_id) | ||
root_endpoint[ac.id][ac.Attributes.GeneratedCommandList.attribute_id].append( | ||
ac.Commands.ReviewFabricRestrictionsResponse.command_id) | ||
|
||
@ async_test_body | ||
async def test_macl_handling(self): | ||
nim_id = self._get_device_type_id('network infrastructure manager') | ||
root_node_id = self._get_device_type_id('root node') | ||
on_off_id = self._get_device_type_id('On/Off Light') | ||
|
||
root = self._create_minimal_dt(device_type_id=root_node_id) | ||
nim = self._create_minimal_dt(device_type_id=nim_id) | ||
self.endpoints_tlv = {0: root, 1: nim} | ||
asserts.assert_true(self._has_nim(), "Did not find NIM in generated device") | ||
|
||
success, problems = self.check_conformance(ignore_in_progress=False, is_ci=False) | ||
self.problems.extend(problems) | ||
asserts.assert_true(success, "Unexpected failure parsing minimal dt") | ||
|
||
self.add_macl(root) | ||
# A MACL is allowed when there is a NIM, so this should succeed as well | ||
success, problems = self.check_conformance(ignore_in_progress=False, is_ci=False) | ||
self.problems.extend(problems) | ||
asserts.assert_true(success, "Unexpected failure with NIM and MACL") | ||
|
||
# A MACL is not allowed when there is no NIM | ||
self.endpoints_tlv[1] = self._create_minimal_dt(device_type_id=on_off_id) | ||
success, problems = self.check_conformance(ignore_in_progress=False, is_ci=False) | ||
self.problems.extend(problems) | ||
asserts.assert_false(success, "Unexpected success with On/Off and MACL") | ||
|
||
# TODO: what happens if there is a NIM and a non-NIM endpoint? | ||
|
||
|
||
if __name__ == "__main__": | ||
default_matter_test_main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters