1#!/usr/bin/env python3.4
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
17import time
18
19import acts.base_test
20import acts_contrib.test_utils.wifi.wifi_test_utils as wutils
21import acts_contrib.test_utils.tel.tel_test_utils as tel_utils
22
23from acts import asserts
24from acts import signals
25from acts import utils
26from acts.test_decorators import test_tracker_info
27from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest
28from acts_contrib.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_2G
29from acts_contrib.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_5G
30
31WifiEnums = wutils.WifiEnums
32
33DEFAULT_TIMEOUT = 10
34PING_ADDR = 'www.google.com'
35
36
37class WifiCellStressTest(WifiBaseTest):
38    """WiFi Cell Data Stress test class.
39
40    Test Bed Requirement:
41    * Two Android device and one of them with a SIM card
42    * Several Wi-Fi networks visible to the device, including an open Wi-Fi
43      network.
44    """
45
46    def __init__(self, configs):
47        super().__init__(configs)
48        self.enable_packet_log = True
49
50    def setup_class(self):
51        super().setup_class()
52        self.dut = self.android_devices[0]
53        self.dut_client = self.android_devices[1]
54        wutils.wifi_test_device_init(self.dut)
55        req_params = []
56        opt_param = [
57            "open_network", "reference_networks", "iperf_server_address",
58            "stress_count", "stress_hours", "attn_vals", "pno_interval",
59            "iperf_server_port", "dbs_supported_models",
60            "sta_sta_supported_models"
61        ]
62
63        self.unpack_userparams(req_param_names=req_params,
64                               opt_param_names=opt_param)
65        self.ap_iface = 'wlan0'
66        if self.dut.model in self.dbs_supported_models:
67            self.ap_iface = 'wlan1'
68        if self.dut.model in self.sta_sta_supported_models:
69            self.ap_iface = 'wlan2'
70
71        if "AccessPoint" in self.user_params:
72            self.legacy_configure_ap_and_start(ap_count=2)
73        elif "OpenWrtAP" in self.user_params:
74            self.configure_openwrt_ap_and_start(open_network=True,
75                                                wpa_network=True,
76                                                ap_count=2)
77
78    def setup_test(self):
79        super().setup_test()
80        self.dut.droid.wakeLockAcquireBright()
81        self.dut.droid.wakeUpNow()
82        wutils.wifi_toggle_state(self.dut_client, True)
83        init_sim_state = tel_utils.is_sim_ready(self.log, self.dut)
84        if init_sim_state:
85            self.check_cell_data_and_enable()
86
87    def teardown_test(self):
88        super().teardown_test()
89        if self.dut.droid.wifiIsApEnabled():
90            wutils.stop_wifi_tethering(self.dut)
91        self.dut.droid.wakeLockRelease()
92        self.dut.droid.goToSleepNow()
93        wutils.reset_wifi(self.dut)
94        wutils.reset_wifi(self.dut_client)
95        self.log.debug("Toggling Airplane mode OFF")
96        asserts.assert_true(
97            acts.utils.force_airplane_mode(self.dut, False),
98            "Can not turn airplane mode off: %s" % self.dut.serial)
99
100    def teardown_class(self):
101        wutils.reset_wifi(self.dut)
102        if "AccessPoint" in self.user_params:
103            del self.user_params["reference_networks"]
104            del self.user_params["open_network"]
105
106    """Helper Functions"""
107
108    def check_cell_data_and_enable(self):
109        """Make sure that cell data is enabled if there is a sim present.
110
111        If a sim is active, cell data needs to be enabled to allow provisioning
112        checks through (when applicable).  This is done to relax hardware
113        requirements on DUTs - without this check, running this set of tests
114        after other wifi tests may cause failures.
115        """
116        if not self.dut.droid.telephonyIsDataEnabled():
117            self.dut.log.info("need to enable data")
118            self.dut.droid.telephonyToggleDataConnection(True)
119            asserts.assert_true(self.dut.droid.telephonyIsDataEnabled(),
120                                "Failed to enable cell data for dut.")
121
122    def run_ping(self, sec):
123        """Run ping for given number of seconds.
124
125        Args:
126            sec: Time in seconds to run the ping traffic.
127
128        """
129        self.log.info("Running ping for %d seconds" % sec)
130        result = self.dut.adb.shell("ping -w %d %s" % (sec, PING_ADDR),
131                                    timeout=sec + 1)
132        self.log.debug("Ping Result = %s" % result)
133        if "100% packet loss" in result:
134            raise signals.TestFailure("100% packet loss during ping")
135
136    def create_softap_config(self):
137        """Create a softap config with ssid and password."""
138        ap_ssid = "softap_" + utils.rand_ascii_str(8)
139        ap_password = utils.rand_ascii_str(8)
140        self.dut.log.info("softap setup: %s %s", ap_ssid, ap_password)
141        config = {wutils.WifiEnums.SSID_KEY: ap_ssid}
142        config[wutils.WifiEnums.PWD_KEY] = ap_password
143        return config
144
145    def check_softap_under_airplane_mode_with_sim(self, band):
146        """Create a softap on/off under airplane mode with sim """
147        self.log.debug("Toggling Airplane mode ON")
148        asserts.assert_true(
149            acts.utils.force_airplane_mode(self.dut, True),
150            "Can not turn on airplane mode on: %s" % self.dut.serial)
151        time.sleep(DEFAULT_TIMEOUT)
152        for count in range(self.stress_count):
153            """Test toggling softap"""
154            self.log.info("Iteration %d", count + 1)
155            softap_config = wutils.create_softap_config()
156            wutils.start_wifi_tethering(
157                self.dut, softap_config[wutils.WifiEnums.SSID_KEY],
158                softap_config[wutils.WifiEnums.PWD_KEY], band)
159            config = {
160                "SSID": softap_config[wutils.WifiEnums.SSID_KEY],
161                "password": softap_config[wutils.WifiEnums.PWD_KEY]
162            }
163            wutils.stop_wifi_tethering(self.dut)
164        self.log.debug("Toggling Airplane mode OFF")
165        asserts.assert_true(
166            acts.utils.force_airplane_mode(self.dut, False),
167            "Can not turn off airplane mode on: %s" % self.dut.serial)
168        self.check_cell_data_and_enable()
169        softap_config = wutils.create_softap_config()
170        wutils.start_wifi_tethering(self.dut,
171                                    softap_config[wutils.WifiEnums.SSID_KEY],
172                                    softap_config[wutils.WifiEnums.PWD_KEY],
173                                    band)
174        config = {
175            "SSID": softap_config[wutils.WifiEnums.SSID_KEY],
176            "password": softap_config[wutils.WifiEnums.PWD_KEY]
177        }
178        wutils.wifi_toggle_state(self.dut_client, True)
179        wutils.connect_to_wifi_network(self.dut_client,
180                                       config,
181                                       check_connectivity=False)
182        # Ping the DUT
183        dut_addr = self.dut.droid.connectivityGetIPv4Addresses(
184            self.ap_iface)[0]
185        asserts.assert_true(
186            utils.adb_shell_ping(self.dut_client,
187                                 count=10,
188                                 dest_ip=dut_addr,
189                                 timeout=20),
190            "%s ping %s failed" % (self.dut_client.serial, dut_addr))
191        wutils.wifi_toggle_state(self.dut_client, True)
192        wutils.stop_wifi_tethering(self.dut)
193
194    """Tests"""
195
196    @test_tracker_info(uuid="f48609e7-7cb4-4dcf-8a39-2f1ea7301740")
197    def test_2g_hotspot_on_off_under_airplane_mode_with_SIM(self):
198        """Tests followed by turn on/off SoftAp on 2G with Cell Data enable
199        under airplane mode
200
201        """
202        self.check_softap_under_airplane_mode_with_sim(WIFI_CONFIG_APBAND_2G)
203
204    @test_tracker_info(uuid="08744735-6d5f-47e5-96b2-af9ecd40597d")
205    def test_5g_hotspot_on_off_under_airplane_mode_with_SIM(self):
206        """Tests followed by turn on/off SoftAp on 5G with Cell Data enable
207        under airplane mode
208
209        """
210        self.check_softap_under_airplane_mode_with_sim(WIFI_CONFIG_APBAND_5G)
211