blob: fcfd115244a5fb40bde79fa6c87022e6e3c398c0 [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([])
83 wutils.reset_wifi(self.dut)
84 self.dut.ed.clear_all_events()
85
86 def on_fail(self, test_name, begin_time):
87 self.dut.take_bug_report(test_name, begin_time)
88 self.dut.cat_adb_log(test_name, begin_time)
89
90 def teardown_class(self):
91 if "AccessPoint" in self.user_params:
92 del self.user_params["reference_networks"]
93 del self.user_params["open_network"]
94
95 """Helper Functions"""
Roshan Pius3e3bd342019-01-09 13:55:17 -080096 def set_approved(self, approved):
97 self.dut.log.debug("Setting suggestions from sl4a app "
98 + "approved" if approved else "not approved")
99 self.dut.adb.shell("cmd wifi network-suggestions-set-user-approved"
100 + " " + SL4A_APK_NAME
101 + " " + ("yes" if approved else "no"))
102
103 def is_approved(self):
104 is_approved_str = self.dut.adb.shell(
105 "cmd wifi network-suggestions-has-user-approved"
106 + " " + SL4A_APK_NAME)
107 return True if (is_approved_str == "yes") else False
108
Roshan Piusd1204442018-11-12 12:20:39 -0800109 def add_suggestions_and_ensure_connection(self, network_suggestions,
110 expected_ssid,
111 expect_post_connection_broadcast):
112 self.dut.log.info("Adding network suggestions");
113 asserts.assert_true(
114 self.dut.droid.wifiAddNetworkSuggestions(network_suggestions),
115 "Failed to add suggestions")
Roshan Pius3e3bd342019-01-09 13:55:17 -0800116 # Enable suggestions by the app.
117 self.dut.log.debug("Enabling suggestions from test");
118 self.set_approved(True)
Roshan Piusd1204442018-11-12 12:20:39 -0800119 wutils.wait_for_connect(self.dut, expected_ssid)
120
121 if expect_post_connection_broadcast is None:
122 return;
123
124 # Check if we expected to get the broadcast.
125 try:
126 self.dut.droid.wifiStartTrackingStateChange()
127 event = self.dut.ed.pop_event(
128 wifi_constants.WIFI_NETWORK_SUGGESTION_POST_CONNECTION, 60)
129 self.dut.droid.wifiStopTrackingStateChange()
130 except queue.Empty:
131 if expect_post_connection_broadcast:
132 raise signals.TestFailure(
133 "Did not receive post connection broadcast")
134 else:
135 if not expect_post_connection_broadcast:
136 raise signals.TestFailure(
137 "Received post connection broadcast")
138
139
Roshan Piusf028bd32018-12-06 15:18:21 -0800140 @test_tracker_info(uuid="bda8ed20-4382-4380-831a-64cf77eca108")
Roshan Piusd1204442018-11-12 12:20:39 -0800141 def test_connect_to_wpa_psk_2g(self):
142 """ Adds a network suggestion and ensure that the device connected.
143
144 Steps:
145 1. Send a network suggestion to the device.
146 2. Wait for the device to connect to it.
147 3. Ensure that we did not receive the post connection broadcast
148 (isAppInteractionRequired = False).
149 4. Remove the suggestions and ensure the device disconnected.
150 """
151 self.add_suggestions_and_ensure_connection(
152 [self.wpa_psk_2g], self.wpa_psk_2g[WifiEnums.SSID_KEY],
153 False)
154 self.dut.log.info("Removing network suggestions");
155 asserts.assert_true(
156 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_2g]),
157 "Failed to remove suggestions")
158 wutils.wait_for_disconnect(self.dut)
159
160
Roshan Piusf028bd32018-12-06 15:18:21 -0800161 @test_tracker_info(uuid="b1d27eea-23c8-4c4f-b944-ef118e4cc35f")
Roshan Piusd1204442018-11-12 12:20:39 -0800162 def test_connect_to_wpa_psk_2g_with_post_connection_broadcast(self):
163 """ Adds a network suggestion and ensure that the device connected.
164
165 Steps:
166 1. Send a network suggestion to the device with
167 isAppInteractionRequired set.
168 2. Wait for the device to connect to it.
169 3. Ensure that we did receive the post connection broadcast
170 (isAppInteractionRequired = True).
171 4. Remove the suggestions and ensure the device disconnected.
172 """
173 network_suggestion = self.wpa_psk_2g
174 network_suggestion[WifiEnums.IS_APP_INTERACTION_REQUIRED] = True
175 self.add_suggestions_and_ensure_connection(
176 [network_suggestion], self.wpa_psk_2g[WifiEnums.SSID_KEY],
177 True)
178 self.dut.log.info("Removing network suggestions");
179 asserts.assert_true(
180 self.dut.droid.wifiRemoveNetworkSuggestions([network_suggestion]),
181 "Failed to remove suggestions")
182 wutils.wait_for_disconnect(self.dut)
183
184
Roshan Piusf028bd32018-12-06 15:18:21 -0800185 @test_tracker_info(uuid="a036a24d-29c0-456d-ae6a-afdde34da710")
Roshan Piusd1204442018-11-12 12:20:39 -0800186 def test_connect_to_wpa_psk_5g_reboot_config_store(self):
187 """
188 Adds a network suggestion and ensure that the device connects to it
189 after reboot.
190
191 Steps:
192 1. Send a network suggestion to the device.
193 2. Wait for the device to connect to it.
194 3. Ensure that we did not receive the post connection broadcast
195 (isAppInteractionRequired = False).
196 4. Reboot the device.
197 5. Wait for the device to connect to back to it.
198 6. Remove the suggestions and ensure the device disconnected.
199 """
200 self.add_suggestions_and_ensure_connection(
201 [self.wpa_psk_5g], self.wpa_psk_5g[WifiEnums.SSID_KEY],
202 None)
203
204 # Reboot and wait for connection back to the same suggestion.
205 self.dut.reboot()
206 time.sleep(DEFAULT_TIMEOUT)
207
208 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
209
210 self.dut.log.info("Removing network suggestions");
211 asserts.assert_true(
212 self.dut.droid.wifiRemoveNetworkSuggestions([self.wpa_psk_5g]),
213 "Failed to remove suggestions")
214 wutils.wait_for_disconnect(self.dut)
Roshan Pius3e3bd342019-01-09 13:55:17 -0800215
216 @test_tracker_info(uuid="554b5861-22d0-4922-a5f4-712b4cf564eb")
217 def test_fail_to_connect_to_wpa_psk_5g_when_not_approved(self):
218 """
219 Adds a network suggestion and ensure that the device does not
220 connect to it until we approve the app.
221
222 Steps:
223 1. Send a network suggestion to the device with the app not approved.
224 2. Ensure the network is present in scan results, but we don't connect
225 to it.
226 3. Now approve the app.
227 4. Wait for the device to connect to it.
228 """
229 self.dut.log.info("Adding network suggestions");
230 asserts.assert_true(
231 self.dut.droid.wifiAddNetworkSuggestions([self.wpa_psk_5g]),
232 "Failed to add suggestions")
233
234 # Disable suggestions by the app.
235 self.set_approved(False)
236
237 # Ensure the app is not approved.
238 asserts.assert_false(
239 self.is_approved(),
240 "Suggestions should be disabled")
241
242 # Start a new scan to trigger auto-join.
243 wutils.start_wifi_connection_scan_and_ensure_network_found(
244 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
245
246 # Ensure we don't connect to the network.
247 asserts.assert_false(
248 wutils.wait_for_connect(
249 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY], assert_on_fail=False),
250 "Should not connect to network suggestions from unapproved app")
251
252 self.dut.log.info("Enabling suggestions from test");
253 # Now Enable suggestions by the app & ensure we connect to the network.
254 self.set_approved(True)
255
256 # Ensure the app is approved.
257 asserts.assert_true(
258 self.is_approved(),
259 "Suggestions should be enabled")
260
261 # Start a new scan to trigger auto-join.
262 wutils.start_wifi_connection_scan_and_ensure_network_found(
263 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])
264
265 wutils.wait_for_connect(self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY])