blob: 3f585a2dffc49b0cc28219f727e9aa054a7a7fa4 [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
Hayden Nixfe643052020-12-09 00:50:05 +000018from acts.controllers.access_point import setup_ap
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070019from acts.controllers.ap_lib import hostapd_constants
20from acts.controllers.ap_lib import hostapd_security
Xianyuan Jia24299b72020-10-21 13:52:47 -070021from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070022from acts.utils import rand_ascii_str
23
24DISCONNECTED = "Disconnected"
25CONNECTION_STOPPED = "ConnectionStopped"
26CONNECTIONS_ENABLED = "ConnectionsEnabled"
27CONNECTIONS_DISABLED = "ConnectionsDisabled"
28WPA2 = "wpa2"
29
30
31class StartStopClientConnectionsTest(WifiBaseTest):
32 """ Tests that we see the expected behavior with enabling and disabling
33 client connections
34
35 Test Bed Requirement:
36 * One or more Fuchsia devices
37 * One Access Point
38 """
39 def setup_class(self):
40 super().setup_class()
41 # Start an AP with a hidden network
42 self.ssid = rand_ascii_str(hostapd_constants.AP_SSID_LENGTH_2G)
43 self.access_point = self.access_points[0]
44 self.password = rand_ascii_str(
45 hostapd_constants.AP_PASSPHRASE_LENGTH_2G)
46 self.security_type = WPA2
47 security = hostapd_security.Security(security_mode=self.security_type,
48 password=self.password)
49
50 self.access_point.stop_all_aps()
51 # TODO(63719) use varying values for AP that shouldn't affect the test.
52 setup_ap(self.access_point,
53 'whirlwind',
54 hostapd_constants.AP_DEFAULT_CHANNEL_5G,
55 self.ssid,
56 security=security)
57
58 if len(self.fuchsia_devices) < 1:
59 raise EnvironmentError("No Fuchsia devices found.")
Hayden Nixfe643052020-12-09 00:50:05 +000060 for fd in self.fuchsia_devices:
61 fd.configure_wlan(association_mechanism='policy',
62 preserve_saved_networks=True)
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070063
64 def setup_test(self):
65 for fd in self.fuchsia_devices:
Hayden Nixfe643052020-12-09 00:50:05 +000066 if not fd.wlan_policy_controller.remove_all_networks():
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070067 raise EnvironmentError(
Hayden Nixfe643052020-12-09 00:50:05 +000068 "Failed to remove all networks in setup")
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070069
70 def teardown_class(self):
71 self.access_point.stop_all_aps()
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070072
73 def connect_and_validate(self, fd, ssid, security_type, expected_response):
74 """ Sends a connect request to the device and verifies we get a response
75 without error. This does not validate that a connection will be
76 attempted. This will fail the test if there is an error sending the
77 connect request, or if we don't get the expected connect response."""
78 result_connect = fd.wlan_policy_lib.wlanConnect(ssid, security_type)
79 if result_connect.get("error") != None:
Hayden Nixfe643052020-12-09 00:50:05 +000080 self.log.error("Error occurred requesting a connection: %s" %
81 result_connect.get("error"))
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070082 raise EnvironmentError("Failed to send connect request")
83 response = result_connect.get("result")
84 if response != expected_response:
85 self.log.error(
86 "Incorrect connect request response. Expected: \"%s\", Actual: %s"
87 % (expected_response, response))
88 raise signals.TestFailure(
89 "Failed to get expected connect response")
90
91 def test_stop_client_connections_update(self):
92 for fd in self.fuchsia_devices:
Hayden Nixfe643052020-12-09 00:50:05 +000093 if not fd.wlan_policy_controller.stop_client_connections():
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -070094 raise EnvironmentError("Failed to stop client connecions")
95
96 # Check that the most recent update says that the device is not
97 # connected to anything and client connections are disabled
98 fd.wlan_policy_lib.wlanSetNewListener()
99 result_update = fd.wlan_policy_lib.wlanGetUpdate()
100 if result_update.get("error") != None:
101 self.log.error("Error occurred getting status update: %s" %
102 result_update.get("error"))
103 raise EnvironmentError("Failed to get update")
104
105 expected_update = {"networks": [], "state": CONNECTIONS_DISABLED}
106 if result_update.get("result") != expected_update:
107 self.log.error(
108 "Most recent status update does not indicate client "
Hayden Nixfe643052020-12-09 00:50:05 +0000109 "connections have stopped. Expected update: %s Actual update: %s"
110 % (expected_update, result_update.get('result')))
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700111 raise signals.TestFailure(
112 "Incorrect update after stopping client connections")
113
114 def test_start_client_connections_update(self):
115 for fd in self.fuchsia_devices:
Hayden Nixfe643052020-12-09 00:50:05 +0000116 if not fd.wlan_policy_controller.start_client_connections():
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700117 raise EnvironmentError("Failed to start client connecions")
118
119 # Check that the most recent update says that the device is not
120 # connected to anything and client connections are disabled
121 fd.wlan_policy_lib.wlanSetNewListener()
122 result_update = fd.wlan_policy_lib.wlanGetUpdate()
123 if result_update.get("error") != None:
124 self.log.error("Error occurred getting status update: %s" %
125 result_update.get("error"))
126 raise EnvironmentError("Failed to get update")
127
128 expected_update = {"networks": [], "state": CONNECTIONS_ENABLED}
129 if result_update.get("result") != expected_update:
130 self.log.error(
131 "Most recent status update does not indicate client "
132 "connections are enabled. Expected update: %s\nActual update:"
133 % (expected_update, result_update))
134 raise signals.TestFailure(
135 "Incorrect update after starting client connections")
136
137 def test_stop_client_connections_rejects_connections(self):
138 # Test that if we turn client connections off, our requests to connect
139 # are rejected.
140 for fd in self.fuchsia_devices:
Hayden Nixfe643052020-12-09 00:50:05 +0000141 if not fd.wlan_policy_controller.stop_client_connections():
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700142 raise EnvironmentError("Failed to stop client connecions")
143
144 # Save the network, otherwise connecting may fail because the
145 # network is not saved instead of client connections being off
Hayden Nixfe643052020-12-09 00:50:05 +0000146 if not fd.wlan_policy_controller.save_network(
147 self.ssid, self.security_type, password=self.password):
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700148 raise EnvironmentError("Failed to save network")
149 expected_response = "RejectedIncompatibleMode"
150 self.connect_and_validate(fd, self.ssid, self.security_type,
151 expected_response)
152
153 def test_start_stop_client_connections(self):
154 # Test that if we turn client connections on the device will connect,
155 # and if we turn of client connections the device will disconnect.
156 for fd in self.fuchsia_devices:
157 # Start client connections and check that we can
Hayden Nixfe643052020-12-09 00:50:05 +0000158 if not fd.wlan_policy_controller.save_network(
159 self.ssid, self.security_type, password=self.password):
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700160 raise EnvironmentError("Failed to save network")
Hayden Nixfe643052020-12-09 00:50:05 +0000161 if not fd.wlan_policy_controller.start_client_connections():
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700162 raise EnvironmentError("Failed to start client connections")
163
164 expected_response = "Acknowledged"
165 self.connect_and_validate(fd, self.ssid, self.security_type,
166 expected_response)
167
Hayden Nixfe643052020-12-09 00:50:05 +0000168 if not fd.wlan_policy_controller.wait_for_connect(
169 self.ssid, self.security_type):
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700170 raise signals.TestFailure(
171 "Failed to connect after starting client connections")
172
173 # Stop client connections again and check that we disconnect
Hayden Nixfe643052020-12-09 00:50:05 +0000174 if not fd.wlan_policy_controller.stop_client_connections():
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700175 raise EnvironmentError("Failed to stop client connecions")
Hayden Nixfe643052020-12-09 00:50:05 +0000176 if not fd.wlan_policy_controller.wait_for_disconnect(
177 self.ssid, self.security_type, DISCONNECTED,
178 CONNECTION_STOPPED):
repo sync tools/test/connectivityae92d752020-10-27 12:42:38 -0700179 raise signals.TestFailure(
180 "Failed to disconnect after client connections stopped")