1#!/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 17import time 18import random 19import re 20import logging 21import acts_contrib.test_utils.wifi.wifi_test_utils as wutils 22import acts_contrib.test_utils.tel.tel_test_utils as tel_utils 23import acts.utils as utils 24from acts import asserts 25from acts.test_decorators import test_tracker_info 26from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest 27from acts import signals 28from acts.controllers import packet_capture 29from acts.controllers.ap_lib.hostapd_constants import BAND_2G 30from acts.controllers.ap_lib.hostapd_constants import BAND_5G 31 32 33WifiEnums = wutils.WifiEnums 34 35 36class WifiChannelSwitchStressTest(WifiBaseTest): 37 38 def setup_class(self): 39 super().setup_class() 40 self.dut = self.android_devices[0] 41 self.dut_client = self.android_devices[1] 42 utils.require_sl4a((self.dut, self.dut_client)) 43 44 if hasattr(self, 'packet_capture'): 45 self.packet_capture = self.packet_capture[0] 46 47 req_params = ["dbs_supported_models"] 48 opt_param = ["stress_count", "cs_count"] 49 self.unpack_userparams( 50 req_param_names=req_params, opt_param_names=opt_param) 51 52 self.AP_IFACE = 'wlan0' 53 if self.dut.model in self.dbs_supported_models: 54 self.AP_IFACE = 'wlan1' 55 56 for ad in self.android_devices: 57 wutils.wifi_test_device_init(ad) 58 utils.sync_device_time(ad) 59 wutils.set_wifi_country_code(ad, WifiEnums.CountryCode.US) 60 61 def setup_test(self): 62 super().setup_test() 63 for ad in self.android_devices: 64 ad.droid.wakeLockAcquireBright() 65 ad.droid.wakeUpNow() 66 try: 67 if self.dut.droid.wifiIsApEnabled(): 68 wutils.stop_wifi_tethering(self.dut) 69 except signals.TestFailure: 70 pass 71 wutils.wifi_toggle_state(self.dut_client, True) 72 init_sim_state = tel_utils.is_sim_ready(self.log, self.dut) 73 if init_sim_state: 74 self.check_cell_data_and_enable() 75 self.config = wutils.create_softap_config() 76 self.channel_list_2g = self.generate_random_list( 77 WifiEnums.ALL_2G_FREQUENCIES) 78 self.channel_list_5g = self.generate_random_list( 79 WifiEnums.NONE_DFS_5G_FREQUENCIES) 80 81 def teardown_test(self): 82 super().teardown_test() 83 for ad in self.android_devices: 84 ad.droid.wakeLockRelease() 85 ad.droid.goToSleepNow() 86 wutils.reset_wifi(ad) 87 try: 88 wutils.stop_wifi_tethering(self.dut) 89 except signals.TestFailure: 90 pass 91 92 def on_fail(self, test_name, begin_time): 93 try: 94 wutils.stop_pcap(self.packet_capture, self.pcap_procs, False) 95 except signals.TestFailure: 96 pass 97 super().on_fail(test_name, begin_time) 98 99 def check_cell_data_and_enable(self): 100 """Make sure that cell data is enabled if there is a sim present. 101 102 If a sim is active, cell data needs to be enabled to allow provisioning 103 checks through (when applicable). This is done to relax hardware 104 requirements on DUTs - without this check, running this set of tests 105 after other wifi tests may cause failures. 106 """ 107 if not self.dut.droid.telephonyIsDataEnabled(): 108 self.dut.log.info("need to enable data") 109 self.dut.droid.telephonyToggleDataConnection(True) 110 asserts.assert_true(self.dut.droid.telephonyIsDataEnabled(), 111 "Failed to enable cell data for dut.") 112 113 def get_wlan0_link(self, dut): 114 """ get wlan0 interface status""" 115 get_wlan0 = 'wpa_cli -iwlan0 -g@android:wpa_wlan0 IFNAME=wlan0 status' 116 out = dut.adb.shell(get_wlan0) 117 out = dict(re.findall(r'(\S+)=(".*?"|\S+)', out)) 118 asserts.assert_true("ssid" in out, 119 "Client doesn't connect to any network") 120 return out 121 122 def get_wlan1_status(self, dut): 123 """ get wlan1 interface status""" 124 get_wlan1 = 'hostapd_cli status' 125 out_wlan1 = dut.adb.shell(get_wlan1) 126 out_wlan1 = dict(re.findall(r'(\S+)=(".*?"|\S+)', out_wlan1)) 127 return out_wlan1 128 129 def generate_random_list(self, lst): 130 """Generate a list where 131 the previous and subsequent items 132 do not repeat""" 133 channel_list = [] 134 num = random.choice(lst) 135 channel_list.append(num) 136 for i in range(1, self.stress_count): 137 num = random.choice(lst) 138 while num == channel_list[-1]: 139 num = random.choice(lst) 140 channel_list.append(num) 141 return channel_list 142 143 def conf_packet_capture(self, band, channel): 144 """Configure packet capture on necessary channels.""" 145 freq_to_chan = wutils.WifiEnums.freq_to_channel[int(channel)] 146 logging.info("Capturing packets from " 147 "frequency:{}, Channel:{}".format(channel, freq_to_chan)) 148 result = self.packet_capture.configure_monitor_mode(band, freq_to_chan) 149 if not result: 150 logging.error("Failed to configure channel " 151 "for {} band".format(band)) 152 self.pcap_procs = wutils.start_pcap( 153 self.packet_capture, band, self.test_name) 154 155 time.sleep(5) 156 157 @test_tracker_info(uuid="b1a8b00e-eca8-4eba-99e9-c7a3beb2a009") 158 def test_softap_channel_switch_stress_2g(self): 159 """ 160 1. Disable DUT's Wi-Fi 161 2. Enable CLIENT's Wi-Fi 162 3. Check DUT's sim is ready or not 163 4. Enable DUT's mobile data 164 5. Bring up DUT's softap in 2g 165 6. CLIENT connect to DUT 166 7. DUT switch to different 2g channel 167 8. Verify CLIENT follow the change 168 9, Repeat step 7 and 8 169 """ 170 wutils.start_wifi_tethering(self.dut, 171 self.config[wutils.WifiEnums.SSID_KEY], 172 self.config[wutils.WifiEnums.PWD_KEY], 173 WifiEnums.WIFI_CONFIG_APBAND_2G) 174 wutils.connect_to_wifi_network(self.dut_client, self.config) 175 time.sleep(10) 176 for count in range(len(self.channel_list_2g)): 177 self.dut.log.info("2g channel switch iteration : {}".format(count+1)) 178 channel_2g = str(self.channel_list_2g[count]) 179 # Configure sniffer before set SoftAP channel 180 if hasattr(self, 'packet_capture'): 181 self.conf_packet_capture(BAND_2G, channel_2g) 182 # Set SoftAP channel 183 wutils.set_softap_channel(self.dut, 184 self.AP_IFACE, 185 self.cs_count, channel_2g) 186 time.sleep(10) 187 softap_frequency = int(self.get_wlan1_status(self.dut)['freq']) 188 self.dut.log.info('softap frequency : {}'.format(softap_frequency)) 189 client_frequency = int(self.get_wlan0_link(self.dut_client)["freq"]) 190 self.dut_client.log.info( 191 "client frequency : {}".format(client_frequency)) 192 asserts.assert_true( 193 softap_frequency == client_frequency, 194 "hotspot frequency != client frequency") 195 if hasattr(self, 'packet_capture'): 196 wutils.stop_pcap(self.packet_capture, self.pcap_procs, False) 197 198 @test_tracker_info(uuid="3411cb7c-2609-433a-97b6-202a096dc71b") 199 def test_softap_channel_switch_stress_5g(self): 200 """ 201 1. Disable DUT's Wi-Fi 202 2. Enable CLIENT's Wi-Fi 203 3. Check DUT's sim is ready or not 204 4. Enable DUT's mobile data 205 5. Bring up DUT's softap in 5g 206 6. CLIENT connect to DUT 207 7. DUT switch to different 5g channel 208 8. Verify CLIENT follow the change 209 9, Repeat step 7 and 8 210 """ 211 wutils.start_wifi_tethering(self.dut, 212 self.config[wutils.WifiEnums.SSID_KEY], 213 self.config[wutils.WifiEnums.PWD_KEY], 214 WifiEnums.WIFI_CONFIG_APBAND_5G) 215 wutils.connect_to_wifi_network(self.dut_client, self.config) 216 time.sleep(10) 217 for count in range(len(self.channel_list_5g)): 218 self.dut.log.info("5g channel switch iteration : {}".format(count+1)) 219 channel_5g = str(self.channel_list_5g[count]) 220 # Configure sniffer before set SoftAP channel 221 if hasattr(self, 'packet_capture'): 222 self.conf_packet_capture(BAND_5G, channel_5g) 223 # Set SoftAP channel 224 wutils.set_softap_channel(self.dut, 225 self.AP_IFACE, 226 self.cs_count, channel_5g) 227 time.sleep(10) 228 softap_frequency = int(self.get_wlan1_status(self.dut)['freq']) 229 self.dut.log.info('softap frequency : {}'.format(softap_frequency)) 230 client_frequency = int(self.get_wlan0_link(self.dut_client)["freq"]) 231 self.dut_client.log.info( 232 "client frequency : {}".format(client_frequency)) 233 asserts.assert_true( 234 softap_frequency == client_frequency, 235 "hotspot frequency != client frequency") 236 if hasattr(self, 'packet_capture'): 237 wutils.stop_pcap(self.packet_capture, self.pcap_procs, False) 238 239 @test_tracker_info(uuid="0f279058-119f-49fc-b8d6-fb2991cc35aa") 240 def test_softap_channel_switch_stress_2g_5g(self): 241 """ 242 1. Disable DUT's Wi-Fi 243 2. Enable CLIENT's Wi-Fi 244 3. Check DUT's sim is ready or not 245 4. Enable DUT's mobile data 246 5. Bring up DUT's softap in 2g 247 6. CLIENT connect to DUT 248 7. DUT switch to different 2g channel 249 8. Verify CLIENT follow the change 250 9. DUT switch to 5g channel 251 10. Verify CLIENT follow the change 252 11. Repeat step 7 to 10 253 """ 254 for count in range(self.stress_count): 255 self.log.info("2g 5g channel switch iteration : {}".format(count+1)) 256 wutils.start_wifi_tethering(self.dut, 257 self.config[wutils.WifiEnums.SSID_KEY], 258 self.config[wutils.WifiEnums.PWD_KEY], 259 WifiEnums.WIFI_CONFIG_APBAND_2G) 260 wutils.connect_to_wifi_network(self.dut_client, self.config) 261 self.log.info("wait 10 secs for client reconnect to dut") 262 time.sleep(10) 263 channel_2g = self.channel_list_2g[count] 264 if hasattr(self, 'packet_capture'): 265 self.conf_packet_capture(BAND_2G, channel_2g) 266 wutils.set_softap_channel(self.dut, 267 self.AP_IFACE, 268 self.cs_count, channel_2g) 269 time.sleep(10) 270 softap_frequency = int(self.get_wlan1_status(self.dut)['freq']) 271 self.dut.log.info('softap frequency : {}'.format(softap_frequency)) 272 client_frequency = int(self.get_wlan0_link(self.dut_client)["freq"]) 273 self.dut_client.log.info( 274 "client frequency : {}".format(client_frequency)) 275 asserts.assert_true( 276 softap_frequency == client_frequency, 277 "hotspot frequency != client frequency") 278 wutils.stop_wifi_tethering(self.dut) 279 if hasattr(self, 'packet_capture'): 280 wutils.stop_pcap(self.packet_capture, self.pcap_procs, False) 281 self.dut.log.info('switch to SoftAP 5g') 282 283 # switch to SoftAp 5g 284 wutils.start_wifi_tethering(self.dut, 285 self.config[wutils.WifiEnums.SSID_KEY], 286 self.config[wutils.WifiEnums.PWD_KEY], 287 WifiEnums.WIFI_CONFIG_APBAND_5G) 288 wutils.connect_to_wifi_network(self.dut_client, self.config) 289 self.log.info("wait 10 secs for client reconnect to dut") 290 time.sleep(10) 291 channel_5g = self.channel_list_5g[count] 292 if hasattr(self, 'packet_capture'): 293 self.conf_packet_capture(BAND_5G, channel_5g) 294 wutils.set_softap_channel(self.dut, 295 self.AP_IFACE, 296 self.cs_count, channel_5g) 297 time.sleep(10) 298 softap_frequency = int(self.get_wlan1_status(self.dut)['freq']) 299 self.dut.log.info('softap frequency : {}'.format(softap_frequency)) 300 client_frequency = int(self.get_wlan0_link(self.dut_client)["freq"]) 301 self.dut_client.log.info( 302 "client frequency : {}".format(client_frequency)) 303 asserts.assert_true( 304 softap_frequency == client_frequency, 305 "hotspot frequency != client frequency") 306 wutils.stop_wifi_tethering(self.dut) 307 if hasattr(self, 'packet_capture'): 308 wutils.stop_pcap(self.packet_capture, self.pcap_procs, False) 309