• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3.4
2#
3#   Copyright 2016 - 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 queue
18
19from acts import signals
20from acts import utils
21from acts.test_decorators import test_tracker_info
22from acts_contrib.test_utils.wifi import wifi_constants
23from acts_contrib.test_utils.wifi import wifi_test_utils as wutils
24from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest
25
26
27class WifiServiceApiTest(WifiBaseTest):
28    """This class tests the API surface of WifiManager in different wifi states.
29
30       Attributes:
31       The tests in this class only require one DUT.
32       The tests in this class do not require a SIM (but it is ok if one is
33           present).
34    """
35
36    TEST_SSID_PREFIX = "test_config_"
37    CONFIG_ELEMENT = 'config'
38    NETWORK_ID_ELEMENT = 'network_id'
39
40    def setup_class(self):
41        """ Sets up the required dependencies from the config file and
42            configures the device for WifiService API tests.
43
44            Returns:
45            True is successfully configured the requirements for testig.
46        """
47        super().setup_class()
48        self.dut = self.android_devices[0]
49        # Do a simple version of init - mainly just sync the time and enable
50        # verbose logging.  We would also like to test with phones in less
51        # constrained states (or add variations where we specifically
52        # constrain).
53        utils.require_sl4a((self.dut, ))
54        utils.sync_device_time(self.dut)
55
56        # Enable verbose logging on the dut
57        self.dut.droid.wifiEnableVerboseLogging(1)
58        if self.dut.droid.wifiGetVerboseLoggingLevel() != 1:
59            raise signals.TestFailure(
60                "Failed to enable WiFi verbose logging on the dut.")
61
62    def teardown_class(self):
63        wutils.reset_wifi(self.dut)
64
65    def create_and_save_wifi_network_config(self):
66        """ Create a config with random SSID and password.
67
68            Returns:
69            A tuple with the config and networkId for the newly created and saved network.
70        """
71        config_ssid = self.TEST_SSID_PREFIX + utils.rand_ascii_str(8)
72        config_password = utils.rand_ascii_str(8)
73        self.dut.log.info("creating config: %s %s", config_ssid,
74                          config_password)
75        config = {wutils.WifiEnums.SSID_KEY: config_ssid}
76        config[wutils.WifiEnums.PWD_KEY] = config_password
77
78        # Now save the config.
79        network_id = self.dut.droid.wifiAddNetwork(config)
80        self.dut.log.info("saved config: network_id = %s", network_id)
81        return {
82            self.NETWORK_ID_ELEMENT: network_id,
83            self.CONFIG_ELEMENT: config
84        }
85
86    def check_network_config_saved(self, config):
87        """ Get the configured networks and check of the provided config
88            is present.  This method only checks if the SSID is the same.
89            TODO: should do a deeper check to make sure this is the
90            correct config.
91
92            Args:
93                config: WifiConfig for a network.
94
95            Returns:
96                True if the WifiConfig is present.
97        """
98        networks = self.dut.droid.wifiGetConfiguredNetworks()
99        if not networks:
100            return False
101        ssid_key = wutils.WifiEnums.SSID_KEY
102        for network in networks:
103            if config[ssid_key] == network[ssid_key]:
104                return True
105        return False
106
107    def forget_network(self, network_id):
108        """ Simple method to call wifiForgetNetwork and wait for confirmation
109            callback.  The method returns False if it was not removed.
110
111            Returns:
112                True if network was successfully deleted.
113        """
114        self.dut.log.info("deleting config: networkId = %s", network_id)
115        self.dut.droid.wifiForgetNetwork(network_id)
116        try:
117            event = self.dut.ed.pop_event(
118                wifi_constants.WIFI_FORGET_NW_SUCCESS, 10)
119            return True
120        except queue.Empty:
121            self.dut.log.error("Failed to forget network")
122            return False
123
124    """ Tests Begin """
125
126    @test_tracker_info(uuid="f4df08c2-d3d5-4032-a433-c15f55130d4a")
127    def test_remove_config_wifi_enabled(self):
128        """ Test if config can be deleted when wifi is enabled.
129
130            1. Enable wifi, if needed
131            2. Create and save a random config.
132            3. Confirm the config is present.
133            4. Remove the config.
134            5. Confirm the config is not listed.
135        """
136        wutils.wifi_toggle_state(self.dut, True)
137        test_network = self.create_and_save_wifi_network_config()
138        if not self.check_network_config_saved(
139                test_network[self.CONFIG_ELEMENT]):
140            raise signals.TestFailure(
141                "Test network not found in list of configured networks.")
142        if not self.forget_network(test_network[self.NETWORK_ID_ELEMENT]):
143            raise signals.TestFailure(
144                "Test network not deleted from configured networks.")
145        if self.check_network_config_saved(test_network[self.CONFIG_ELEMENT]):
146            raise signals.TestFailure(
147                "Deleted network was in configured networks list.")
148
149    @test_tracker_info(uuid="9af96c7d-a316-4d57-ba5f-c992427c237b")
150    def test_remove_config_wifi_disabled(self):
151        """ Test if config can be deleted when wifi is disabled.
152
153            1. Enable wifi, if needed
154            2. Create and save a random config.
155            3. Confirm the config is present.
156            4. Disable wifi.
157            5. Remove the config.
158            6. Confirm the config is not listed.
159        """
160        wutils.wifi_toggle_state(self.dut, True)
161        test_network = self.create_and_save_wifi_network_config()
162        if not self.check_network_config_saved(
163                test_network[self.CONFIG_ELEMENT]):
164            raise signals.TestFailure(
165                "Test network not found in list of configured networks.")
166        wutils.wifi_toggle_state(self.dut, False)
167        if not self.forget_network(test_network[self.NETWORK_ID_ELEMENT]):
168            raise signals.TestFailure("Failed to delete network.")
169        if self.check_network_config_saved(test_network[self.CONFIG_ELEMENT]):
170            raise signals.TestFailure(
171                "Test network was found in list of configured networks.")
172
173    @test_tracker_info(uuid="79204ae6-323b-4257-a2cb-2225d44199d4")
174    def test_retrieve_config_wifi_enabled(self):
175        """ Test if config can be retrieved when wifi is enabled.
176
177            1. Enable wifi
178            2. Create and save a random config
179            3. Retrieve the config
180            4. Remove the config (clean up from the test)
181        """
182        wutils.wifi_toggle_state(self.dut, True)
183        test_network = self.create_and_save_wifi_network_config()
184
185        if not self.check_network_config_saved(
186                test_network[self.CONFIG_ELEMENT]):
187            raise signals.TestFailure(
188                "Test network not found in list of configured networks.")
189        if not self.forget_network(test_network[self.NETWORK_ID_ELEMENT]):
190            raise signals.TestFailure("Failed to delete network.")
191
192    @test_tracker_info(uuid="58fb4f81-bc19-43e1-b0af-89dbd17f45b2")
193    def test_retrieve_config_wifi_disabled(self):
194        """ Test if config can be retrieved when wifi is disabled.
195
196            1. Disable wifi
197            2. Create and save a random config
198            3. Retrieve the config
199            4. Remove the config (clean up from the test)
200        """
201        wutils.wifi_toggle_state(self.dut, False)
202        test_network = self.create_and_save_wifi_network_config()
203        if not self.check_network_config_saved(
204                test_network[self.CONFIG_ELEMENT]):
205            raise signals.TestFailure(
206                "Test network not found in list of configured networks.")
207        if not self.forget_network(test_network[self.NETWORK_ID_ELEMENT]):
208            raise signals.TestFailure("Failed to delete network.")
209
210    """ Tests End """
211
212
213if __name__ == "__main__":
214    pass
215