|
| 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 | + |
| 18 | +import chip.clusters as Clusters |
| 19 | +from chip.tlv import TLVReader |
| 20 | +from chip.utils import CommissioningBuildingBlocks |
| 21 | +from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main |
| 22 | +from mobly import asserts |
| 23 | +from test_plan_support import (commission_from_existing, commission_if_required, read_attribute, remove_fabric, |
| 24 | + verify_commissioning_successful, verify_success) |
| 25 | + |
| 26 | + |
| 27 | +def verify_fabric(controller: str) -> str: |
| 28 | + return (f"- Verify there is one entry returned. Verify FabricIndex matches `fabric_index_{controller}`.\n" |
| 29 | + f"- Verify the RootPublicKey matches the public key for rcac_{controller}.\n" |
| 30 | + f"- Verify the VendorID matches the vendor ID for {controller}.\n" |
| 31 | + f"- Verify the FabricID matches the fabricID for {controller}") |
| 32 | + |
| 33 | + |
| 34 | +class TC_OPCREDS_3_2(MatterBaseTest): |
| 35 | + def desc_TC_OPCREDS_3_2(self): |
| 36 | + return " Attribute-CurrentFabricIndex validation [DUTServer]" |
| 37 | + |
| 38 | + def steps_TC_OPCREDS_3_2(self): |
| 39 | + return [TestStep(0, commission_if_required('CR1'), is_commissioning=True), |
| 40 | + TestStep(1, f"{commission_from_existing('CR1', 'CR2')}\n. Save the FabricIndex from the NOCResponse as `fabric_index_CR2`.", |
| 41 | + verify_commissioning_successful()), |
| 42 | + TestStep(2, f"{commission_from_existing('CR1', 'CR3')}\n. Save the FabricIndex from the NOCResponse as `fabric_index_CR3`.", |
| 43 | + verify_commissioning_successful()), |
| 44 | + TestStep(3, f"CR2 {read_attribute('CurrentFabricIndex')}", |
| 45 | + "Verify the returned value is `fabric_index_CR2`"), |
| 46 | + TestStep(4, f"CR3 {read_attribute('CurrentFabricIndex')}", |
| 47 | + "Verify the returned value is `fabric_index_CR3`"), |
| 48 | + TestStep( |
| 49 | + 5, f"CR2 {read_attribute('Fabrics')} using a fabric-filtered read", verify_fabric('CR2')), |
| 50 | + TestStep( |
| 51 | + 6, f"CR3 {read_attribute('Fabrics')} using a fabric-filtered read", verify_fabric('CR3')), |
| 52 | + TestStep(7, remove_fabric( |
| 53 | + 'fabric_index_CR2', 'CR1'), verify_success()), |
| 54 | + TestStep(8, remove_fabric( |
| 55 | + 'fabric_index_CR3', 'CR1'), verify_success()), |
| 56 | + ] |
| 57 | + |
| 58 | + @async_test_body |
| 59 | + async def test_TC_OPCREDS_3_2(self): |
| 60 | + opcreds = Clusters.OperationalCredentials |
| 61 | + |
| 62 | + self.step(0) |
| 63 | + |
| 64 | + self.step(1) |
| 65 | + dev_ctrl = self.default_controller |
| 66 | + |
| 67 | + new_certificate_authority = self.certificate_authority_manager.NewCertificateAuthority() |
| 68 | + cr2_vid = 0xFFF2 |
| 69 | + cr2_fabricId = 2222 |
| 70 | + cr2_new_fabric_admin = new_certificate_authority.NewFabricAdmin( |
| 71 | + vendorId=cr2_vid, fabricId=cr2_fabricId) |
| 72 | + cr2_nodeid = self.default_controller.nodeId+1 |
| 73 | + cr2_dut_node_id = self.dut_node_id+1 |
| 74 | + |
| 75 | + cr2_new_admin_ctrl = cr2_new_fabric_admin.NewController( |
| 76 | + nodeId=cr2_nodeid) |
| 77 | + success, nocResp, rcacResp = await CommissioningBuildingBlocks.AddNOCForNewFabricFromExisting( |
| 78 | + commissionerDevCtrl=dev_ctrl, newFabricDevCtrl=cr2_new_admin_ctrl, |
| 79 | + existingNodeId=self.dut_node_id, newNodeId=cr2_dut_node_id |
| 80 | + ) |
| 81 | + |
| 82 | + fabric_index_CR2 = nocResp.fabricIndex |
| 83 | + tlvReaderRCAC_CR2 = TLVReader(rcacResp).get()["Any"] |
| 84 | + rcac_CR2 = tlvReaderRCAC_CR2[9] # public key is field 9 |
| 85 | + |
| 86 | + self.step(2) |
| 87 | + new_certificate_authority = self.certificate_authority_manager.NewCertificateAuthority() |
| 88 | + cr3_vid = 0xFFF3 |
| 89 | + cr3_fabricId = 3333 |
| 90 | + cr3_new_fabric_admin = new_certificate_authority.NewFabricAdmin( |
| 91 | + vendorId=cr3_vid, fabricId=cr3_fabricId) |
| 92 | + cr3_nodeid = self.default_controller.nodeId+2 |
| 93 | + cr3_dut_node_id = self.dut_node_id+2 |
| 94 | + |
| 95 | + cr3_new_admin_ctrl = cr3_new_fabric_admin.NewController( |
| 96 | + nodeId=cr3_nodeid) |
| 97 | + success, nocResp, rcacResp = await CommissioningBuildingBlocks.AddNOCForNewFabricFromExisting( |
| 98 | + commissionerDevCtrl=dev_ctrl, newFabricDevCtrl=cr3_new_admin_ctrl, |
| 99 | + existingNodeId=self.dut_node_id, newNodeId=cr3_dut_node_id |
| 100 | + ) |
| 101 | + |
| 102 | + fabric_index_CR3 = nocResp.fabricIndex |
| 103 | + tlvReaderRCAC_CR3 = TLVReader(rcacResp).get()["Any"] |
| 104 | + rcac_CR3 = tlvReaderRCAC_CR3[9] |
| 105 | + |
| 106 | + self.step(3) |
| 107 | + cr2_read_fabricIndex = await self.read_single_attribute_check_success( |
| 108 | + dev_ctrl=cr2_new_admin_ctrl, |
| 109 | + node_id=cr2_dut_node_id, |
| 110 | + cluster=opcreds, |
| 111 | + attribute=opcreds.Attributes.CurrentFabricIndex |
| 112 | + ) |
| 113 | + |
| 114 | + asserts.assert_equal(fabric_index_CR2, cr2_read_fabricIndex, |
| 115 | + "Fail fabric_index_CR2 is not equal to read fabricIndex from CR2") |
| 116 | + |
| 117 | + self.step(4) |
| 118 | + cr3_read_fabricIndex = await self.read_single_attribute_check_success( |
| 119 | + dev_ctrl=cr3_new_admin_ctrl, |
| 120 | + node_id=cr3_dut_node_id, |
| 121 | + cluster=opcreds, |
| 122 | + attribute=opcreds.Attributes.CurrentFabricIndex |
| 123 | + ) |
| 124 | + |
| 125 | + asserts.assert_equal(fabric_index_CR3, cr3_read_fabricIndex, |
| 126 | + "Fail fabric_index_CR3 is not equal to read fabricIndex from CR3") |
| 127 | + |
| 128 | + self.step(5) |
| 129 | + cr2_fabric = await self.read_single_attribute_check_success( |
| 130 | + dev_ctrl=cr2_new_admin_ctrl, |
| 131 | + node_id=cr2_dut_node_id, |
| 132 | + cluster=opcreds, |
| 133 | + attribute=opcreds.Attributes.Fabrics, |
| 134 | + fabric_filtered=True |
| 135 | + ) |
| 136 | + |
| 137 | + for fabric in cr2_fabric: |
| 138 | + cr2_fabric_fabricIndex = fabric.fabricIndex |
| 139 | + cr2_fabric_rootPublicKey = fabric.rootPublicKey |
| 140 | + cr2_fabric_vendorId = fabric.vendorID |
| 141 | + cr2_fabric_fabricId = fabric.fabricID |
| 142 | + |
| 143 | + asserts.assert_equal(cr2_fabric_fabricIndex, |
| 144 | + fabric_index_CR2, "Unexpected CR2 fabric index") |
| 145 | + asserts.assert_equal(cr2_fabric_rootPublicKey, rcac_CR2, |
| 146 | + "Unexpected RootPublicKey does not match with rcac_CR2") |
| 147 | + asserts.assert_equal(cr2_fabric_vendorId, cr2_vid, |
| 148 | + "Unexpected vendorId does not match with CR2 VendorID") |
| 149 | + asserts.assert_equal(cr2_fabric_fabricId, cr2_fabricId, |
| 150 | + "Unexpected fabricId does not match with CR2 fabricID") |
| 151 | + |
| 152 | + self.step(6) |
| 153 | + cr3_fabric = await self.read_single_attribute_check_success( |
| 154 | + dev_ctrl=cr3_new_admin_ctrl, |
| 155 | + node_id=cr3_dut_node_id, |
| 156 | + cluster=opcreds, |
| 157 | + attribute=opcreds.Attributes.Fabrics, |
| 158 | + fabric_filtered=True |
| 159 | + ) |
| 160 | + |
| 161 | + for fabric in cr3_fabric: |
| 162 | + cr3_fabric_fabricIndex = fabric.fabricIndex |
| 163 | + cr3_fabric_rootPublicKey = fabric.rootPublicKey |
| 164 | + cr3_fabric_vendorId = fabric.vendorID |
| 165 | + cr3_fabric_fabricId = fabric.fabricID |
| 166 | + |
| 167 | + asserts.assert_equal(cr3_fabric_fabricIndex, |
| 168 | + fabric_index_CR3, "Unexpected CR3 fabric index") |
| 169 | + asserts.assert_equal(cr3_fabric_rootPublicKey, rcac_CR3, |
| 170 | + "Unexpected RootPublicKey does not match with rcac_CR3") |
| 171 | + asserts.assert_equal(cr3_fabric_vendorId, cr3_vid, |
| 172 | + "Unexpected vendorId does not match with CR3 VendorID") |
| 173 | + asserts.assert_equal(cr3_fabric_fabricId, cr3_fabricId, |
| 174 | + "Unexpected fabricId does not match with CR3 fabricID") |
| 175 | + |
| 176 | + self.step(7) |
| 177 | + cmd = opcreds.Commands.RemoveFabric(fabric_index_CR2) |
| 178 | + resp = await self.send_single_cmd(cmd=cmd) |
| 179 | + asserts.assert_equal( |
| 180 | + resp.statusCode, opcreds.Enums.NodeOperationalCertStatusEnum.kOk) |
| 181 | + |
| 182 | + self.step(8) |
| 183 | + cmd = opcreds.Commands.RemoveFabric(fabric_index_CR3) |
| 184 | + resp = await self.send_single_cmd(cmd=cmd) |
| 185 | + asserts.assert_equal( |
| 186 | + resp.statusCode, opcreds.Enums.NodeOperationalCertStatusEnum.kOk) |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + default_matter_test_main() |
0 commit comments