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.test_utils.wifi.wifi_test_utils as wutils
23
24from acts import asserts
25from acts.test_decorators import test_tracker_info
26from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest
27
28WifiEnums = wutils.WifiEnums
29
30
31class WifiIOTtpeTest(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 __init__(self, controllers):
40        self.attenuators = None
41        WifiBaseTest.__init__(self, controllers)
42
43    def setup_class(self):
44        self.dut = self.android_devices[0]
45        wutils.wifi_test_device_init(self.dut)
46
47        req_params = [ "iot_networks", ]
48        opt_params = [ "open_network", "iperf_server_address" ]
49        self.unpack_userparams(req_param_names=req_params,
50                               opt_param_names=opt_params)
51
52        asserts.assert_true(
53            len(self.iot_networks) > 0,
54            "Need at least one iot network with psk.")
55
56        if getattr(self, 'open_network', False):
57            self.iot_networks.append(self.open_network)
58
59        wutils.wifi_toggle_state(self.dut, True)
60        if "iperf_server_address" in self.user_params:
61            self.iperf_server = self.iperf_servers[0]
62            self.iperf_server.start()
63
64        # create hashmap for testcase name and SSIDs
65        self.iot_test_prefix = "test_iot_connection_to_"
66        self.ssid_map = {}
67        for network in self.iot_networks:
68            SSID = network['SSID'].replace('-','_')
69            self.ssid_map[SSID] = network
70
71    def setup_test(self):
72        self.dut.droid.wakeLockAcquireBright()
73        self.dut.droid.wakeUpNow()
74
75    def teardown_test(self):
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    def on_fail(self, test_name, begin_time):
85        self.dut.take_bug_report(test_name, begin_time)
86        self.dut.cat_adb_log(test_name, begin_time)
87
88    """Helper Functions"""
89
90    def connect_to_wifi_network(self, network):
91        """Connection logic for open and psk wifi networks.
92
93        Args:
94            params: Dictionary with network info.
95        """
96        SSID = network[WifiEnums.SSID_KEY]
97        self.dut.ed.clear_all_events()
98        wutils.start_wifi_connection_scan(self.dut)
99        scan_results = self.dut.droid.wifiGetScanResults()
100        wutils.assert_network_in_list({WifiEnums.SSID_KEY: SSID}, scan_results)
101        wutils.wifi_connect(self.dut, network, num_of_tries=3)
102
103    def run_iperf_client(self, network):
104        """Run iperf traffic after connection.
105
106        Args:
107            params: Dictionary with network info.
108        """
109        if "iperf_server_address" in self.user_params:
110            wait_time = 5
111            SSID = network[WifiEnums.SSID_KEY]
112            self.log.info("Starting iperf traffic through {}".format(SSID))
113            time.sleep(wait_time)
114            port_arg = "-p {}".format(self.iperf_server.port)
115            success, data = self.dut.run_iperf_client(self.iperf_server_address,
116                                                      port_arg)
117            self.log.debug(pprint.pformat(data))
118            asserts.assert_true(success, "Error occurred in iPerf traffic.")
119
120    def connect_to_wifi_network_and_run_iperf(self, network):
121        """Connection logic for open and psk wifi networks.
122
123        Logic steps are
124        1. Connect to the network.
125        2. Run iperf traffic.
126
127        Args:
128            params: A dictionary with network info.
129        """
130        self.connect_to_wifi_network(network)
131        self.run_iperf_client(network)
132
133    """Tests"""
134
135    @test_tracker_info(uuid="0e4ad6ed-595c-4629-a4c9-c6be9c3c58e0")
136    def test_iot_connection_to_ASUS_RT_AC68U_2G(self):
137        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
138        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
139
140    @test_tracker_info(uuid="a76d8acc-808e-4a5d-a52b-5ba07d07b810")
141    def test_iot_connection_to_ASUS_RT_AC68U_5G(self):
142        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
143        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
144
145    @test_tracker_info(uuid="659a3e5e-07eb-4905-9cda-92e959c7b674")
146    def test_iot_connection_to_D_Link_DIR_868L_2G(self):
147        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
148        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
149
150    @test_tracker_info(uuid="6bcfd736-30fc-48a8-b4fb-723d1d113f3c")
151    def test_iot_connection_to_D_Link_DIR_868L_5G(self):
152        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
153        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
154
155    @test_tracker_info(uuid="c9da945a-2c4a-44e1-881d-adf307b39b21")
156    def test_iot_connection_to_TP_LINK_WR940N_2G(self):
157        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
158        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
159
160    @test_tracker_info(uuid="db0d224d-df81-401f-bf35-08ad02e41a71")
161    def test_iot_connection_to_ASUS_RT_N66U_2G(self):
162        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
163        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
164
165    @test_tracker_info(uuid="845ff1d6-618d-40f3-81c3-6ed3a0751fde")
166    def test_iot_connection_to_ASUS_RT_N66U_5G(self):
167        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
168        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
169
170    @test_tracker_info(uuid="6908039b-ccc9-4777-a0f1-3494ce642014")
171    def test_iot_connection_to_ASUS_RT_AC54U_2G(self):
172        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
173        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
174
175    @test_tracker_info(uuid="2647c15f-2aad-47d7-8dee-b2ee1ac4cef6")
176    def test_iot_connection_to_ASUS_RT_AC54U_5G(self):
177        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
178        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
179
180    @test_tracker_info(uuid="99678f66-ddf1-454d-87e4-e55177ec380d")
181    def test_iot_connection_to_ASUS_RT_N56U_2G(self):
182        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
183        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
184
185    @test_tracker_info(uuid="4dd75e81-9a8e-44fd-9449-09f5ab8a63c3")
186    def test_iot_connection_to_ASUS_RT_N56U_5G(self):
187        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
188        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
189
190    @test_tracker_info(uuid="315397ce-50d5-4abf-a11c-1abcaef832d3")
191    def test_iot_connection_to_BELKIN_F9K1002v1_2G(self):
192        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
193        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
194
195    @test_tracker_info(uuid="05ba464a-b1ef-4ac1-a32f-c919ec4aa1dd")
196    def test_iot_connection_to_CISCO_E1200_2G(self):
197        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
198        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
199
200    @test_tracker_info(uuid="04912868-4a47-40ce-877e-4e4c89849557")
201    def test_iot_connection_to_TP_LINK_C2_2G(self):
202        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
203        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
204
205    @test_tracker_info(uuid="53517a21-3802-4185-b8bb-6eaace063a42")
206    def test_iot_connection_to_TP_LINK_C2_5G(self):
207        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
208        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
209
210    @test_tracker_info(uuid="71c08c1c-415d-4da4-a151-feef43fb6ad8")
211    def test_iot_connection_to_ASUS_RT_AC66U_2G(self):
212        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
213        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
214
215    @test_tracker_info(uuid="2322c155-07d1-47c9-bd21-2e358e3df6ee")
216    def test_iot_connection_to_ASUS_RT_AC66U_5G(self):
217        ssid_key = self.current_test_name.replace(self.iot_test_prefix, "")
218        self.connect_to_wifi_network_and_run_iperf(self.ssid_map[ssid_key])
219