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 sys 20import time 21 22from acts import base_test 23from acts import signals 24from acts import utils 25from acts.test_decorators import test_tracker_info 26from acts.test_utils.wifi import wifi_constants 27from acts.test_utils.wifi import wifi_test_utils as wutils 28 29 30class WifiServiceApiTest(base_test.BaseTestClass): 31 """This class tests the API surface of WifiManager in different wifi states. 32 33 Attributes: 34 The tests in this class only require one DUT. 35 The tests in this class do not require a SIM (but it is ok if one is 36 present). 37 """ 38 39 40 TEST_SSID_PREFIX = "test_config_" 41 CONFIG_ELEMENT = 'config' 42 NETWORK_ID_ELEMENT = 'network_id' 43 44 def setup_class(self): 45 """ Sets up the required dependencies from the config file and 46 configures the device for WifiService API tests. 47 48 Returns: 49 True is successfully configured the requirements for testig. 50 """ 51 self.dut = self.android_devices[0] 52 # Do a simple version of init - mainly just sync the time and enable 53 # verbose logging. We would also like to test with phones in less 54 # constrained states (or add variations where we specifically 55 # constrain). 56 utils.require_sl4a((self.dut, )) 57 utils.sync_device_time(self.dut) 58 59 # Enable verbose logging on the dut 60 self.dut.droid.wifiEnableVerboseLogging(1) 61 if self.dut.droid.wifiGetVerboseLoggingLevel() != 1: 62 raise signals.TestFailure( 63 "Failed to enable WiFi verbose logging on the dut.") 64 65 def teardown_class(self): 66 wutils.reset_wifi(self.dut) 67 68 def on_fail(self, test_name, begin_time): 69 self.dut.take_bug_report(test_name, begin_time) 70 71 def create_and_save_wifi_network_config(self): 72 """ Create a config with random SSID and password. 73 74 Returns: 75 A tuple with the config and networkId for the newly created and saved network. 76 """ 77 config_ssid = self.TEST_SSID_PREFIX + utils.rand_ascii_str(8) 78 config_password = utils.rand_ascii_str(8) 79 self.dut.log.info("creating config: %s %s", config_ssid, config_password) 80 config = {wutils.WifiEnums.SSID_KEY: config_ssid} 81 config[wutils.WifiEnums.PWD_KEY] = config_password 82 83 # Now save the config. 84 network_id = self.dut.droid.wifiAddNetwork(config) 85 self.dut.log.info("saved config: network_id = %s", network_id) 86 return {self.NETWORK_ID_ELEMENT: network_id, self.CONFIG_ELEMENT: config} 87 88 def check_network_config_saved(self, config): 89 """ Get the configured networks and check of the provided config 90 is present. This method only checks if the SSID is the same. 91 TODO: should do a deeper check to make sure this is the 92 correct config. 93 94 Args: 95 config: WifiConfig for a network. 96 97 Returns: 98 True if the WifiConfig is present. 99 """ 100 networks = self.dut.droid.wifiGetConfiguredNetworks() 101 if not networks: 102 return False 103 ssid_key = wutils.WifiEnums.SSID_KEY 104 for network in networks: 105 if config[ssid_key] == network[ssid_key]: 106 return True 107 return False 108 109 def forget_network(self, network_id): 110 """ Simple method to call wifiForgetNetwork and wait for confirmation 111 callback. The method returns False if it was not removed. 112 113 Returns: 114 True if network was successfully deleted. 115 """ 116 self.dut.log.info("deleting config: networkId = %s", network_id) 117 self.dut.droid.wifiForgetNetwork(network_id) 118 try: 119 event = self.dut.ed.pop_event(wifi_constants.WIFI_FORGET_NW_SUCCESS, 10) 120 return True 121 except queue.Empty: 122 self.dut.log.error("Failed to forget network") 123 return False 124 125 126 """ Tests Begin """ 127 @test_tracker_info(uuid="f4df08c2-d3d5-4032-a433-c15f55130d4a") 128 def test_remove_config_wifi_enabled(self): 129 """ Test if config can be deleted when wifi is enabled. 130 131 1. Enable wifi, if needed 132 2. Create and save a random config. 133 3. Confirm the config is present. 134 4. Remove the config. 135 5. Confirm the config is not listed. 136 """ 137 wutils.wifi_toggle_state(self.dut, True) 138 test_network = self.create_and_save_wifi_network_config() 139 if not self.check_network_config_saved(test_network[self.CONFIG_ELEMENT]): 140 raise signals.TestFailure( 141 "Test network not found in list of configured networks.") 142 if not self.forget_network(test_network[self.NETWORK_ID_ELEMENT]): 143 raise signals.TestFailure( 144 "Test network not deleted from configured networks.") 145 if self.check_network_config_saved(test_network[self.CONFIG_ELEMENT]): 146 raise signals.TestFailure( 147 "Deleted network was in configured networks list.") 148 149 @test_tracker_info(uuid="9af96c7d-a316-4d57-ba5f-c992427c237b") 150 def test_remove_config_wifi_disabled(self): 151 """ Test if config can be deleted when wifi is disabled. 152 153 1. Enable wifi, if needed 154 2. Create and save a random config. 155 3. Confirm the config is present. 156 4. Disable wifi. 157 5. Remove the config. 158 6. Confirm the config is not listed. 159 """ 160 wutils.wifi_toggle_state(self.dut, True) 161 test_network = self.create_and_save_wifi_network_config() 162 if not self.check_network_config_saved(test_network[self.CONFIG_ELEMENT]): 163 raise signals.TestFailure( 164 "Test network not found in list of configured networks.") 165 wutils.wifi_toggle_state(self.dut, False) 166 if not self.forget_network(test_network[self.NETWORK_ID_ELEMENT]): 167 raise signals.TestFailure("Failed to delete network.") 168 if self.check_network_config_saved(test_network[self.CONFIG_ELEMENT]): 169 raise signals.TestFailure( 170 "Test network was found in list of configured networks.") 171 172 @test_tracker_info(uuid="79204ae6-323b-4257-a2cb-2225d44199d4") 173 def test_retrieve_config_wifi_enabled(self): 174 """ Test if config can be retrieved when wifi is enabled. 175 176 1. Enable wifi 177 2. Create and save a random config 178 3. Retrieve the config 179 4. Remove the config (clean up from the test) 180 """ 181 wutils.wifi_toggle_state(self.dut, True) 182 test_network = self.create_and_save_wifi_network_config() 183 184 if not self.check_network_config_saved(test_network[self.CONFIG_ELEMENT]): 185 raise signals.TestFailure( 186 "Test network not found in list of configured networks.") 187 if not self.forget_network(test_network[self.NETWORK_ID_ELEMENT]): 188 raise signals.TestFailure("Failed to delete network.") 189 190 @test_tracker_info(uuid="58fb4f81-bc19-43e1-b0af-89dbd17f45b2") 191 def test_retrieve_config_wifi_disabled(self): 192 """ Test if config can be retrieved when wifi is disabled. 193 194 1. Disable wifi 195 2. Create and save a random config 196 3. Retrieve the config 197 4. Remove the config (clean up from the test) 198 """ 199 wutils.wifi_toggle_state(self.dut, False) 200 test_network = self.create_and_save_wifi_network_config() 201 if not self.check_network_config_saved(test_network[self.CONFIG_ELEMENT]): 202 raise signals.TestFailure( 203 "Test network not found in list of configured networks.") 204 if not self.forget_network(test_network[self.NETWORK_ID_ELEMENT]): 205 raise signals.TestFailure("Failed to delete network.") 206 207 """ Tests End """ 208 209 210if __name__ == "__main__": 211 pass 212