1#!/usr/bin/env python3.4 2# 3# Copyright 2016 - 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 logging 18import queue 19import time 20 21import acts.test_utils.wifi.wifi_test_utils as wutils 22 23from acts import asserts 24from acts import signals 25from acts import utils 26from acts.base_test import BaseTestClass 27from acts.test_utils.tel import tel_defines 28from acts.test_utils.tel.tel_test_utils import is_sim_ready 29 30class WifiSoftApTest(BaseTestClass): 31 32 def setup_class(self): 33 """It will setup the required dependencies from config file and configure 34 the devices for softap mode testing. 35 36 Returns: 37 True if successfully configured the requirements for testing. 38 """ 39 self.dut = self.android_devices[0] 40 self.dut_client = self.android_devices[1] 41 # Do a simple version of init - mainly just sync the time and enable 42 # verbose logging. This test will fail if the DUT has a sim and cell 43 # data is disabled. We would also like to test with phones in less 44 # constrained states (or add variations where we specifically 45 # constrain). 46 utils.require_sl4a((self.dut, self.dut_client)) 47 utils.sync_device_time(self.dut) 48 utils.sync_device_time(self.dut_client) 49 50 # Enable verbose logging on the duts 51 self.dut.droid.wifiEnableVerboseLogging(1) 52 msg = "Failed to enable WiFi verbose logging on the softap dut." 53 asserts.assert_true(self.dut.droid.wifiGetVerboseLoggingLevel() == 1, msg) 54 self.dut_client.droid.wifiEnableVerboseLogging(1) 55 msg = "Failed to enable WiFi verbose logging on the client dut." 56 asserts.assert_true(self.dut_client.droid.wifiGetVerboseLoggingLevel() == 1, msg) 57 58 def teardown_class(self): 59 wutils.reset_wifi(self.dut) 60 wutils.reset_wifi(self.dut_client) 61 62 """ Helper Functions """ 63 def verify_return_to_wifi_enabled(self): 64 """Verifies that wifi is enabled 65 66 We consider wifi as enabled if two things have happened. First, supplicant 67 is started correctly (seen with the supplicant connection change). Second, 68 supplicant should initially enter the disconnected state. 69 """ 70 curr_state = "waiting for supplicant to come back up" 71 try: 72 self.dut.droid.wifiStartTrackingStateChange() 73 event = self.dut.ed.pop_event("SupplicantConnectionChanged", 10) 74 except queue.Empty: 75 self.log.exception("Failed to restart wifi: current state = %s", 76 curr_state) 77 asserts.fail(curr_state) 78 finally: 79 self.dut.droid.wifiStopTrackingStateChange() 80 81 #TODO(silberst): uncomment and remove loop below when b/30037819 is fixed 82 #curr_state = "waiting for wifi to go back into connect mode" 83 #try: 84 # self.dut.droid.wifiStartTrackingStateChange() 85 # event = self.dut.ed.pop_event("WifiNetworkDisconnected", 10) 86 # self.dut.droid.wifiStopTrackingStateChange() 87 #except queue.Empty: 88 # self.log.exception("Failed to restart wifi: current state = %s", curr_state) 89 # asserts.fail(curr_state) 90 attempt_count = 0 91 max_attempts = 3 92 while attempt_count < max_attempts: 93 if not self.dut.droid.wifiCheckState(): 94 attempt_count += 1 95 time.sleep(5) 96 else: 97 return 98 asserts.fail("failed waiting for wifi to return to connect mode") 99 100 def create_softap_config(self): 101 """Create a softap config with ssid and password.""" 102 ap_ssid = "softap_" + utils.rand_ascii_str(8) 103 ap_password = utils.rand_ascii_str(8) 104 self.log.info("softap setup: %s %s", ap_ssid, ap_password) 105 config = {wutils.WifiEnums.SSID_KEY: ap_ssid} 106 config[wutils.WifiEnums.PWD_KEY] = ap_password 107 return config 108 109 def confirm_softap_in_scan_results(self, ap_ssid): 110 """Confirm the ap started by wifi tethering is seen in scan results. 111 112 Args: 113 ap_ssid: SSID of the ap we are looking for. 114 """ 115 #TODO(silberst): debug and remove the extra scan before submitting this test 116 wutils.start_wifi_connection_scan(self.dut_client) 117 client_scan_results = self.dut_client.droid.wifiGetScanResults() 118 wutils.start_wifi_connection_scan(self.dut_client) 119 client_scan_results = self.dut_client.droid.wifiGetScanResults() 120 for result in client_scan_results: 121 self.log.debug("scan found: %s", result[wutils.WifiEnums.SSID_KEY]) 122 123 asserts.assert_true(wutils.match_networks( 124 {wutils.WifiEnums.SSID_KEY: ap_ssid}, client_scan_results), 125 "Did not find SSID in scan results") 126 127 def check_cell_data_and_enable(self): 128 """Make sure that cell data is enabled if there is a sim present. 129 130 If a sim is active, cell data needs to be enabled to allow provisioning 131 checks through (when applicable). This is done to relax hardware 132 requirements on DUTs - without this check, running this set of tests 133 after other wifi tests may cause failures. 134 """ 135 # We do have a sim. Make sure data is enabled so we can tether. 136 if not self.dut.droid.telephonyIsDataEnabled(): 137 self.log.info("need to enable data") 138 self.dut.droid.telephonyToggleDataConnection(True) 139 asserts.assert_true(self.dut.droid.telephonyIsDataEnabled(), 140 "Failed to enable cell data for softap dut.") 141 142 143 """ Tests Begin """ 144 def test_switch_to_softap_mode(self): 145 """Test switch to softap mode. 146 147 1. Report current state. 148 2. Switch to AP mode. 149 3. Switch back to previous state. 150 """ 151 success = True 152 initial_wifi_state = self.dut.droid.wifiCheckState() 153 initial_cell_state = is_sim_ready(self.log, self.dut) 154 self.log.info("current state: %s", initial_wifi_state) 155 self.log.info("is sim ready? %s", initial_cell_state) 156 if initial_cell_state: 157 self.log.info("cell is on") 158 self.check_cell_data_and_enable() 159 160 config = self.create_softap_config() 161 try: 162 self.dut.droid.wifiStartTrackingTetherStateChange() 163 success = self.dut.droid.wifiSetApEnabled(True, config) 164 asserts.assert_true(success, "switch to softap mode failed") 165 curr_state = "waiting for WifiManagerApEnabled" 166 event = self.dut.ed.pop_event("WifiManagerApEnabled", 5) 167 self.confirm_softap_in_scan_results(config[wutils.WifiEnums.SSID_KEY]) 168 success = self.dut.droid.wifiSetApEnabled(False, None) 169 asserts.assert_true(success, "turn off softap mode failed") 170 asserts.assert_true(not self.dut.droid.wifiIsApEnabled(), 171 "SoftAp is still reported as running") 172 curr_state = "waiting for WifiManagerApDisabled" 173 event = self.dut.ed.pop_event("WifiManagerApDisabled", 5) 174 if initial_wifi_state: 175 self.verify_return_to_wifi_enabled() 176 elif not self.dut.droid.wifiCheckState(): 177 # really need to verify that wifi did not come back up, and will not 178 # TODO(silberst): look at alternatives to this simple check 179 asserts.fail("Wifi was disabled before softap and now it is enabled") 180 except queue.Empty: 181 self.log.exception("Failed to fully start/stop softap mode: current state = %s", 182 curr_state) 183 finally: 184 #self.dut.droid.wifiStopTrackingTetherStateChange() 185 self.dut.droid.wifiStopTrackingTetherStateChange() 186 self.dut_client.droid.wifiStopTrackingStateChange() 187 188 def test_check_wifi_tethering_supported(self): 189 """Test check for wifi tethering support. 190 191 1. Call method to check if wifi hotspot is supported 192 """ 193 # TODO(silberst): wifiIsPortableHotspotSupported() is currently failing. 194 # Remove the extra check and logging when b/30800811 is resolved 195 portable_hotspot_supported = self.dut.droid.wifiIsPortableHotspotSupported() 196 tethering_supported = self.dut.droid.connectivityIsTetheringSupported() 197 if not portable_hotspot_supported: 198 self.log.info("DUT should support wifi tethering but is reporting false.") 199 if not tethering_supported: 200 self.log.info("DUT should also support wifi tethering when called from ConnectivityManager") 201 asserts.assert_true(self.dut.droid.wifiIsPortableHotspotSupported(), 202 "DUT should support wifi tethering but is reporting false.") 203 asserts.assert_true(self.dut.droid.connectivityIsTetheringSupported(), 204 "DUT should also support wifi tethering when called from ConnectivityManager") 205 206 def test_full_tether_startup(self): 207 """Test full startup of wifi tethering 208 209 1. Report current state. 210 2. Switch to AP mode. 211 3. verify SoftAP active. 212 4. Shutdown wifi tethering. 213 5. verify back to previous mode. 214 """ 215 success = True 216 initial_wifi_state = self.dut.droid.wifiCheckState() 217 initial_cell_state = is_sim_ready(self.log, self.dut) 218 self.log.info("current state: %s", initial_wifi_state) 219 self.log.info("is sim ready? %s", initial_cell_state) 220 221 if initial_cell_state: 222 self.check_cell_data_and_enable() 223 224 config = self.create_softap_config() 225 226 success = wutils.start_wifi_tethering(self.dut, 227 config[wutils.WifiEnums.SSID_KEY], 228 config[wutils.WifiEnums.PWD_KEY]) 229 asserts.assert_true(success, "call to start_wifi_tethering returned false. Check config") 230 self.confirm_softap_in_scan_results(config[wutils.WifiEnums.SSID_KEY]) 231 wutils.stop_wifi_tethering(self.dut) 232 asserts.assert_true(not self.dut.droid.wifiIsApEnabled(), 233 "SoftAp is still reported as running") 234 if initial_wifi_state: 235 self.verify_return_to_wifi_enabled() 236 elif not self.dut.droid.wifiCheckState(): 237 asserts.fail("Wifi was disabled before softap and now it is enabled") 238 239 """ Tests End """ 240if __name__ == "__main__": 241 pass 242