| #!/usr/bin/env python3 |
| # |
| # Copyright 2022 The Fuchsia Authors |
| # |
| # 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. |
| |
| import logging |
| |
| from honeydew.typing.wlan import ( |
| ConnectionState, |
| DisconnectStatus, |
| RequestStatus, |
| SecurityType, |
| WlanClientState, |
| ) |
| from mobly import signals, test_runner |
| |
| from antlion.controllers.access_point import setup_ap |
| from antlion.controllers.ap_lib import hostapd_constants, hostapd_security |
| from antlion.controllers.fuchsia_lib.lib_controllers.wlan_policy_controller import ( |
| WlanPolicyControllerError, |
| ) |
| from antlion.test_utils.wifi import base_test |
| from antlion.utils import rand_ascii_str |
| |
| |
| class StartStopClientConnectionsTest(base_test.WifiBaseTest): |
| """Tests that we see the expected behavior with enabling and disabling |
| client connections |
| |
| Test Bed Requirement: |
| * One or more Fuchsia devices |
| * One Access Point |
| """ |
| |
| def setup_class(self) -> None: |
| super().setup_class() |
| self.log = logging.getLogger() |
| # Start an AP with a hidden network |
| self.ssid = rand_ascii_str(hostapd_constants.AP_SSID_LENGTH_2G) |
| self.access_point = self.access_points[0] |
| self.password = rand_ascii_str(hostapd_constants.AP_PASSPHRASE_LENGTH_2G) |
| self.security_type = SecurityType.WPA2 |
| security = hostapd_security.Security( |
| security_mode=hostapd_security.SecurityMode.WPA2, password=self.password |
| ) |
| |
| self.access_point.stop_all_aps() |
| # TODO(63719) use varying values for AP that shouldn't affect the test. |
| setup_ap( |
| self.access_point, |
| "whirlwind", |
| hostapd_constants.AP_DEFAULT_CHANNEL_5G, |
| self.ssid, |
| security=security, |
| ) |
| |
| if len(self.fuchsia_devices) < 1: |
| raise EnvironmentError("No Fuchsia devices found.") |
| for fd in self.fuchsia_devices: |
| fd.configure_wlan( |
| association_mechanism="policy", preserve_saved_networks=True |
| ) |
| |
| def setup_test(self) -> None: |
| for fd in self.fuchsia_devices: |
| fd.sl4f.wlan_policy_lib.remove_all_networks() |
| fd.wlan_policy_controller.wait_for_no_connections() |
| |
| def teardown_class(self) -> None: |
| self.access_point.stop_all_aps() |
| |
| def test_stop_client_connections_update(self) -> None: |
| """Test that we can stop client connections. |
| |
| The fuchsia device always starts client connections during configure_wlan. We |
| verify first that we are in a client connections enabled state. |
| """ |
| for fd in self.fuchsia_devices: |
| fd.wlan_policy_controller.wait_for_client_state( |
| WlanClientState.CONNECTIONS_ENABLED |
| ) |
| fd.sl4f.wlan_policy_lib.stop_client_connections() |
| try: |
| fd.wlan_policy_controller.wait_for_client_state( |
| WlanClientState.CONNECTIONS_DISABLED |
| ) |
| except WlanPolicyControllerError as e: |
| raise signals.TestFailure("Failed to stop client connections.") from e |
| |
| def test_start_client_connections_update(self) -> None: |
| """Test that we can start client connections.""" |
| for fd in self.fuchsia_devices: |
| fd.sl4f.wlan_policy_lib.stop_client_connections() |
| fd.wlan_policy_controller.wait_for_client_state( |
| WlanClientState.CONNECTIONS_DISABLED |
| ) |
| fd.sl4f.wlan_policy_lib.start_client_connections() |
| try: |
| fd.wlan_policy_controller.wait_for_client_state( |
| WlanClientState.CONNECTIONS_ENABLED |
| ) |
| except WlanPolicyControllerError as e: |
| raise signals.TestFailure("Failed to start client connections.") from e |
| |
| def test_stop_client_connections_rejects_connections(self) -> None: |
| """Test that if client connections are disabled connection attempts fail.""" |
| for fd in self.fuchsia_devices: |
| fd.sl4f.wlan_policy_lib.save_network( |
| self.ssid, self.security_type, self.password |
| ) |
| fd.sl4f.wlan_policy_lib.stop_client_connections() |
| fd.wlan_policy_controller.wait_for_client_state( |
| WlanClientState.CONNECTIONS_DISABLED |
| ) |
| request_status = fd.sl4f.wlan_policy_lib.connect( |
| self.ssid, self.security_type |
| ) |
| if request_status is not RequestStatus.REJECTED_INCOMPATIBLE_MODE: |
| raise signals.TestFailure( |
| "Connection request not rejected as incompatible." |
| ) |
| |
| def test_start_stop_client_connections(self) -> None: |
| """Test automated behavior when starting/stoping client connections. |
| |
| When starting and stopping the client connections the device should connect and |
| disconnect from the saved network. |
| """ |
| for fd in self.fuchsia_devices: |
| fd.sl4f.wlan_policy_lib.save_network( |
| self.ssid, self.security_type, self.password |
| ) |
| fd.sl4f.wlan_policy_lib.start_client_connections() |
| fd.wlan_policy_controller.wait_for_client_state( |
| WlanClientState.CONNECTIONS_ENABLED |
| ) |
| fd.sl4f.wlan_policy_lib.connect(self.ssid, self.security_type) |
| try: |
| fd.wlan_policy_controller.wait_for_network_state( |
| self.ssid, ConnectionState.CONNECTED |
| ) |
| except WlanPolicyControllerError as e: |
| raise signals.TestFailure( |
| "network not in connected state after client connections started" |
| ) from e |
| |
| fd.sl4f.wlan_policy_lib.stop_client_connections() |
| fd.wlan_policy_controller.wait_for_client_state( |
| WlanClientState.CONNECTIONS_DISABLED |
| ) |
| try: |
| fd.wlan_policy_controller.wait_for_network_state( |
| self.ssid, |
| ConnectionState.DISCONNECTED, |
| DisconnectStatus.CONNECTION_STOPPED, |
| ) |
| except WlanPolicyControllerError as e: |
| raise signals.TestFailure( |
| "network not in disconnected state after client connections stopped" |
| ) from e |
| |
| |
| if __name__ == "__main__": |
| test_runner.main() |