blob: b6be560fefa189827525dd8d686e9d12e52be2dc [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
34
35# Default timeout used for reboot, toggle WiFi and Airplane mode,
36# for the system to settle down after the operation.
37DEFAULT_TIMEOUT = 10
38
Nate Jiang1ef58c32019-07-15 19:13:26 -070039
Roshan Piusd1204442018-11-12 12:20:39 -080040class WifiNetworkSuggestionTest(WifiBaseTest):
41 """Tests for WifiNetworkSuggestion API surface.
42
43 Test Bed Requirement:
44 * one Android device
45 * Several Wi-Fi networks visible to the device, including an open Wi-Fi
46 network.
47 """
48
49 def __init__(self, controllers):
50 WifiBaseTest.__init__(self, controllers)
51
52 def setup_class(self):
53 self.dut = self.android_devices[0]
54 wutils.wifi_test_device_init(self.dut)
55 req_params = []
56 opt_param = [
57 "open_network", "reference_networks"
58 ]
59 self.unpack_userparams(
60 req_param_names=req_params, opt_param_names=opt_param)
61
62 if "AccessPoint" in self.user_params:
63 self.legacy_configure_ap_and_start(wpa_network=True,
64 wep_network=True)
65
66 asserts.assert_true(
67 len(self.reference_networks) > 0,
68 "Need at least one reference network with psk.")
69 self.wpa_psk_2g = self.reference_networks[0]["2g"]
70 self.wpa_psk_5g = self.reference_networks[0]["5g"]
71 self.open_2g = self.open_network[0]["2g"]
72 self.open_5g = self.open_network[0]["5g"]
73 self.dut.droid.wifiRemoveNetworkSuggestions([])
74
75 def setup_test(self):
76 self.dut.droid.wakeLockAcquireBright()
77 self.dut.droid.wakeUpNow()
Roshan Pius89b22bd2019-01-24 14:12:49 -080078 self.clear_deleted_ephemeral_networks()
Roshan Piusd1204442018-11-12 12:20:39 -080079 wutils.wifi_toggle_state(self.dut, True)
Roshan Pius159222c2019-02-07 10:33:32 -080080 self.dut.ed.clear_all_events()
Roshan Piusd1204442018-11-12 12:20:39 -080081
82 def teardown_test(self):
83 self.dut.droid.wakeLockRelease()
84 self.dut.droid.goToSleepNow()
85 self.dut.droid.wifiRemoveNetworkSuggestions([])
Roshan Piusffc29912019-01-18 13:39:49 -080086 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -080087 wutils.reset_wifi(self.dut)
Nate Jiang1ef58c32019-07-15 19:13:26 -070088 wutils.wifi_toggle_state(self.dut, False)
Roshan Piusd1204442018-11-12 12:20:39 -080089 self.dut.ed.clear_all_events()
90
91 def on_fail(self, test_name, begin_time):
92 self.dut.take_bug_report(test_name, begin_time)
93 self.dut.cat_adb_log(test_name, begin_time)
94
95 def teardown_class(self):
96 if "AccessPoint" in self.user_params:
97 del self.user_params["reference_networks"]
98 del self.user_params["open_network"]
99
100 """Helper Functions"""
Roshan Pius3e3bd342019-01-09 13:55:17 -0800101 def set_approved(self, approved):
102 self.dut.log.debug("Setting suggestions from sl4a app "
103 + "approved" if approved else "not approved")
104 self.dut.adb.shell("cmd wifi network-suggestions-set-user-approved"
105 + " " + SL4A_APK_NAME
106 + " " + ("yes" if approved else "no"))
107
108 def is_approved(self):
109 is_approved_str = self.dut.adb.shell(
110 "cmd wifi network-suggestions-has-user-approved"
111 + " " + SL4A_APK_NAME)
112 return True if (is_approved_str == "yes") else False
113
Roshan Pius89b22bd2019-01-24 14:12:49 -0800114 def clear_deleted_ephemeral_networks(self):
115 self.dut.log.debug("Clearing deleted ephemeral networks")
116 self.dut.adb.shell(
117 "cmd wifi clear-deleted-ephemeral-networks")
118
Roshan Piusd1204442018-11-12 12:20:39 -0800119 def add_suggestions_and_ensure_connection(self, network_suggestions,
120 expected_ssid,
121 expect_post_connection_broadcast):
Roshan Pius3702f242019-02-28 09:14:40 -0800122 if expect_post_connection_broadcast is not None:
123 self.dut.droid.wifiStartTrackingNetworkSuggestionStateChange()
124
Roshan Piusd1204442018-11-12 12:20:39 -0800125 self.dut.log.info("Adding network suggestions");
126 asserts.assert_true(
127 self.dut.droid.wifiAddNetworkSuggestions(network_suggestions),
128 "Failed to add suggestions")
Roshan Pius3e3bd342019-01-09 13:55:17 -0800129 # Enable suggestions by the app.
130 self.dut.log.debug("Enabling suggestions from test");
131 self.set_approved(True)
lutina49d3e512019-07-05 14:42:33 +0800132 wutils.start_wifi_connection_scan_and_return_status(self.dut)
Roshan Piusd1204442018-11-12 12:20:39 -0800133 wutils.wait_for_connect(self.dut, expected_ssid)
134
135 if expect_post_connection_broadcast is None:
136 return;
137
138 # Check if we expected to get the broadcast.
139 try:
Roshan Piusd1204442018-11-12 12:20:39 -0800140 event = self.dut.ed.pop_event(
141 wifi_constants.WIFI_NETWORK_SUGGESTION_POST_CONNECTION, 60)
Roshan Piusd1204442018-11-12 12:20:39 -0800142 except queue.Empty:
143 if expect_post_connection_broadcast:
144 raise signals.TestFailure(
145 "Did not receive post connection broadcast")
146 else:
147 if not expect_post_connection_broadcast:
148 raise signals.TestFailure(
149 "Received post connection broadcast")
Roshan Pius3702f242019-02-28 09:14:40 -0800150 finally:
151 self.dut.droid.wifiStopTrackingNetworkSuggestionStateChange()
Roshan Piusff6ca4c2019-03-26 13:53:31 -0700152 self.dut.ed.clear_all_events()
Roshan Piusd1204442018-11-12 12:20:39 -0800153
Roshan Piusf028bd32018-12-06 15:18:21 -0800154 @test_tracker_info(uuid="bda8ed20-4382-4380-831a-64cf77eca108")
Roshan Piusd1204442018-11-12 12:20:39 -0800155 def test_connect_to_wpa_psk_2g(self):
156 """ Adds a network suggestion and ensure that the device connected.
157
158 Steps:
159 1. Send a network suggestion to the device.
160 2. Wait for the device to connect to it.
161 3. Ensure that we did not receive the post connection broadcast
162 (isAppInteractionRequired = False).
Roshan Piusffc29912019-01-18 13:39:49 -0800163 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800164 """
165 self.add_suggestions_and_ensure_connection(
166 [self.wpa_psk_2g], self.wpa_psk_2g[WifiEnums.SSID_KEY],
167 False)
Nate Jiang1ef58c32019-07-15 19:13:26 -0700168
169 # Remove suggestion trigger disconnect and wait for the disconnect.
Roshan Piusd1204442018-11-12 12:20:39 -0800170 self.dut.log.info("Removing network suggestions");
171 asserts.assert_true(
172 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_2g]),
173 "Failed to remove suggestions")
174 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800175 self.dut.ed.clear_all_events()
176
177 # Now ensure that we didn't connect back.
178 asserts.assert_false(
179 wutils.wait_for_connect(self.dut,
180 self.wpa_psk_2g[WifiEnums.SSID_KEY],
181 assert_on_fail=False),
182 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800183
Roshan Piusc6fceca2019-03-22 13:23:58 -0700184 @test_tracker_info(uuid="f54bc250-d9e9-4f00-8b5b-b866e8550b43")
185 def test_connect_to_highest_priority(self):
186 """
187 Adds network suggestions and ensures that device connects to
188 the suggestion with the highest priority.
189
190 Steps:
191 1. Send 2 network suggestions to the device (with different priorities).
192 2. Wait for the device to connect to the network with the highest
193 priority.
194 3. Re-add the suggestions with the priorities reversed.
195 4. Again wait for the device to connect to the network with the highest
196 priority.
197 """
198 network_suggestion_2g = self.wpa_psk_2g
199 network_suggestion_5g = self.wpa_psk_5g
200
201 # Add suggestions & wait for the connection event.
202 network_suggestion_2g[WifiEnums.PRIORITY] = 5
203 network_suggestion_5g[WifiEnums.PRIORITY] = 2
204 self.add_suggestions_and_ensure_connection(
205 [network_suggestion_2g, network_suggestion_5g],
206 self.wpa_psk_2g[WifiEnums.SSID_KEY],
207 None)
208
Nate Jiang1ef58c32019-07-15 19:13:26 -0700209 # Remove all suggestions trigger disconnect and wait for the disconnect.
210 self.dut.log.info("Removing network suggestions")
Roshan Piusc6fceca2019-03-22 13:23:58 -0700211 asserts.assert_true(
212 self.dut.droid.wifiRemoveNetworkSuggestions([]),
213 "Failed to remove suggestions")
Roshan Piusc6fceca2019-03-22 13:23:58 -0700214 wutils.wait_for_disconnect(self.dut)
215 self.dut.ed.clear_all_events()
216
217 # Reverse the priority.
218 # Add suggestions & wait for the connection event.
219 network_suggestion_2g[WifiEnums.PRIORITY] = 2
220 network_suggestion_5g[WifiEnums.PRIORITY] = 5
221 self.add_suggestions_and_ensure_connection(
222 [network_suggestion_2g, network_suggestion_5g],
223 self.wpa_psk_5g[WifiEnums.SSID_KEY],
224 None)
225
Roshan Piusf028bd32018-12-06 15:18:21 -0800226 @test_tracker_info(uuid="b1d27eea-23c8-4c4f-b944-ef118e4cc35f")
Roshan Piusd1204442018-11-12 12:20:39 -0800227 def test_connect_to_wpa_psk_2g_with_post_connection_broadcast(self):
228 """ Adds a network suggestion and ensure that the device connected.
229
230 Steps:
231 1. Send a network suggestion to the device with
232 isAppInteractionRequired set.
233 2. Wait for the device to connect to it.
234 3. Ensure that we did receive the post connection broadcast
235 (isAppInteractionRequired = True).
Roshan Piusffc29912019-01-18 13:39:49 -0800236 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800237 """
238 network_suggestion = self.wpa_psk_2g
239 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
240 self.add_suggestions_and_ensure_connection(
241 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
242 True)
Nate Jiang1ef58c32019-07-15 19:13:26 -0700243
244 # Remove suggestion trigger disconnect and wait for the disconnect.
Roshan Piusd1204442018-11-12 12:20:39 -0800245 self.dut.log.info("Removing network suggestions");
246 asserts.assert_true(
247 self.dut.droid.wifiRemoveNetworkSuggestions([network_suggestion]),
248 "Failed to remove suggestions")
249 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800250 self.dut.ed.clear_all_events()
251
252 # Now ensure that we didn't connect back.
253 asserts.assert_false(
254 wutils.wait_for_connect(self.dut,
255 self.wpa_psk_2g[WifiEnums.SSID_KEY],
256 assert_on_fail=False),
257 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800258
Roshan Piusf028bd32018-12-06 15:18:21 -0800259 @test_tracker_info(uuid="a036a24d-29c0-456d-ae6a-afdde34da710")
Roshan Piusd1204442018-11-12 12:20:39 -0800260 def test_connect_to_wpa_psk_5g_reboot_config_store(self):
261 """
262 Adds a network suggestion and ensure that the device connects to it
263 after reboot.
264
265 Steps:
266 1. Send a network suggestion to the device.
267 2. Wait for the device to connect to it.
268 3. Ensure that we did not receive the post connection broadcast
269 (isAppInteractionRequired = False).
270 4. Reboot the device.
271 5. Wait for the device to connect to back to it.
Roshan Piusffc29912019-01-18 13:39:49 -0800272 6. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800273 """
274 self.add_suggestions_and_ensure_connection(
275 [self.wpa_psk_5g], self.wpa_psk_5g[WifiEnums.SSID_KEY],
276 None)
277
278 # Reboot and wait for connection back to the same suggestion.
279 self.dut.reboot()
280 time.sleep(DEFAULT_TIMEOUT)
281
282 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
283
Nate Jiang1ef58c32019-07-15 19:13:26 -0700284 # Remove suggestion trigger disconnect and wait for the disconnect.
Roshan Piusd1204442018-11-12 12:20:39 -0800285 self.dut.log.info("Removing network suggestions");
286 asserts.assert_true(
287 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_5g]),
288 "Failed to remove suggestions")
289 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800290 self.dut.ed.clear_all_events()
291
292 # Now ensure that we didn't connect back.
293 asserts.assert_false(
294 wutils.wait_for_connect(self.dut,
295 self.wpa_psk_5g[WifiEnums.SSID_KEY],
296 assert_on_fail=False),
297 "Device should not connect back")
298
Roshan Pius3e3bd342019-01-09 13:55:17 -0800299 @test_tracker_info(uuid="554b5861-22d0-4922-a5f4-712b4cf564eb")
300 def test_fail_to_connect_to_wpa_psk_5g_when_not_approved(self):
301 """
302 Adds a network suggestion and ensure that the device does not
303 connect to it until we approve the app.
304
305 Steps:
306 1. Send a network suggestion to the device with the app not approved.
307 2. Ensure the network is present in scan results, but we don't connect
308 to it.
309 3. Now approve the app.
310 4. Wait for the device to connect to it.
311 """
312 self.dut.log.info("Adding network suggestions");
313 asserts.assert_true(
314 self.dut.droid.wifiAddNetworkSuggestions([self.wpa_psk_5g]),
315 "Failed to add suggestions")
316
317 # Disable suggestions by the app.
318 self.set_approved(False)
319
320 # Ensure the app is not approved.
321 asserts.assert_false(
322 self.is_approved(),
323 "Suggestions should be disabled")
324
325 # Start a new scan to trigger auto-join.
326 wutils.start_wifi_connection_scan_and_ensure_network_found(
327 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
328
329 # Ensure we don't connect to the network.
330 asserts.assert_false(
331 wutils.wait_for_connect(
332 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY], assert_on_fail=False),
333 "Should not connect to network suggestions from unapproved app")
334
335 self.dut.log.info("Enabling suggestions from test");
336 # Now Enable suggestions by the app & ensure we connect to the network.
337 self.set_approved(True)
338
339 # Ensure the app is approved.
340 asserts.assert_true(
341 self.is_approved(),
342 "Suggestions should be enabled")
343
344 # Start a new scan to trigger auto-join.
345 wutils.start_wifi_connection_scan_and_ensure_network_found(
346 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
347
348 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
Roshan Pius89b22bd2019-01-24 14:12:49 -0800349
Roshan Pius3d57f132019-01-28 10:25:18 -0800350 @test_tracker_info(uuid="98400dea-776e-4a0a-9024-18845b27331c")
Roshan Pius89b22bd2019-01-24 14:12:49 -0800351 def test_fail_to_connect_to_wpa_psk_2g_after_user_forgot_network(self):
352 """
353 Adds a network suggestion and ensures that the device does not
354 connect to it after the user forgot the network previously.
355
356 Steps:
357 1. Send a network suggestion to the device with
358 isAppInteractionRequired set.
359 2. Wait for the device to connect to it.
360 3. Ensure that we did receive the post connection broadcast
361 (isAppInteractionRequired = True).
362 4. Simulate user forgetting the network and the device does not
363 connecting back even though the suggestion is active from the app.
364 """
365 network_suggestion = self.wpa_psk_2g
366 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
367 self.add_suggestions_and_ensure_connection(
368 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
369 True)
370
371 # Simulate user forgeting the ephemeral network.
372 self.dut.droid.wifiDisableEphemeralNetwork(
373 self.wpa_psk_2g[WifiEnums.SSID_KEY])
374 wutils.wait_for_disconnect(self.dut)
375 self.dut.log.info("Disconnected from network %s", self.wpa_psk_2g)
376 self.dut.ed.clear_all_events()
377
378 # Now ensure that we don't connect back even though the suggestion
379 # is still active.
380 asserts.assert_false(
381 wutils.wait_for_connect(self.dut,
382 self.wpa_psk_2g[WifiEnums.SSID_KEY],
383 assert_on_fail=False),
384 "Device should not connect back")