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 acts
19
20from acts import asserts
21from acts import signals
22from acts import utils
23from acts.test_decorators import test_tracker_info
24from acts_contrib.test_utils.tel.tel_wifi_utils import WIFI_CONFIG_APBAND_2G
25from acts_contrib.test_utils.tel.tel_wifi_utils import WIFI_CONFIG_APBAND_5G
26from WifiStaApConcurrencyTest import WifiStaApConcurrencyTest
27import acts_contrib.test_utils.wifi.wifi_test_utils as wutils
28
29WifiEnums = wutils.WifiEnums
30DEFAULT_TIMEOUT = 10
31
32# Channels to configure the AP for various test scenarios.
33WIFI_NETWORK_AP_CHANNEL_2G = 1
34WIFI_NETWORK_AP_CHANNEL_5G = 36
35WIFI_NETWORK_AP_CHANNEL_5G_DFS = 132
36
37
38class WifiStaApConcurrencyStressTest(WifiStaApConcurrencyTest):
39    """Stress tests for STA + AP concurrency scenarios.
40
41    Test Bed Requirement:
42    * At least two Android devices (For AP)
43    * One Wi-Fi network visible to the device (for STA).
44    """
45
46    def __init__(self, controllers):
47        WifiStaApConcurrencyTest.__init__(self, controllers)
48        self.tests = (
49            "test_stress_wifi_connection_2G_softap_2G",
50            "test_stress_wifi_connection_5G_softap_5G",
51            "test_stress_wifi_connection_5G_DFS_softap_5G",
52            "test_stress_wifi_connection_5G_softap_2G",
53            "test_stress_wifi_connection_5G_DFS_softap_2G",
54            "test_stress_wifi_connection_2G_softap_5G",
55            "test_stress_wifi_connection_5G_softap_2G_with_location_scan_on",
56            "test_stress_softap_2G_wifi_connection_2G",
57            "test_stress_softap_5G_wifi_connection_5G",
58            "test_stress_softap_5G_wifi_connection_5G_DFS",
59            "test_stress_softap_5G_wifi_connection_2G",
60            "test_stress_softap_2G_wifi_connection_5G",
61            "test_stress_softap_2G_wifi_connection_5G_DFS",
62            "test_stress_softap_5G_wifi_connection_2G_with_location_scan_on",
63            "test_2g_sta_mode_and_hotspot_5g_on_off_stress_under_airplane_mode"
64        )
65
66    def setup_class(self):
67        super().setup_class()
68        opt_param = ["stress_count"]
69        self.unpack_userparams(opt_param_names=opt_param)
70
71    def setup_test(self):
72        super().setup_test()
73        for ad in self.android_devices:
74            ad.droid.wakeLockAcquireBright()
75            ad.droid.wakeUpNow()
76        wutils.wifi_toggle_state(self.dut, True)
77
78    def teardown_test(self):
79        super().teardown_test()
80        for ad in self.android_devices:
81            ad.droid.wakeLockRelease()
82            ad.droid.goToSleepNow()
83        if self.dut.droid.wifiIsApEnabled():
84            wutils.stop_wifi_tethering(self.dut)
85        for ad in self.android_devices:
86            wutils.reset_wifi(ad)
87        self.log.debug("Toggling Airplane mode OFF")
88        asserts.assert_true(
89            acts.utils.force_airplane_mode(self.dut, False),
90            "Can not turn airplane mode off: %s" % self.dut.serial)
91
92    """Helper Functions"""
93
94    def connect_to_wifi_network_and_verify(self, params):
95        """Connection logic for open and psk wifi networks.
96        Args:
97            params: A tuple of network info and AndroidDevice object.
98        """
99        network, ad = params
100        SSID = network[WifiEnums.SSID_KEY]
101        wutils.reset_wifi(ad)
102        wutils.connect_to_wifi_network(ad, network)
103        if len(self.android_devices) > 2:
104            wutils.reset_wifi(self.android_devices[2])
105            wutils.connect_to_wifi_network(self.android_devices[2], network)
106
107    def verify_wifi_full_on_off(self, network, softap_config):
108        wutils.wifi_toggle_state(self.dut, True)
109        self.connect_to_wifi_network_and_verify((network, self.dut))
110        if len(self.android_devices) > 2:
111            self.log.info(
112                "Testbed has extra android devices, do more validation")
113            self.verify_traffic_between_dut_clients(self.dut,
114                                                    self.android_devices[2])
115        wutils.wifi_toggle_state(self.dut, False)
116
117    def verify_softap_full_on_off(self, network, softap_band):
118        softap_config = self.start_softap_and_verify(softap_band)
119        if len(self.android_devices) > 2:
120            self.log.info(
121                "Testbed has extra android devices, do more validation")
122            self.verify_traffic_between_dut_clients(self.dut_client,
123                                                    self.android_devices[2])
124        wutils.reset_wifi(self.dut_client)
125        if len(self.android_devices) > 2:
126            wutils.reset_wifi(self.android_devices[2])
127        wutils.stop_wifi_tethering(self.dut)
128
129    """Tests"""
130
131    @test_tracker_info(uuid="615997cc-8290-4af3-b3ac-1f5bd5af6ed1")
132    def test_stress_wifi_connection_2G_softap_2G(self):
133        """Tests connection to 2G network the enable/disable SoftAp on 2G N times.
134        """
135        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
136        wutils.wifi_toggle_state(self.dut, True)
137        self.connect_to_wifi_network_and_verify((self.open_2g, self.dut))
138        for count in range(self.stress_count):
139            self.log.info("Iteration %d", count + 1)
140            self.verify_softap_full_on_off(self.open_2g, WIFI_CONFIG_APBAND_2G)
141
142    @test_tracker_info(uuid="03362d54-a624-4fb8-ad97-7abb9e6f655c")
143    def test_stress_wifi_connection_5G_softap_5G(self):
144        """Tests connection to 5G network followed by bringing up SoftAp on 5G.
145        """
146        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
147        wutils.wifi_toggle_state(self.dut, True)
148        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
149        for count in range(self.stress_count):
150            self.log.info("Iteration %d", count + 1)
151            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_5G)
152
153    @test_tracker_info(uuid="fdda4ff2-38d5-4398-9a59-c7cee407a2b3")
154    def test_stress_wifi_connection_5G_DFS_softap_5G(self):
155        """Tests connection to 5G DFS network followed by bringing up SoftAp on 5G.
156        """
157        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
158        wutils.wifi_toggle_state(self.dut, True)
159        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
160        for count in range(self.stress_count):
161            self.log.info("Iteration %d", count + 1)
162            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_5G)
163
164    @test_tracker_info(uuid="b3621721-7714-43eb-8438-b578164b9194")
165    def test_stress_wifi_connection_5G_softap_2G(self):
166        """Tests connection to 5G network followed by bringing up SoftAp on 2G.
167        """
168        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
169        wutils.wifi_toggle_state(self.dut, True)
170        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
171        for count in range(self.stress_count):
172            self.log.info("Iteration %d", count + 1)
173            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_2G)
174
175    @test_tracker_info(uuid="bde1443f-f912-408e-b01a-537548dd023c")
176    def test_stress_wifi_connection_5G_DFS_softap_2G(self):
177        """Tests connection to 5G DFS network followed by bringing up SoftAp on 2G.
178        """
179        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
180        wutils.wifi_toggle_state(self.dut, True)
181        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
182        for count in range(self.stress_count):
183            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_2G)
184
185    @test_tracker_info(uuid="2b6a891a-e0d6-4660-abf6-579099ce6924")
186    def test_stress_wifi_connection_2G_softap_5G(self):
187        """Tests connection to 2G network followed by bringing up SoftAp on 5G.
188        """
189        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
190        wutils.wifi_toggle_state(self.dut, True)
191        self.connect_to_wifi_network_and_verify((self.open_2g, self.dut))
192        for count in range(self.stress_count):
193            self.log.info("Iteration %d", count + 1)
194            self.verify_softap_full_on_off(self.open_2g, WIFI_CONFIG_APBAND_5G)
195
196    @test_tracker_info(uuid="f28abf22-9df0-4500-b342-6682ca305e60")
197    def test_stress_wifi_connection_5G_softap_2G_with_location_scan_on(self):
198        """Tests connection to 5G network followed by bringing up SoftAp on 2G
199        with location scans turned on.
200        """
201        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
202        self.turn_location_on_and_scan_toggle_on()
203        wutils.wifi_toggle_state(self.dut, True)
204        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
205        for count in range(self.stress_count):
206            self.log.info("Iteration %d", count + 1)
207            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_2G)
208
209    @test_tracker_info(uuid="0edb1500-6c60-442e-9268-a2ad9ee2b55c")
210    def test_stress_softap_2G_wifi_connection_2G(self):
211        """Tests enable SoftAp on 2G then connection/disconnection to 2G network for N times.
212        """
213        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
214        softap_config = self.start_softap_and_verify(WIFI_CONFIG_APBAND_2G,
215                                                     check_connectivity=False)
216        for count in range(self.stress_count):
217            self.log.info("Iteration %d", count + 1)
218            self.verify_wifi_full_on_off(self.open_2g, softap_config)
219
220    @test_tracker_info(uuid="162a6679-edd5-4daa-9f25-75d79cf4bb4a")
221    def test_stress_softap_5G_wifi_connection_5G(self):
222        """Tests enable SoftAp on 5G then connection/disconnection to 5G network for N times.
223        """
224        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
225        softap_config = self.start_softap_and_verify(WIFI_CONFIG_APBAND_5G,
226                                                     check_connectivity=False)
227        for count in range(self.stress_count):
228            self.log.info("Iteration %d", count + 1)
229            self.verify_wifi_full_on_off(self.open_5g, softap_config)
230
231    @test_tracker_info(uuid="ee98f2dd-c4f9-4f48-ab59-f577267760d5")
232    def test_stress_softap_5G_wifi_connection_5G_DFS(self):
233        """Tests enable SoftAp on 5G then connection/disconnection to 5G DFS network for N times.
234        """
235        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
236        softap_config = self.start_softap_and_verify(WIFI_CONFIG_APBAND_5G,
237                                                     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.open_5g, softap_config)
241
242    @test_tracker_info(uuid="b50750b5-d5b9-4687-b9e7-9fb15f54b428")
243    def test_stress_softap_5G_wifi_connection_2G(self):
244        """Tests enable SoftAp on 5G then connection/disconnection to 2G network for N times.
245        """
246        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
247        softap_config = self.start_softap_and_verify(WIFI_CONFIG_APBAND_5G,
248                                                     check_connectivity=False)
249        for count in range(self.stress_count):
250            self.log.info("Iteration %d", count + 1)
251            self.verify_wifi_full_on_off(self.open_2g, softap_config)
252
253    @test_tracker_info(uuid="9a2865db-8e4b-4339-9999-000ce9b6970b")
254    def test_stress_softap_2G_wifi_connection_5G(self):
255        """Tests enable SoftAp on 2G then connection/disconnection to 5G network for N times.
256        """
257        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
258        softap_config = self.start_softap_and_verify(WIFI_CONFIG_APBAND_2G,
259                                                     check_connectivity=False)
260        for count in range(self.stress_count):
261            self.log.info("Iteration %d", count + 1)
262            self.verify_wifi_full_on_off(self.open_5g, softap_config)
263
264    @test_tracker_info(uuid="add6609d-91d6-4b89-94c5-0ad8b941e3d1")
265    def test_stress_softap_2G_wifi_connection_5G_DFS(self):
266        """Tests enable SoftAp on 2G then connection/disconnection to 5G DFS network for N times.
267        """
268        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
269        softap_config = self.start_softap_and_verify(WIFI_CONFIG_APBAND_2G,
270                                                     check_connectivity=False)
271        for count in range(self.stress_count):
272            self.log.info("Iteration %d", count + 1)
273            self.verify_wifi_full_on_off(self.open_5g, softap_config)
274
275    @test_tracker_info(uuid="ee42afb6-99d0-4330-933f-d4dd8c3626c6")
276    def test_stress_softap_5G_wifi_connection_2G_with_location_scan_on(self):
277        """Tests enable SoftAp on 5G then connection/disconnection to 2G network for N times
278        with location scans turned on.
279        """
280        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
281        self.turn_location_on_and_scan_toggle_on()
282        softap_config = self.start_softap_and_verify(WIFI_CONFIG_APBAND_5G,
283                                                     check_connectivity=False)
284        for count in range(self.stress_count):
285            self.log.info("Iteration %d", count + 1)
286            self.verify_wifi_full_on_off(self.open_2g, softap_config)
287
288    @test_tracker_info(uuid="36c7f847-4b3e-4bb1-a280-cfe2b6afc903")
289    def test_2g_sta_mode_and_hotspot_5g_on_off_stress_under_airplane_mode(
290            self):
291        """Tests connection to 2G network followed by bringing up SoftAp on 5G
292        under airplane mode
293        """
294        self.log.debug("Toggling Airplane mode ON")
295        asserts.assert_true(
296            acts.utils.force_airplane_mode(self.dut, True),
297            "Can not turn on airplane mode on: %s" % self.dut.serial)
298        time.sleep(DEFAULT_TIMEOUT)
299        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
300        wutils.wifi_toggle_state(self.dut, True)
301        self.connect_to_wifi_network_and_verify((self.open_2g, self.dut))
302        time.sleep(DEFAULT_TIMEOUT)
303        for count in range(self.stress_count):
304            self.log.info("Iteration %d", count + 1)
305            self.verify_softap_full_on_off(self.open_2g, WIFI_CONFIG_APBAND_5G)
306