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