1#!/usr/bin/env python3.4
2#
3#   Copyright 2017 - 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 time
20
21import acts.signals
22import acts_contrib.test_utils.wifi.wifi_test_utils as wutils
23
24from acts import asserts
25from acts.test_decorators import test_tracker_info
26from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest
27
28WifiEnums = wutils.WifiEnums
29
30
31class WifiIOTTest(WifiBaseTest):
32    """ Tests for wifi IOT
33
34        Test Bed Requirement:
35          * One Android device
36          * Wi-Fi IOT networks visible to the device
37    """
38
39    def setup_class(self):
40        super().setup_class()
41
42        self.dut = self.android_devices[0]
43        wutils.wifi_test_device_init(self.dut)
44
45        req_params = [ "iot_networks", ]
46        opt_params = [ "open_network", "iperf_server_address" ]
47        self.unpack_userparams(req_param_names=req_params,
48                               opt_param_names=opt_params)
49
50        asserts.assert_true(
51            len(self.iot_networks) > 0,
52            "Need at least one iot network with psk.")
53
54        if getattr(self, 'open_network', False):
55            self.iot_networks.append(self.open_network)
56
57        wutils.wifi_toggle_state(self.dut, True)
58        if "iperf_server_address" in self.user_params:
59            self.iperf_server = self.iperf_servers[0]
60            self.iperf_server.start()
61
62        # create hashmap for testcase name and SSIDs
63        self.iot_test_prefix = "test_iot_connection_to_"
64        self.ssid_map = {}
65        for network in self.iot_networks:
66            SSID = network['SSID'].replace('-','_')
67            self.ssid_map[SSID] = network
68
69    def setup_test(self):
70        super().setup_test()
71        self.dut.droid.wakeLockAcquireBright()
72        self.dut.droid.wakeUpNow()
73
74    def teardown_test(self):
75        super().teardown_test()
76        self.dut.droid.wakeLockRelease()
77        self.dut.droid.goToSleepNow()
78        wutils.reset_wifi(self.dut)
79
80    def teardown_class(self):
81        if "iperf_server_address" in self.user_params:
82            self.iperf_server.stop()
83
84    """Helper Functions"""
85
86    def connect_to_wifi_network(self, network):
87        """Connection logic for open and psk wifi networks.
88
89        Args:
90            params: Dictionary with network info.
91        """
92        SSID = network[WifiEnums.SSID_KEY]
93        self.dut.ed.clear_all_events()
94        wutils.start_wifi_connection_scan(self.dut)
95        scan_results = self.dut.droid.wifiGetScanResults()
96        wutils.assert_network_in_list({WifiEnums.SSID_KEY: SSID}, scan_results)
97        wutils.wifi_connect(self.dut, network, num_of_tries=3)
98
99    def run_iperf_client(self, network):
100        """Run iperf traffic after connection.
101
102        Args:
103            params: Dictionary with network info.
104        """
105        if "iperf_server_address" in self.user_params:
106            wait_time = 5
107            SSID = network[WifiEnums.SSID_KEY]
108            self.log.info("Starting iperf traffic through {}".format(SSID))
109            time.sleep(wait_time)
110            port_arg = "-p {}".format(self.iperf_server.port)
111            success, data = self.dut.run_iperf_client(self.iperf_server_address,
112                                                      port_arg)
113            self.log.debug(pprint.pformat(data))
114            asserts.assert_true(success, "Error occurred in iPerf traffic.")
115
116    def connect_to_wifi_network_and_run_iperf(self, network):
117        """Connection logic for open and psk wifi networks.
118
119        Logic steps are
120        1. Connect to the network.
121        2. Run iperf traffic.
122
123        Args:
124            params: A dictionary with network info.
125        """
126        self.connect_to_wifi_network(network)
127        self.run_iperf_client(network)
128
129    """Tests"""
130
131    @test_tracker_info(uuid="a57cc861-b6c2-47e4-9db6-7a3ab32c6e20")
132    def test_iot_connection_to_ubiquity_ap1_2g(self):
133        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
134        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
135
136    @test_tracker_info(uuid="2065c2f7-2b89-4da7-a15d-e5dc17b88d52")
137    def test_iot_connection_to_ubiquity_ap1_5g(self):
138        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
139        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
140
141    @test_tracker_info(uuid="6870e35b-f7a7-45bf-b021-fea049ae53de")
142    def test_iot_connection_to_AirportExpress_2G(self):
143        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
144        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
145
146    @test_tracker_info(uuid="95f4b405-79d7-4873-a152-4384acc88f41")
147    def test_iot_connection_to_AirportExpress_5G(self):
148        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
149        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
150
151    @test_tracker_info(uuid="02a8cc75-6781-4153-8d90-bed7568a1e78")
152    def test_iot_connection_to_AirportExtreme_2G(self):
153        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
154        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
155
156    @test_tracker_info(uuid="83a42c97-1358-4ba7-bdb2-238fdb1c945e")
157    def test_iot_connection_to_AirportExtreme_5G(self):
158        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
159        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
160
161    @test_tracker_info(uuid="d56cc46a-f772-4c96-b84e-4e05c82f5f9d")
162    def test_iot_connection_to_AirportExtremeOld_2G(self):
163        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
164        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
165
166    @test_tracker_info(uuid="4b57277d-ea96-4379-bd71-8b4f03253ec8")
167    def test_iot_connection_to_AirportExtremeOld_5G(self):
168        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
169        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
170
171    @test_tracker_info(uuid="2503d9ed-35df-4be0-b838-590324cecaee")
172    def iot_connection_to_Dlink_AC1200_2G(self):
173        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
174        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
175
176    @test_tracker_info(uuid="0a44e148-a4bf-43f4-88eb-e4c1ffa850ce")
177    def iot_connection_to_Dlink_AC1200_5G(self):
178        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
179        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
180
181    @test_tracker_info(uuid="6bd77417-089f-4fb1-b4c2-2cd673c64bcb")
182    def test_iot_connection_to_Dlink_AC3200_2G(self):
183        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
184        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
185
186    @test_tracker_info(uuid="9fbff6e7-36c8-4342-9c29-bce6a8ef04ec")
187    def test_iot_connection_to_Dlink_AC3200_5G_1(self):
188        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
189        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
190
191    @test_tracker_info(uuid="bfccdaa9-8e01-488c-9768-8c71ab5ec157")
192    def test_iot_connection_to_Dlink_AC3200_5G_2(self):
193        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
194        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
195
196    @test_tracker_info(uuid="0e4978de-0435-4856-ae5a-c39cc64e375b")
197    def test_iot_connection_to_Dlink_N750_2G(self):
198        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
199        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
200
201    @test_tracker_info(uuid="cdb82797-9981-4ba6-8958-025f59c60e83")
202    def test_iot_connection_to_Dlink_N750_5G(self):
203        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
204        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
205
206    @test_tracker_info(uuid="0bf8f129-eb96-4b1e-94bd-8dd93e8731e3")
207    def iot_connection_to_Linksys_E800_2G(self):
208        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
209        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
210
211    @test_tracker_info(uuid="f231216d-6ab6-46b7-a0a5-1ac15935e412")
212    def test_iot_connection_to_Linksys_AC1900_2G(self):
213        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
214        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
215
216    @test_tracker_info(uuid="5acd4bec-b210-4b4c-8b2c-60f3f67798a9")
217    def test_iot_connection_to_Linksys_AC1900_5G(self):
218        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
219        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
220
221    @test_tracker_info(uuid="f4fd9877-b13f-47b0-9523-1ce363200c2f")
222    def iot_connection_to_Linksys_AC2400_2g(self):
223        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
224        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
225
226    @test_tracker_info(uuid="438d679a-4f6c-476d-9eba-63b6f1f2bef4")
227    def iot_connection_to_Linksys_AC2400_5g(self):
228        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
229        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
230
231    @test_tracker_info(uuid="b9bc00d8-46c5-4c5e-bd58-93ab1ca8d53b")
232    def iot_connection_to_NETGEAR_AC1900_2G(self):
233        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
234        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
235
236    @test_tracker_info(uuid="fb4c7d80-4c12-4b08-a40a-2745e2bd167b")
237    def iot_connection_to_NETGEAR_AC1900_5G(self):
238        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
239        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
240
241    @test_tracker_info(uuid="054d2ffc-97fd-4613-bf47-acedd0fa4701")
242    def test_iot_connection_to_NETGEAR_AC3200_2G(self):
243        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
244        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
245
246    @test_tracker_info(uuid="d15a789a-def5-4c6a-b59e-1a75f73cc6a9")
247    def test_iot_connection_to_NETGEAR_AC3200_5G_1(self):
248        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
249        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
250
251    @test_tracker_info(uuid="1de6369e-97da-479f-b17c-9144bb814f51")
252    def test_iot_connection_to_NETGEAR_AC3200_5G_2(self):
253        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
254        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
255
256    @test_tracker_info(uuid="008ec18e-fd48-4359-8a0d-223c921a1faa")
257    def iot_connection_to_NETGEAR_N300_2G(self):
258        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
259        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
260
261    @test_tracker_info(uuid="c61eeaf0-af02-46bf-bcec-871e2f9dee71")
262    def iot_connection_to_WNDR4500v2_AES_2G(self):
263        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
264        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
265
266    @test_tracker_info(uuid="dcad3474-4022-48bc-8529-07321611b616")
267    def iot_connection_to_WNDR4500v2_WEP_SHARED128_5G(self):
268        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
269        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
270
271    @test_tracker_info(uuid="3573a880-4542-4dea-9909-aa2f9865a059")
272    def iot_connection_to_ARCHER_WEP_OPEN_64_2G(self):
273        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
274        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
275
276    @test_tracker_info(uuid="9c15c52e-945a-4b9b-bf0e-5bd6293dad1c")
277    def iot_connection_to_ARCHER_WEP_OPEN_128_5G(self):
278        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
279        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
280
281    @test_tracker_info(uuid="e5517b82-c225-449d-83ac-055a561a764f")
282    def test_iot_connection_to_TP_LINK_AC1700_2G(self):
283        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
284        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
285
286    @test_tracker_info(uuid="9531d3cc-129d-4501-a5e3-d7502120cd8b")
287    def test_iot_connection_to_TP_LINK_AC1700_5G(self):
288        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
289        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
290
291    @test_tracker_info(uuid="eab810d4-8e07-49c9-86c1-cb8d1a0285d0")
292    def iot_connection_to_TP_LINK_N300_2G(self):
293        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
294        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
295
296    @test_tracker_info(uuid="05d4cb25-a58d-46b4-a5ff-6e3fe28f2b16")
297    def iot_connection_to_fritz_7490_5g(self):
298        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
299        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
300
301    @test_tracker_info(uuid="8333e5e6-72fd-4957-bab0-fa45ce1d765a")
302    def iot_connection_to_NETGEAR_R8500_2G(self):
303        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
304        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
305
306    @test_tracker_info(uuid="c88053fb-730f-4447-a802-1fb9721f69df")
307    def iot_connection_to_NETGEAR_R8500_5G1(self):
308        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
309        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
310
311    @test_tracker_info(uuid="f5d1e44b-396b-4976-bb0c-160bdce89a59")
312    def iot_connection_to_NETGEAR_R8500_5G2(self):
313        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
314        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
315
316    @test_tracker_info(uuid="7c12f943-d9e2-45b1-aa84-fcb43efbbb04")
317    def test_iot_connection_to_TP_LINK_5504_2G(self):
318        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
319        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
320
321    @test_tracker_info(uuid="52be6b76-5e43-4289-83e1-4cd0d995d39b")
322    def test_iot_connection_to_TP_LINK_5504_5G_1(self):
323        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
324        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
325
326    @test_tracker_info(uuid="0b43d1da-e207-443d-b16c-c4ee3e924036")
327    def test_iot_connection_to_TP_LINK_5504_5G_2(self):
328        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
329        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
330
331    @test_tracker_info(uuid="4adcef5c-589a-4398-a28c-28a56d762f72")
332    def test_iot_connection_to_TP_LINK_C2600_2G(self):
333        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
334        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
335
336    @test_tracker_info(uuid="3955a443-505b-4015-9daa-f52abbad8377")
337    def test_iot_connection_to_TP_LINK_C2600_5G(self):
338        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
339        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
340
341    @test_tracker_info(uuid="3e9115dd-adb6-40a4-9831-dca8f1f32abe")
342    def test_iot_connection_to_Linksys06832_2G(self):
343        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
344        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
345
346    @test_tracker_info(uuid="5dca028a-7384-444f-b231-973054afe215")
347    def test_iot_connection_to_Linksys06832_5G(self):
348        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
349        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
350
351    @test_tracker_info(uuid="e639f6db-ad8e-4b4f-91f3-10acdf93142a")
352    def test_iot_connection_to_AmpedAthena_2G(self):
353        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
354        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
355
356    @test_tracker_info(uuid="3dd90d80-952f-4f17-a48a-fe42e7d6e1ff")
357    def test_iot_connection_to_AmpedAthena_5G(self):
358        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
359        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
360
361    @test_tracker_info(uuid="b9babe3a-ecba-4c5c-bc6b-0ba48c744e66")
362    def test_iot_connection_to_ASUS_AC3100_2G(self):
363        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
364        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
365
366    @test_tracker_info(uuid="f8f06f92-821d-4e80-8f1e-efb6c6adc12a")
367    def test_iot_connection_to_ASUS_AC3100_5G(self):
368        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
369        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
370
371    @test_tracker_info(uuid="f4d227df-1151-469a-b01c-f4b1c1f7a84b")
372    def iot_connection_to_NETGEAR_WGR614_2G(self):
373        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
374        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
375