|
| 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: run1 |
| 19 | +# test-runner-run/run1/app: ${ALL_CLUSTERS_APP} |
| 20 | +# test-runner-run/run1/factoryreset: True |
| 21 | +# test-runner-run/run1/quiet: True |
| 22 | +# test-runner-run/run1/app-args: --discriminator 1234 --KVS kvs1 --trace-to json:${TRACE_APP}.json |
| 23 | +# test-runner-run/run1/script-args: --storage-path admin_storage.json --commissioning-method on-network --discriminator 1234 --passcode 20202021 --trace-to json:${TRACE_TEST_JSON}.json --trace-to perfetto:${TRACE_TEST_PERFETTO}.perfetto --PICS src/app/tests/suites/certification/ci-pics-values |
| 24 | +# === END CI TEST ARGUMENTS === |
| 25 | + |
| 26 | +import logging |
| 27 | +import random |
| 28 | +from time import sleep |
| 29 | + |
| 30 | +import chip.clusters as Clusters |
| 31 | +from chip import ChipDeviceCtrl |
| 32 | +from chip.ChipDeviceCtrl import CommissioningParameters |
| 33 | +from chip.exceptions import ChipStackError |
| 34 | +from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main |
| 35 | +from mobly import asserts |
| 36 | + |
| 37 | + |
| 38 | +class TC_CADMIN_1_9(MatterBaseTest): |
| 39 | + async def OpenCommissioningWindow(self) -> CommissioningParameters: |
| 40 | + try: |
| 41 | + cluster = Clusters.GeneralCommissioning |
| 42 | + attribute = cluster.Attributes.BasicCommissioningInfo |
| 43 | + duration = await self.read_single_attribute_check_success(endpoint=0, cluster=cluster, attribute=attribute) |
| 44 | + params = await self.th1.OpenCommissioningWindow( |
| 45 | + nodeid=self.dut_node_id, timeout=duration.maxCumulativeFailsafeSeconds, iteration=10000, discriminator=self.discriminator, option=1) |
| 46 | + return params |
| 47 | + |
| 48 | + except Exception as e: |
| 49 | + logging.exception('Error running OpenCommissioningWindow %s', e) |
| 50 | + asserts.assert_true(False, 'Failed to open commissioning window') |
| 51 | + |
| 52 | + def steps_TC_CADMIN_1_9(self) -> list[TestStep]: |
| 53 | + return [ |
| 54 | + TestStep(1, "Commissioning, already done", is_commissioning=True), |
| 55 | + TestStep( |
| 56 | + 2, "TH1 opens commissioning window on DUT with duration set to value for maxCumulativeFailsafeSeconds"), |
| 57 | + TestStep(3, "TH2 attempts to connect 20 times to endpoint with incorrect passcode"), |
| 58 | + TestStep(4, "TH2 attempts to connect to endpoint with correct passcode"), |
| 59 | + TestStep(5, "TH1 opening Commissioning Window one more time to validate ability to do so"), |
| 60 | + TestStep(6, "TH1 revoking Commissioning Window"), |
| 61 | + ] |
| 62 | + |
| 63 | + def generate_unique_random_value(self, value): |
| 64 | + while True: |
| 65 | + random_value = random.randint(10000000, 99999999) |
| 66 | + if random_value != value: |
| 67 | + return random_value |
| 68 | + |
| 69 | + async def CommissionOnNetwork( |
| 70 | + self, setup_code: int |
| 71 | + ): |
| 72 | + ctx = asserts.assert_raises(ChipStackError) |
| 73 | + with ctx: |
| 74 | + await self.th2.CommissionOnNetwork( |
| 75 | + nodeId=self.dut_node_id, setupPinCode=setup_code, |
| 76 | + filterType=ChipDeviceCtrl.DiscoveryFilterType.LONG_DISCRIMINATOR, filter=self.discriminator) |
| 77 | + errcode = ctx.exception.chip_error |
| 78 | + return errcode |
| 79 | + |
| 80 | + async def CommissionAttempt( |
| 81 | + self, setupPinCode: int, expectedErrCode: int): |
| 82 | + |
| 83 | + if expectedErrCode == 3: |
| 84 | + for cycle in range(20): |
| 85 | + logging.info("-----------------Current Iteration {}-------------------------".format(cycle+1)) |
| 86 | + setup_code = self.generate_unique_random_value(setupPinCode) |
| 87 | + errcode = await self.CommissionOnNetwork(setup_code) |
| 88 | + logging.info('Commissioning complete done. Successful? {}, errorcode = {}, cycle={}'.format( |
| 89 | + errcode.is_success, errcode, (cycle+1))) |
| 90 | + asserts.assert_false(errcode.is_success, 'Commissioning complete did not error as expected') |
| 91 | + asserts.assert_true(errcode.sdk_code == expectedErrCode, |
| 92 | + 'Unexpected error code returned from CommissioningComplete') |
| 93 | + |
| 94 | + elif expectedErrCode == 50: |
| 95 | + logging.info("-----------------Attempting connection expecting timeout-------------------------") |
| 96 | + errcode = await self.CommissionOnNetwork(setupPinCode) |
| 97 | + logging.info('Commissioning complete done. Successful? {}, errorcode = {}'.format(errcode.is_success, errcode)) |
| 98 | + asserts.assert_false(errcode.is_success, 'Commissioning complete did not error as expected') |
| 99 | + asserts.assert_true(errcode.sdk_code == expectedErrCode, 'Unexpected error code returned from CommissioningComplete') |
| 100 | + |
| 101 | + def pics_TC_CADMIN_1_9(self) -> list[str]: |
| 102 | + return ["CADMIN.S"] |
| 103 | + |
| 104 | + @async_test_body |
| 105 | + async def test_TC_CADMIN_1_9(self): |
| 106 | + self.step(1) |
| 107 | + |
| 108 | + # Establishing TH1 and TH2 |
| 109 | + self.th1 = self.default_controller |
| 110 | + self.discriminator = random.randint(0, 4095) |
| 111 | + th2_certificate_authority = self.certificate_authority_manager.NewCertificateAuthority() |
| 112 | + th2_fabric_admin = th2_certificate_authority.NewFabricAdmin(vendorId=0xFFF1, fabricId=self.th1.fabricId + 1) |
| 113 | + self.th2 = th2_fabric_admin.NewController(nodeId=2, useTestCommissioner=True) |
| 114 | + |
| 115 | + self.step(2) |
| 116 | + params = await self.OpenCommissioningWindow() |
| 117 | + setupPinCode = params.setupPinCode |
| 118 | + |
| 119 | + self.step(3) |
| 120 | + await self.CommissionAttempt(setupPinCode, expectedErrCode=0x03) |
| 121 | + # TODO: Found if we don't add sleep time after test completes that we get unexpected error code and response after the 21st iteration. |
| 122 | + # Link to Bug Filed: https://github.com/project-chip/connectedhomeip/issues/34383 |
| 123 | + sleep(1) |
| 124 | + |
| 125 | + self.step(4) |
| 126 | + await self.CommissionAttempt(setupPinCode, expectedErrCode=0x32) |
| 127 | + |
| 128 | + self.step(5) |
| 129 | + params = await self.OpenCommissioningWindow() |
| 130 | + |
| 131 | + self.step(6) |
| 132 | + revokeCmd = Clusters.AdministratorCommissioning.Commands.RevokeCommissioning() |
| 133 | + await self.th1.SendCommand(nodeid=self.dut_node_id, endpoint=0, payload=revokeCmd, timedRequestTimeoutMs=6000) |
| 134 | + # The failsafe cleanup is scheduled after the command completes, so give it a bit of time to do that |
| 135 | + sleep(1) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + default_matter_test_main() |
0 commit comments