1#!/usr/bin/env python3
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""
17Script for testing WiFi recovery after rebooting the AP.
18
19Override default number of iterations using the following
20parameter in the test config file.
21
22"reboot_ap_stress_test_iterations": "10"
23"""
24
25import os
26import uuid
27import time
28
29from acts import asserts
30from acts import signals
31from acts.base_test import BaseTestClass
32from acts.controllers.ap_lib import hostapd_constants
33from acts.test_utils.abstract_devices.utils_lib.wlan_utils import disconnect
34from acts.test_utils.abstract_devices.utils_lib.wlan_utils import is_connected
35from acts.test_utils.abstract_devices.utils_lib.wlan_utils import setup_ap
36from acts.test_utils.abstract_devices.utils_lib.wlan_utils import setup_ap_and_associate
37from acts.test_utils.abstract_devices.wlan_device import create_wlan_device
38from acts.test_utils.fuchsia import utils
39from acts.test_utils.tel.tel_test_utils import setup_droid_properties
40from acts.utils import rand_ascii_str
41
42
43class RebootAPStressTest(BaseTestClass):
44    # Default number of test iterations here.
45    # Override using parameter in config file.
46    # Eg: "reboot_ap_stress_test_iterations": "10"
47    num_of_iterations = 3
48
49    # Default wait time in seconds for the device
50    # to connect back after AP reboot.
51    # Override using parameter in config file.
52    # Eg: "wait_to_connect_after_ap_reboot_s": "60"
53    wait_to_connect_after_ap_reboot_s = 30
54
55    # Time to wait for device to disconnect
56    # after AP reboot.
57    wait_after_ap_reboot_s = 1
58
59    def setup_class(self):
60        super().setup_class()
61        self.ssid = rand_ascii_str(10)
62        self.wlan_device = create_wlan_device(self.fuchsia_devices[0])
63        self.ap = self.access_points[0]
64        self.num_of_iterations = int(
65            self.user_params.get("reboot_ap_stress_test_iterations",
66                                 self.num_of_iterations))
67        self.wait_to_connect_after_ap_reboot_s = int(
68            self.user_params.get("wait_to_connect_after_ap_reboot_s",
69                                 self.wait_to_connect_after_ap_reboot_s))
70    def teardown_test(self):
71        disconnect(self.wlan_device)
72        self.wlan_device.reset_wifi()
73        self.ap.stop_all_aps()
74
75    def setup_ap(self):
76        setup_ap(access_point=self.ap,
77            profile_name='whirlwind',
78            channel=hostapd_constants.AP_DEFAULT_CHANNEL_2G,
79            ssid=self.ssid)
80
81    def test_reboot_AP_stress(self):
82        setup_ap_and_associate(
83            access_point=self.ap,
84            client=self.wlan_device,
85            profile_name='whirlwind',
86            channel=hostapd_constants.AP_DEFAULT_CHANNEL_2G,
87            ssid=self.ssid)
88
89        asserts.assert_true(is_connected(self.wlan_device),
90                        'Failed to connect.')
91
92        for _ in range(0, self.num_of_iterations):
93            # Stop AP
94            self.ap.stop_all_aps()
95            time.sleep(self.wait_after_ap_reboot_s)
96
97            # Did we disconnect from AP?
98            asserts.assert_false(is_connected(self.wlan_device),
99                        'Failed to disconnect.')
100
101            # Start AP
102            self.setup_ap()
103
104            # Give the device time to connect back
105            time.sleep(self.wait_to_connect_after_ap_reboot_s)
106
107            # Did we connect back to WiFi?
108            asserts.assert_true(is_connected(self.wlan_device),
109                        'Failed to connect back.')
110
111        return True
112