1#!/usr/bin/env python3 2# 3# Copyright 2022 - 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""" 17Sanity tests for connectivity tests in telephony 18""" 19 20import time 21import logging 22import os 23 24from acts.test_decorators import test_tracker_info 25from acts.controllers.anritsu_lib._anritsu_utils import AnritsuError 26from acts.controllers.anritsu_lib.md8475a import MD8475A 27from acts.controllers.anritsu_lib.md8475a import BtsBandwidth 28from acts_contrib.test_utils.tel.anritsu_utils import cb_serial_number 29from acts_contrib.test_utils.tel.anritsu_utils import set_system_model_lte 30from acts_contrib.test_utils.tel.anritsu_utils import set_usim_parameters 31from acts_contrib.test_utils.tel.anritsu_utils import set_post_sim_params 32from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_CDMA 33from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_GSM_ONLY 34from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_GSM_UMTS 35from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_LTE_CDMA_EVDO 36from acts_contrib.test_utils.tel.tel_defines import RAT_1XRTT 37from acts_contrib.test_utils.tel.tel_defines import RAT_GSM 38from acts_contrib.test_utils.tel.tel_defines import RAT_LTE 39from acts_contrib.test_utils.tel.tel_defines import RAT_WCDMA 40from acts_contrib.test_utils.tel.tel_defines import RAT_FAMILY_CDMA2000 41from acts_contrib.test_utils.tel.tel_defines import RAT_FAMILY_GSM 42from acts_contrib.test_utils.tel.tel_defines import RAT_FAMILY_LTE 43from acts_contrib.test_utils.tel.tel_defines import RAT_FAMILY_UMTS 44from acts_contrib.test_utils.tel.tel_defines import POWER_LEVEL_OUT_OF_SERVICE 45from acts_contrib.test_utils.tel.tel_defines import POWER_LEVEL_FULL_SERVICE 46from acts_contrib.test_utils.tel.tel_data_utils import check_data_stall_detection 47from acts_contrib.test_utils.tel.tel_data_utils import check_network_validation_fail 48from acts_contrib.test_utils.tel.tel_data_utils import check_data_stall_recovery 49from acts_contrib.test_utils.tel.tel_logging_utils import start_qxdm_loggers 50from acts_contrib.test_utils.tel.tel_phone_setup_utils import ensure_network_rat 51from acts_contrib.test_utils.tel.tel_phone_setup_utils import ensure_phones_idle 52from acts_contrib.test_utils.tel.tel_test_utils import get_host_ip_address 53from acts_contrib.test_utils.tel.tel_test_utils import toggle_airplane_mode 54from acts_contrib.test_utils.tel.tel_test_utils import iperf_test_by_adb 55from acts_contrib.test_utils.tel.tel_test_utils import get_device_epoch_time 56from acts_contrib.test_utils.tel.tel_test_utils import break_internet_except_sl4a_port 57from acts_contrib.test_utils.tel.tel_test_utils import resume_internet_with_sl4a_port 58from acts_contrib.test_utils.tel.tel_test_utils import test_data_browsing_success_using_sl4a 59from acts_contrib.test_utils.tel.tel_test_utils import test_data_browsing_failure_using_sl4a 60from acts_contrib.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest 61from acts.utils import adb_shell_ping 62 63DEFAULT_PING_DURATION = 30 64 65 66class TelLabDataTest(TelephonyBaseTest): 67 SETTLING_TIME = 30 68 SERIAL_NO = cb_serial_number() 69 70 def setup_class(self): 71 super().setup_class() 72 self.ad = self.android_devices[0] 73 self.ip_server = self.iperf_servers[0] 74 self.port_num = self.ip_server.port 75 self.log.info("Iperf Port is %s", self.port_num) 76 self.ad.sim_card = getattr(self.ad, "sim_card", None) 77 self.log.info("SIM Card is %s", self.ad.sim_card) 78 self.md8475a_ip_address = self.user_params[ 79 "anritsu_md8475a_ip_address"] 80 self.wlan_option = self.user_params.get("anritsu_wlan_option", False) 81 self.md8475_version = self.user_params.get("md8475", "A") 82 self.step_size = self.user_params.get("power_step_size", 5) 83 self.start_power_level = self.user_params.get("start_power_level", -40) 84 self.stop_power_level = self.user_params.get("stop_power_level", -100) 85 self.lte_bandwidth = self.user_params.get("lte_bandwidth", 20) 86 self.MAX_ITERATIONS = abs(int((self.stop_power_level - \ 87 self.start_power_level) / self.step_size)) 88 self.log.info("Max iterations is %d", self.MAX_ITERATIONS) 89 90 try: 91 self.anritsu = MD8475A(self.md8475a_ip_address, self.wlan_option, 92 self.md8475_version) 93 except AnritsuError: 94 self.log.error("Error in connecting to Anritsu Simulator") 95 return False 96 return True 97 98 def setup_test(self): 99 if getattr(self, "qxdm_log", True): 100 start_qxdm_loggers(self.log, self.android_devices) 101 ensure_phones_idle(self.log, self.android_devices) 102 toggle_airplane_mode(self.log, self.ad, True) 103 return True 104 105 def teardown_test(self): 106 self.log.info("Stopping Simulation") 107 self.anritsu.stop_simulation() 108 toggle_airplane_mode(self.log, self.ad, True) 109 return True 110 111 def teardown_class(self): 112 self.anritsu.disconnect() 113 return True 114 115 def _setup_data(self, set_simulation_func, rat): 116 try: 117 [self.bts1] = set_simulation_func(self.anritsu, self.user_params, 118 self.ad.sim_card) 119 set_usim_parameters(self.anritsu, self.ad.sim_card) 120 set_post_sim_params(self.anritsu, self.user_params, 121 self.ad.sim_card) 122 if self.lte_bandwidth == 20: 123 self.bts1.bandwidth = BtsBandwidth.LTE_BANDWIDTH_20MHz 124 elif self.lte_bandwidth == 15: 125 self.bts1.bandwidth = BtsBandwidth.LTE_BANDWIDTH_15MHz 126 elif self.lte_bandwidth == 10: 127 self.bts1.bandwidth = BtsBandwidth.LTE_BANDWIDTH_10MHz 128 else: 129 self.bts1.bandwidth = BtsBandwidth.LTE_BANDWIDTH_5MHz 130 131 self.anritsu.start_simulation() 132 133 if rat == RAT_LTE: 134 preferred_network_setting = NETWORK_MODE_LTE_CDMA_EVDO 135 rat_family = RAT_FAMILY_LTE 136 elif rat == RAT_WCDMA: 137 preferred_network_setting = NETWORK_MODE_GSM_UMTS 138 rat_family = RAT_FAMILY_UMTS 139 elif rat == RAT_GSM: 140 preferred_network_setting = NETWORK_MODE_GSM_ONLY 141 rat_family = RAT_FAMILY_GSM 142 elif rat == RAT_1XRTT: 143 preferred_network_setting = NETWORK_MODE_CDMA 144 rat_family = RAT_FAMILY_CDMA2000 145 else: 146 self.log.error("No valid RAT provided for SMS test.") 147 return False 148 149 if not ensure_network_rat( 150 self.log, 151 self.ad, 152 preferred_network_setting, 153 rat_family, 154 toggle_apm_after_setting=True): 155 self.log.error( 156 "Failed to set rat family {}, preferred network:{}".format( 157 rat_family, preferred_network_setting)) 158 return False 159 160 self.anritsu.wait_for_registration_state() 161 time.sleep(self.SETTLING_TIME) 162 163 # Fetch IP address of the host machine 164 destination_ip = get_host_ip_address(self) 165 166 if not adb_shell_ping(self.ad, DEFAULT_PING_DURATION, 167 destination_ip): 168 self.log.error("Pings failed to Destination.") 169 return False 170 self.bts1.output_level = self.start_power_level 171 172 # Power, iperf, file output, power change 173 for iteration in range(1, self.MAX_ITERATIONS + 1): 174 self.log.info("------- Current Iteration: %d / %d -------", 175 iteration, self.MAX_ITERATIONS) 176 current_power = self.bts1.output_level 177 self.log.info("Current Power Level is %s", current_power) 178 179 self.ip_server.start() 180 tput_dict = {"Uplink": 0, "Downlink": 0} 181 if iperf_test_by_adb( 182 self.log, 183 self.ad, 184 destination_ip, 185 self.port_num, 186 True, 187 10, 188 rate_dict=tput_dict): 189 uplink = tput_dict["Uplink"] 190 downlink = tput_dict["Downlink"] 191 else: 192 self.log.error("iperf failed to Destination.") 193 self.log.info("Iteration %d Failed", iteration) 194 if float(current_power) < -55.0: 195 return True 196 else: 197 return False 198 self.ip_server.stop() 199 200 self.log.info("Iteration %d Passed", iteration) 201 self.logpath = os.path.join(logging.log_path, "power_tput.txt") 202 line = "Power " + current_power + " DL TPUT " + str(downlink) 203 with open(self.logpath, "a") as tput_file: 204 tput_file.write(line) 205 tput_file.write("\n") 206 current_power = float(current_power) 207 new_power = current_power - self.step_size 208 self.log.info("Setting Power Level to %f", new_power) 209 self.bts1.output_level = new_power 210 211 except AnritsuError as e: 212 self.log.error("Error in connection with Anritsu Simulator: " + 213 str(e)) 214 return False 215 except Exception as e: 216 self.log.error("Exception during Data procedure: " + str(e)) 217 return False 218 return True 219 220 221 def _data_stall_detection_recovery(self, set_simulation_func, rat): 222 try: 223 [self.bts1] = set_simulation_func(self.anritsu, self.user_params, 224 self.ad.sim_card) 225 set_usim_parameters(self.anritsu, self.ad.sim_card) 226 set_post_sim_params(self.anritsu, self.user_params, 227 self.ad.sim_card) 228 229 self.anritsu.start_simulation() 230 231 if rat == RAT_LTE: 232 preferred_network_setting = NETWORK_MODE_LTE_CDMA_EVDO 233 rat_family = RAT_FAMILY_LTE 234 elif rat == RAT_WCDMA: 235 preferred_network_setting = NETWORK_MODE_GSM_UMTS 236 rat_family = RAT_FAMILY_UMTS 237 elif rat == RAT_GSM: 238 preferred_network_setting = NETWORK_MODE_GSM_ONLY 239 rat_family = RAT_FAMILY_GSM 240 elif rat == RAT_1XRTT: 241 preferred_network_setting = NETWORK_MODE_CDMA 242 rat_family = RAT_FAMILY_CDMA2000 243 else: 244 self.log.error("No valid RAT provided for Data Stall test.") 245 return False 246 247 if not ensure_network_rat( 248 self.log, 249 self.ad, 250 preferred_network_setting, 251 rat_family, 252 toggle_apm_after_setting=True): 253 self.log.error( 254 "Failed to set rat family {}, preferred network:{}".format( 255 rat_family, preferred_network_setting)) 256 return False 257 258 self.anritsu.wait_for_registration_state() 259 time.sleep(self.SETTLING_TIME) 260 261 self.bts1.output_level = self.start_power_level 262 263 cmd = ('ss -l -p -n | grep "tcp.*droid_script" | tr -s " " ' 264 '| cut -d " " -f 5 | sed s/.*://g') 265 sl4a_port = self.ad.adb.shell(cmd) 266 267 if not test_data_browsing_success_using_sl4a(self.log, self.ad): 268 self.ad.log.error("Browsing failed before the test, aborting!") 269 return False 270 271 begin_time = get_device_epoch_time(self.ad) 272 break_internet_except_sl4a_port(self.ad, sl4a_port) 273 274 if not test_data_browsing_failure_using_sl4a(self.log, self.ad): 275 self.ad.log.error("Browsing success even after breaking " \ 276 "the internet, aborting!") 277 return False 278 279 if not check_data_stall_detection(self.ad): 280 self.ad.log.error("NetworkMonitor unable to detect Data Stall") 281 282 if not check_network_validation_fail(self.ad, begin_time): 283 self.ad.log.error("Unable to detect NW validation fail") 284 return False 285 286 if not check_data_stall_recovery(self.ad, begin_time): 287 self.ad.log.error("Recovery was not triggerred") 288 return False 289 290 resume_internet_with_sl4a_port(self.ad, sl4a_port) 291 292 if not test_data_browsing_success_using_sl4a(self.log, self.ad): 293 self.ad.log.error("Browsing failed after resuming internet") 294 return False 295 296 self.ad.log.info("Triggering Out of Service Sceanrio") 297 self.bts1.output_level = POWER_LEVEL_OUT_OF_SERVICE 298 time.sleep(30) 299 begin_time = get_device_epoch_time(self.ad) 300 301 if not test_data_browsing_failure_using_sl4a(self.log, self.ad): 302 self.ad.log.error("Browsing success even in OOS, aborting!") 303 return False 304 305 if not check_network_validation_fail(self.ad, begin_time): 306 self.ad.log.error("Unable to detect NW validation fail") 307 return False 308 309 if check_data_stall_recovery(self.ad, begin_time): 310 self.ad.log.error("FAILURE - Data Recovery was performed") 311 return False 312 self.ad.log.info("SUCCESS - Data Recovery was not performed") 313 314 self.ad.log.info("Bringing up Strong Cellular Signal") 315 self.bts1.output_level = POWER_LEVEL_FULL_SERVICE 316 time.sleep(30) 317 318 if not test_data_browsing_success_using_sl4a(self.log, self.ad): 319 self.ad.log.error("Browsing failed after full service") 320 return False 321 return True 322 323 except AnritsuError as e: 324 self.log.error("Error in connection with Anritsu Simulator: " + 325 str(e)) 326 return False 327 except Exception as e: 328 self.log.error("Exception during Data procedure: " + str(e)) 329 return False 330 finally: 331 resume_internet_with_sl4a_port(self.ad, sl4a_port) 332 333 """ Tests Begin """ 334 335 @test_tracker_info(uuid="df40279a-46dc-40ee-9205-bce2d0fba7e8") 336 @TelephonyBaseTest.tel_test_wrap 337 def test_lte_pings_iperf(self): 338 """ Test Pings functionality on LTE 339 340 Make Sure Phone is in LTE mode 341 Ping to destination server IP 342 iperf server on host machine 343 iperf client in on adb 344 iperf DL 345 346 Returns: 347 True if pass; False if fail 348 """ 349 return self._setup_data(set_system_model_lte, RAT_LTE) 350 351 352 @test_tracker_info(uuid="") 353 def test_data_stall_recovery_in_out_of_service(self): 354 """ Data Stall Recovery Testing 355 356 1. Ensure device is camped, browsing working fine 357 2. Break Internet access, browsing should fail 358 3. Check for Data Stall Detection 359 4. Check for Data Stall Recovery 360 5. Trigger OOS scenario 361 6. Check for Data Stall 362 7. Recovery should not be triggered 363 364 """ 365 return self._data_stall_detection_recovery(set_system_model_lte, RAT_LTE) 366 367 368 """ Tests End """ 369