1#!/usr/bin/env python3.4
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 itertools
18import pprint
19import queue
20import time
21
22import acts.base_test
23import acts.signals
24import acts.test_utils.wifi.wifi_test_utils as wutils
25import acts.utils
26
27from acts import asserts
28from acts.test_decorators import test_tracker_info
29from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest
30
31
32class WifiHiddenSSIDTest(WifiBaseTest):
33    """Tests for APIs in Android's WifiManager class.
34
35    Test Bed Requirement:
36    * One Android device
37    * Several Wi-Fi networks visible to the device, including an open Wi-Fi
38      network.
39    """
40
41    def setup_class(self):
42        super().setup_class()
43
44        self.dut = self.android_devices[0]
45        wutils.wifi_test_device_init(self.dut)
46        req_params = []
47        opt_param = [
48            "open_network", "reference_networks"]
49        self.unpack_userparams(
50            req_param_names=req_params, opt_param_names=opt_param)
51
52        if "AccessPoint" in self.user_params:
53            self.legacy_configure_ap_and_start(hidden=True)
54
55        asserts.assert_true(
56            len(self.reference_networks) > 0,
57            "Need at least one reference network with psk.")
58        self.open_hidden_2g = self.open_network[0]["2g"]
59        self.open_hidden_5g = self.open_network[0]["5g"]
60        self.wpa_hidden_2g = self.reference_networks[0]["2g"]
61        self.wpa_hidden_5g = self.reference_networks[0]["5g"]
62
63    def setup_test(self):
64        self.dut.droid.wakeLockAcquireBright()
65        self.dut.droid.wakeUpNow()
66
67    def teardown_test(self):
68        self.dut.droid.wakeLockRelease()
69        self.dut.droid.goToSleepNow()
70
71    def on_fail(self, test_name, begin_time):
72        self.dut.take_bug_report(test_name, begin_time)
73        self.dut.cat_adb_log(test_name, begin_time)
74
75    def teardown_class(self):
76        wutils.reset_wifi(self.dut)
77        if "AccessPoint" in self.user_params:
78            del self.user_params["reference_networks"]
79            del self.user_params["open_network"]
80
81    """Helper Functions"""
82
83    def check_hiddenSSID_in_scan(self, ap_ssid, max_tries=2):
84        """Check if the ap started by wifi tethering is seen in scan results.
85
86        Args:
87            ap_ssid: SSID of the ap we are looking for.
88            max_tries: Number of scans to try.
89        Returns:
90            True: if ap_ssid is found in scan results.
91            False: if ap_ssid is not found in scan results.
92        """
93        for num_tries in range(max_tries):
94            wutils.start_wifi_connection_scan(self.dut)
95            scan_results = self.dut.droid.wifiGetScanResults()
96            match_results = wutils.match_networks(
97                {wutils.WifiEnums.SSID_KEY: ap_ssid}, scan_results)
98            if len(match_results) > 0:
99                return True
100        return False
101
102    def add_hiddenSSID_and_connect(self, hidden_network):
103        """Add the hidden network and connect to it.
104
105        Args:
106            hidden_network: The hidden network config to connect to.
107
108        """
109        wutils.connect_to_wifi_network(self.dut, hidden_network, hidden=True)
110        if not wutils.validate_connection(self.dut):
111            raise signals.TestFailure("Fail to connect to internet on %s" %
112                                       hidden_network)
113
114    """Tests"""
115
116    @test_tracker_info(uuid="d0871f98-6049-4937-a288-ec4a2746c771")
117    def test_connect_to_wpa_hidden_2g(self):
118        """Connect to a WPA, 2G network.
119
120        Steps:
121        1. Add a WPA, 2G hidden network.
122        2. Ensure the network is visible in scan.
123        3. Connect and run ping.
124
125        """
126        self.add_hiddenSSID_and_connect(self.wpa_hidden_2g)
127
128    @test_tracker_info(uuid="c558b31a-549a-4012-9052-275623992187")
129    def test_connect_to_wpa_hidden_5g(self):
130        """Connect to a WPA, 5G hidden  network.
131
132        Steps:
133        1. Add a WPA, 5G hidden network.
134        2. Ensure the network is visible in scan.
135        3. Connect and run ping.
136
137        """
138        self.add_hiddenSSID_and_connect(self.wpa_hidden_5g)
139
140    @test_tracker_info(uuid="cdfce76f-6374-439d-aa1d-e920508269d2")
141    def test_connect_to_open_hidden_2g(self):
142        """Connect to a Open, 2G hidden  network.
143
144        Steps:
145        1. Add a Open, 2G hidden network.
146        2. Ensure the network is visible in scan.
147        3. Connect and run ping.
148
149        """
150        self.add_hiddenSSID_and_connect(self.open_hidden_2g)
151
152    @test_tracker_info(uuid="29ccbae4-4382-4df8-8fc5-00e3104230d0")
153    def test_connect_to_open_hidden_5g(self):
154        """Connect to a Open, 5G hidden  network.
155
156        Steps:
157        1. Add a Open, 5G hidden network.
158        2. Ensure the network is visible in scan.
159        3. Connect and run ping.
160
161        """
162        self.add_hiddenSSID_and_connect(self.open_hidden_5g)
163