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        super().setup_class()
63        opt_param = ["stress_count"]
64        self.unpack_userparams(opt_param_names=opt_param)
65
66    """Helper Functions"""
67    def connect_to_wifi_network_and_verify(self, params):
68        """Connection logic for open and psk wifi networks.
69        Args:
70            params: A tuple of network info and AndroidDevice object.
71        """
72        network, ad = params
73        SSID = network[WifiEnums.SSID_KEY]
74        wutils.reset_wifi(ad)
75        wutils.connect_to_wifi_network(ad, network)
76        if len(self.android_devices) > 2:
77            wutils.reset_wifi(self.android_devices[2])
78            wutils.connect_to_wifi_network(self.android_devices[2], network)
79
80    def verify_wifi_full_on_off(self, network, softap_config):
81        wutils.wifi_toggle_state(self.dut, True)
82        self.connect_to_wifi_network_and_verify((network, self.dut))
83        if len(self.android_devices) > 2:
84            self.log.info("Testbed has extra android devices, do more validation")
85            self.verify_traffic_between_dut_clients(
86                    self.dut, self.android_devices[2])
87        wutils.wifi_toggle_state(self.dut, False)
88
89    def verify_softap_full_on_off(self, network, softap_band):
90        softap_config = self.start_softap_and_verify(softap_band)
91        if len(self.android_devices) > 2:
92            self.log.info("Testbed has extra android devices, do more validation")
93            self.verify_traffic_between_dut_clients(
94                    self.dut_client, self.android_devices[2])
95        wutils.reset_wifi(self.dut_client)
96        if len(self.android_devices) > 2:
97            wutils.reset_wifi(self.android_devices[2])
98        wutils.stop_wifi_tethering(self.dut)
99
100    """Tests"""
101    @test_tracker_info(uuid="615997cc-8290-4af3-b3ac-1f5bd5af6ed1")
102    def test_stress_wifi_connection_2G_softap_2G(self):
103        """Tests connection to 2G network the enable/disable SoftAp on 2G N times.
104        """
105        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
106        wutils.wifi_toggle_state(self.dut, True)
107        self.connect_to_wifi_network_and_verify((self.open_2g, self.dut))
108        for count in range(self.stress_count):
109            self.log.info("Iteration %d", count+1)
110            self.verify_softap_full_on_off(self.open_2g, WIFI_CONFIG_APBAND_2G)
111
112    @test_tracker_info(uuid="03362d54-a624-4fb8-ad97-7abb9e6f655c")
113    def test_stress_wifi_connection_5G_softap_5G(self):
114        """Tests connection to 5G network followed by bringing up SoftAp on 5G.
115        """
116        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
117        wutils.wifi_toggle_state(self.dut, True)
118        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
119        for count in range(self.stress_count):
120            self.log.info("Iteration %d", count+1)
121            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_5G)
122
123    @test_tracker_info(uuid="fdda4ff2-38d5-4398-9a59-c7cee407a2b3")
124    def test_stress_wifi_connection_5G_DFS_softap_5G(self):
125        """Tests connection to 5G DFS network followed by bringing up SoftAp on 5G.
126        """
127        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
128        wutils.wifi_toggle_state(self.dut, True)
129        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
130        for count in range(self.stress_count):
131            self.log.info("Iteration %d", count+1)
132            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_5G)
133
134    @test_tracker_info(uuid="b3621721-7714-43eb-8438-b578164b9194")
135    def test_stress_wifi_connection_5G_softap_2G(self):
136        """Tests connection to 5G network followed by bringing up SoftAp on 2G.
137        """
138        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
139        wutils.wifi_toggle_state(self.dut, True)
140        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
141        for count in range(self.stress_count):
142            self.log.info("Iteration %d", count+1)
143            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_2G)
144
145    @test_tracker_info(uuid="bde1443f-f912-408e-b01a-537548dd023c")
146    def test_stress_wifi_connection_5G_DFS_softap_2G(self):
147        """Tests connection to 5G DFS network followed by bringing up SoftAp on 2G.
148        """
149        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
150        wutils.wifi_toggle_state(self.dut, True)
151        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
152        for count in range(self.stress_count):
153            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_2G)
154
155    @test_tracker_info(uuid="2b6a891a-e0d6-4660-abf6-579099ce6924")
156    def test_stress_wifi_connection_2G_softap_5G(self):
157        """Tests connection to 2G network followed by bringing up SoftAp on 5G.
158        """
159        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
160        wutils.wifi_toggle_state(self.dut, True)
161        self.connect_to_wifi_network_and_verify((self.open_2g, self.dut))
162        for count in range(self.stress_count):
163            self.log.info("Iteration %d", count+1)
164            self.verify_softap_full_on_off(self.open_2g, WIFI_CONFIG_APBAND_5G)
165
166    @test_tracker_info(uuid="f28abf22-9df0-4500-b342-6682ca305e60")
167    def test_stress_wifi_connection_5G_softap_2G_with_location_scan_on(self):
168        """Tests connection to 5G network followed by bringing up SoftAp on 2G
169        with location scans turned on.
170        """
171        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
172        self.turn_location_on_and_scan_toggle_on()
173        wutils.wifi_toggle_state(self.dut, True)
174        self.connect_to_wifi_network_and_verify((self.open_5g, self.dut))
175        for count in range(self.stress_count):
176            self.log.info("Iteration %d", count+1)
177            self.verify_softap_full_on_off(self.open_5g, WIFI_CONFIG_APBAND_2G)
178
179    @test_tracker_info(uuid="0edb1500-6c60-442e-9268-a2ad9ee2b55c")
180    def test_stress_softap_2G_wifi_connection_2G(self):
181        """Tests enable SoftAp on 2G then connection/disconnection to 2G network for N times.
182        """
183        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
184        softap_config = self.start_softap_and_verify(
185                WIFI_CONFIG_APBAND_2G, check_connectivity=False)
186        for count in range(self.stress_count):
187            self.log.info("Iteration %d", count+1)
188            self.verify_wifi_full_on_off(self.open_2g, softap_config)
189
190    @test_tracker_info(uuid="162a6679-edd5-4daa-9f25-75d79cf4bb4a")
191    def test_stress_softap_5G_wifi_connection_5G(self):
192        """Tests enable SoftAp on 5G then connection/disconnection to 5G network for N times.
193        """
194        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
195        softap_config = self.start_softap_and_verify(
196                WIFI_CONFIG_APBAND_5G, check_connectivity=False)
197        for count in range(self.stress_count):
198            self.log.info("Iteration %d", count+1)
199            self.verify_wifi_full_on_off(self.open_5g, softap_config)
200
201    @test_tracker_info(uuid="ee98f2dd-c4f9-4f48-ab59-f577267760d5")
202    def test_stress_softap_5G_wifi_connection_5G_DFS(self):
203        """Tests enable SoftAp on 5G then connection/disconnection to 5G DFS network for N times.
204        """
205        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
206        softap_config = self.start_softap_and_verify(
207                WIFI_CONFIG_APBAND_5G, check_connectivity=False)
208        for count in range(self.stress_count):
209            self.log.info("Iteration %d", count+1)
210            self.verify_wifi_full_on_off(self.open_5g, softap_config)
211
212    @test_tracker_info(uuid="b50750b5-d5b9-4687-b9e7-9fb15f54b428")
213    def test_stress_softap_5G_wifi_connection_2G(self):
214        """Tests enable SoftAp on 5G then connection/disconnection to 2G network for N times.
215        """
216        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
217        softap_config = self.start_softap_and_verify(
218                WIFI_CONFIG_APBAND_5G, check_connectivity=False)
219        for count in range(self.stress_count):
220            self.log.info("Iteration %d", count+1)
221            self.verify_wifi_full_on_off(self.open_2g, softap_config)
222
223    @test_tracker_info(uuid="9a2865db-8e4b-4339-9999-000ce9b6970b")
224    def test_stress_softap_2G_wifi_connection_5G(self):
225        """Tests enable SoftAp on 2G then connection/disconnection to 5G network for N times.
226        """
227        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G)
228        softap_config = self.start_softap_and_verify(
229                WIFI_CONFIG_APBAND_2G, check_connectivity=False)
230        for count in range(self.stress_count):
231            self.log.info("Iteration %d", count+1)
232            self.verify_wifi_full_on_off(self.open_5g, softap_config)
233
234    @test_tracker_info(uuid="add6609d-91d6-4b89-94c5-0ad8b941e3d1")
235    def test_stress_softap_2G_wifi_connection_5G_DFS(self):
236        """Tests enable SoftAp on 2G then connection/disconnection to 5G DFS network for N times.
237        """
238        self.configure_ap(channel_5g=WIFI_NETWORK_AP_CHANNEL_5G_DFS)
239        softap_config = self.start_softap_and_verify(
240                WIFI_CONFIG_APBAND_2G, check_connectivity=False)
241        for count in range(self.stress_count):
242            self.log.info("Iteration %d", count+1)
243            self.verify_wifi_full_on_off(self.open_5g, softap_config)
244
245    @test_tracker_info(uuid="ee42afb6-99d0-4330-933f-d4dd8c3626c6")
246    def test_stress_softap_5G_wifi_connection_2G_with_location_scan_on(self):
247        """Tests enable SoftAp on 5G then connection/disconnection to 2G network for N times
248        with location scans turned on.
249        """
250        self.configure_ap(channel_2g=WIFI_NETWORK_AP_CHANNEL_2G)
251        self.turn_location_on_and_scan_toggle_on()
252        softap_config = self.start_softap_and_verify(
253                WIFI_CONFIG_APBAND_5G, check_connectivity=False)
254        for count in range(self.stress_count):
255            self.log.info("Iteration %d", count+1)
256            self.verify_wifi_full_on_off(self.open_2g, softap_config)
257