blob: 5ca70f4e1363364e9b9e19f87012e69320f3ee8f [file] [log] [blame]
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -07001#!/usr/bin/env python3
2#
3# Copyright 2020 - 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
17from acts import signals
18from acts.controllers.ap_lib import hostapd_constants
19from acts.controllers.ap_lib import hostapd_security
Xianyuan Jia24299b72020-10-21 13:52:47 -070020from acts_contrib.test_utils.abstract_devices.utils_lib.wlan_utils import setup_ap
21from acts_contrib.test_utils.abstract_devices.utils_lib.wlan_policy_utils import setup_policy_tests, restore_state, save_network, start_connections, stop_connections
22from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070023from acts.utils import rand_ascii_str
24
25DISCONNECTED = "Disconnected"
26CONNECTION_STOPPED = "ConnectionStopped"
27CONNECTIONS_ENABLED = "ConnectionsEnabled"
28CONNECTIONS_DISABLED = "ConnectionsDisabled"
29WPA2 = "wpa2"
30
31
32class StartStopClientConnectionsTest(WifiBaseTest):
33 """ Tests that we see the expected behavior with enabling and disabling
34 client connections
35
36 Test Bed Requirement:
37 * One or more Fuchsia devices
38 * One Access Point
39 """
40 def setup_class(self):
41 super().setup_class()
42 # Start an AP with a hidden network
43 self.ssid = rand_ascii_str(hostapd_constants.AP_SSID_LENGTH_2G)
44 self.access_point = self.access_points[0]
45 self.password = rand_ascii_str(
46 hostapd_constants.AP_PASSPHRASE_LENGTH_2G)
47 self.security_type = WPA2
48 security = hostapd_security.Security(security_mode=self.security_type,
49 password=self.password)
50
51 self.access_point.stop_all_aps()
52 # TODO(63719) use varying values for AP that shouldn't affect the test.
53 setup_ap(self.access_point,
54 'whirlwind',
55 hostapd_constants.AP_DEFAULT_CHANNEL_5G,
56 self.ssid,
57 security=security)
58
59 if len(self.fuchsia_devices) < 1:
60 raise EnvironmentError("No Fuchsia devices found.")
61 # Save the existing saved networks before we remove them for tests
62 # And remember whether client connections have started
63 self.preexisting_state = setup_policy_tests(self.fuchsia_devices)
64
65 def setup_test(self):
66 for fd in self.fuchsia_devices:
67 # Set new update listener so that the next test can always get the
68 # most recent udpdate immediately.
69 new_listener_result = fd.wlan_policy_lib.wlanSetNewListener()
70 if new_listener_result.get("error") != None:
71 self.log.warn(
72 "Error occurred initializing a new update listener for the facade, may"
73 "cause errors in updates for tests: %s" %
74 new_listener_result.get("error"))
75
76 resultRemove = fd.wlan_policy_lib.wlanRemoveAllNetworks()
77 if resultRemove.get("error") != None:
78 raise EnvironmentError(
79 "Failed to remove all networks in setup: %s" %
80 resultRemove.get("error"))
81
82 def teardown_class(self):
83 self.access_point.stop_all_aps()
84 for fd in self.fuchsia_devices:
85 # Put back the networks that were saved before tests began.
86 restore_state(self.fuchsia_devices, self.preexisting_state)
87
88 def connect_and_validate(self, fd, ssid, security_type, expected_response):
89 """ Sends a connect request to the device and verifies we get a response
90 without error. This does not validate that a connection will be
91 attempted. This will fail the test if there is an error sending the
92 connect request, or if we don't get the expected connect response."""
93 result_connect = fd.wlan_policy_lib.wlanConnect(ssid, security_type)
94 if result_connect.get("error") != None:
95 logging.error("Error occurred requesting a connection: %s" %
96 result_connect.get("error"))
97 raise EnvironmentError("Failed to send connect request")
98 response = result_connect.get("result")
99 if response != expected_response:
100 self.log.error(
101 "Incorrect connect request response. Expected: \"%s\", Actual: %s"
102 % (expected_response, response))
103 raise signals.TestFailure(
104 "Failed to get expected connect response")
105
106 def test_stop_client_connections_update(self):
107 for fd in self.fuchsia_devices:
108 if not stop_connections(fd):
109 raise EnvironmentError("Failed to stop client connecions")
110
111 # Check that the most recent update says that the device is not
112 # connected to anything and client connections are disabled
113 fd.wlan_policy_lib.wlanSetNewListener()
114 result_update = fd.wlan_policy_lib.wlanGetUpdate()
115 if result_update.get("error") != None:
116 self.log.error("Error occurred getting status update: %s" %
117 result_update.get("error"))
118 raise EnvironmentError("Failed to get update")
119
120 expected_update = {"networks": [], "state": CONNECTIONS_DISABLED}
121 if result_update.get("result") != expected_update:
122 self.log.error(
123 "Most recent status update does not indicate client "
124 "connections have stopped. Expected update: %s\nActual update:"
125 % (expected_update, result_update))
126 raise signals.TestFailure(
127 "Incorrect update after stopping client connections")
128
129 def test_start_client_connections_update(self):
130 for fd in self.fuchsia_devices:
131 if not start_connections(fd):
132 raise EnvironmentError("Failed to start client connecions")
133
134 # Check that the most recent update says that the device is not
135 # connected to anything and client connections are disabled
136 fd.wlan_policy_lib.wlanSetNewListener()
137 result_update = fd.wlan_policy_lib.wlanGetUpdate()
138 if result_update.get("error") != None:
139 self.log.error("Error occurred getting status update: %s" %
140 result_update.get("error"))
141 raise EnvironmentError("Failed to get update")
142
143 expected_update = {"networks": [], "state": CONNECTIONS_ENABLED}
144 if result_update.get("result") != expected_update:
145 self.log.error(
146 "Most recent status update does not indicate client "
147 "connections are enabled. Expected update: %s\nActual update:"
148 % (expected_update, result_update))
149 raise signals.TestFailure(
150 "Incorrect update after starting client connections")
151
152 def test_stop_client_connections_rejects_connections(self):
153 # Test that if we turn client connections off, our requests to connect
154 # are rejected.
155 for fd in self.fuchsia_devices:
156 if not stop_connections(fd):
157 raise EnvironmentError("Failed to stop client connecions")
158
159 # Save the network, otherwise connecting may fail because the
160 # network is not saved instead of client connections being off
161 if not save_network(fd, self.ssid, self.security_type,
162 self.password):
163 raise EnvironmentError("Failed to save network")
164 expected_response = "RejectedIncompatibleMode"
165 self.connect_and_validate(fd, self.ssid, self.security_type,
166 expected_response)
167
168 def test_start_stop_client_connections(self):
169 # Test that if we turn client connections on the device will connect,
170 # and if we turn of client connections the device will disconnect.
171 for fd in self.fuchsia_devices:
172 # Start client connections and check that we can
173 if not save_network(fd, self.ssid, self.security_type,
174 self.password):
175 raise EnvironmentError("Failed to save network")
176 if not start_connections(fd):
177 raise EnvironmentError("Failed to start client connections")
178
179 expected_response = "Acknowledged"
180 self.connect_and_validate(fd, self.ssid, self.security_type,
181 expected_response)
182
183 if not fd.wait_for_connect(self.ssid, self.security_type):
184 raise signals.TestFailure(
185 "Failed to connect after starting client connections")
186
187 # Stop client connections again and check that we disconnect
188 if not stop_connections(fd):
189 raise EnvironmentError("Failed to stop client connecions")
190 if not fd.wait_for_disconnect(self.ssid, self.security_type,
191 DISCONNECTED, CONNECTION_STOPPED):
192 raise signals.TestFailure(
193 "Failed to disconnect after client connections stopped")