blob: 6d4d8d322c67b7f27613b90234bc820371c1e7de [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):
120 self.dut.log.info("Adding network suggestions");
121 asserts.assert_true(
122 self.dut.droid.wifiAddNetworkSuggestions(network_suggestions),
123 "Failed to add suggestions")
Roshan Pius3e3bd342019-01-09 13:55:17 -0800124 # Enable suggestions by the app.
125 self.dut.log.debug("Enabling suggestions from test");
126 self.set_approved(True)
Roshan Piusd1204442018-11-12 12:20:39 -0800127 wutils.wait_for_connect(self.dut, expected_ssid)
128
129 if expect_post_connection_broadcast is None:
130 return;
131
132 # Check if we expected to get the broadcast.
133 try:
134 self.dut.droid.wifiStartTrackingStateChange()
135 event = self.dut.ed.pop_event(
136 wifi_constants.WIFI_NETWORK_SUGGESTION_POST_CONNECTION, 60)
137 self.dut.droid.wifiStopTrackingStateChange()
138 except queue.Empty:
139 if expect_post_connection_broadcast:
140 raise signals.TestFailure(
141 "Did not receive post connection broadcast")
142 else:
143 if not expect_post_connection_broadcast:
144 raise signals.TestFailure(
145 "Received post connection broadcast")
146
147
Roshan Piusf028bd32018-12-06 15:18:21 -0800148 @test_tracker_info(uuid="bda8ed20-4382-4380-831a-64cf77eca108")
Roshan Piusd1204442018-11-12 12:20:39 -0800149 def test_connect_to_wpa_psk_2g(self):
150 """ Adds a network suggestion and ensure that the device connected.
151
152 Steps:
153 1. Send a network suggestion to the device.
154 2. Wait for the device to connect to it.
155 3. Ensure that we did not receive the post connection broadcast
156 (isAppInteractionRequired = False).
Roshan Piusffc29912019-01-18 13:39:49 -0800157 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800158 """
159 self.add_suggestions_and_ensure_connection(
160 [self.wpa_psk_2g], self.wpa_psk_2g[WifiEnums.SSID_KEY],
161 False)
162 self.dut.log.info("Removing network suggestions");
163 asserts.assert_true(
164 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_2g]),
165 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800166 # Ensure we did not disconnect
167 wutils.ensure_no_disconnect(self.dut)
168
169 # Trigger a disconnect and wait for the disconnect.
170 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800171 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800172 self.dut.ed.clear_all_events()
173
174 # Now ensure that we didn't connect back.
175 asserts.assert_false(
176 wutils.wait_for_connect(self.dut,
177 self.wpa_psk_2g[WifiEnums.SSID_KEY],
178 assert_on_fail=False),
179 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800180
181
Roshan Piusf028bd32018-12-06 15:18:21 -0800182 @test_tracker_info(uuid="b1d27eea-23c8-4c4f-b944-ef118e4cc35f")
Roshan Piusd1204442018-11-12 12:20:39 -0800183 def test_connect_to_wpa_psk_2g_with_post_connection_broadcast(self):
184 """ Adds a network suggestion and ensure that the device connected.
185
186 Steps:
187 1. Send a network suggestion to the device with
188 isAppInteractionRequired set.
189 2. Wait for the device to connect to it.
190 3. Ensure that we did receive the post connection broadcast
191 (isAppInteractionRequired = True).
Roshan Piusffc29912019-01-18 13:39:49 -0800192 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800193 """
194 network_suggestion = self.wpa_psk_2g
195 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
196 self.add_suggestions_and_ensure_connection(
197 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
198 True)
199 self.dut.log.info("Removing network suggestions");
200 asserts.assert_true(
201 self.dut.droid.wifiRemoveNetworkSuggestions([network_suggestion]),
202 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800203 # Ensure we did not disconnect
204 wutils.ensure_no_disconnect(self.dut)
205
206 # Trigger a disconnect and wait for the disconnect.
207 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800208 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800209 self.dut.ed.clear_all_events()
210
211 # Now ensure that we didn't connect back.
212 asserts.assert_false(
213 wutils.wait_for_connect(self.dut,
214 self.wpa_psk_2g[WifiEnums.SSID_KEY],
215 assert_on_fail=False),
216 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800217
218
Roshan Piusf028bd32018-12-06 15:18:21 -0800219 @test_tracker_info(uuid="a036a24d-29c0-456d-ae6a-afdde34da710")
Roshan Piusd1204442018-11-12 12:20:39 -0800220 def test_connect_to_wpa_psk_5g_reboot_config_store(self):
221 """
222 Adds a network suggestion and ensure that the device connects to it
223 after reboot.
224
225 Steps:
226 1. Send a network suggestion to the device.
227 2. Wait for the device to connect to it.
228 3. Ensure that we did not receive the post connection broadcast
229 (isAppInteractionRequired = False).
230 4. Reboot the device.
231 5. Wait for the device to connect to back to it.
Roshan Piusffc29912019-01-18 13:39:49 -0800232 6. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800233 """
234 self.add_suggestions_and_ensure_connection(
235 [self.wpa_psk_5g], self.wpa_psk_5g[WifiEnums.SSID_KEY],
236 None)
237
238 # Reboot and wait for connection back to the same suggestion.
239 self.dut.reboot()
240 time.sleep(DEFAULT_TIMEOUT)
241
242 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
243
244 self.dut.log.info("Removing network suggestions");
245 asserts.assert_true(
246 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_5g]),
247 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800248 # Ensure we did not disconnect
249 wutils.ensure_no_disconnect(self.dut)
250
251 # Trigger a disconnect and wait for the disconnect.
252 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800253 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800254 self.dut.ed.clear_all_events()
255
256 # Now ensure that we didn't connect back.
257 asserts.assert_false(
258 wutils.wait_for_connect(self.dut,
259 self.wpa_psk_5g[WifiEnums.SSID_KEY],
260 assert_on_fail=False),
261 "Device should not connect back")
262
Roshan Pius3e3bd342019-01-09 13:55:17 -0800263
264 @test_tracker_info(uuid="554b5861-22d0-4922-a5f4-712b4cf564eb")
265 def test_fail_to_connect_to_wpa_psk_5g_when_not_approved(self):
266 """
267 Adds a network suggestion and ensure that the device does not
268 connect to it until we approve the app.
269
270 Steps:
271 1. Send a network suggestion to the device with the app not approved.
272 2. Ensure the network is present in scan results, but we don't connect
273 to it.
274 3. Now approve the app.
275 4. Wait for the device to connect to it.
276 """
277 self.dut.log.info("Adding network suggestions");
278 asserts.assert_true(
279 self.dut.droid.wifiAddNetworkSuggestions([self.wpa_psk_5g]),
280 "Failed to add suggestions")
281
282 # Disable suggestions by the app.
283 self.set_approved(False)
284
285 # Ensure the app is not approved.
286 asserts.assert_false(
287 self.is_approved(),
288 "Suggestions should be disabled")
289
290 # Start a new scan to trigger auto-join.
291 wutils.start_wifi_connection_scan_and_ensure_network_found(
292 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
293
294 # Ensure we don't connect to the network.
295 asserts.assert_false(
296 wutils.wait_for_connect(
297 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY], assert_on_fail=False),
298 "Should not connect to network suggestions from unapproved app")
299
300 self.dut.log.info("Enabling suggestions from test");
301 # Now Enable suggestions by the app & ensure we connect to the network.
302 self.set_approved(True)
303
304 # Ensure the app is approved.
305 asserts.assert_true(
306 self.is_approved(),
307 "Suggestions should be enabled")
308
309 # Start a new scan to trigger auto-join.
310 wutils.start_wifi_connection_scan_and_ensure_network_found(
311 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
312
313 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
Roshan Pius89b22bd2019-01-24 14:12:49 -0800314
Roshan Pius3d57f132019-01-28 10:25:18 -0800315 @test_tracker_info(uuid="98400dea-776e-4a0a-9024-18845b27331c")
Roshan Pius89b22bd2019-01-24 14:12:49 -0800316 def test_fail_to_connect_to_wpa_psk_2g_after_user_forgot_network(self):
317 """
318 Adds a network suggestion and ensures that the device does not
319 connect to it after the user forgot the network previously.
320
321 Steps:
322 1. Send a network suggestion to the device with
323 isAppInteractionRequired set.
324 2. Wait for the device to connect to it.
325 3. Ensure that we did receive the post connection broadcast
326 (isAppInteractionRequired = True).
327 4. Simulate user forgetting the network and the device does not
328 connecting back even though the suggestion is active from the app.
329 """
330 network_suggestion = self.wpa_psk_2g
331 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
332 self.add_suggestions_and_ensure_connection(
333 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
334 True)
335
336 # Simulate user forgeting the ephemeral network.
337 self.dut.droid.wifiDisableEphemeralNetwork(
338 self.wpa_psk_2g[WifiEnums.SSID_KEY])
339 wutils.wait_for_disconnect(self.dut)
340 self.dut.log.info("Disconnected from network %s", self.wpa_psk_2g)
341 self.dut.ed.clear_all_events()
342
343 # Now ensure that we don't connect back even though the suggestion
344 # is still active.
345 asserts.assert_false(
346 wutils.wait_for_connect(self.dut,
347 self.wpa_psk_2g[WifiEnums.SSID_KEY],
348 assert_on_fail=False),
349 "Device should not connect back")