• 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 time
18
19from acts import asserts
20from acts.base_test import BaseTestClass
21from acts.test_utils.wifi.wifi_test_utils import wifi_forget_network
22from acts.test_utils.wifi.wifi_test_utils import wifi_test_device_init
23from acts.test_utils.wifi.wifi_test_utils import WifiEnums
24from acts.test_utils.wifi.wifi_test_utils import start_wifi_connection_scan
25from acts.test_utils.wifi.wifi_test_utils import check_internet_connection
26from acts.test_utils.wifi.wifi_test_utils import track_connection
27
28NETWORK_ID_ERROR = "Network don't have ID"
29NETWORK_ERROR = "Device is not connected to reference network"
30
31class WifiAutoJoinTest(BaseTestClass):
32
33    def __init__(self, controllers):
34        BaseTestClass.__init__(self, controllers)
35        self.tests = (
36            "test_autojoin_out_of_range",
37            "test_autojoin_Ap1_2g",
38            "test_autojoin_Ap1_2gto5g",
39            "test_autojoin_in_AP1_5gto2g",
40            "test_autojoin_swtich_AP1toAp2",
41            "test_autojoin_Ap2_2gto5g",
42            "test_autojoin_Ap2_5gto2g",
43            "test_autojoin_out_of_range",
44            "test_autojoin_Ap2_2g",
45            "test_autojoin_Ap2_2gto5g",
46            "test_autojoin_in_Ap2_5gto2g",
47            "test_autojoin_swtich_AP2toAp1",
48            "test_autojoin_Ap1_2gto5g",
49            "test_autojoin_Ap1_5gto2g",
50            "test_autojoin_swtich_to_blacklist_AP",
51            "test_autojoin_in_blacklist_AP",
52            "test_autojoin_back_from_blacklist_AP",
53            )
54
55    def setup_class(self):
56        """It will setup the required dependencies from config file and configure
57           the required networks for auto-join testing. Configured networks will
58           not be removed. If networks are already configured it will skip
59           configuring the networks
60
61        Returns:
62            True if successfully configured the requirements for testing.
63        """
64        self.dut = self.android_devices[0]
65        wifi_test_device_init(self.dut)
66        req_params = ("reference_networks", "other_network", "atten_val",
67                      "ping_addr", "max_bugreports" )
68        self.unpack_userparams(req_params)
69        self.log.debug("Connect networks :: {}".format(self.other_network))
70        configured_networks = self.dut.droid.wifiGetConfiguredNetworks()
71        self.log.debug("Configured networks :: {}".format(configured_networks))
72        count_confnet = 0
73        result = False
74        if self.reference_networks[0]['2g']['ssid'] == self.reference_networks[0]['5g']['ssid']:
75            self.ref_ssid_count = 1
76        else:
77            self.ref_ssid_count = 2 # Different SSID for 2g and 5g
78        for confnet in configured_networks:
79            if confnet[WifiEnums.SSID_KEY] == self.reference_networks[0]['2g']['ssid']:
80                count_confnet += 1
81            elif confnet[WifiEnums.SSID_KEY] == self.reference_networks[0]['5g']['ssid']:
82                count_confnet += 1
83        self.log.info("count_confnet {}".format(count_confnet))
84        if count_confnet == self.ref_ssid_count:
85            return True
86        else:
87            self.log.info("Configured networks for testing")
88            self.attenuators[0].set_atten(0)
89            self.attenuators[1].set_atten(90)
90            self.attenuators[2].set_atten(90)
91            wait_time = 15
92            self.dut.droid.wakeLockAcquireBright()
93            self.dut.droid.wakeUpNow()
94            try:
95                self.dut.droid.wifiPriorityConnect(self.reference_networks[0]['2g'])
96                connect_result = self.dut.ed.pop_event("WifiManagerPriorityConnectOnSuccess", 1)
97                self.log.info(connect_result)
98                time.sleep(wait_time)
99                if self.ref_ssid_count == 2: #add 5g network as well
100                    self.dut.droid.wifiPriorityConnect(self.reference_networks[0]['5g'])
101                    connect_result = self.dut.ed.pop_event("WifiManagerPriorityConnectOnSuccess", 1)
102                    self.log.info(connect_result)
103                    time.sleep(wait_time)
104                self.dut.droid.wifiPriorityConnect(self.other_network)
105                connect_result = self.dut.ed.pop_event("WifiManagerPriorityConnectOnSuccess")
106                self.log.info(connect_result)
107                track_connection(self.dut, self.other_network["ssid"], 1)
108                wifi_forget_network(self.dut, self.other_network["ssid"])
109                time.sleep(wait_time)
110                current_network = self.dut.droid.wifiGetConnectionInfo()
111                self.log.info("Current network: {}".format(current_network))
112                asserts.assert_true('network_id' in current_network, NETWORK_ID_ERROR)
113                asserts.assert_true(current_network['network_id'] >= 0, NETWORK_ERROR)
114                self.ip_address = self.dut.droid.wifiGetConfigFile();
115                self.log.info("IP info: {}".format(self.ip_address))
116            finally:
117                self.dut.droid.wifiLockRelease()
118                self.dut.droid.goToSleepNow()
119
120    def check_connection(self, network_bssid):
121        """Check current wifi connection networks.
122        Args:
123            network_bssid: Network bssid to which connection.
124        Returns:
125            True if connection to given network happen, else return False.
126        """
127        time.sleep(40) #time for connection state to be updated
128        self.log.info("Check network for {}".format(network_bssid))
129        current_network = self.dut.droid.wifiGetConnectionInfo()
130        self.log.debug("Current network:  {}".format(current_network))
131        if WifiEnums.BSSID_KEY in current_network:
132            return current_network[WifiEnums.BSSID_KEY] == network_bssid
133        return False
134
135    def set_attn_and_validate_connection(self, attn_value, bssid):
136        """Validate wifi connection status on different attenuation setting.
137
138        Args:
139            attn_value: Attenuation value for different APs signal.
140            bssid: Bssid of excepted network.
141
142        Returns:
143            True if bssid of current network match, else false.
144        """
145        self.attenuators[0].set_atten(attn_value[0])
146        self.attenuators[1].set_atten(attn_value[1])
147        self.attenuators[2].set_atten(attn_value[2])
148        self.dut.droid.wakeLockAcquireBright()
149        self.dut.droid.wakeUpNow()
150        try:
151            asserts.assert_true(self.check_connection(bssid),
152                    "Device is not connected to required bssid {}".format(bssid))
153            time.sleep(10) #wait for connection to be active
154            asserts.assert_true(check_internet_connection(self.dut, self.ping_addr),
155                             "Error, No Internet connection for current bssid {}".
156                             format(bssid))
157        finally:
158            self.dut.droid.wifiLockRelease()
159            self.dut.droid.goToSleepNow()
160
161    def on_fail(self, test_name, begin_time):
162        if self.max_bugreports > 0:
163            self.dut.take_bug_report(test_name, begin_time)
164            self.max_bugreports -= 1
165        self.dut.cat_adb_log(test_name, begin_time)
166
167    """ Tests Begin """
168    def test_autojoin_Ap1_2g(self):
169        """Test wifi auto join functionality move in range of AP1.
170
171         1. Attenuate the signal to low range of AP1 and Ap2 not visible at all.
172         2. Wake up the device.
173         3. Check that device is connected to right BSSID and maintain stable
174            connection to BSSID in range.
175        """
176        att0,att1,att2 =  self.atten_val["Ap1_2g"]
177        variance = 5
178        attenuations = ([att0+variance*2, att1, att2], [att0+variance, att1, att2],
179                        [att0, att1, att2], [att0-variance, att1, att2])
180        name_func = lambda att_value, bssid : ("test_autojoin_Ap1_2g_AP1_{}_AP2"
181                     "_{}_AP3_{}").format(att_value[0], att_value[1], att_value[2])
182        failed = self.run_generated_testcases(
183                        self.set_attn_and_validate_connection,
184                        attenuations,
185                        args = (self.reference_networks[0]["2g"]['bssid'],),
186                        name_func = name_func)
187        asserts.assert_true(not failed, "Number of test_autojoin_Ap1_2g failed {}".
188                         format(len(failed)))
189
190    def test_autojoin_Ap1_2gto5g(self):
191        """Test wifi auto join functionality move to high range.
192
193         1. Attenuate the signal to high range of AP1.
194         2. Wake up the device.
195         3. Check that device is connected to right BSSID and maintain stable
196            connection to BSSID in range.
197        """
198        att0,att1,att2 =  self.atten_val["Ap1_2gto5g"]
199        variance = 5
200        attenuations = ([att0+variance*2, att1, att2], [att0+variance, att1, att2],
201                        [att0, att1, att2])
202        name_func = lambda att_value, bssid : ("test_autojoin_Ap1_2gto5g_AP1_{}_AP2"
203                     "_{}_AP3_{}").format(att_value[0], att_value[1],att_value[2])
204        failed = self.run_generated_testcases(
205                        self.set_attn_and_validate_connection,
206                        attenuations,
207                        args = (self.reference_networks[0]["5g"]['bssid'],),
208                        name_func = name_func)
209        asserts.assert_true(not failed, "Number of test_autojoin_Ap1_2gto5g failed {}".
210                         format(len(failed)))
211
212
213    def test_autojoin_in_AP1_5gto2g(self):
214        """Test wifi auto join functionality move to low range toward AP2.
215
216         1. Attenuate the signal to medium range of AP1 and low range of AP2.
217         2. Wake up the device.
218         3. Check that device is connected to right BSSID and maintain stable
219            connection to BSSID in range.
220        """
221        att0,att1,att2 =  self.atten_val["In_AP1_5gto2g"]
222        variance = 5
223        attenuations = ([att0-variance, att1+variance, att2], [att0, att1, att2],
224                        [att0+variance, att1-variance, att2])
225        name_func = lambda att_value, bssid : ("test_autojoin_in_AP1_5gto2g_AP1_{}_AP2"
226                     "_{}_AP3_{}").format(att_value[0], att_value[1],att_value[2])
227        failed = self.run_generated_testcases(
228                        self.set_attn_and_validate_connection,
229                        attenuations,
230                        args = (self.reference_networks[0]["2g"]['bssid'],),
231                        name_func = name_func)
232        asserts.assert_true(not failed, "Number of test_autojoin_in_AP1_5gto2g failed {}".
233                         format(len(failed)))
234
235    def test_autojoin_swtich_AP1toAp2(self):
236        """Test wifi auto join functionality move from low range of AP1 to better
237           range of AP2.
238
239         1. Attenuate the signal to low range of AP1 and medium range of AP2.
240         2. Wake up the device.
241         3. Check that device is connected to right BSSID and maintain stable
242            connection to BSSID in range.
243        """
244        att0,att1,att2 =  self.atten_val["Swtich_AP1toAp2"]
245        variance = 5
246        attenuations = ([att0-variance, att1+variance, att2], [att0, att1, att2],
247                        [att0+variance, att1-variance, att2])
248        name_func = lambda att_value, bssid : ("test_autojoin_swtich_AP1toAp2_AP1_{}_AP2"
249                     "_{}_AP3_{}").format(att_value[0], att_value[1],att_value[2])
250        failed = self.run_generated_testcases(
251                        self.set_attn_and_validate_connection,
252                        attenuations,
253                        args = (self.reference_networks[1]["2g"]['bssid'],),
254                        name_func = name_func)
255        asserts.assert_true(not failed, "Number of test_autojoin_swtich_AP1toAp2 failed {}".
256                         format(len(failed)))
257
258    def test_autojoin_Ap2_2gto5g(self):
259        """Test wifi auto join functionality move to high range of AP2.
260
261         1. Attenuate the signal to out range of AP1 and high range of AP2.
262         2. Wake up the device.
263         3. Check that device is connected to right BSSID and maintain stable
264            connection to BSSID in range.
265        """
266        att0,att1,att2 =  self.atten_val["Ap2_2gto5g"]
267        variance = 5
268        attenuations = ([att0-variance, att1+variance*2, att2],
269                        [att0, att1+variance, att2], [att0, att1, att2])
270        name_func = lambda att_value, bssid : ("test_autojoin_Ap2_2gto5g_AP1_{}_AP2"
271                     "_{}_AP3_{}").format(att_value[0], att_value[1], att_value[2])
272        failed = self.run_generated_testcases(
273                        self.set_attn_and_validate_connection,
274                        attenuations,
275                        args = (self.reference_networks[1]["5g"]['bssid'],),
276                        name_func = name_func)
277        asserts.assert_true(not failed, "Number of test_autojoin_Ap2_2gto5g failed {}".
278                         format(len(failed)))
279
280    def test_autojoin_Ap2_5gto2g(self):
281        """Test wifi auto join functionality move to low range of AP2.
282
283         1. Attenuate the signal to low range of AP2.
284         2. Wake up the device.
285         3. Check that device is connected to right BSSID and maintain stable.
286        """
287        att0,att1,att2 =  self.atten_val["Ap2_5gto2g"]
288        variance = 5
289        attenuations = ([att0, att1-variance, att2], [att0, att1, att2],
290                        [att0, att1+variance, att2])
291        name_func = lambda att_value, bssid : ("test_autojoin_Ap2_5gto2g_AP1_{}_AP2"
292                     "_{}_AP3_{}").format(att_value[0], att_value[1], att_value[2])
293        failed = self.run_generated_testcases(
294                        self.set_attn_and_validate_connection,
295                        attenuations,
296                        args = (self.reference_networks[1]["2g"]['bssid'],),
297                        name_func = name_func)
298        asserts.assert_true(not failed, "Number of test_autojoin_Ap2_5gto2g failed {}".
299                         format(len(failed)))
300
301    def test_autojoin_out_of_range(self):
302        """Test wifi auto join functionality move to low range.
303
304         1. Attenuate the signal to out of range.
305         2. Wake up the device.
306         3. Start the scan.
307         4. Check that device is not connected to any network.
308        """
309        self.attenuators[0].set_atten(90)
310        self.attenuators[1].set_atten(90)
311        self.attenuators[2].set_atten(90)
312        self.dut.droid.wakeLockAcquireBright()
313        self.dut.droid.wakeUpNow()
314        try:
315            start_wifi_connection_scan(self.dut)
316            wifi_results = self.dut.droid.wifiGetScanResults()
317            self.log.debug("Scan result {}".format(wifi_results))
318            time.sleep(20)
319            current_network = self.dut.droid.wifiGetConnectionInfo()
320            self.log.info("Current network: {}".format(current_network))
321            asserts.assert_true(('network_id' in current_network and
322                              current_network['network_id'] == -1),
323                             "Device is connected to network {}".format(current_network))
324        finally:
325            self.dut.droid.wifiLockRelease()
326            self.dut.droid.goToSleepNow()
327
328    def test_autojoin_Ap2_2g(self):
329        """Test wifi auto join functionality move in low range of AP2.
330
331         1. Attenuate the signal to move in range of AP2 and Ap1 not visible at all.
332         2. Wake up the device.
333         3. Check that device is connected to right BSSID and maintain stable
334            connection to BSSID in range.
335        """
336        att0,att1,att2 =  self.atten_val["Ap2_2g"]
337        variance = 5
338        attenuations = ([att0,att1+variance*2,att2],
339                        [att0,att1+variance,att2],[att0,att1,att2],
340                        [att0,att1-variance,att2])
341        name_func = lambda att_value, bssid : ("test_autojoin_Ap2_2g_AP1_{}_AP2"
342                     "_{}_AP3_{}").format(att_value[0], att_value[1],att_value[2])
343        failed = self.run_generated_testcases(
344                        self.set_attn_and_validate_connection,
345                        attenuations,
346                        args = (self.reference_networks[1]["2g"]['bssid'],),
347                        name_func = name_func)
348        asserts.assert_true(not failed, "Number of test_autojoin_Ap2_2g failed {}".
349                         format(len(failed)))
350
351    def test_autojoin_in_Ap2_5gto2g(self):
352        """Test wifi auto join functionality move to medium range of Ap2 and
353           low range of AP1.
354
355         1. Attenuate the signal to move in medium range of AP2 and low range of AP1.
356         2. Wake up the device.
357         3. Check that device is connected to right BSSID and maintain stable
358            connection to BSSID in range.
359        """
360        att0,att1,att2 =  self.atten_val["In_Ap2_5gto2g"]
361        variance = 5
362        attenuations = ([att0,att1-variance,att2],[att0,att1,att2],
363                        [att0,att1+variance,att2])
364        name_func = lambda att_value, bssid : ("test_autojoin_in_Ap2_5gto2g_AP1_{}_AP2"
365                     "_{}_AP3_{}").format(att_value[0], att_value[1],att_value[2])
366        failed = self.run_generated_testcases(
367                        self.set_attn_and_validate_connection,
368                        attenuations,
369                        args = (self.reference_networks[1]["2g"]['bssid'],),
370                        name_func = name_func)
371        asserts.assert_true(not failed, "Number of test_autojoin_in_Ap2_5gto2g failed {}".
372                         format(len(failed)))
373
374    def test_autojoin_swtich_AP2toAp1(self):
375        """Test wifi auto join functionality move from low range of AP2 to better
376           range of AP1.
377
378         1. Attenuate the signal to low range of AP2 and medium range of AP1.
379         2. Wake up the device.
380         3. Check that device is connected to right BSSID and maintain stable
381            connection to BSSID in range.
382        """
383        att0,att1,att2 =  self.atten_val["Swtich_AP2toAp1"]
384        variance = 5
385        attenuations = ([att0+variance,att1-variance,att2],[att0,att1,att2],
386                        [att0-variance,att1+variance,att2])
387        name_func = lambda att_value, bssid : ("test_autojoin_swtich_AP2toAp1_AP1_{}_AP2"
388                     "_{}_AP3_{}").format(att_value[0], att_value[1],att_value[2])
389        failed = self.run_generated_testcases(
390                        self.set_attn_and_validate_connection,
391                        attenuations,
392                        args = (self.reference_networks[0]["2g"]['bssid'],),
393                        name_func = name_func)
394        asserts.assert_true(not failed, "Number of test_autojoin_swtich_AP2toAp1 failed {}".
395                         format(len(failed)))
396
397    def test_autojoin_Ap1_5gto2g(self):
398        """Test wifi auto join functionality move to medium range of AP1.
399
400         1. Attenuate the signal to medium range of AP1.
401         2. Wake up the device.
402         3. Check that device is connected to right BSSID and maintain stable
403            connection to BSSID in range.
404        """
405        att0,att1,att2 =  self.atten_val["Ap1_5gto2g"]
406        variance = 5
407        attenuations = ([att0,att1,att2], [att0+variance,att1,att2],
408                        [att0+variance*2,att1,att2])
409        name_func = lambda att_value, bssid : ("test_autojoin_Ap1_5gto2g_AP1_{}_AP2"
410                     "_{}_AP3_{}").format(att_value[0], att_value[1],att_value[2])
411        failed = self.run_generated_testcases(
412                        self.set_attn_and_validate_connection,
413                        attenuations,
414                        args = (self.reference_networks[0]["2g"]['bssid'],),
415                        name_func = name_func)
416        asserts.assert_true(not failed, "Number of test_autojoin_Ap1_5gto2g failed {}".
417                         format(len(failed)))
418
419    def test_autojoin_swtich_to_blacklist_AP(self):
420        """Test wifi auto join functionality in medium range of blacklist BSSID.
421
422         1. Attenuate the signal to low range of AP1 and medium range of AP3.
423         2. Wake up the device.
424         3. Check that device is connected to AP1 BSSID and maintain stable
425            connection to BSSID.
426        """
427        self.set_attn_and_validate_connection(self.atten_val["Swtich_to_blacklist"],
428                                              self.reference_networks[0]["2g"]['bssid'])
429
430    def test_autojoin_in_blacklist_AP(self):
431        """Test wifi auto join functionality in high range of blacklist BSSID.
432
433         1. Attenuate the signal to out of range of AP1 and full range of AP3.
434         2. Wake up the device.
435         3. Check that device is disconnected form all AP.
436        """
437        attn0, attn1, attn2 = self.atten_val["In_blacklist"]
438        self.attenuators[0].set_atten(attn0)
439        self.attenuators[1].set_atten(attn1)
440        self.attenuators[2].set_atten(attn2)
441        self.dut.droid.wakeLockAcquireBright()
442        self.dut.droid.wakeUpNow()
443        try:
444            start_wifi_connection_scan(self.dut)
445            wifi_results = self.dut.droid.wifiGetScanResults()
446            self.log.debug("Scan result {}".format(wifi_results))
447            time.sleep(20)
448            current_network = self.dut.droid.wifiGetConnectionInfo()
449            self.log.info("Current network: {}".format(current_network))
450            asserts.assert_true(('network_id' in current_network and
451                              current_network['network_id'] == -1),
452                             "Device is still connected to blacklisted network {}".
453                             format(current_network))
454        finally:
455            self.dut.droid.wifiLockRelease()
456            self.dut.droid.goToSleepNow()
457
458    def test_autojoin_back_from_blacklist_AP(self):
459        """Test wifi auto join functionality in medium range of blacklist BSSID.
460
461         1. Attenuate the signal to medium of range of AP1 and low range of AP3.
462         2. Wake up the device.
463         3. Check that device is disconnected form all AP.
464        """
465        self.set_attn_and_validate_connection(self.atten_val["Back_from_blacklist"],
466                                              self.reference_networks[0]["2g"]['bssid'])
467    """ Tests End """
468if __name__ == "__main__":
469    pass
470