repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2020 - The Android Open Source Project |
| 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 | from acts import signals |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 18 | from acts.controllers.access_point import setup_ap |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 19 | from acts.controllers.ap_lib import hostapd_constants |
| 20 | from acts.controllers.ap_lib import hostapd_security |
Xianyuan Jia | 24299b7 | 2020-10-21 13:52:47 -0700 | [diff] [blame] | 21 | from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 22 | from acts.utils import rand_ascii_str |
| 23 | |
| 24 | DISCONNECTED = "Disconnected" |
| 25 | CONNECTION_STOPPED = "ConnectionStopped" |
| 26 | CONNECTIONS_ENABLED = "ConnectionsEnabled" |
| 27 | CONNECTIONS_DISABLED = "ConnectionsDisabled" |
| 28 | WPA2 = "wpa2" |
| 29 | |
| 30 | |
| 31 | class StartStopClientConnectionsTest(WifiBaseTest): |
| 32 | """ Tests that we see the expected behavior with enabling and disabling |
| 33 | client connections |
| 34 | |
| 35 | Test Bed Requirement: |
| 36 | * One or more Fuchsia devices |
| 37 | * One Access Point |
| 38 | """ |
| 39 | def setup_class(self): |
| 40 | super().setup_class() |
| 41 | # Start an AP with a hidden network |
| 42 | self.ssid = rand_ascii_str(hostapd_constants.AP_SSID_LENGTH_2G) |
| 43 | self.access_point = self.access_points[0] |
| 44 | self.password = rand_ascii_str( |
| 45 | hostapd_constants.AP_PASSPHRASE_LENGTH_2G) |
| 46 | self.security_type = WPA2 |
| 47 | security = hostapd_security.Security(security_mode=self.security_type, |
| 48 | password=self.password) |
| 49 | |
| 50 | self.access_point.stop_all_aps() |
| 51 | # TODO(63719) use varying values for AP that shouldn't affect the test. |
| 52 | setup_ap(self.access_point, |
| 53 | 'whirlwind', |
| 54 | hostapd_constants.AP_DEFAULT_CHANNEL_5G, |
| 55 | self.ssid, |
| 56 | security=security) |
| 57 | |
| 58 | if len(self.fuchsia_devices) < 1: |
| 59 | raise EnvironmentError("No Fuchsia devices found.") |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 60 | for fd in self.fuchsia_devices: |
| 61 | fd.configure_wlan(association_mechanism='policy', |
| 62 | preserve_saved_networks=True) |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 63 | |
| 64 | def setup_test(self): |
| 65 | for fd in self.fuchsia_devices: |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 66 | if not fd.wlan_policy_controller.remove_all_networks(): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 67 | raise EnvironmentError( |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 68 | "Failed to remove all networks in setup") |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 69 | |
| 70 | def teardown_class(self): |
| 71 | self.access_point.stop_all_aps() |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 72 | |
| 73 | def connect_and_validate(self, fd, ssid, security_type, expected_response): |
| 74 | """ Sends a connect request to the device and verifies we get a response |
| 75 | without error. This does not validate that a connection will be |
| 76 | attempted. This will fail the test if there is an error sending the |
| 77 | connect request, or if we don't get the expected connect response.""" |
| 78 | result_connect = fd.wlan_policy_lib.wlanConnect(ssid, security_type) |
| 79 | if result_connect.get("error") != None: |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 80 | self.log.error("Error occurred requesting a connection: %s" % |
| 81 | result_connect.get("error")) |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 82 | raise EnvironmentError("Failed to send connect request") |
| 83 | response = result_connect.get("result") |
| 84 | if response != expected_response: |
| 85 | self.log.error( |
| 86 | "Incorrect connect request response. Expected: \"%s\", Actual: %s" |
| 87 | % (expected_response, response)) |
| 88 | raise signals.TestFailure( |
| 89 | "Failed to get expected connect response") |
| 90 | |
| 91 | def test_stop_client_connections_update(self): |
| 92 | for fd in self.fuchsia_devices: |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 93 | if not fd.wlan_policy_controller.stop_client_connections(): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 94 | raise EnvironmentError("Failed to stop client connecions") |
| 95 | |
| 96 | # Check that the most recent update says that the device is not |
| 97 | # connected to anything and client connections are disabled |
| 98 | fd.wlan_policy_lib.wlanSetNewListener() |
| 99 | result_update = fd.wlan_policy_lib.wlanGetUpdate() |
| 100 | if result_update.get("error") != None: |
| 101 | self.log.error("Error occurred getting status update: %s" % |
| 102 | result_update.get("error")) |
| 103 | raise EnvironmentError("Failed to get update") |
| 104 | |
| 105 | expected_update = {"networks": [], "state": CONNECTIONS_DISABLED} |
| 106 | if result_update.get("result") != expected_update: |
| 107 | self.log.error( |
| 108 | "Most recent status update does not indicate client " |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 109 | "connections have stopped. Expected update: %s Actual update: %s" |
| 110 | % (expected_update, result_update.get('result'))) |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 111 | raise signals.TestFailure( |
| 112 | "Incorrect update after stopping client connections") |
| 113 | |
| 114 | def test_start_client_connections_update(self): |
| 115 | for fd in self.fuchsia_devices: |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 116 | if not fd.wlan_policy_controller.start_client_connections(): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 117 | raise EnvironmentError("Failed to start client connecions") |
| 118 | |
| 119 | # Check that the most recent update says that the device is not |
| 120 | # connected to anything and client connections are disabled |
| 121 | fd.wlan_policy_lib.wlanSetNewListener() |
| 122 | result_update = fd.wlan_policy_lib.wlanGetUpdate() |
| 123 | if result_update.get("error") != None: |
| 124 | self.log.error("Error occurred getting status update: %s" % |
| 125 | result_update.get("error")) |
| 126 | raise EnvironmentError("Failed to get update") |
| 127 | |
| 128 | expected_update = {"networks": [], "state": CONNECTIONS_ENABLED} |
| 129 | if result_update.get("result") != expected_update: |
| 130 | self.log.error( |
| 131 | "Most recent status update does not indicate client " |
| 132 | "connections are enabled. Expected update: %s\nActual update:" |
| 133 | % (expected_update, result_update)) |
| 134 | raise signals.TestFailure( |
| 135 | "Incorrect update after starting client connections") |
| 136 | |
| 137 | def test_stop_client_connections_rejects_connections(self): |
| 138 | # Test that if we turn client connections off, our requests to connect |
| 139 | # are rejected. |
| 140 | for fd in self.fuchsia_devices: |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 141 | if not fd.wlan_policy_controller.stop_client_connections(): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 142 | raise EnvironmentError("Failed to stop client connecions") |
| 143 | |
| 144 | # Save the network, otherwise connecting may fail because the |
| 145 | # network is not saved instead of client connections being off |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 146 | if not fd.wlan_policy_controller.save_network( |
| 147 | self.ssid, self.security_type, password=self.password): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 148 | raise EnvironmentError("Failed to save network") |
| 149 | expected_response = "RejectedIncompatibleMode" |
| 150 | self.connect_and_validate(fd, self.ssid, self.security_type, |
| 151 | expected_response) |
| 152 | |
| 153 | def test_start_stop_client_connections(self): |
| 154 | # Test that if we turn client connections on the device will connect, |
| 155 | # and if we turn of client connections the device will disconnect. |
| 156 | for fd in self.fuchsia_devices: |
| 157 | # Start client connections and check that we can |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 158 | if not fd.wlan_policy_controller.save_network( |
| 159 | self.ssid, self.security_type, password=self.password): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 160 | raise EnvironmentError("Failed to save network") |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 161 | if not fd.wlan_policy_controller.start_client_connections(): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 162 | raise EnvironmentError("Failed to start client connections") |
| 163 | |
| 164 | expected_response = "Acknowledged" |
| 165 | self.connect_and_validate(fd, self.ssid, self.security_type, |
| 166 | expected_response) |
| 167 | |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 168 | if not fd.wlan_policy_controller.wait_for_connect( |
| 169 | self.ssid, self.security_type): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 170 | raise signals.TestFailure( |
| 171 | "Failed to connect after starting client connections") |
| 172 | |
| 173 | # Stop client connections again and check that we disconnect |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 174 | if not fd.wlan_policy_controller.stop_client_connections(): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 175 | raise EnvironmentError("Failed to stop client connecions") |
Hayden Nix | fe64305 | 2020-12-09 00:50:05 +0000 | [diff] [blame^] | 176 | if not fd.wlan_policy_controller.wait_for_disconnect( |
| 177 | self.ssid, self.security_type, DISCONNECTED, |
| 178 | CONNECTION_STOPPED): |
repo sync tools/test/connectivity | ae92d75 | 2020-10-27 12:42:38 -0700 | [diff] [blame] | 179 | raise signals.TestFailure( |
| 180 | "Failed to disconnect after client connections stopped") |