|
| 1 | +# |
| 2 | +# Copyright (c) 2024 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 | +# === BEGIN CI TEST ARGUMENTS === |
| 18 | +# test-runner-runs: |
| 19 | +# run1: |
| 20 | +# app: ${ALL_CLUSTERS_APP} |
| 21 | +# app-args: --discriminator 1234 --KVS kvs1 --trace-to json:${TRACE_APP}.json |
| 22 | +# script-args: > |
| 23 | +# --storage-path admin_storage.json |
| 24 | +# --commissioning-method on-network |
| 25 | +# --discriminator 1234 |
| 26 | +# --passcode 20202021 |
| 27 | +# --trace-to json:${TRACE_TEST_JSON}.json |
| 28 | +# --trace-to perfetto:${TRACE_TEST_PERFETTO}.perfetto |
| 29 | +# --PICS src/app/tests/suites/certification/ci-pics-values |
| 30 | +# factory-reset: true |
| 31 | +# quiet: true |
| 32 | +# === END CI TEST ARGUMENTS === |
| 33 | + |
| 34 | +import random |
| 35 | + |
| 36 | +import chip.clusters as Clusters |
| 37 | +from chip import ChipDeviceCtrl |
| 38 | +from chip.exceptions import ChipStackError |
| 39 | +from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main |
| 40 | +from mobly import asserts |
| 41 | + |
| 42 | + |
| 43 | +class TC_CADMIN_1_19(MatterBaseTest): |
| 44 | + def generate_unique_random_value(self, value): |
| 45 | + while True: |
| 46 | + random_value = random.randint(10000000, 99999999) |
| 47 | + asserts.assert_equal(random_value, value) |
| 48 | + return random_value |
| 49 | + |
| 50 | + async def get_fabrics(self, th: ChipDeviceCtrl) -> int: |
| 51 | + OC_cluster = Clusters.OperationalCredentials |
| 52 | + fabrics = await self.read_single_attribute_check_success(dev_ctrl=th, fabric_filtered=False, endpoint=0, cluster=OC_cluster, attribute=OC_cluster.Attributes.Fabrics) |
| 53 | + return fabrics |
| 54 | + |
| 55 | + def steps_TC_CADMIN_1_19(self) -> list[TestStep]: |
| 56 | + return [ |
| 57 | + TestStep( |
| 58 | + 1, "TH_CR1 reads the BasicCommissioningInfo attribute from the General Commissioning cluster and saves the MaxCumulativeFailsafeSeconds field as max_window_duration."), |
| 59 | + TestStep(2, "TH_CR1 reads the Fabrics attribute from the Node Operational Credentials cluster using a non-fabric-filtered read. Save the number of fabrics in the list as initial_number_of_fabrics"), |
| 60 | + TestStep(3, "TH_CR1 reads the SupportedFabrics attribute from the Node Operational Credentials cluster. Save max_fabrics", |
| 61 | + "Verify that max_fabrics is larger than initial_number_of_fabrics. If not, instruct the tester to remove one non-test-harness fabric and re-start the test."), |
| 62 | + TestStep(4, "Repeat the following steps (5a and 5b) max_fabrics - initial_number_of_fabrics times"), |
| 63 | + TestStep( |
| 64 | + "4a", "TH_CR1 send an OpenCommissioningWindow command to DUT_CE using a commissioning timeout of max_window_duration", "{resDutSuccess}"), |
| 65 | + TestStep("4b", "TH creates a controller on a new fabric and commissions DUT_CE using that controller", |
| 66 | + "Commissioning is successful"), |
| 67 | + TestStep(5, "TH reads the CommissionedFabrics attributes from the Node Operational Credentials cluster.", |
| 68 | + "Verify this is equal to max_fabrics"), |
| 69 | + TestStep( |
| 70 | + 6, "TH_CR1 send an OpenCommissioningWindow command to DUT_CE using a commissioning timeout of max_window_duration", "{resDutSuccess}"), |
| 71 | + TestStep(7, "TH creates a controller on a new fabric and commissions DUT_CE using that controller", |
| 72 | + "Verify DUT_CE responds with NOCResponse with a StatusCode field value of TableFull(5)"), |
| 73 | + TestStep(8, "Repeat the following steps (9a and 9b) for each controller (TH_CRn) created by this test"), |
| 74 | + TestStep("8a", "The controller reads the CurrentFabricIndex from the Node Operational Credentials cluster. Save as fabric_index."), |
| 75 | + TestStep("8b", "TH_CR1 sends the RemoveFabric command to DUT_CE", "{resDutSuccess}"), |
| 76 | + TestStep(9, "TH reads the CommissionedFabrics attributes from the Node Operational Credentials cluster.", |
| 77 | + "Verify this is equal to initial_number_of_fabrics."), |
| 78 | + ] |
| 79 | + |
| 80 | + def pics_TC_CADMIN_1_19(self) -> list[str]: |
| 81 | + return ["CADMIN.S"] |
| 82 | + |
| 83 | + @async_test_body |
| 84 | + async def test_TC_CADMIN_1_19(self): |
| 85 | + self.step(1) |
| 86 | + # Establishing TH1 |
| 87 | + self.th1 = self.default_controller |
| 88 | + |
| 89 | + GC_cluster = Clusters.GeneralCommissioning |
| 90 | + attribute = GC_cluster.Attributes.BasicCommissioningInfo |
| 91 | + duration = await self.read_single_attribute_check_success(endpoint=0, cluster=GC_cluster, attribute=attribute) |
| 92 | + self.max_window_duration = duration.maxCumulativeFailsafeSeconds |
| 93 | + |
| 94 | + self.step(2) |
| 95 | + fabrics = await self.get_fabrics(th=self.th1) |
| 96 | + initial_number_of_fabrics = len(fabrics) |
| 97 | + |
| 98 | + self.step(3) |
| 99 | + OC_cluster = Clusters.OperationalCredentials |
| 100 | + max_fabrics = await self.read_single_attribute_check_success(dev_ctrl=self.th1, fabric_filtered=False, endpoint=0, cluster=OC_cluster, attribute=OC_cluster.Attributes.SupportedFabrics) |
| 101 | + asserts.assert_greater(max_fabrics, initial_number_of_fabrics, |
| 102 | + "max fabrics must be greater than initial fabrics, please remove one non-test-harness fabric and try test again") |
| 103 | + |
| 104 | + self.step(4) |
| 105 | + fids_ca_dir = {} |
| 106 | + fids_fa_dir = {} |
| 107 | + fids = {} |
| 108 | + for fid in range(0, max_fabrics - initial_number_of_fabrics): |
| 109 | + self.print_step("commissioning iteration", fid + 1) |
| 110 | + # Make sure that current test step is 5, resets here after each loop |
| 111 | + self.current_step_index = 4 |
| 112 | + |
| 113 | + self.step("4a") |
| 114 | + params = await self.openCommissioningWindow(dev_ctrl=self.th1, timeout=self.max_window_duration, node_id=self.dut_node_id) |
| 115 | + |
| 116 | + self.step("4b") |
| 117 | + fids_ca_dir[fid] = self.certificate_authority_manager.NewCertificateAuthority() |
| 118 | + fids_fa_dir[fid] = fids_ca_dir[fid].NewFabricAdmin(vendorId=0xFFF1, fabricId=fid + 1) |
| 119 | + fids[fid] = fids_fa_dir[fid].NewController(nodeId=fid + 1) |
| 120 | + |
| 121 | + await fids[fid].CommissionOnNetwork( |
| 122 | + nodeId=self.dut_node_id, setupPinCode=params.commissioningParameters.setupPinCode, |
| 123 | + filterType=ChipDeviceCtrl.DiscoveryFilterType.LONG_DISCRIMINATOR, filter=params.randomDiscriminator) |
| 124 | + |
| 125 | + self.step(5) |
| 126 | + # TH reads the CommissionedFabrics attributes from the Node Operational Credentials cluster |
| 127 | + current_fabrics = await self.read_single_attribute_check_success(dev_ctrl=self.th1, fabric_filtered=False, endpoint=0, cluster=OC_cluster, attribute=OC_cluster.Attributes.SupportedFabrics) |
| 128 | + asserts.assert_equal(current_fabrics, max_fabrics, "Expected number of fabrics not correct") |
| 129 | + |
| 130 | + self.step(6) |
| 131 | + params = await self.openCommissioningWindow(dev_ctrl=self.th1, node_id=self.dut_node_id) |
| 132 | + |
| 133 | + self.step(7) |
| 134 | + # TH creates a controller on a new fabric and attempts to commission DUT_CE using that controller |
| 135 | + next_fabric = current_fabrics + 1 |
| 136 | + fids_ca_dir[next_fabric] = self.certificate_authority_manager.NewCertificateAuthority() |
| 137 | + fids_fa_dir[next_fabric] = fids_ca_dir[current_fabrics + |
| 138 | + 1].NewFabricAdmin(vendorId=0xFFF1, fabricId=next_fabric) |
| 139 | + try: |
| 140 | + fids[next_fabric] = fids_fa_dir[next_fabric].NewController(nodeId=next_fabric) |
| 141 | + await fids[next_fabric].CommissionOnNetwork( |
| 142 | + nodeId=self.dut_node_id, setupPinCode=params.commissioningParameters.setupPinCode, |
| 143 | + filterType=ChipDeviceCtrl.DiscoveryFilterType.LONG_DISCRIMINATOR, filter=params.randomDiscriminator) |
| 144 | + |
| 145 | + except ChipStackError as e: |
| 146 | + # When attempting to create a new controller we are expected to get the following response: |
| 147 | + # src/credentials/FabricTable.cpp:833: CHIP Error 0x0000000B: No memory |
| 148 | + # Since the FabricTable is full and unable to create any new fabrics |
| 149 | + self.print_step("Max number of fabrics", "reached") |
| 150 | + asserts.assert_equal(e.err, 0x0000000B, |
| 151 | + "Expected to return table is full since max number of fabrics has been created already") |
| 152 | + |
| 153 | + self.step(8) |
| 154 | + for thc in fids.keys(): |
| 155 | + # Make sure that current test step is 11 (9 + 2 since 5a and 5b test steps included in count), resets here after each loop |
| 156 | + self.current_step_index = 10 |
| 157 | + |
| 158 | + self.step("8a") |
| 159 | + fabric_index = await self.read_single_attribute_check_success(dev_ctrl=fids[thc], endpoint=0, cluster=OC_cluster, attribute=OC_cluster.Attributes.CurrentFabricIndex) |
| 160 | + |
| 161 | + self.step("8b") |
| 162 | + removeFabricCmd = Clusters.OperationalCredentials.Commands.RemoveFabric(fabric_index) |
| 163 | + await self.th1.SendCommand(nodeid=self.dut_node_id, endpoint=0, payload=removeFabricCmd) |
| 164 | + |
| 165 | + self.step(9) |
| 166 | + # TH reads the CommissionedFabrics attributes from the Node Operational Credentials cluster. |
| 167 | + current_fabrics = await self.read_single_attribute_check_success(dev_ctrl=self.th1, fabric_filtered=False, endpoint=0, cluster=OC_cluster, attribute=OC_cluster.Attributes.CommissionedFabrics) |
| 168 | + asserts.assert_equal(current_fabrics, initial_number_of_fabrics, "Expected number of fabrics not correct") |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + default_matter_test_main() |
0 commit comments