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 17 import logging 18 import queue 19 import time 20 21 from acts import asserts 22 from acts import utils 23 from acts import base_test 24 from acts.test_decorators import test_tracker_info 25 from acts.test_utils.tel import tel_defines 26 from acts.test_utils.tel import tel_test_utils as tel_utils 27 from acts.test_utils.wifi import wifi_test_utils as wutils 28 29 30 class WifiSoftApTest(base_test.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 asserts.assert_equal(self.dut.droid.wifiGetVerboseLoggingLevel(), 1, 53 "Failed to enable WiFi verbose logging on the softap dut.") 54 self.dut_client.droid.wifiEnableVerboseLogging(1) 55 asserts.assert_equal(self.dut_client.droid.wifiGetVerboseLoggingLevel(), 1, 56 "Failed to enable WiFi verbose logging on the client dut.") 57 58 def teardown_class(self): 59 wutils.reset_wifi(self.dut) 60 wutils.reset_wifi(self.dut_client) 61 62 def on_fail(self, test_name, begin_time): 63 self.dut.take_bug_report(test_name, begin_time) 64 65 """ Helper Functions """ 66 def verify_return_to_wifi_enabled(self): 67 """Verifies that wifi is enabled 68 69 We consider wifi as enabled if two things have happened. First, supplicant 70 is started correctly (seen with the supplicant connection change). Second, 71 supplicant should initially enter the disconnected state. 72 """ 73 curr_state = "waiting for supplicant to come back up" 74 try: 75 self.dut.droid.wifiStartTrackingStateChange() 76 event = self.dut.ed.pop_event("SupplicantConnectionChanged", 10) 77 except queue.Empty: 78 self.log.exception("Failed to restart wifi: current state = %s", 79 curr_state) 80 asserts.fail(curr_state) 81 finally: 82 self.dut.droid.wifiStopTrackingStateChange() 83 84 #TODO(silberst): uncomment and remove loop below when b/30037819 is fixed 85 #curr_state = "waiting for wifi to go back into connect mode" 86 #try: 87 # self.dut.droid.wifiStartTrackingStateChange() 88 # event = self.dut.ed.pop_event("WifiNetworkDisconnected", 10) 89 # self.dut.droid.wifiStopTrackingStateChange() 90 #except queue.Empty: 91 # self.log.exception("Failed to restart wifi: current state = %s", curr_state) 92 # asserts.fail(curr_state) 93 attempt_count = 0 94 max_attempts = 3 95 while attempt_count < max_attempts: 96 if not self.dut.droid.wifiCheckState(): 97 attempt_count += 1 98 time.sleep(5) 99 else: 100 return 101 asserts.fail("failed waiting for wifi to return to connect mode") 102 103 def create_softap_config(self): 104 """Create a softap config with ssid and password.""" 105 ap_ssid = "softap_" + utils.rand_ascii_str(8) 106 ap_password = utils.rand_ascii_str(8) 107 self.dut.log.info("softap setup: %s %s", ap_ssid, ap_password) 108 config = {wutils.WifiEnums.SSID_KEY: ap_ssid} 109 config[wutils.WifiEnums.PWD_KEY] = ap_password 110 return config 111 112 def confirm_softap_in_scan_results(self, ap_ssid): 113 """Confirm the ap started by wifi tethering is seen in scan results. 114 115 Args: 116 ap_ssid: SSID of the ap we are looking for. 117 """ 118 #TODO(silberst): debug and remove the extra scan before submitting this test 119 wutils.start_wifi_connection_scan(self.dut_client) 120 client_scan_results = self.dut_client.droid.wifiGetScanResults() 121 wutils.start_wifi_connection_scan(self.dut_client) 122 client_scan_results = self.dut_client.droid.wifiGetScanResults() 123 for result in client_scan_results: 124 self.dut.log.debug("scan found: %s", result[wutils.WifiEnums.SSID_KEY]) 125 wutils.assert_network_in_list({wutils.WifiEnums.SSID_KEY: ap_ssid}, 126 client_scan_results) 127 128 def check_cell_data_and_enable(self): 129 """Make sure that cell data is enabled if there is a sim present. 130 131 If a sim is active, cell data needs to be enabled to allow provisioning 132 checks through (when applicable). This is done to relax hardware 133 requirements on DUTs - without this check, running this set of tests 134 after other wifi tests may cause failures. 135 """ 136 # We do have a sim. Make sure data is enabled so we can tether. 137 if not self.dut.droid.telephonyIsDataEnabled(): 138 self.dut.log.info("need to enable data") 139 self.dut.droid.telephonyToggleDataConnection(True) 140 asserts.assert_true(self.dut.droid.telephonyIsDataEnabled(), 141 "Failed to enable cell data for softap dut.") 142 143 144 """ Tests Begin """ 145 146 @test_tracker_info(uuid="495f1252-e440-461c-87a7-2c45f369e129") 147 def test_check_wifi_tethering_supported(self): 148 """Test check for wifi tethering support. 149 150 1. Call method to check if wifi hotspot is supported 151 """ 152 # TODO(silberst): wifiIsPortableHotspotSupported() is currently failing. 153 # Remove the extra check and logging when b/30800811 is resolved 154 hotspot_supported = self.dut.droid.wifiIsPortableHotspotSupported() 155 tethering_supported = self.dut.droid.connectivityIsTetheringSupported() 156 self.log.info( 157 "IsPortableHotspotSupported: %s, IsTetheringSupported %s." % ( 158 hotspot_supported, tethering_supported)) 159 asserts.assert_true(hotspot_supported, 160 "DUT should support wifi tethering but is reporting false.") 161 asserts.assert_true(tethering_supported, 162 "DUT should also support wifi tethering when called from ConnectivityManager") 163 164 @test_tracker_info(uuid="09c19c35-c708-48a5-939b-ac2bbb403d54") 165 def test_full_tether_startup(self): 166 """Test full startup of wifi tethering 167 168 1. Report current state. 169 2. Switch to AP mode. 170 3. verify SoftAP active. 171 4. Shutdown wifi tethering. 172 5. verify back to previous mode. 173 """ 174 initial_wifi_state = self.dut.droid.wifiCheckState() 175 initial_cell_state = tel_utils.is_sim_ready(self.log, self.dut) 176 self.dut.log.info("current state: %s", initial_wifi_state) 177 self.dut.log.info("is sim ready? %s", initial_cell_state) 178 if initial_cell_state: 179 self.check_cell_data_and_enable() 180 config = self.create_softap_config() 181 wutils.start_wifi_tethering(self.dut, 182 config[wutils.WifiEnums.SSID_KEY], 183 config[wutils.WifiEnums.PWD_KEY]) 184 self.confirm_softap_in_scan_results(config[wutils.WifiEnums.SSID_KEY]) 185 wutils.stop_wifi_tethering(self.dut) 186 asserts.assert_false(self.dut.droid.wifiIsApEnabled(), 187 "SoftAp is still reported as running") 188 if initial_wifi_state: 189 self.verify_return_to_wifi_enabled() 190 elif self.dut.droid.wifiCheckState(): 191 asserts.fail("Wifi was disabled before softap and now it is enabled") 192 193 """ Tests End """ 194 195 196 if __name__ == "__main__": 197 pass 198