blob: e3998c3ce949f47833d4ccec4ad3e0bf59671939 [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
39class 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 Pius89b22bd2019-01-24 14:12:49 -080077 self.clear_deleted_ephemeral_networks()
Roshan Piusd1204442018-11-12 12:20:39 -080078 wutils.wifi_toggle_state(self.dut, True)
Roshan Pius159222c2019-02-07 10:33:32 -080079 self.dut.ed.clear_all_events()
Roshan Piusd1204442018-11-12 12:20:39 -080080
81 def teardown_test(self):
82 self.dut.droid.wakeLockRelease()
83 self.dut.droid.goToSleepNow()
84 self.dut.droid.wifiRemoveNetworkSuggestions([])
Roshan Piusffc29912019-01-18 13:39:49 -080085 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -080086 wutils.reset_wifi(self.dut)
87 self.dut.ed.clear_all_events()
88
89 def on_fail(self, test_name, begin_time):
90 self.dut.take_bug_report(test_name, begin_time)
91 self.dut.cat_adb_log(test_name, begin_time)
92
93 def teardown_class(self):
94 if "AccessPoint" in self.user_params:
95 del self.user_params["reference_networks"]
96 del self.user_params["open_network"]
97
98 """Helper Functions"""
Roshan Pius3e3bd342019-01-09 13:55:17 -080099 def set_approved(self, approved):
100 self.dut.log.debug("Setting suggestions from sl4a app "
101 + "approved" if approved else "not approved")
102 self.dut.adb.shell("cmd wifi network-suggestions-set-user-approved"
103 + " " + SL4A_APK_NAME
104 + " " + ("yes" if approved else "no"))
105
106 def is_approved(self):
107 is_approved_str = self.dut.adb.shell(
108 "cmd wifi network-suggestions-has-user-approved"
109 + " " + SL4A_APK_NAME)
110 return True if (is_approved_str == "yes") else False
111
Roshan Pius89b22bd2019-01-24 14:12:49 -0800112 def clear_deleted_ephemeral_networks(self):
113 self.dut.log.debug("Clearing deleted ephemeral networks")
114 self.dut.adb.shell(
115 "cmd wifi clear-deleted-ephemeral-networks")
116
Roshan Piusd1204442018-11-12 12:20:39 -0800117 def add_suggestions_and_ensure_connection(self, network_suggestions,
118 expected_ssid,
119 expect_post_connection_broadcast):
Roshan Pius3702f242019-02-28 09:14:40 -0800120 if expect_post_connection_broadcast is not None:
121 self.dut.droid.wifiStartTrackingNetworkSuggestionStateChange()
122
Roshan Piusd1204442018-11-12 12:20:39 -0800123 self.dut.log.info("Adding network suggestions");
124 asserts.assert_true(
125 self.dut.droid.wifiAddNetworkSuggestions(network_suggestions),
126 "Failed to add suggestions")
Roshan Pius3e3bd342019-01-09 13:55:17 -0800127 # Enable suggestions by the app.
128 self.dut.log.debug("Enabling suggestions from test");
129 self.set_approved(True)
Roshan Piusd1204442018-11-12 12:20:39 -0800130 wutils.wait_for_connect(self.dut, expected_ssid)
131
132 if expect_post_connection_broadcast is None:
133 return;
134
135 # Check if we expected to get the broadcast.
136 try:
Roshan Piusd1204442018-11-12 12:20:39 -0800137 event = self.dut.ed.pop_event(
138 wifi_constants.WIFI_NETWORK_SUGGESTION_POST_CONNECTION, 60)
Roshan Piusd1204442018-11-12 12:20:39 -0800139 except queue.Empty:
140 if expect_post_connection_broadcast:
141 raise signals.TestFailure(
142 "Did not receive post connection broadcast")
143 else:
144 if not expect_post_connection_broadcast:
145 raise signals.TestFailure(
146 "Received post connection broadcast")
Roshan Pius3702f242019-02-28 09:14:40 -0800147 finally:
148 self.dut.droid.wifiStopTrackingNetworkSuggestionStateChange()
Roshan Piusd1204442018-11-12 12:20:39 -0800149
150
Roshan Piusf028bd32018-12-06 15:18:21 -0800151 @test_tracker_info(uuid="bda8ed20-4382-4380-831a-64cf77eca108")
Roshan Piusd1204442018-11-12 12:20:39 -0800152 def test_connect_to_wpa_psk_2g(self):
153 """ Adds a network suggestion and ensure that the device connected.
154
155 Steps:
156 1. Send a network suggestion to the device.
157 2. Wait for the device to connect to it.
158 3. Ensure that we did not receive the post connection broadcast
159 (isAppInteractionRequired = False).
Roshan Piusffc29912019-01-18 13:39:49 -0800160 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800161 """
162 self.add_suggestions_and_ensure_connection(
163 [self.wpa_psk_2g], self.wpa_psk_2g[WifiEnums.SSID_KEY],
164 False)
165 self.dut.log.info("Removing network suggestions");
166 asserts.assert_true(
167 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_2g]),
168 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800169 # Ensure we did not disconnect
170 wutils.ensure_no_disconnect(self.dut)
171
172 # Trigger a disconnect and wait for the disconnect.
173 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800174 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
184
Roshan Piusc6fceca2019-03-22 13:23:58 -0700185 @test_tracker_info(uuid="f54bc250-d9e9-4f00-8b5b-b866e8550b43")
186 def test_connect_to_highest_priority(self):
187 """
188 Adds network suggestions and ensures that device connects to
189 the suggestion with the highest priority.
190
191 Steps:
192 1. Send 2 network suggestions to the device (with different priorities).
193 2. Wait for the device to connect to the network with the highest
194 priority.
195 3. Re-add the suggestions with the priorities reversed.
196 4. Again wait for the device to connect to the network with the highest
197 priority.
198 """
199 network_suggestion_2g = self.wpa_psk_2g
200 network_suggestion_5g = self.wpa_psk_5g
201
202 # Add suggestions & wait for the connection event.
203 network_suggestion_2g[WifiEnums.PRIORITY] = 5
204 network_suggestion_5g[WifiEnums.PRIORITY] = 2
205 self.add_suggestions_and_ensure_connection(
206 [network_suggestion_2g, network_suggestion_5g],
207 self.wpa_psk_2g[WifiEnums.SSID_KEY],
208 None)
209
210 # Remove all suggestions
211 self.dut.log.info("Removing network suggestions");
212 asserts.assert_true(
213 self.dut.droid.wifiRemoveNetworkSuggestions([]),
214 "Failed to remove suggestions")
215 # Trigger a disconnect and wait for the disconnect.
216 self.dut.droid.wifiDisconnect()
217 wutils.wait_for_disconnect(self.dut)
218 self.dut.ed.clear_all_events()
219
220 # Reverse the priority.
221 # Add suggestions & wait for the connection event.
222 network_suggestion_2g[WifiEnums.PRIORITY] = 2
223 network_suggestion_5g[WifiEnums.PRIORITY] = 5
224 self.add_suggestions_and_ensure_connection(
225 [network_suggestion_2g, network_suggestion_5g],
226 self.wpa_psk_5g[WifiEnums.SSID_KEY],
227 None)
228
229
Roshan Piusf028bd32018-12-06 15:18:21 -0800230 @test_tracker_info(uuid="b1d27eea-23c8-4c4f-b944-ef118e4cc35f")
Roshan Piusd1204442018-11-12 12:20:39 -0800231 def test_connect_to_wpa_psk_2g_with_post_connection_broadcast(self):
232 """ Adds a network suggestion and ensure that the device connected.
233
234 Steps:
235 1. Send a network suggestion to the device with
236 isAppInteractionRequired set.
237 2. Wait for the device to connect to it.
238 3. Ensure that we did receive the post connection broadcast
239 (isAppInteractionRequired = True).
Roshan Piusffc29912019-01-18 13:39:49 -0800240 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800241 """
242 network_suggestion = self.wpa_psk_2g
243 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
244 self.add_suggestions_and_ensure_connection(
245 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
246 True)
247 self.dut.log.info("Removing network suggestions");
248 asserts.assert_true(
249 self.dut.droid.wifiRemoveNetworkSuggestions([network_suggestion]),
250 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800251 # Ensure we did not disconnect
252 wutils.ensure_no_disconnect(self.dut)
253
254 # Trigger a disconnect and wait for the disconnect.
255 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800256 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800257 self.dut.ed.clear_all_events()
258
259 # Now ensure that we didn't connect back.
260 asserts.assert_false(
261 wutils.wait_for_connect(self.dut,
262 self.wpa_psk_2g[WifiEnums.SSID_KEY],
263 assert_on_fail=False),
264 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800265
266
Roshan Piusf028bd32018-12-06 15:18:21 -0800267 @test_tracker_info(uuid="a036a24d-29c0-456d-ae6a-afdde34da710")
Roshan Piusd1204442018-11-12 12:20:39 -0800268 def test_connect_to_wpa_psk_5g_reboot_config_store(self):
269 """
270 Adds a network suggestion and ensure that the device connects to it
271 after reboot.
272
273 Steps:
274 1. Send a network suggestion to the device.
275 2. Wait for the device to connect to it.
276 3. Ensure that we did not receive the post connection broadcast
277 (isAppInteractionRequired = False).
278 4. Reboot the device.
279 5. Wait for the device to connect to back to it.
Roshan Piusffc29912019-01-18 13:39:49 -0800280 6. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800281 """
282 self.add_suggestions_and_ensure_connection(
283 [self.wpa_psk_5g], self.wpa_psk_5g[WifiEnums.SSID_KEY],
284 None)
285
286 # Reboot and wait for connection back to the same suggestion.
287 self.dut.reboot()
288 time.sleep(DEFAULT_TIMEOUT)
289
290 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
291
292 self.dut.log.info("Removing network suggestions");
293 asserts.assert_true(
294 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_5g]),
295 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800296 # Ensure we did not disconnect
297 wutils.ensure_no_disconnect(self.dut)
298
299 # Trigger a disconnect and wait for the disconnect.
300 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800301 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800302 self.dut.ed.clear_all_events()
303
304 # Now ensure that we didn't connect back.
305 asserts.assert_false(
306 wutils.wait_for_connect(self.dut,
307 self.wpa_psk_5g[WifiEnums.SSID_KEY],
308 assert_on_fail=False),
309 "Device should not connect back")
310
Roshan Pius3e3bd342019-01-09 13:55:17 -0800311
312 @test_tracker_info(uuid="554b5861-22d0-4922-a5f4-712b4cf564eb")
313 def test_fail_to_connect_to_wpa_psk_5g_when_not_approved(self):
314 """
315 Adds a network suggestion and ensure that the device does not
316 connect to it until we approve the app.
317
318 Steps:
319 1. Send a network suggestion to the device with the app not approved.
320 2. Ensure the network is present in scan results, but we don't connect
321 to it.
322 3. Now approve the app.
323 4. Wait for the device to connect to it.
324 """
325 self.dut.log.info("Adding network suggestions");
326 asserts.assert_true(
327 self.dut.droid.wifiAddNetworkSuggestions([self.wpa_psk_5g]),
328 "Failed to add suggestions")
329
330 # Disable suggestions by the app.
331 self.set_approved(False)
332
333 # Ensure the app is not approved.
334 asserts.assert_false(
335 self.is_approved(),
336 "Suggestions should be disabled")
337
338 # Start a new scan to trigger auto-join.
339 wutils.start_wifi_connection_scan_and_ensure_network_found(
340 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
341
342 # Ensure we don't connect to the network.
343 asserts.assert_false(
344 wutils.wait_for_connect(
345 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY], assert_on_fail=False),
346 "Should not connect to network suggestions from unapproved app")
347
348 self.dut.log.info("Enabling suggestions from test");
349 # Now Enable suggestions by the app & ensure we connect to the network.
350 self.set_approved(True)
351
352 # Ensure the app is approved.
353 asserts.assert_true(
354 self.is_approved(),
355 "Suggestions should be enabled")
356
357 # Start a new scan to trigger auto-join.
358 wutils.start_wifi_connection_scan_and_ensure_network_found(
359 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
360
361 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
Roshan Pius89b22bd2019-01-24 14:12:49 -0800362
Roshan Pius3d57f132019-01-28 10:25:18 -0800363 @test_tracker_info(uuid="98400dea-776e-4a0a-9024-18845b27331c")
Roshan Pius89b22bd2019-01-24 14:12:49 -0800364 def test_fail_to_connect_to_wpa_psk_2g_after_user_forgot_network(self):
365 """
366 Adds a network suggestion and ensures that the device does not
367 connect to it after the user forgot the network previously.
368
369 Steps:
370 1. Send a network suggestion to the device with
371 isAppInteractionRequired set.
372 2. Wait for the device to connect to it.
373 3. Ensure that we did receive the post connection broadcast
374 (isAppInteractionRequired = True).
375 4. Simulate user forgetting the network and the device does not
376 connecting back even though the suggestion is active from the app.
377 """
378 network_suggestion = self.wpa_psk_2g
379 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
380 self.add_suggestions_and_ensure_connection(
381 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
382 True)
383
384 # Simulate user forgeting the ephemeral network.
385 self.dut.droid.wifiDisableEphemeralNetwork(
386 self.wpa_psk_2g[WifiEnums.SSID_KEY])
387 wutils.wait_for_disconnect(self.dut)
388 self.dut.log.info("Disconnected from network %s", self.wpa_psk_2g)
389 self.dut.ed.clear_all_events()
390
391 # Now ensure that we don't connect back even though the suggestion
392 # is still active.
393 asserts.assert_false(
394 wutils.wait_for_connect(self.dut,
395 self.wpa_psk_2g[WifiEnums.SSID_KEY],
396 assert_on_fail=False),
397 "Device should not connect back")