Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3.4 |
| 2 | # |
| 3 | # Copyright 2018 - 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 | import itertools |
| 18 | import pprint |
| 19 | import queue |
| 20 | import time |
| 21 | |
| 22 | import acts.base_test |
| 23 | import acts.signals as signals |
| 24 | import acts.test_utils.wifi.wifi_test_utils as wutils |
| 25 | import acts.utils |
| 26 | |
| 27 | from acts import asserts |
Roshan Pius | 3e3bd34 | 2019-01-09 13:55:17 -0800 | [diff] [blame] | 28 | from acts.controllers.android_device import SL4A_APK_NAME |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 29 | from acts.test_decorators import test_tracker_info |
| 30 | from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest |
| 31 | from acts.test_utils.wifi import wifi_constants |
| 32 | |
| 33 | WifiEnums = wutils.WifiEnums |
| 34 | |
| 35 | # Default timeout used for reboot, toggle WiFi and Airplane mode, |
| 36 | # for the system to settle down after the operation. |
| 37 | DEFAULT_TIMEOUT = 10 |
| 38 | |
| 39 | class WifiNetworkSuggestionTest(WifiBaseTest): |
| 40 | """Tests for WifiNetworkSuggestion API surface. |
| 41 | |
| 42 | Test Bed Requirement: |
| 43 | * one Android device |
| 44 | * Several Wi-Fi networks visible to the device, including an open Wi-Fi |
| 45 | network. |
| 46 | """ |
| 47 | |
| 48 | def __init__(self, controllers): |
| 49 | WifiBaseTest.__init__(self, controllers) |
| 50 | |
| 51 | def setup_class(self): |
| 52 | self.dut = self.android_devices[0] |
| 53 | wutils.wifi_test_device_init(self.dut) |
| 54 | req_params = [] |
| 55 | opt_param = [ |
| 56 | "open_network", "reference_networks" |
| 57 | ] |
| 58 | self.unpack_userparams( |
| 59 | req_param_names=req_params, opt_param_names=opt_param) |
| 60 | |
| 61 | if "AccessPoint" in self.user_params: |
| 62 | self.legacy_configure_ap_and_start(wpa_network=True, |
| 63 | wep_network=True) |
| 64 | |
| 65 | asserts.assert_true( |
| 66 | len(self.reference_networks) > 0, |
| 67 | "Need at least one reference network with psk.") |
| 68 | self.wpa_psk_2g = self.reference_networks[0]["2g"] |
| 69 | self.wpa_psk_5g = self.reference_networks[0]["5g"] |
| 70 | self.open_2g = self.open_network[0]["2g"] |
| 71 | self.open_5g = self.open_network[0]["5g"] |
| 72 | self.dut.droid.wifiRemoveNetworkSuggestions([]) |
| 73 | |
| 74 | def setup_test(self): |
| 75 | self.dut.droid.wakeLockAcquireBright() |
| 76 | self.dut.droid.wakeUpNow() |
Roshan Pius | 89b22bd | 2019-01-24 14:12:49 -0800 | [diff] [blame^] | 77 | self.clear_deleted_ephemeral_networks() |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 78 | wutils.wifi_toggle_state(self.dut, True) |
| 79 | |
| 80 | def teardown_test(self): |
| 81 | self.dut.droid.wakeLockRelease() |
| 82 | self.dut.droid.goToSleepNow() |
| 83 | self.dut.droid.wifiRemoveNetworkSuggestions([]) |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 84 | self.dut.droid.wifiDisconnect() |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 85 | wutils.reset_wifi(self.dut) |
| 86 | self.dut.ed.clear_all_events() |
| 87 | |
| 88 | def on_fail(self, test_name, begin_time): |
| 89 | self.dut.take_bug_report(test_name, begin_time) |
| 90 | self.dut.cat_adb_log(test_name, begin_time) |
| 91 | |
| 92 | def teardown_class(self): |
| 93 | if "AccessPoint" in self.user_params: |
| 94 | del self.user_params["reference_networks"] |
| 95 | del self.user_params["open_network"] |
| 96 | |
| 97 | """Helper Functions""" |
Roshan Pius | 3e3bd34 | 2019-01-09 13:55:17 -0800 | [diff] [blame] | 98 | def set_approved(self, approved): |
| 99 | self.dut.log.debug("Setting suggestions from sl4a app " |
| 100 | + "approved" if approved else "not approved") |
| 101 | self.dut.adb.shell("cmd wifi network-suggestions-set-user-approved" |
| 102 | + " " + SL4A_APK_NAME |
| 103 | + " " + ("yes" if approved else "no")) |
| 104 | |
| 105 | def is_approved(self): |
| 106 | is_approved_str = self.dut.adb.shell( |
| 107 | "cmd wifi network-suggestions-has-user-approved" |
| 108 | + " " + SL4A_APK_NAME) |
| 109 | return True if (is_approved_str == "yes") else False |
| 110 | |
Roshan Pius | 89b22bd | 2019-01-24 14:12:49 -0800 | [diff] [blame^] | 111 | def clear_deleted_ephemeral_networks(self): |
| 112 | self.dut.log.debug("Clearing deleted ephemeral networks") |
| 113 | self.dut.adb.shell( |
| 114 | "cmd wifi clear-deleted-ephemeral-networks") |
| 115 | |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 116 | def add_suggestions_and_ensure_connection(self, network_suggestions, |
| 117 | expected_ssid, |
| 118 | expect_post_connection_broadcast): |
| 119 | self.dut.log.info("Adding network suggestions"); |
| 120 | asserts.assert_true( |
| 121 | self.dut.droid.wifiAddNetworkSuggestions(network_suggestions), |
| 122 | "Failed to add suggestions") |
Roshan Pius | 3e3bd34 | 2019-01-09 13:55:17 -0800 | [diff] [blame] | 123 | # Enable suggestions by the app. |
| 124 | self.dut.log.debug("Enabling suggestions from test"); |
| 125 | self.set_approved(True) |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 126 | wutils.wait_for_connect(self.dut, expected_ssid) |
| 127 | |
| 128 | if expect_post_connection_broadcast is None: |
| 129 | return; |
| 130 | |
| 131 | # Check if we expected to get the broadcast. |
| 132 | try: |
| 133 | self.dut.droid.wifiStartTrackingStateChange() |
| 134 | event = self.dut.ed.pop_event( |
| 135 | wifi_constants.WIFI_NETWORK_SUGGESTION_POST_CONNECTION, 60) |
| 136 | self.dut.droid.wifiStopTrackingStateChange() |
| 137 | except queue.Empty: |
| 138 | if expect_post_connection_broadcast: |
| 139 | raise signals.TestFailure( |
| 140 | "Did not receive post connection broadcast") |
| 141 | else: |
| 142 | if not expect_post_connection_broadcast: |
| 143 | raise signals.TestFailure( |
| 144 | "Received post connection broadcast") |
| 145 | |
| 146 | |
Roshan Pius | f028bd3 | 2018-12-06 15:18:21 -0800 | [diff] [blame] | 147 | @test_tracker_info(uuid="bda8ed20-4382-4380-831a-64cf77eca108") |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 148 | def test_connect_to_wpa_psk_2g(self): |
| 149 | """ Adds a network suggestion and ensure that the device connected. |
| 150 | |
| 151 | Steps: |
| 152 | 1. Send a network suggestion to the device. |
| 153 | 2. Wait for the device to connect to it. |
| 154 | 3. Ensure that we did not receive the post connection broadcast |
| 155 | (isAppInteractionRequired = False). |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 156 | 4. Remove the suggestions and ensure the device does not connect back. |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 157 | """ |
| 158 | self.add_suggestions_and_ensure_connection( |
| 159 | [self.wpa_psk_2g], self.wpa_psk_2g[WifiEnums.SSID_KEY], |
| 160 | False) |
| 161 | self.dut.log.info("Removing network suggestions"); |
| 162 | asserts.assert_true( |
| 163 | self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_2g]), |
| 164 | "Failed to remove suggestions") |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 165 | # Ensure we did not disconnect |
| 166 | wutils.ensure_no_disconnect(self.dut) |
| 167 | |
| 168 | # Trigger a disconnect and wait for the disconnect. |
| 169 | self.dut.droid.wifiDisconnect() |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 170 | wutils.wait_for_disconnect(self.dut) |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 171 | self.dut.ed.clear_all_events() |
| 172 | |
| 173 | # Now ensure that we didn't connect back. |
| 174 | asserts.assert_false( |
| 175 | wutils.wait_for_connect(self.dut, |
| 176 | self.wpa_psk_2g[WifiEnums.SSID_KEY], |
| 177 | assert_on_fail=False), |
| 178 | "Device should not connect back") |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 179 | |
| 180 | |
Roshan Pius | f028bd3 | 2018-12-06 15:18:21 -0800 | [diff] [blame] | 181 | @test_tracker_info(uuid="b1d27eea-23c8-4c4f-b944-ef118e4cc35f") |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 182 | def test_connect_to_wpa_psk_2g_with_post_connection_broadcast(self): |
| 183 | """ Adds a network suggestion and ensure that the device connected. |
| 184 | |
| 185 | Steps: |
| 186 | 1. Send a network suggestion to the device with |
| 187 | isAppInteractionRequired set. |
| 188 | 2. Wait for the device to connect to it. |
| 189 | 3. Ensure that we did receive the post connection broadcast |
| 190 | (isAppInteractionRequired = True). |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 191 | 4. Remove the suggestions and ensure the device does not connect back. |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 192 | """ |
| 193 | network_suggestion = self.wpa_psk_2g |
| 194 | network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True |
| 195 | self.add_suggestions_and_ensure_connection( |
| 196 | [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY], |
| 197 | True) |
| 198 | self.dut.log.info("Removing network suggestions"); |
| 199 | asserts.assert_true( |
| 200 | self.dut.droid.wifiRemoveNetworkSuggestions([network_suggestion]), |
| 201 | "Failed to remove suggestions") |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 202 | # Ensure we did not disconnect |
| 203 | wutils.ensure_no_disconnect(self.dut) |
| 204 | |
| 205 | # Trigger a disconnect and wait for the disconnect. |
| 206 | self.dut.droid.wifiDisconnect() |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 207 | wutils.wait_for_disconnect(self.dut) |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 208 | self.dut.ed.clear_all_events() |
| 209 | |
| 210 | # Now ensure that we didn't connect back. |
| 211 | asserts.assert_false( |
| 212 | wutils.wait_for_connect(self.dut, |
| 213 | self.wpa_psk_2g[WifiEnums.SSID_KEY], |
| 214 | assert_on_fail=False), |
| 215 | "Device should not connect back") |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 216 | |
| 217 | |
Roshan Pius | f028bd3 | 2018-12-06 15:18:21 -0800 | [diff] [blame] | 218 | @test_tracker_info(uuid="a036a24d-29c0-456d-ae6a-afdde34da710") |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 219 | def test_connect_to_wpa_psk_5g_reboot_config_store(self): |
| 220 | """ |
| 221 | Adds a network suggestion and ensure that the device connects to it |
| 222 | after reboot. |
| 223 | |
| 224 | Steps: |
| 225 | 1. Send a network suggestion to the device. |
| 226 | 2. Wait for the device to connect to it. |
| 227 | 3. Ensure that we did not receive the post connection broadcast |
| 228 | (isAppInteractionRequired = False). |
| 229 | 4. Reboot the device. |
| 230 | 5. Wait for the device to connect to back to it. |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 231 | 6. Remove the suggestions and ensure the device does not connect back. |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 232 | """ |
| 233 | self.add_suggestions_and_ensure_connection( |
| 234 | [self.wpa_psk_5g], self.wpa_psk_5g[WifiEnums.SSID_KEY], |
| 235 | None) |
| 236 | |
| 237 | # Reboot and wait for connection back to the same suggestion. |
| 238 | self.dut.reboot() |
| 239 | time.sleep(DEFAULT_TIMEOUT) |
| 240 | |
| 241 | wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY]) |
| 242 | |
| 243 | self.dut.log.info("Removing network suggestions"); |
| 244 | asserts.assert_true( |
| 245 | self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_5g]), |
| 246 | "Failed to remove suggestions") |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 247 | # Ensure we did not disconnect |
| 248 | wutils.ensure_no_disconnect(self.dut) |
| 249 | |
| 250 | # Trigger a disconnect and wait for the disconnect. |
| 251 | self.dut.droid.wifiDisconnect() |
Roshan Pius | d120444 | 2018-11-12 12:20:39 -0800 | [diff] [blame] | 252 | wutils.wait_for_disconnect(self.dut) |
Roshan Pius | ffc2991 | 2019-01-18 13:39:49 -0800 | [diff] [blame] | 253 | self.dut.ed.clear_all_events() |
| 254 | |
| 255 | # Now ensure that we didn't connect back. |
| 256 | asserts.assert_false( |
| 257 | wutils.wait_for_connect(self.dut, |
| 258 | self.wpa_psk_5g[WifiEnums.SSID_KEY], |
| 259 | assert_on_fail=False), |
| 260 | "Device should not connect back") |
| 261 | |
Roshan Pius | 3e3bd34 | 2019-01-09 13:55:17 -0800 | [diff] [blame] | 262 | |
| 263 | @test_tracker_info(uuid="554b5861-22d0-4922-a5f4-712b4cf564eb") |
| 264 | def test_fail_to_connect_to_wpa_psk_5g_when_not_approved(self): |
| 265 | """ |
| 266 | Adds a network suggestion and ensure that the device does not |
| 267 | connect to it until we approve the app. |
| 268 | |
| 269 | Steps: |
| 270 | 1. Send a network suggestion to the device with the app not approved. |
| 271 | 2. Ensure the network is present in scan results, but we don't connect |
| 272 | to it. |
| 273 | 3. Now approve the app. |
| 274 | 4. Wait for the device to connect to it. |
| 275 | """ |
| 276 | self.dut.log.info("Adding network suggestions"); |
| 277 | asserts.assert_true( |
| 278 | self.dut.droid.wifiAddNetworkSuggestions([self.wpa_psk_5g]), |
| 279 | "Failed to add suggestions") |
| 280 | |
| 281 | # Disable suggestions by the app. |
| 282 | self.set_approved(False) |
| 283 | |
| 284 | # Ensure the app is not approved. |
| 285 | asserts.assert_false( |
| 286 | self.is_approved(), |
| 287 | "Suggestions should be disabled") |
| 288 | |
| 289 | # Start a new scan to trigger auto-join. |
| 290 | wutils.start_wifi_connection_scan_and_ensure_network_found( |
| 291 | self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY]) |
| 292 | |
| 293 | # Ensure we don't connect to the network. |
| 294 | asserts.assert_false( |
| 295 | wutils.wait_for_connect( |
| 296 | self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY], assert_on_fail=False), |
| 297 | "Should not connect to network suggestions from unapproved app") |
| 298 | |
| 299 | self.dut.log.info("Enabling suggestions from test"); |
| 300 | # Now Enable suggestions by the app & ensure we connect to the network. |
| 301 | self.set_approved(True) |
| 302 | |
| 303 | # Ensure the app is approved. |
| 304 | asserts.assert_true( |
| 305 | self.is_approved(), |
| 306 | "Suggestions should be enabled") |
| 307 | |
| 308 | # Start a new scan to trigger auto-join. |
| 309 | wutils.start_wifi_connection_scan_and_ensure_network_found( |
| 310 | self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY]) |
| 311 | |
| 312 | wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY]) |
Roshan Pius | 89b22bd | 2019-01-24 14:12:49 -0800 | [diff] [blame^] | 313 | |
| 314 | @test_tracker_info(uuid="") |
| 315 | def test_fail_to_connect_to_wpa_psk_2g_after_user_forgot_network(self): |
| 316 | """ |
| 317 | Adds a network suggestion and ensures that the device does not |
| 318 | connect to it after the user forgot the network previously. |
| 319 | |
| 320 | Steps: |
| 321 | 1. Send a network suggestion to the device with |
| 322 | isAppInteractionRequired set. |
| 323 | 2. Wait for the device to connect to it. |
| 324 | 3. Ensure that we did receive the post connection broadcast |
| 325 | (isAppInteractionRequired = True). |
| 326 | 4. Simulate user forgetting the network and the device does not |
| 327 | connecting back even though the suggestion is active from the app. |
| 328 | """ |
| 329 | network_suggestion = self.wpa_psk_2g |
| 330 | network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True |
| 331 | self.add_suggestions_and_ensure_connection( |
| 332 | [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY], |
| 333 | True) |
| 334 | |
| 335 | # Simulate user forgeting the ephemeral network. |
| 336 | self.dut.droid.wifiDisableEphemeralNetwork( |
| 337 | self.wpa_psk_2g[WifiEnums.SSID_KEY]) |
| 338 | wutils.wait_for_disconnect(self.dut) |
| 339 | self.dut.log.info("Disconnected from network %s", self.wpa_psk_2g) |
| 340 | self.dut.ed.clear_all_events() |
| 341 | |
| 342 | # Now ensure that we don't connect back even though the suggestion |
| 343 | # is still active. |
| 344 | asserts.assert_false( |
| 345 | wutils.wait_for_connect(self.dut, |
| 346 | self.wpa_psk_2g[WifiEnums.SSID_KEY], |
| 347 | assert_on_fail=False), |
| 348 | "Device should not connect back") |