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 setup_ap
35from acts.test_utils.abstract_devices.utils_lib.wlan_utils import setup_ap_and_associate
36from acts.test_utils.abstract_devices.wlan_device import create_wlan_device
37from acts.test_utils.fuchsia import utils
38from acts.test_utils.tel.tel_test_utils import setup_droid_properties
39from acts.utils import rand_ascii_str
40
41
42class RebootAPStressTest(BaseTestClass):
43    # Default number of test iterations here.
44    # Override using parameter in config file.
45    # Eg: "reboot_ap_stress_test_iterations": "10"
46    num_of_iterations = 3
47
48    # Default wait time in seconds for the device
49    # to connect back after AP reboot.
50    # Override using parameter in config file.
51    # Eg: "wait_to_connect_after_ap_reboot_s": "60"
52    wait_to_connect_after_ap_reboot_s = 30
53
54    # Time to wait for device to disconnect
55    # after AP reboot.
56    wait_after_ap_reboot_s = 1
57
58    def setup_class(self):
59        super().setup_class()
60        self.ssid = rand_ascii_str(10)
61        self.wlan_device = create_wlan_device(self.fuchsia_devices[0])
62        self.ap = self.access_points[0]
63        self.num_of_iterations = int(
64            self.user_params.get("reboot_ap_stress_test_iterations",
65                                 self.num_of_iterations))
66        self.wait_to_connect_after_ap_reboot_s = int(
67            self.user_params.get("wait_to_connect_after_ap_reboot_s",
68                                 self.wait_to_connect_after_ap_reboot_s))
69
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(access_point=self.ap,
83                               client=self.wlan_device,
84                               profile_name='whirlwind',
85                               channel=hostapd_constants.AP_DEFAULT_CHANNEL_2G,
86                               ssid=self.ssid)
87
88        asserts.assert_true(self.wlan_device.is_connected(),
89                            'Failed to connect.')
90
91        for _ in range(0, self.num_of_iterations):
92            # Stop AP
93            self.ap.stop_all_aps()
94            time.sleep(self.wait_after_ap_reboot_s)
95
96            # Did we disconnect from AP?
97            asserts.assert_false(self.wlan_device.is_connected(),
98                                 'Failed to disconnect.')
99
100            # Start AP
101            self.setup_ap()
102
103            # Give the device time to connect back
104            time.sleep(self.wait_to_connect_after_ap_reboot_s)
105
106            # Did we connect back to WiFi?
107            asserts.assert_true(self.wlan_device.is_connected(),
108                                'Failed to connect back.')
109
110        return True
111