blob: d07201abba29092b06613eb5f42d0eb936570a46 [file] [log] [blame]
Roshan Piusd1204442018-11-12 12:20:39 -08001#!/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
17import itertools
18import pprint
19import queue
20import time
21
22import acts.base_test
23import acts.signals as signals
24import acts.test_utils.wifi.wifi_test_utils as wutils
25import acts.utils
26
27from acts import asserts
Roshan Pius3e3bd342019-01-09 13:55:17 -080028from acts.controllers.android_device import SL4A_APK_NAME
Roshan Piusd1204442018-11-12 12:20:39 -080029from acts.test_decorators import test_tracker_info
30from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest
31from acts.test_utils.wifi import wifi_constants
32
33WifiEnums = wutils.WifiEnums
Girish Moturub1e7a5c2019-07-29 13:48:11 -070034# EAP Macros
35EAP = WifiEnums.Eap
36EapPhase2 = WifiEnums.EapPhase2
37# Enterprise Config Macros
38Ent = WifiEnums.Enterprise
Roshan Piusd1204442018-11-12 12:20:39 -080039
40# Default timeout used for reboot, toggle WiFi and Airplane mode,
41# for the system to settle down after the operation.
42DEFAULT_TIMEOUT = 10
43
Nate Jiang1ef58c32019-07-15 19:13:26 -070044
Roshan Piusd1204442018-11-12 12:20:39 -080045class WifiNetworkSuggestionTest(WifiBaseTest):
46 """Tests for WifiNetworkSuggestion API surface.
47
48 Test Bed Requirement:
49 * one Android device
50 * Several Wi-Fi networks visible to the device, including an open Wi-Fi
51 network.
52 """
53
54 def __init__(self, controllers):
55 WifiBaseTest.__init__(self, controllers)
56
57 def setup_class(self):
58 self.dut = self.android_devices[0]
59 wutils.wifi_test_device_init(self.dut)
Girish Moturub1e7a5c2019-07-29 13:48:11 -070060 req_params = ["radius_conf_2g", "radius_conf_5g", "ca_cert",
61 "eap_identity", "eap_password",]
Roshan Piusd1204442018-11-12 12:20:39 -080062 opt_param = [
63 "open_network", "reference_networks"
64 ]
65 self.unpack_userparams(
66 req_param_names=req_params, opt_param_names=opt_param)
67
68 if "AccessPoint" in self.user_params:
Girish Moturub1e7a5c2019-07-29 13:48:11 -070069 self.legacy_configure_ap_and_start(
70 wpa_network=True, ent_network=True,
71 radius_conf_2g=self.radius_conf_2g,
72 radius_conf_5g=self.radius_conf_5g,)
Roshan Piusd1204442018-11-12 12:20:39 -080073
74 asserts.assert_true(
75 len(self.reference_networks) > 0,
76 "Need at least one reference network with psk.")
77 self.wpa_psk_2g = self.reference_networks[0]["2g"]
78 self.wpa_psk_5g = self.reference_networks[0]["5g"]
79 self.open_2g = self.open_network[0]["2g"]
80 self.open_5g = self.open_network[0]["5g"]
Girish Moturub1e7a5c2019-07-29 13:48:11 -070081 self.ent_network_2g = self.ent_networks[0]["2g"]
82 self.ent_network_5g = self.ent_networks[0]["5g"]
83 self.config_aka = {
84 Ent.EAP: int(EAP.AKA),
85 WifiEnums.SSID_KEY: self.ent_network_2g[WifiEnums.SSID_KEY],
86 }
87 self.config_ttls = {
88 Ent.EAP: int(EAP.TTLS),
89 Ent.CA_CERT: self.ca_cert,
90 Ent.IDENTITY: self.eap_identity,
91 Ent.PASSWORD: self.eap_password,
92 Ent.PHASE2: int(EapPhase2.MSCHAPV2),
93 WifiEnums.SSID_KEY: self.ent_network_2g[WifiEnums.SSID_KEY],
94 }
Roshan Piusd1204442018-11-12 12:20:39 -080095 self.dut.droid.wifiRemoveNetworkSuggestions([])
96
97 def setup_test(self):
98 self.dut.droid.wakeLockAcquireBright()
99 self.dut.droid.wakeUpNow()
Roshan Pius89b22bd2019-01-24 14:12:49 -0800100 self.clear_deleted_ephemeral_networks()
Roshan Piusd1204442018-11-12 12:20:39 -0800101 wutils.wifi_toggle_state(self.dut, True)
Roshan Pius159222c2019-02-07 10:33:32 -0800102 self.dut.ed.clear_all_events()
Roshan Piusd1204442018-11-12 12:20:39 -0800103
104 def teardown_test(self):
105 self.dut.droid.wakeLockRelease()
106 self.dut.droid.goToSleepNow()
107 self.dut.droid.wifiRemoveNetworkSuggestions([])
Roshan Piusffc29912019-01-18 13:39:49 -0800108 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800109 wutils.reset_wifi(self.dut)
Nate Jiang1ef58c32019-07-15 19:13:26 -0700110 wutils.wifi_toggle_state(self.dut, False)
Roshan Piusd1204442018-11-12 12:20:39 -0800111 self.dut.ed.clear_all_events()
112
113 def on_fail(self, test_name, begin_time):
114 self.dut.take_bug_report(test_name, begin_time)
115 self.dut.cat_adb_log(test_name, begin_time)
116
117 def teardown_class(self):
118 if "AccessPoint" in self.user_params:
119 del self.user_params["reference_networks"]
120 del self.user_params["open_network"]
121
122 """Helper Functions"""
Roshan Pius3e3bd342019-01-09 13:55:17 -0800123 def set_approved(self, approved):
124 self.dut.log.debug("Setting suggestions from sl4a app "
125 + "approved" if approved else "not approved")
126 self.dut.adb.shell("cmd wifi network-suggestions-set-user-approved"
127 + " " + SL4A_APK_NAME
128 + " " + ("yes" if approved else "no"))
129
130 def is_approved(self):
131 is_approved_str = self.dut.adb.shell(
132 "cmd wifi network-suggestions-has-user-approved"
133 + " " + SL4A_APK_NAME)
134 return True if (is_approved_str == "yes") else False
135
Roshan Pius89b22bd2019-01-24 14:12:49 -0800136 def clear_deleted_ephemeral_networks(self):
137 self.dut.log.debug("Clearing deleted ephemeral networks")
138 self.dut.adb.shell(
139 "cmd wifi clear-deleted-ephemeral-networks")
140
Roshan Piusd1204442018-11-12 12:20:39 -0800141 def add_suggestions_and_ensure_connection(self, network_suggestions,
142 expected_ssid,
143 expect_post_connection_broadcast):
Roshan Pius3702f242019-02-28 09:14:40 -0800144 if expect_post_connection_broadcast is not None:
145 self.dut.droid.wifiStartTrackingNetworkSuggestionStateChange()
146
Roshan Piusd1204442018-11-12 12:20:39 -0800147 self.dut.log.info("Adding network suggestions");
148 asserts.assert_true(
149 self.dut.droid.wifiAddNetworkSuggestions(network_suggestions),
150 "Failed to add suggestions")
Roshan Pius3e3bd342019-01-09 13:55:17 -0800151 # Enable suggestions by the app.
152 self.dut.log.debug("Enabling suggestions from test");
153 self.set_approved(True)
lutina49d3e512019-07-05 14:42:33 +0800154 wutils.start_wifi_connection_scan_and_return_status(self.dut)
Roshan Piusd1204442018-11-12 12:20:39 -0800155 wutils.wait_for_connect(self.dut, expected_ssid)
156
157 if expect_post_connection_broadcast is None:
158 return;
159
160 # Check if we expected to get the broadcast.
161 try:
Roshan Piusd1204442018-11-12 12:20:39 -0800162 event = self.dut.ed.pop_event(
163 wifi_constants.WIFI_NETWORK_SUGGESTION_POST_CONNECTION, 60)
Roshan Piusd1204442018-11-12 12:20:39 -0800164 except queue.Empty:
165 if expect_post_connection_broadcast:
166 raise signals.TestFailure(
167 "Did not receive post connection broadcast")
168 else:
169 if not expect_post_connection_broadcast:
170 raise signals.TestFailure(
171 "Received post connection broadcast")
Roshan Pius3702f242019-02-28 09:14:40 -0800172 finally:
173 self.dut.droid.wifiStopTrackingNetworkSuggestionStateChange()
Roshan Piusff6ca4c2019-03-26 13:53:31 -0700174 self.dut.ed.clear_all_events()
Roshan Piusd1204442018-11-12 12:20:39 -0800175
Girish Moturub1e7a5c2019-07-29 13:48:11 -0700176 def _test_connect_to_wifi_network_reboot_config_store(self,
177 network_suggestions,
178 wifi_network):
179 """ Test network suggestion with reboot config store
180
181 Args:
182 1. network_suggestions: network suggestions in list to add to the device.
183 2. wifi_network: expected wifi network to connect to
184 """
185
186 self.add_suggestions_and_ensure_connection(
187 network_suggestions, wifi_network[WifiEnums.SSID_KEY], None)
188
189 # Reboot and wait for connection back to the same suggestion.
190 self.dut.reboot()
191 time.sleep(DEFAULT_TIMEOUT)
192
193 wutils.wait_for_connect(self.dut, wifi_network[WifiEnums.SSID_KEY])
194
195 self.dut.log.info("Removing network suggestions")
196 asserts.assert_true(
197 self.dut.droid.wifiRemoveNetworkSuggestions(network_suggestions),
198 "Failed to remove suggestions")
199
200 # Ensure we did not disconnect
201 wutils.ensure_no_disconnect(self.dut)
202
203 # Trigger a disconnect and wait for the disconnect.
204 self.dut.droid.wifiDisconnect()
205 wutils.wait_for_disconnect(self.dut)
206 self.dut.ed.clear_all_events()
207
208 # Now ensure that we didn't connect back.
209 asserts.assert_false(
210 wutils.wait_for_connect(self.dut,
211 wifi_network[WifiEnums.SSID_KEY],
212 assert_on_fail=False),
213 "Device should not connect back")
214
Roshan Piusf028bd32018-12-06 15:18:21 -0800215 @test_tracker_info(uuid="bda8ed20-4382-4380-831a-64cf77eca108")
Roshan Piusd1204442018-11-12 12:20:39 -0800216 def test_connect_to_wpa_psk_2g(self):
217 """ Adds a network suggestion and ensure that the device connected.
218
219 Steps:
220 1. Send a network suggestion to the device.
221 2. Wait for the device to connect to it.
222 3. Ensure that we did not receive the post connection broadcast
223 (isAppInteractionRequired = False).
Roshan Piusffc29912019-01-18 13:39:49 -0800224 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800225 """
226 self.add_suggestions_and_ensure_connection(
227 [self.wpa_psk_2g], self.wpa_psk_2g[WifiEnums.SSID_KEY],
228 False)
Nate Jiang1ef58c32019-07-15 19:13:26 -0700229
230 # Remove suggestion trigger disconnect and wait for the disconnect.
Roshan Piusd1204442018-11-12 12:20:39 -0800231 self.dut.log.info("Removing network suggestions");
232 asserts.assert_true(
233 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_2g]),
234 "Failed to remove suggestions")
235 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800236 self.dut.ed.clear_all_events()
237
238 # Now ensure that we didn't connect back.
239 asserts.assert_false(
240 wutils.wait_for_connect(self.dut,
241 self.wpa_psk_2g[WifiEnums.SSID_KEY],
242 assert_on_fail=False),
243 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800244
Roshan Piusc6fceca2019-03-22 13:23:58 -0700245 @test_tracker_info(uuid="f54bc250-d9e9-4f00-8b5b-b866e8550b43")
246 def test_connect_to_highest_priority(self):
247 """
248 Adds network suggestions and ensures that device connects to
249 the suggestion with the highest priority.
250
251 Steps:
252 1. Send 2 network suggestions to the device (with different priorities).
253 2. Wait for the device to connect to the network with the highest
254 priority.
255 3. Re-add the suggestions with the priorities reversed.
256 4. Again wait for the device to connect to the network with the highest
257 priority.
258 """
259 network_suggestion_2g = self.wpa_psk_2g
260 network_suggestion_5g = self.wpa_psk_5g
261
262 # Add suggestions & wait for the connection event.
263 network_suggestion_2g[WifiEnums.PRIORITY] = 5
264 network_suggestion_5g[WifiEnums.PRIORITY] = 2
265 self.add_suggestions_and_ensure_connection(
266 [network_suggestion_2g, network_suggestion_5g],
267 self.wpa_psk_2g[WifiEnums.SSID_KEY],
268 None)
269
Nate Jiang1ef58c32019-07-15 19:13:26 -0700270 # Remove all suggestions trigger disconnect and wait for the disconnect.
271 self.dut.log.info("Removing network suggestions")
Roshan Piusc6fceca2019-03-22 13:23:58 -0700272 asserts.assert_true(
273 self.dut.droid.wifiRemoveNetworkSuggestions([]),
274 "Failed to remove suggestions")
Roshan Piusc6fceca2019-03-22 13:23:58 -0700275 wutils.wait_for_disconnect(self.dut)
276 self.dut.ed.clear_all_events()
277
278 # Reverse the priority.
279 # Add suggestions & wait for the connection event.
280 network_suggestion_2g[WifiEnums.PRIORITY] = 2
281 network_suggestion_5g[WifiEnums.PRIORITY] = 5
282 self.add_suggestions_and_ensure_connection(
283 [network_suggestion_2g, network_suggestion_5g],
284 self.wpa_psk_5g[WifiEnums.SSID_KEY],
285 None)
286
Roshan Piusf028bd32018-12-06 15:18:21 -0800287 @test_tracker_info(uuid="b1d27eea-23c8-4c4f-b944-ef118e4cc35f")
Roshan Piusd1204442018-11-12 12:20:39 -0800288 def test_connect_to_wpa_psk_2g_with_post_connection_broadcast(self):
289 """ Adds a network suggestion and ensure that the device connected.
290
291 Steps:
292 1. Send a network suggestion to the device with
293 isAppInteractionRequired set.
294 2. Wait for the device to connect to it.
295 3. Ensure that we did receive the post connection broadcast
296 (isAppInteractionRequired = True).
Roshan Piusffc29912019-01-18 13:39:49 -0800297 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800298 """
299 network_suggestion = self.wpa_psk_2g
300 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
301 self.add_suggestions_and_ensure_connection(
302 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
303 True)
Nate Jiang1ef58c32019-07-15 19:13:26 -0700304
305 # Remove suggestion trigger disconnect and wait for the disconnect.
Roshan Piusd1204442018-11-12 12:20:39 -0800306 self.dut.log.info("Removing network suggestions");
307 asserts.assert_true(
308 self.dut.droid.wifiRemoveNetworkSuggestions([network_suggestion]),
309 "Failed to remove suggestions")
310 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800311 self.dut.ed.clear_all_events()
312
313 # Now ensure that we didn't connect back.
314 asserts.assert_false(
315 wutils.wait_for_connect(self.dut,
316 self.wpa_psk_2g[WifiEnums.SSID_KEY],
317 assert_on_fail=False),
318 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800319
Roshan Piusf028bd32018-12-06 15:18:21 -0800320 @test_tracker_info(uuid="a036a24d-29c0-456d-ae6a-afdde34da710")
Roshan Piusd1204442018-11-12 12:20:39 -0800321 def test_connect_to_wpa_psk_5g_reboot_config_store(self):
322 """
323 Adds a network suggestion and ensure that the device connects to it
324 after reboot.
325
326 Steps:
327 1. Send a network suggestion to the device.
328 2. Wait for the device to connect to it.
329 3. Ensure that we did not receive the post connection broadcast
330 (isAppInteractionRequired = False).
331 4. Reboot the device.
332 5. Wait for the device to connect to back to it.
Roshan Piusffc29912019-01-18 13:39:49 -0800333 6. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800334 """
Girish Moturub1e7a5c2019-07-29 13:48:11 -0700335 self._test_connect_to_wifi_network_reboot_config_store(
336 [self.wpa_psk_5g], self.wpa_psk_5g)
Roshan Piusd1204442018-11-12 12:20:39 -0800337
Girish Moturub1e7a5c2019-07-29 13:48:11 -0700338 def test_connect_to_wpa_ent_config_aka_reboot_config_store(self):
339 """
340 Adds a network suggestion and ensure that the device connects to it
341 after reboot.
Roshan Piusd1204442018-11-12 12:20:39 -0800342
Girish Moturub1e7a5c2019-07-29 13:48:11 -0700343 Steps:
344 1. Send a Enterprise AKA network suggestion to the device.
345 2. Wait for the device to connect to it.
346 3. Ensure that we did not receive the post connection broadcast.
347 4. Reboot the device.
348 5. Wait for the device to connect to the wifi network.
349 6. Remove suggestions and ensure device doesn't connect back to it.
350 """
351 self._test_connect_to_wifi_network_reboot_config_store(
352 [self.config_aka], self.ent_network_2g)
Roshan Piusd1204442018-11-12 12:20:39 -0800353
Girish Moturub1e7a5c2019-07-29 13:48:11 -0700354 def test_connect_to_wpa_ent_config_ttls_pap_reboot_config_store(self):
355 """
356 Adds a network suggestion and ensure that the device connects to it
357 after reboot.
Roshan Piusffc29912019-01-18 13:39:49 -0800358
Girish Moturub1e7a5c2019-07-29 13:48:11 -0700359 Steps:
360 1. Send a Enterprise TTLS PAP network suggestion to the device.
361 2. Wait for the device to connect to it.
362 3. Ensure that we did not receive the post connection broadcast.
363 4. Reboot the device.
364 5. Wait for the device to connect to the wifi network.
365 6. Remove suggestions and ensure device doesn't connect back to it.
366 """
367 config = dict(self.config_ttls)
368 config[WifiEnums.Enterprise.PHASE2] = WifiEnums.EapPhase2.PAP.value
369
370 self._test_connect_to_wifi_network_reboot_config_store(
371 [config], self.ent_network_2g)
Roshan Piusffc29912019-01-18 13:39:49 -0800372
Roshan Pius3e3bd342019-01-09 13:55:17 -0800373 @test_tracker_info(uuid="554b5861-22d0-4922-a5f4-712b4cf564eb")
374 def test_fail_to_connect_to_wpa_psk_5g_when_not_approved(self):
375 """
376 Adds a network suggestion and ensure that the device does not
377 connect to it until we approve the app.
378
379 Steps:
380 1. Send a network suggestion to the device with the app not approved.
381 2. Ensure the network is present in scan results, but we don't connect
382 to it.
383 3. Now approve the app.
384 4. Wait for the device to connect to it.
385 """
386 self.dut.log.info("Adding network suggestions");
387 asserts.assert_true(
388 self.dut.droid.wifiAddNetworkSuggestions([self.wpa_psk_5g]),
389 "Failed to add suggestions")
390
391 # Disable suggestions by the app.
392 self.set_approved(False)
393
394 # Ensure the app is not approved.
395 asserts.assert_false(
396 self.is_approved(),
397 "Suggestions should be disabled")
398
399 # Start a new scan to trigger auto-join.
400 wutils.start_wifi_connection_scan_and_ensure_network_found(
401 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
402
403 # Ensure we don't connect to the network.
404 asserts.assert_false(
405 wutils.wait_for_connect(
406 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY], assert_on_fail=False),
407 "Should not connect to network suggestions from unapproved app")
408
409 self.dut.log.info("Enabling suggestions from test");
410 # Now Enable suggestions by the app & ensure we connect to the network.
411 self.set_approved(True)
412
413 # Ensure the app is approved.
414 asserts.assert_true(
415 self.is_approved(),
416 "Suggestions should be enabled")
417
418 # Start a new scan to trigger auto-join.
419 wutils.start_wifi_connection_scan_and_ensure_network_found(
420 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
421
422 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
Roshan Pius89b22bd2019-01-24 14:12:49 -0800423
Roshan Pius3d57f132019-01-28 10:25:18 -0800424 @test_tracker_info(uuid="98400dea-776e-4a0a-9024-18845b27331c")
Roshan Pius89b22bd2019-01-24 14:12:49 -0800425 def test_fail_to_connect_to_wpa_psk_2g_after_user_forgot_network(self):
426 """
427 Adds a network suggestion and ensures that the device does not
428 connect to it after the user forgot the network previously.
429
430 Steps:
431 1. Send a network suggestion to the device with
432 isAppInteractionRequired set.
433 2. Wait for the device to connect to it.
434 3. Ensure that we did receive the post connection broadcast
435 (isAppInteractionRequired = True).
436 4. Simulate user forgetting the network and the device does not
437 connecting back even though the suggestion is active from the app.
438 """
439 network_suggestion = self.wpa_psk_2g
440 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
441 self.add_suggestions_and_ensure_connection(
442 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
443 True)
444
445 # Simulate user forgeting the ephemeral network.
446 self.dut.droid.wifiDisableEphemeralNetwork(
447 self.wpa_psk_2g[WifiEnums.SSID_KEY])
448 wutils.wait_for_disconnect(self.dut)
449 self.dut.log.info("Disconnected from network %s", self.wpa_psk_2g)
450 self.dut.ed.clear_all_events()
451
452 # Now ensure that we don't connect back even though the suggestion
453 # is still active.
454 asserts.assert_false(
455 wutils.wait_for_connect(self.dut,
456 self.wpa_psk_2g[WifiEnums.SSID_KEY],
457 assert_on_fail=False),
458 "Device should not connect back")