Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 1 | #!/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 Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 17 | import sys |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 18 | import time |
| 19 | |
| 20 | import acts.controllers.packet_capture as packet_capture |
Bindu Mahadev | 6b94679 | 2018-09-11 16:26:00 -0700 | [diff] [blame] | 21 | import acts.signals as signals |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 22 | import acts.test_utils.wifi.rpm_controller_utils as rutils |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 23 | import acts.test_utils.wifi.wifi_datastore_utils as dutils |
| 24 | import acts.test_utils.wifi.wifi_test_utils as wutils |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 25 | |
| 26 | from acts import asserts |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 27 | from acts.base_test import BaseTestClass |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 28 | from acts.controllers.ap_lib import hostapd_constants |
| 29 | from acts.test_decorators import test_tracker_info |
| 30 | from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest |
| 31 | |
| 32 | WifiEnums = wutils.WifiEnums |
| 33 | |
| 34 | WAIT_BEFORE_CONNECTION = 1 |
| 35 | SINGLE_BAND = 1 |
| 36 | DUAL_BAND = 2 |
| 37 | |
| 38 | TIMEOUT = 1 |
| 39 | PING_ADDR = 'www.google.com' |
| 40 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 41 | |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 42 | class 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 Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 50 | 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 Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 78 | def setup_class(self): |
| 79 | self.dut = self.android_devices[0] |
| 80 | wutils.wifi_test_device_init(self.dut) |
| 81 | |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 82 | asserts.assert_true( |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 83 | self.lock_pcap(), |
| 84 | "Could not lock a Packet Capture. Aborting Interop test.") |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 85 | |
| 86 | wutils.wifi_toggle_state(self.dut, True) |
| 87 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 88 | 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 Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 117 | 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 Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 126 | |
| 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 Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 138 | ssid) |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 139 | 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 Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 150 | timeout=sec + 1) |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 151 | 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 Mahadev | 6b94679 | 2018-09-11 16:26:00 -0700 | [diff] [blame] | 155 | def run_connect_disconnect(self, network): |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 156 | """Run connect/disconnect to a given network in loop. |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 157 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 158 | Args: |
| 159 | network: dict, network information. |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 160 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 161 | Raises: TestFailure if the network connection fails. |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 162 | |
| 163 | """ |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 164 | 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 Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 182 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 183 | def interop_base_test(self, ssid, hostname): |
| 184 | """Base test for all the connect-disconnect interop tests. |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 185 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 186 | Args: |
| 187 | ssid: string, SSID of the network to connect to. |
| 188 | hostname: string, hostname of the AP. |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 189 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 190 | 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 Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 196 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 197 | """ |
| 198 | network = {} |
| 199 | network['password'] = 'password' |
| 200 | network['SSID'] = ssid |
| 201 | wutils.reset_wifi(self.dut) |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 202 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 203 | # 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 Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 209 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 210 | ap_info = dutils.show_device(hostname) |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 211 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 212 | band = SINGLE_BAND |
| 213 | if ('ssid_2g' in ap_info) and ('ssid_5g' in ap_info): |
| 214 | band = DUAL_BAND |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 215 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 216 | # Get AP RPM attributes and Turn ON AP. |
| 217 | rpm_ip = ap_info['rpm_ip'] |
| 218 | rpm_port = ap_info['rpm_port'] |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 219 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 220 | rutils.turn_on_ap(self.pcap, ssid, rpm_port, rpm_ip=rpm_ip) |
| 221 | self.log.info("Finished turning ON AP.") |
Bindu Mahadev | 6b94679 | 2018-09-11 16:26:00 -0700 | [diff] [blame] | 222 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 223 | self.run_connect_disconnect(network) |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 224 | |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 225 | # 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 Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 229 | |
| 230 | # Un-Lock AP in datastore. |
| 231 | self.log.debug("Un-lock AP in datastore") |
Bindu Mahadev | 2813814 | 2018-09-17 15:34:07 -0700 | [diff] [blame^] | 232 | if not dutils.unlock_device(hostname): |
Bindu Mahadev | b270aa2 | 2018-08-27 13:34:58 -0700 | [diff] [blame] | 233 | 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) |