blob: 057ec510cd4af7079465502604ba59a3fb1f5ae5 [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()
77 wutils.wifi_toggle_state(self.dut, True)
78
79 def teardown_test(self):
80 self.dut.droid.wakeLockRelease()
81 self.dut.droid.goToSleepNow()
82 self.dut.droid.wifiRemoveNetworkSuggestions([])
Roshan Piusffc29912019-01-18 13:39:49 -080083 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -080084 wutils.reset_wifi(self.dut)
85 self.dut.ed.clear_all_events()
86
87 def on_fail(self, test_name, begin_time):
88 self.dut.take_bug_report(test_name, begin_time)
89 self.dut.cat_adb_log(test_name, begin_time)
90
91 def teardown_class(self):
92 if "AccessPoint" in self.user_params:
93 del self.user_params["reference_networks"]
94 del self.user_params["open_network"]
95
96 """Helper Functions"""
Roshan Pius3e3bd342019-01-09 13:55:17 -080097 def set_approved(self, approved):
98 self.dut.log.debug("Setting suggestions from sl4a app "
99 + "approved" if approved else "not approved")
100 self.dut.adb.shell("cmd wifi network-suggestions-set-user-approved"
101 + " " + SL4A_APK_NAME
102 + " " + ("yes" if approved else "no"))
103
104 def is_approved(self):
105 is_approved_str = self.dut.adb.shell(
106 "cmd wifi network-suggestions-has-user-approved"
107 + " " + SL4A_APK_NAME)
108 return True if (is_approved_str == "yes") else False
109
Roshan Piusd1204442018-11-12 12:20:39 -0800110 def add_suggestions_and_ensure_connection(self, network_suggestions,
111 expected_ssid,
112 expect_post_connection_broadcast):
113 self.dut.log.info("Adding network suggestions");
114 asserts.assert_true(
115 self.dut.droid.wifiAddNetworkSuggestions(network_suggestions),
116 "Failed to add suggestions")
Roshan Pius3e3bd342019-01-09 13:55:17 -0800117 # Enable suggestions by the app.
118 self.dut.log.debug("Enabling suggestions from test");
119 self.set_approved(True)
Roshan Piusd1204442018-11-12 12:20:39 -0800120 wutils.wait_for_connect(self.dut, expected_ssid)
121
122 if expect_post_connection_broadcast is None:
123 return;
124
125 # Check if we expected to get the broadcast.
126 try:
127 self.dut.droid.wifiStartTrackingStateChange()
128 event = self.dut.ed.pop_event(
129 wifi_constants.WIFI_NETWORK_SUGGESTION_POST_CONNECTION, 60)
130 self.dut.droid.wifiStopTrackingStateChange()
131 except queue.Empty:
132 if expect_post_connection_broadcast:
133 raise signals.TestFailure(
134 "Did not receive post connection broadcast")
135 else:
136 if not expect_post_connection_broadcast:
137 raise signals.TestFailure(
138 "Received post connection broadcast")
139
140
Roshan Piusf028bd32018-12-06 15:18:21 -0800141 @test_tracker_info(uuid="bda8ed20-4382-4380-831a-64cf77eca108")
Roshan Piusd1204442018-11-12 12:20:39 -0800142 def test_connect_to_wpa_psk_2g(self):
143 """ Adds a network suggestion and ensure that the device connected.
144
145 Steps:
146 1. Send a network suggestion to the device.
147 2. Wait for the device to connect to it.
148 3. Ensure that we did not receive the post connection broadcast
149 (isAppInteractionRequired = False).
Roshan Piusffc29912019-01-18 13:39:49 -0800150 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800151 """
152 self.add_suggestions_and_ensure_connection(
153 [self.wpa_psk_2g], self.wpa_psk_2g[WifiEnums.SSID_KEY],
154 False)
155 self.dut.log.info("Removing network suggestions");
156 asserts.assert_true(
157 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_2g]),
158 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800159 # Ensure we did not disconnect
160 wutils.ensure_no_disconnect(self.dut)
161
162 # Trigger a disconnect and wait for the disconnect.
163 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800164 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800165 self.dut.ed.clear_all_events()
166
167 # Now ensure that we didn't connect back.
168 asserts.assert_false(
169 wutils.wait_for_connect(self.dut,
170 self.wpa_psk_2g[WifiEnums.SSID_KEY],
171 assert_on_fail=False),
172 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800173
174
Roshan Piusf028bd32018-12-06 15:18:21 -0800175 @test_tracker_info(uuid="b1d27eea-23c8-4c4f-b944-ef118e4cc35f")
Roshan Piusd1204442018-11-12 12:20:39 -0800176 def test_connect_to_wpa_psk_2g_with_post_connection_broadcast(self):
177 """ Adds a network suggestion and ensure that the device connected.
178
179 Steps:
180 1. Send a network suggestion to the device with
181 isAppInteractionRequired set.
182 2. Wait for the device to connect to it.
183 3. Ensure that we did receive the post connection broadcast
184 (isAppInteractionRequired = True).
Roshan Piusffc29912019-01-18 13:39:49 -0800185 4. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800186 """
187 network_suggestion = self.wpa_psk_2g
188 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
189 self.add_suggestions_and_ensure_connection(
190 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
191 True)
192 self.dut.log.info("Removing network suggestions");
193 asserts.assert_true(
194 self.dut.droid.wifiRemoveNetworkSuggestions([network_suggestion]),
195 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800196 # Ensure we did not disconnect
197 wutils.ensure_no_disconnect(self.dut)
198
199 # Trigger a disconnect and wait for the disconnect.
200 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800201 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800202 self.dut.ed.clear_all_events()
203
204 # Now ensure that we didn't connect back.
205 asserts.assert_false(
206 wutils.wait_for_connect(self.dut,
207 self.wpa_psk_2g[WifiEnums.SSID_KEY],
208 assert_on_fail=False),
209 "Device should not connect back")
Roshan Piusd1204442018-11-12 12:20:39 -0800210
211
Roshan Piusf028bd32018-12-06 15:18:21 -0800212 @test_tracker_info(uuid="a036a24d-29c0-456d-ae6a-afdde34da710")
Roshan Piusd1204442018-11-12 12:20:39 -0800213 def test_connect_to_wpa_psk_5g_reboot_config_store(self):
214 """
215 Adds a network suggestion and ensure that the device connects to it
216 after reboot.
217
218 Steps:
219 1. Send a network suggestion to the device.
220 2. Wait for the device to connect to it.
221 3. Ensure that we did not receive the post connection broadcast
222 (isAppInteractionRequired = False).
223 4. Reboot the device.
224 5. Wait for the device to connect to back to it.
Roshan Piusffc29912019-01-18 13:39:49 -0800225 6. Remove the suggestions and ensure the device does not connect back.
Roshan Piusd1204442018-11-12 12:20:39 -0800226 """
227 self.add_suggestions_and_ensure_connection(
228 [self.wpa_psk_5g], self.wpa_psk_5g[WifiEnums.SSID_KEY],
229 None)
230
231 # Reboot and wait for connection back to the same suggestion.
232 self.dut.reboot()
233 time.sleep(DEFAULT_TIMEOUT)
234
235 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
236
237 self.dut.log.info("Removing network suggestions");
238 asserts.assert_true(
239 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_5g]),
240 "Failed to remove suggestions")
Roshan Piusffc29912019-01-18 13:39:49 -0800241 # Ensure we did not disconnect
242 wutils.ensure_no_disconnect(self.dut)
243
244 # Trigger a disconnect and wait for the disconnect.
245 self.dut.droid.wifiDisconnect()
Roshan Piusd1204442018-11-12 12:20:39 -0800246 wutils.wait_for_disconnect(self.dut)
Roshan Piusffc29912019-01-18 13:39:49 -0800247 self.dut.ed.clear_all_events()
248
249 # Now ensure that we didn't connect back.
250 asserts.assert_false(
251 wutils.wait_for_connect(self.dut,
252 self.wpa_psk_5g[WifiEnums.SSID_KEY],
253 assert_on_fail=False),
254 "Device should not connect back")
255
Roshan Pius3e3bd342019-01-09 13:55:17 -0800256
257 @test_tracker_info(uuid="554b5861-22d0-4922-a5f4-712b4cf564eb")
258 def test_fail_to_connect_to_wpa_psk_5g_when_not_approved(self):
259 """
260 Adds a network suggestion and ensure that the device does not
261 connect to it until we approve the app.
262
263 Steps:
264 1. Send a network suggestion to the device with the app not approved.
265 2. Ensure the network is present in scan results, but we don't connect
266 to it.
267 3. Now approve the app.
268 4. Wait for the device to connect to it.
269 """
270 self.dut.log.info("Adding network suggestions");
271 asserts.assert_true(
272 self.dut.droid.wifiAddNetworkSuggestions([self.wpa_psk_5g]),
273 "Failed to add suggestions")
274
275 # Disable suggestions by the app.
276 self.set_approved(False)
277
278 # Ensure the app is not approved.
279 asserts.assert_false(
280 self.is_approved(),
281 "Suggestions should be disabled")
282
283 # Start a new scan to trigger auto-join.
284 wutils.start_wifi_connection_scan_and_ensure_network_found(
285 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
286
287 # Ensure we don't connect to the network.
288 asserts.assert_false(
289 wutils.wait_for_connect(
290 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY], assert_on_fail=False),
291 "Should not connect to network suggestions from unapproved app")
292
293 self.dut.log.info("Enabling suggestions from test");
294 # Now Enable suggestions by the app & ensure we connect to the network.
295 self.set_approved(True)
296
297 # Ensure the app is approved.
298 asserts.assert_true(
299 self.is_approved(),
300 "Suggestions should be enabled")
301
302 # Start a new scan to trigger auto-join.
303 wutils.start_wifi_connection_scan_and_ensure_network_found(
304 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
305
306 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])