1#!/usr/bin/env python3
2#
3#   Copyright 2018 - 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 pprint
19
20from acts import asserts
21from acts import signals
22from acts import utils
23from acts.test_decorators import test_tracker_info
24from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_2G
25from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_5G
26from WifiStaApConcurrencyTest import WifiStaApConcurrencyTest
27import acts.test_utils.wifi.wifi_test_utils as wutils
28
29WifiEnums = wutils.WifiEnums
30
31# Channels to configure the AP for various test scenarios.
32WIFI_NETWORK_AP_CHANNEL_2G = 1
33WIFI_NETWORK_AP_CHANNEL_5G = 36
34WIFI_NETWORK_AP_CHANNEL_5G_DFS = 132
35
36class WifiStaApConcurrencyStressTest(WifiStaApConcurrencyTest):
37    """Stress tests for STA + AP concurrency scenarios.
38
39    Test Bed Requirement:
40    * At least two Android devices (For AP)
41    * One Wi-Fi network visible to the device (for STA).
42    """
43
44    def __init__(self, controllers):
45        WifiStaApConcurrencyTest.__init__(self, controllers)
46        self.tests = ("test_stress_wifi_connection_2G_softap_2G",
47                      "test_stress_wifi_connection_5G_softap_5G",
48                      "test_stress_wifi_connection_5G_DFS_softap_5G",
49                      "test_stress_wifi_connection_5G_softap_2G",
50                      "test_stress_wifi_connection_5G_DFS_softap_2G",
51                      "test_stress_wifi_connection_2G_softap_5G",
52                      "test_stress_wifi_connection_5G_softap_2G_with_location_scan_on",
53                      "test_stress_softap_2G_wifi_connection_2G",
54                      "test_stress_softap_5G_wifi_connection_5G",
55                      "test_stress_softap_5G_wifi_connection_5G_DFS",
56                      "test_stress_softap_5G_wifi_connection_2G",
57                      "test_stress_softap_2G_wifi_connection_5G",
58                      "test_stress_softap_2G_wifi_connection_5G_DFS",
59                      "test_stress_softap_5G_wifi_connection_2G_with_location_scan_on")
60
61    def setup_class(self):
62        self.dut = self.android_devices[0]
63        self.dut_client = self.android_devices[1]
64        wutils.wifi_test_device_init(self.dut)
65        wutils.wifi_test_device_init(self.dut_client)
66        # Do a simple version of init - mainly just sync the time and enable
67        # verbose logging.  This test will fail if the DUT has a sim and cell
68        # data is disabled.  We would also like to test with phones in less
69        # constrained states (or add variations where we specifically
70        # constrain).
71        utils.require_sl4a((self.dut, self.dut_client))
72        utils.sync_device_time(self.dut)
73        utils.sync_device_time(self.dut_client)
74        # Set country code explicitly to "US".
75        wutils.set_wifi_country_code(self.dut, wutils.WifiEnums.CountryCode.US)
76        wutils.set_wifi_country_code(self.dut_client, wutils.WifiEnums.CountryCode.US)
77        # Enable verbose logging on the duts
78        self.dut.droid.wifiEnableVerboseLogging(1)
79        asserts.assert_equal(self.dut.droid.wifiGetVerboseLoggingLevel(), 1,
80            "Failed to enable WiFi verbose logging on the softap dut.")
81        self.dut_client.droid.wifiEnableVerboseLogging(1)
82        asserts.assert_equal(self.dut_client.droid.wifiGetVerboseLoggingLevel(), 1,
83            "Failed to enable WiFi verbose logging on the client dut.")
84
85        req_params = ["AccessPoint", "dbs_supported_models", "stress_count"]
86        opt_param = ["iperf_server_address"]
87        self.unpack_userparams(
88            req_param_names=req_params, opt_param_names=opt_param)
89
90        if self.dut.model not in self.dbs_supported_models:
91            asserts.skip(
92                ("Device %s does not support dual interfaces.")
93                % self.dut.model)
94
95        if "iperf_server_address" in self.user_params:
96            self.iperf_server = self.iperf_servers[0]
97        if hasattr(self, 'iperf_server'):
98            self.iperf_server.start()
99
100        # Set the client wifi state to on before the test begins.
101        wutils.wifi_toggle_state(self.dut_client, True)
102
103        # Init extra devices
104        if len(self.android_devices) > 2:
105            wutils.wifi_test_device_init(self.android_devices[2])
106            utils.sync_device_time(self.android_devices[2])
107            wutils.set_wifi_country_code(self.android_devices[2], wutils.WifiEnums.CountryCode.US)
108            self.android_devices[2].droid.wifiEnableVerboseLogging(1)
109            asserts.assert_equal(self.android_devices[2].droid.wifiGetVerboseLoggingLevel(), 1,
110                "Failed to enable WiFi verbose logging on the client dut.")
111
112    """Helper Functions"""
113
114    def verify_wifi_full_on_off(self, network, softap_config):
115        wutils.wifi_toggle_state(self.dut, True)
116        self.connect_to_wifi_network_and_verify((network, self.dut))
117        self.run_iperf_client((network, self.dut))
118        self.run_iperf_client((softap_config, self.dut_client))
119        if len(self.android_devices) > 2:
120            self.log.info("Testbed has extra android devices, do more validation")
121            self.verify_traffic_between_ap_clients(
122                    self.dut, self.android_devices[2])
123        wutils.wifi_toggle_state(self.dut, False)
124
125    def verify_softap_full_on_off(self, network, softap_band):
126        softap_config = self.start_softap_and_verify(softap_band)
127        self.run_iperf_client((network, self.dut))
128        self.run_iperf_client((softap_config, self.dut_client))
129        if len(self.android_devices) > 2:
130            self.log.info("Testbed has extra android devices, do more validation")
131            self.verify_traffic_between_softap_clients(
132                    self.dut_client, self.android_devices[2])
133        wutils.reset_wifi(self.dut_client)
134        if len(self.android_devices) > 2:
135            wutils.reset_wifi(self.android_devices[2])
136        wutils.stop_wifi_tethering(self.dut)
137
138    """Tests"""
139    @test_tracker_info(uuid="615997cc-8290-4af3-b3ac-1f5bd5af6ed1")
140    def test_stress_wifi_connection_2G_softap_2G(self):
141        """Tests connection to 2G network the enable/disable SoftAp on 2G N times.
142        """
143        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
144        wutils.wifi_toggle_state(self.dut, True)
145        self.connect_to_wifi_network_and_verify((self.wpapsk_2g, self.dut))
146        for count in range(self.stress_count):
147            self.log.info("Iteration %d", count+1)
148            self.verify_softap_full_on_off(self.wpapsk_2g, WIFI_CONFIG_APBAND_2G)
149        raise signals.TestPass(details="", extras={"Iterations":"%d" %
150            self.stress_count, "Pass":"%d" %(count+1)})
151
152    @test_tracker_info(uuid="03362d54-a624-4fb8-ad97-7abb9e6f655c")
153    def test_stress_wifi_connection_5G_softap_5G(self):
154        """Tests connection to 5G network followed by bringing up SoftAp on 5G.
155        """
156        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
157        wutils.wifi_toggle_state(self.dut, True)
158        self.connect_to_wifi_network_and_verify((self.wpapsk_5g, self.dut))
159        for count in range(self.stress_count):
160            self.log.info("Iteration %d", count+1)
161            self.verify_softap_full_on_off(self.wpapsk_5g, WIFI_CONFIG_APBAND_5G)
162        raise signals.TestPass(details="", extras={"Iterations":"%d" %
163            self.stress_count, "Pass":"%d" %(count+1)})
164
165    @test_tracker_info(uuid="fdda4ff2-38d5-4398-9a59-c7cee407a2b3")
166    def test_stress_wifi_connection_5G_DFS_softap_5G(self):
167        """Tests connection to 5G DFS network followed by bringing up SoftAp on 5G.
168        """
169        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
170        wutils.wifi_toggle_state(self.dut, True)
171        self.connect_to_wifi_network_and_verify((self.wpapsk_5g, self.dut))
172        for count in range(self.stress_count):
173            self.log.info("Iteration %d", count+1)
174            self.verify_softap_full_on_off(self.wpapsk_5g, WIFI_CONFIG_APBAND_5G)
175        raise signals.TestPass(details="", extras={"Iterations":"%d" %
176            self.stress_count, "Pass":"%d" %(count+1)})
177
178    @test_tracker_info(uuid="b3621721-7714-43eb-8438-b578164b9194")
179    def test_stress_wifi_connection_5G_softap_2G(self):
180        """Tests connection to 5G network followed by bringing up SoftAp on 2G.
181        """
182        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
183        wutils.wifi_toggle_state(self.dut, True)
184        self.connect_to_wifi_network_and_verify((self.wpapsk_5g, self.dut))
185        for count in range(self.stress_count):
186            self.log.info("Iteration %d", count+1)
187            self.verify_softap_full_on_off(self.wpapsk_5g, WIFI_CONFIG_APBAND_2G)
188        raise signals.TestPass(details="", extras={"Iterations":"%d" %
189            self.stress_count, "Pass":"%d" %(count+1)})
190
191    @test_tracker_info(uuid="bde1443f-f912-408e-b01a-537548dd023c")
192    def test_stress_wifi_connection_5G_DFS_softap_2G(self):
193        """Tests connection to 5G DFS network followed by bringing up SoftAp on 2G.
194        """
195        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
196        wutils.wifi_toggle_state(self.dut, True)
197        self.connect_to_wifi_network_and_verify((self.wpapsk_5g, self.dut))
198        for count in range(self.stress_count):
199            self.verify_softap_full_on_off(self.wpapsk_5g, WIFI_CONFIG_APBAND_2G)
200        raise signals.TestPass(details="", extras={"Iterations":"%d" %
201            self.stress_count, "Pass":"%d" %(count+1)})
202
203    @test_tracker_info(uuid="2b6a891a-e0d6-4660-abf6-579099ce6924")
204    def test_stress_wifi_connection_2G_softap_5G(self):
205        """Tests connection to 2G network followed by bringing up SoftAp on 5G.
206        """
207        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
208        wutils.wifi_toggle_state(self.dut, True)
209        self.connect_to_wifi_network_and_verify((self.wpapsk_2g, self.dut))
210        for count in range(self.stress_count):
211            self.log.info("Iteration %d", count+1)
212            self.verify_softap_full_on_off(self.wpapsk_2g, WIFI_CONFIG_APBAND_5G)
213        raise signals.TestPass(details="", extras={"Iterations":"%d" %
214            self.stress_count, "Pass":"%d" %(count+1)})
215
216    @test_tracker_info(uuid="f28abf22-9df0-4500-b342-6682ca305e60")
217    def test_stress_wifi_connection_5G_softap_2G_with_location_scan_on(self):
218        """Tests connection to 5G network followed by bringing up SoftAp on 2G
219        with location scans turned on.
220        """
221        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
222        self.turn_location_on_and_scan_toggle_on()
223        wutils.wifi_toggle_state(self.dut, True)
224        self.connect_to_wifi_network_and_verify((self.wpapsk_5g, self.dut))
225        for count in range(self.stress_count):
226            self.log.info("Iteration %d", count+1)
227            self.verify_softap_full_on_off(self.wpapsk_5g, WIFI_CONFIG_APBAND_2G)
228        raise signals.TestPass(details="", extras={"Iterations":"%d" %
229            self.stress_count, "Pass":"%d" %(count+1)})
230
231    @test_tracker_info(uuid="0edb1500-6c60-442e-9268-a2ad9ee2b55c")
232    def test_stress_softap_2G_wifi_connection_2G(self):
233        """Tests enable SoftAp on 2G then connection/disconnection to 2G network for N times.
234        """
235        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
236        softap_config = self.start_softap_and_verify(
237                WIFI_CONFIG_APBAND_2G, check_connectivity=False)
238        for count in range(self.stress_count):
239            self.log.info("Iteration %d", count+1)
240            self.verify_wifi_full_on_off(self.wpapsk_2g, softap_config)
241        raise signals.TestPass(details="", extras={"Iterations":"%d" %
242            self.stress_count, "Pass":"%d" %(count+1)})
243
244    @test_tracker_info(uuid="162a6679-edd5-4daa-9f25-75d79cf4bb4a")
245    def test_stress_softap_5G_wifi_connection_5G(self):
246        """Tests enable SoftAp on 5G then connection/disconnection to 5G network for N times.
247        """
248        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
249        softap_config = self.start_softap_and_verify(
250                WIFI_CONFIG_APBAND_5G, check_connectivity=False)
251        for count in range(self.stress_count):
252            self.log.info("Iteration %d", count+1)
253            self.verify_wifi_full_on_off(self.wpapsk_5g, softap_config)
254        raise signals.TestPass(details="", extras={"Iterations":"%d" %
255            self.stress_count, "Pass":"%d" %(count+1)})
256
257    @test_tracker_info(uuid="ee98f2dd-c4f9-4f48-ab59-f577267760d5")
258    def test_stress_softap_5G_wifi_connection_5G_DFS(self):
259        """Tests enable SoftAp on 5G then connection/disconnection to 5G DFS network for N times.
260        """
261        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
262        softap_config = self.start_softap_and_verify(
263                WIFI_CONFIG_APBAND_5G, check_connectivity=False)
264        for count in range(self.stress_count):
265            self.log.info("Iteration %d", count+1)
266            self.verify_wifi_full_on_off(self.wpapsk_5g, softap_config)
267        raise signals.TestPass(details="", extras={"Iterations":"%d" %
268            self.stress_count, "Pass":"%d" %(count+1)})
269
270    @test_tracker_info(uuid="b50750b5-d5b9-4687-b9e7-9fb15f54b428")
271    def test_stress_softap_5G_wifi_connection_2G(self):
272        """Tests enable SoftAp on 5G then connection/disconnection to 2G network for N times.
273        """
274        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
275        softap_config = self.start_softap_and_verify(
276                WIFI_CONFIG_APBAND_5G, check_connectivity=False)
277        for count in range(self.stress_count):
278            self.log.info("Iteration %d", count+1)
279            self.verify_wifi_full_on_off(self.wpapsk_2g, softap_config)
280        raise signals.TestPass(details="", extras={"Iterations":"%d" %
281            self.stress_count, "Pass":"%d" %(count+1)})
282
283    @test_tracker_info(uuid="9a2865db-8e4b-4339-9999-000ce9b6970b")
284    def test_stress_softap_2G_wifi_connection_5G(self):
285        """Tests enable SoftAp on 2G then connection/disconnection to 5G network for N times.
286        """
287        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
288        softap_config = self.start_softap_and_verify(
289                WIFI_CONFIG_APBAND_2G, check_connectivity=False)
290        for count in range(self.stress_count):
291            self.log.info("Iteration %d", count+1)
292            self.verify_wifi_full_on_off(self.wpapsk_5g, softap_config)
293        raise signals.TestPass(details="", extras={"Iterations":"%d" %
294            self.stress_count, "Pass":"%d" %(count+1)})
295
296    @test_tracker_info(uuid="add6609d-91d6-4b89-94c5-0ad8b941e3d1")
297    def test_stress_softap_2G_wifi_connection_5G_DFS(self):
298        """Tests enable SoftAp on 2G then connection/disconnection to 5G DFS network for N times.
299        """
300        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
301        softap_config = self.start_softap_and_verify(
302                WIFI_CONFIG_APBAND_2G, check_connectivity=False)
303        for count in range(self.stress_count):
304            self.log.info("Iteration %d", count+1)
305            self.verify_wifi_full_on_off(self.wpapsk_5g, softap_config)
306        raise signals.TestPass(details="", extras={"Iterations":"%d" %
307            self.stress_count, "Pass":"%d" %(count+1)})
308
309    @test_tracker_info(uuid="ee42afb6-99d0-4330-933f-d4dd8c3626c6")
310    def test_stress_softap_5G_wifi_connection_2G_with_location_scan_on(self):
311        """Tests enable SoftAp on 5G then connection/disconnection to 2G network for N times
312        with location scans turned on.
313        """
314        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
315        self.turn_location_on_and_scan_toggle_on()
316        softap_config = self.start_softap_and_verify(
317                WIFI_CONFIG_APBAND_5G, check_connectivity=False)
318        for count in range(self.stress_count):
319            self.log.info("Iteration %d", count+1)
320            self.verify_wifi_full_on_off(self.wpapsk_2g, softap_config)
321        raise signals.TestPass(details="", extras={"Iterations":"%d" %
322            self.stress_count, "Pass":"%d" %(count+1)})
323