|
| 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 logging |
| 19 | +import random |
| 20 | +import string |
| 21 | +from typing import Optional |
| 22 | + |
| 23 | +import chip.clusters as Clusters |
| 24 | +from chip.clusters.Types import NullValue |
| 25 | +from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main, type_matches |
| 26 | +from mobly import asserts |
| 27 | + |
| 28 | + |
| 29 | +class TC_CNET_4_4(MatterBaseTest): |
| 30 | + def steps_TC_CNET_4_4(self): |
| 31 | + return [TestStep("precondition", "TH is commissioned", is_commissioning=True), |
| 32 | + TestStep(1, 'TH reads from the DUT the Network Commissioning Cluster FeatureMap. If the FeatureMap does not include the WI flag (bit 0), skip the remaining steps in this test case'), |
| 33 | + TestStep(2, 'TH reads from the DUT the SupportedWifiBands attribute and saves as supported_wifi_bands'), |
| 34 | + TestStep(3, 'TH reads from the DUT the Networks attribute.'), |
| 35 | + TestStep(4, 'TH sends ScanNetworks command to the DUT with the SSID field set to null and Breadcrumb field set to 1'), |
| 36 | + TestStep(5, 'TH reads from the DUT the Breadcrumb attribute from the General Commissioning Cluster'), |
| 37 | + TestStep(6, 'TH sends ScanNetworks Command to the DUT with SSID field set to known_ssid and Breadcrumb field set to 2'), |
| 38 | + TestStep(7, 'TH reads Breadcrumb attribute from the General Commissioning Cluster'), |
| 39 | + TestStep(8, 'TH sends ScanNetworks Command to the DUT with SSID field set to a string of 31 random alphabetical characters and Breadcrumb field set to 2')] |
| 40 | + |
| 41 | + def def_TC_CNET_4_4(self): |
| 42 | + return '[TC-CNET-4.4] [Wi-Fi] Verification for ScanNetworks command [DUT-Server]' |
| 43 | + |
| 44 | + def pics_TC_CNET_4_4(self): |
| 45 | + return ['CNET.S'] |
| 46 | + |
| 47 | + @async_test_body |
| 48 | + async def test_TC_CNET_4_4(self): |
| 49 | + # Commissioning is already done |
| 50 | + self.step("precondition") |
| 51 | + |
| 52 | + cnet = Clusters.NetworkCommissioning |
| 53 | + attr = cnet.Attributes |
| 54 | + |
| 55 | + self.step(1) |
| 56 | + feature_map = await self.read_single_attribute_check_success(cluster=cnet, attribute=attr.FeatureMap) |
| 57 | + if not (feature_map & cnet.Bitmaps.Feature.kWiFiNetworkInterface): |
| 58 | + logging.info('Device does not support WiFi on endpoint, skipping remaining steps') |
| 59 | + self.skip_all_remaining_steps(2) |
| 60 | + return |
| 61 | + |
| 62 | + self.step(2) |
| 63 | + supported_wifi_bands = await self.read_single_attribute_check_success(cluster=cnet, attribute=attr.SupportedWiFiBands) |
| 64 | + |
| 65 | + self.step(3) |
| 66 | + networks = await self.read_single_attribute_check_success(cluster=cnet, attribute=attr.Networks) |
| 67 | + connected = [network for network in networks if network.connected is True] |
| 68 | + asserts.assert_greater_equal(len(connected), 1, "Did not find any connected networks on a commissioned device") |
| 69 | + known_ssid = connected[0].networkID |
| 70 | + |
| 71 | + async def scan_and_check(ssid_to_scan: Optional[bytes], breadcrumb: int, expect_results: bool = True): |
| 72 | + all_security = 0 |
| 73 | + for security_bitmask in cnet.Bitmaps.WiFiSecurityBitmap: |
| 74 | + all_security |= security_bitmask |
| 75 | + |
| 76 | + ssid = ssid_to_scan if ssid_to_scan is not None else NullValue |
| 77 | + cmd = cnet.Commands.ScanNetworks(ssid=ssid, breadcrumb=breadcrumb) |
| 78 | + scan_results = await self.send_single_cmd(cmd=cmd) |
| 79 | + asserts.assert_true(type_matches(scan_results, cnet.Commands.ScanNetworksResponse), |
| 80 | + "Unexpected value returned from scan network") |
| 81 | + logging.info(f"Scan results: {scan_results}") |
| 82 | + |
| 83 | + if scan_results.debugText: |
| 84 | + debug_text_len = len(scan_results.debug_text) |
| 85 | + asserts.assert_less_equal(debug_text_len, 512, f"DebugText length {debug_text_len} was out of range") |
| 86 | + |
| 87 | + if expect_results: |
| 88 | + asserts.assert_equal(scan_results.networkingStatus, cnet.Enums.NetworkCommissioningStatusEnum.kSuccess, |
| 89 | + f"ScanNetworks was expected to have succeeded, got {scan_results.networkingStatus} instead") |
| 90 | + asserts.assert_greater_equal(len(scan_results.wiFiScanResults), 1, "No responses returned from ScanNetwork command") |
| 91 | + else: |
| 92 | + asserts.assert_equal(scan_results.networkingStatus, cnet.Enums.NetworkCommissioningStatusEnum.kNetworkNotFound, |
| 93 | + f"ScanNetworks was expected to received NetworkNotFound(5), got {scan_results.networkingStatus} instead") |
| 94 | + return |
| 95 | + |
| 96 | + for network in scan_results.wiFiScanResults: |
| 97 | + asserts.assert_true((network.security & ~all_security) == 0, "Unexpected bitmap in the security field") |
| 98 | + asserts.assert_less_equal(len(network.ssid), 32, f"Returned SSID {network.ssid} is too long") |
| 99 | + if ssid_to_scan is not None: |
| 100 | + asserts.assert_equal(network.ssid, ssid_to_scan, "Unexpected SSID returned in directed scan") |
| 101 | + asserts.assert_true(type_matches(network.bssid, bytes), "Incorrect type for BSSID") |
| 102 | + asserts.assert_equal(len(network.bssid), 6, "Unexpected length of BSSID") |
| 103 | + # TODO: this is inherited from the old test plan, but we should match the channel to the supported band. This range is unreasonably large. |
| 104 | + asserts.assert_less_equal(network.channel, 65535, "Unexpected channel value") |
| 105 | + if network.wiFiBand: |
| 106 | + asserts.assert_true(network.wiFiBand in supported_wifi_bands, |
| 107 | + "Listed wiFiBand is not in supported_wifi_bands") |
| 108 | + if network.rssi: |
| 109 | + asserts.assert_greater_equal(network.rssi, -120, "RSSI out of range") |
| 110 | + asserts.assert_less_equal(network.rssi, 0, "RSSI out of range") |
| 111 | + |
| 112 | + self.step(4) |
| 113 | + await scan_and_check(ssid_to_scan=None, breadcrumb=1, expect_results=True) |
| 114 | + |
| 115 | + self.step(5) |
| 116 | + breadcrumb = await self.read_single_attribute_check_success(cluster=Clusters.GeneralCommissioning, attribute=Clusters.GeneralCommissioning.Attributes.Breadcrumb, endpoint=0) |
| 117 | + asserts.assert_equal(breadcrumb, 1, "Incorrect breadcrumb value") |
| 118 | + |
| 119 | + self.step(6) |
| 120 | + await scan_and_check(ssid_to_scan=known_ssid, breadcrumb=2, expect_results=True) |
| 121 | + |
| 122 | + self.step(7) |
| 123 | + breadcrumb = await self.read_single_attribute_check_success(cluster=Clusters.GeneralCommissioning, attribute=Clusters.GeneralCommissioning.Attributes.Breadcrumb, endpoint=0) |
| 124 | + asserts.assert_equal(breadcrumb, 2, "Incorrect breadcrumb value") |
| 125 | + |
| 126 | + self.step(8) |
| 127 | + random_ssid = ''.join(random.choice(string.ascii_letters) for _ in range(31)).encode("utf-8") |
| 128 | + await scan_and_check(ssid_to_scan=random_ssid, breadcrumb=2, expect_results=False) |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + default_matter_test_main() |
0 commit comments