blob: 15a1710ee7cafa0333c9c917e03bcbc8ed34b36a [file] [log] [blame]
Bindu Mahadevb270aa22018-08-27 13:34:58 -07001#!/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
Bindu Mahadev28138142018-09-17 15:34:07 -070017import sys
Bindu Mahadevb270aa22018-08-27 13:34:58 -070018import time
19
20import acts.controllers.packet_capture as packet_capture
Bindu Mahadev6b946792018-09-11 16:26:00 -070021import acts.signals as signals
Bindu Mahadevb270aa22018-08-27 13:34:58 -070022import acts.test_utils.wifi.rpm_controller_utils as rutils
Bindu Mahadev28138142018-09-17 15:34:07 -070023import acts.test_utils.wifi.wifi_datastore_utils as dutils
24import acts.test_utils.wifi.wifi_test_utils as wutils
Bindu Mahadevb270aa22018-08-27 13:34:58 -070025
26from acts import asserts
Bindu Mahadev28138142018-09-17 15:34:07 -070027from acts.base_test import BaseTestClass
Bindu Mahadevb270aa22018-08-27 13:34:58 -070028from acts.controllers.ap_lib import hostapd_constants
29from acts.test_decorators import test_tracker_info
30from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest
31
32WifiEnums = wutils.WifiEnums
33
34WAIT_BEFORE_CONNECTION = 1
35SINGLE_BAND = 1
36DUAL_BAND = 2
37
38TIMEOUT = 1
39PING_ADDR = 'www.google.com'
40
Bindu Mahadev28138142018-09-17 15:34:07 -070041
Bindu Mahadevb270aa22018-08-27 13:34:58 -070042class WifiChaosTest(WifiBaseTest):
43 """ Tests for wifi IOT
44
45 Test Bed Requirement:
46 * One Android device
47 * Wi-Fi IOT networks visible to the device
48 """
49
Bindu Mahadev28138142018-09-17 15:34:07 -070050 def __init__(self, configs):
51 BaseTestClass.__init__(self, configs)
52 self.generate_interop_tests()
53
54 def generate_interop_testcase(self, base_test, testcase_name, ssid_dict):
55 """Generates a single test case from the given data.
56
57 Args:
58 base_test: The base test case function to run.
59 testcase_name: The name of the test case.
60 ssid_dict: The information about the network under test.
61 """
62 ssid = testcase_name
63 test_tracker_uuid = ssid_dict[testcase_name]['uuid']
64 hostname = ssid_dict[testcase_name]['host']
65 if not testcase_name.startswith('test_'):
66 testcase_name = 'test_%s' % testcase_name
67 test_case = test_tracker_info(uuid=test_tracker_uuid)(
68 lambda: base_test(ssid, hostname))
69 setattr(self, testcase_name, test_case)
70 self.tests.append(testcase_name)
71
72 def generate_interop_tests(self):
73 for ssid_dict in self.user_params['interop_ssid']:
74 testcase_name = list(ssid_dict)[0]
75 self.generate_interop_testcase(self.interop_base_test,
76 testcase_name, ssid_dict)
77
Bindu Mahadevb270aa22018-08-27 13:34:58 -070078 def setup_class(self):
79 self.dut = self.android_devices[0]
80 wutils.wifi_test_device_init(self.dut)
81
Bindu Mahadevb270aa22018-08-27 13:34:58 -070082 asserts.assert_true(
Bindu Mahadev28138142018-09-17 15:34:07 -070083 self.lock_pcap(),
84 "Could not lock a Packet Capture. Aborting Interop test.")
Bindu Mahadevb270aa22018-08-27 13:34:58 -070085
86 wutils.wifi_toggle_state(self.dut, True)
87
Bindu Mahadev28138142018-09-17 15:34:07 -070088 def lock_pcap(self):
89 """Lock a Packet Capturere to use for the test."""
90
91 # Get list of devices from the datastore.
92 locked_pcap = False
93 devices = dutils.get_devices()
94
95 for device in devices:
96
97 device_name = device['hostname']
98 device_type = device['ap_label']
99 if device_type == 'PCAP'and dutils.lock_device(device_name):
100 host = device['ip_address']
101 self.log.info("Locked Packet Capture device: %s" % device_name)
102 locked_pcap = True
103 break
104
105 elif device_type == 'PCAP':
106 self.log.warning("Failed to lock %s PCAP.")
107
108 if not locked_pcap:
109 return False
110
111 pcap_config = {'ssh_config':{'user':'root'} }
112 pcap_config['ssh_config']['host'] = host
113
114 self.pcap = packet_capture.PacketCapture(pcap_config)
115 return True
116
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700117 def setup_test(self):
118 self.dut.droid.wakeLockAcquireBright()
119 self.dut.droid.wakeUpNow()
120
121 def teardown_test(self):
122 self.dut.droid.wakeLockRelease()
123 self.dut.droid.goToSleepNow()
124 wutils.reset_wifi(self.dut)
125
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700126
127 """Helper Functions"""
128
129 def scan_and_connect_by_id(self, network, net_id):
130 """Scan for network and connect using network id.
131
132 Args:
133 net_id: Integer specifying the network id of the network.
134
135 """
136 ssid = network[WifiEnums.SSID_KEY]
137 wutils.start_wifi_connection_scan_and_ensure_network_found(self.dut,
Bindu Mahadev28138142018-09-17 15:34:07 -0700138 ssid)
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700139 wutils.wifi_connect_by_id(self.dut, net_id)
140
141 def run_ping(self, sec):
142 """Run ping for given number of seconds.
143
144 Args:
145 sec: Time in seconds to run teh ping traffic.
146
147 """
148 self.log.info("Running ping for %d seconds" % sec)
149 result = self.dut.adb.shell("ping -w %d %s" % (sec, PING_ADDR),
Bindu Mahadev28138142018-09-17 15:34:07 -0700150 timeout=sec + 1)
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700151 self.log.debug("Ping Result = %s" % result)
152 if "100% packet loss" in result:
153 raise signals.TestFailure("100% packet loss during ping")
154
Bindu Mahadev6b946792018-09-11 16:26:00 -0700155 def run_connect_disconnect(self, network):
Bindu Mahadev28138142018-09-17 15:34:07 -0700156 """Run connect/disconnect to a given network in loop.
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700157
Bindu Mahadev28138142018-09-17 15:34:07 -0700158 Args:
159 network: dict, network information.
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700160
Bindu Mahadev28138142018-09-17 15:34:07 -0700161 Raises: TestFailure if the network connection fails.
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700162
163 """
Bindu Mahadev28138142018-09-17 15:34:07 -0700164 for attempt in range(1):
165 # TODO:(bmahadev) Change it to 5 or more attempts later.
166 try:
167 begin_time = time.time()
168 ssid = network[WifiEnums.SSID_KEY]
169 net_id = self.dut.droid.wifiAddNetwork(network)
170 asserts.assert_true(net_id != -1, "Add network %s failed" % network)
171 self.log.info("Connecting to %s" % ssid)
172 self.scan_and_connect_by_id(network, net_id)
173 self.run_ping(1)
174 wutils.wifi_forget_network(self.dut, ssid)
175 time.sleep(WAIT_BEFORE_CONNECTION)
176 except:
177 self.log.error("Connection to %s network failed on the %d "
178 "attempt." % (ssid, attempt))
179 self.dut.take_bug_report(ssid, begin_time)
180 self.dut.cat_adb_log(ssid, begin_time)
181 raise signals.TestFailure("Failed to connect to %s" % ssid)
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700182
Bindu Mahadev28138142018-09-17 15:34:07 -0700183 def interop_base_test(self, ssid, hostname):
184 """Base test for all the connect-disconnect interop tests.
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700185
Bindu Mahadev28138142018-09-17 15:34:07 -0700186 Args:
187 ssid: string, SSID of the network to connect to.
188 hostname: string, hostname of the AP.
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700189
Bindu Mahadev28138142018-09-17 15:34:07 -0700190 Steps:
191 1. Lock AP in datstore.
192 2. Turn on AP on the rpm switch.
193 3. Run connect-disconnect in loop.
194 4. Turn off AP on the rpm switch.
195 5. Unlock AP in datastore.
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700196
Bindu Mahadev28138142018-09-17 15:34:07 -0700197 """
198 network = {}
199 network['password'] = 'password'
200 network['SSID'] = ssid
201 wutils.reset_wifi(self.dut)
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700202
Bindu Mahadev28138142018-09-17 15:34:07 -0700203 # Lock AP in datastore.
204 self.log.info("Lock AP in datastore")
205 if not dutils.lock_device(hostname):
206 self.log.warning("Failed to lock %s AP. Unlock AP in datastore"
207 " and try again.")
208 raise signals.TestFailure("Failed to lock AP")
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700209
Bindu Mahadev28138142018-09-17 15:34:07 -0700210 ap_info = dutils.show_device(hostname)
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700211
Bindu Mahadev28138142018-09-17 15:34:07 -0700212 band = SINGLE_BAND
213 if ('ssid_2g' in ap_info) and ('ssid_5g' in ap_info):
214 band = DUAL_BAND
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700215
Bindu Mahadev28138142018-09-17 15:34:07 -0700216 # Get AP RPM attributes and Turn ON AP.
217 rpm_ip = ap_info['rpm_ip']
218 rpm_port = ap_info['rpm_port']
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700219
Bindu Mahadev28138142018-09-17 15:34:07 -0700220 rutils.turn_on_ap(self.pcap, ssid, rpm_port, rpm_ip=rpm_ip)
221 self.log.info("Finished turning ON AP.")
Bindu Mahadev6b946792018-09-11 16:26:00 -0700222
Bindu Mahadev28138142018-09-17 15:34:07 -0700223 self.run_connect_disconnect(network)
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700224
Bindu Mahadev28138142018-09-17 15:34:07 -0700225 # Un-lock only if it's a single band AP or we are running the last band.
226 if (band == SINGLE_BAND) or (
227 band == DUAL_BAND and hostapd_constants.BAND_5G in \
228 sys._getframe().f_code.co_name):
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700229
230 # Un-Lock AP in datastore.
231 self.log.debug("Un-lock AP in datastore")
Bindu Mahadev28138142018-09-17 15:34:07 -0700232 if not dutils.unlock_device(hostname):
Bindu Mahadevb270aa22018-08-27 13:34:58 -0700233 self.log.warning("Failed to unlock %s AP. Check AP in datastore.")
234
235 # Turn OFF AP from the RPM port.
236 rutils.turn_off_ap(rpm_port, rpm_ip)