1# Lint as: python2, python3 2# Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import logging 7 8from autotest_lib.client.bin import test 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.cros.networking.chrome_testing \ 11 import chrome_networking_test_context as cntc 12from autotest_lib.client.cros.networking.chrome_testing \ 13 import chrome_networking_test_api as cnta 14from autotest_lib.client.cros.networking.chrome_testing import test_utils 15from collections import namedtuple 16 17NetworkInfo = namedtuple('NetworkInfo', 'name guid connectionState security') 18 19class network_ChromeWifiEndToEnd(test.test): 20 """ 21 Tests the following with chrome.networkingPrivate APIs: 22 1. Tests that the configured wifi networks are seen by Chrome. 23 2. Tests the transitioning between various available networks. 24 3. Tests that the enabling and disabling WiFi works. 25 4. Tests that the DUT autoconnects to previously connected WiFi network. 26 27 """ 28 version = 1 29 30 31 def _extract_wifi_network_info(self, networks_found): 32 """Extract the needed information from the list of networks found 33 via API. 34 35 @param networks_found: Networks found via getVisibleNetworks api. 36 @return Formated list of available wifi networks. 37 38 """ 39 network_list = [] 40 41 for network in networks_found: 42 network = NetworkInfo(name=network['Name'], 43 guid=network['GUID'], 44 connectionState=network['ConnectionState'], 45 security=network['WiFi']['Security']) 46 network_list.append(network) 47 48 return network_list 49 50 51 def _wifi_network_comparison( 52 self, configured_service_name_list, wifi_name_list): 53 """Compare the known wifi SSID's against the SSID's returned via API. 54 55 @param configured_service_name_list: Known SSID's that are configured 56 by the network_WiFi_ChromeEndToEnd test. 57 @param wifi_name_list: List of SSID's returned by the 58 getVisibleNetworks API. 59 @raises error.TestFail if network names do not match. 60 61 """ 62 for name in configured_service_name_list: 63 if name not in wifi_name_list: 64 raise error.TestFail( 65 'Following network does not match: %s' % name) 66 logging.info('Network names match!') 67 68 69 def _enable_disable_network_check( 70 self, original_enabled_networks, new_enabled_networks): 71 """Tests enabling and disabling of WiFi. 72 73 @param original_enabled_networks: List of network devices that were 74 enabled when the test started. 75 @param new_enabled_networks: List of network devices that are now 76 now enabled. 77 @raises error.TestFail if WiFi state is not toggled. 78 79 """ 80 # Make sure we leave the WiFi network device in enabled state before 81 # ending the test. 82 self.chrome_net.enable_network_device(self.chrome_net.WIFI_DEVICE) 83 84 if (self.chrome_net.WIFI_DEVICE in original_enabled_networks and 85 self.chrome_net.WIFI_DEVICE in new_enabled_networks): 86 raise error.TestFail('WiFi was not disabled.') 87 if (self.chrome_net.WIFI_DEVICE not in original_enabled_networks 88 and self.chrome_net.WIFI_DEVICE not in 89 new_enabled_networks): 90 raise error.TestFail('WiFi was not enabled.') 91 logging.info('Enabling / Disabling WiFi works!') 92 93 94 def _connect_to_network(self, network): 95 """Connects to the given network using networkingPrivate API. 96 97 @param network: Namedtuple containing network attributes. 98 99 """ 100 new_network_connect = self.chrome_net._chrome_testing.call_test_function( 101 test_utils.LONG_TIMEOUT, 102 'connectToNetwork', 103 '"' + network.guid +'"') 104 if (new_network_connect['status'] == 105 'chrome-test-call-status-failure'): 106 raise error.TestFail( 107 'Could not connect to %s network. Error returned by ' 108 'chrome.networkingPrivate.startConnect API: %s' % 109 (network.name, new_network_connect['error'])) 110 111 112 def _find_and_transition_wifi_networks_in_range(self): 113 """Verify all WiFi networks in range are displayed.""" 114 known_service_names_in_wifi_cell = [self.SSID_1, self.SSID_2] 115 networks_found_via_api = self.chrome_net.get_wifi_networks() 116 network_list = self._extract_wifi_network_info(networks_found_via_api) 117 logging.info('Networks found via API: %s', networks_found_via_api) 118 119 wifi_names_found_via_api = [] 120 known_wifi_network_details = [] 121 122 for network in network_list: 123 if network.name in known_service_names_in_wifi_cell: 124 known_wifi_network_details.append(network) 125 wifi_names_found_via_api.append(network.name) 126 127 if self.TEST in ('all', 'findVerifyWiFiNetworks'): 128 self._wifi_network_comparison( 129 known_service_names_in_wifi_cell, wifi_names_found_via_api) 130 if self.TEST in ('all', 'transitionWiFiNetworks'): 131 self._transition_wifi_networks(known_wifi_network_details) 132 133 134 def _enable_disable_wifi(self): 135 """Verify that the test is able to enable and disable WiFi.""" 136 original_enabled_networks = self.chrome_net.get_enabled_devices() 137 if self.chrome_net.WIFI_DEVICE in original_enabled_networks: 138 self.chrome_net.disable_network_device(self.chrome_net.WIFI_DEVICE) 139 else: 140 self.chrome_net.enable_network_device(self.chrome_net.WIFI_DEVICE) 141 new_enabled_networks = self.chrome_net.get_enabled_devices() 142 self._enable_disable_network_check( 143 original_enabled_networks, new_enabled_networks) 144 145 146 def _transition_wifi_networks(self, known_wifi_networks): 147 """Verify that the test is able to transition between the two known 148 wifi networks. 149 150 @param known_wifi_networks: List of known wifi networks. 151 @raises error.TestFail if device is not able to transition to a 152 known wifi network. 153 154 """ 155 if not known_wifi_networks: 156 raise error.TestFail('No pre-configured network available for ' 157 'connection/transition.') 158 159 for network in known_wifi_networks: 160 self._connect_to_network(network) 161 logging.info('Successfully transitioned to: %s', network.name) 162 163 164 def _autoconnect_wifi(self): 165 """Test and verify the device autoconnects to WiFi network. 166 167 @raises error.TestFail if device is not able to autoconnect to a 168 previously connected WiFi network. 169 170 """ 171 networks = self._extract_wifi_network_info( \ 172 self.chrome_net.get_wifi_networks()) 173 logging.info('Networks found before connection: %s', networks) 174 network_to_connect = networks.pop() 175 original_network_name = network_to_connect.name 176 177 if network_to_connect.connectionState == 'NotConnected': 178 self._connect_to_network(network_to_connect) 179 logging.info('Connected to WiFi network: %s', 180 network_to_connect.name) 181 182 self.chrome_net.disable_network_device(self.chrome_net.WIFI_DEVICE) 183 self.chrome_net.enable_network_device(self.chrome_net.WIFI_DEVICE) 184 self.chrome_net.scan_for_networks() 185 186 networks = self._extract_wifi_network_info( \ 187 self.chrome_net.get_wifi_networks()) 188 logging.info('Networks found after connection: %s', networks) 189 network_to_connect = networks.pop() 190 191 while network_to_connect.name != original_network_name: 192 if not networks: 193 raise error.TestFail('Previously connected WiFi network not ' 194 'found.') 195 network_to_connect = networks.pop() 196 197 if network_to_connect.connectionState == 'NotConnected': 198 raise error.TestFail('Did not autoconnect to remembered network.') 199 logging.info('Successfully autoconnected to remembered network.') 200 201 202 def run_once(self, ssid_1, ssid_2, test): 203 """Run the test. 204 205 @param ssid_1: SSID of the first AP. 206 @param ssid_2: SSID of the second AP. 207 @param test: Set by the server test control file depending on the test 208 that is being run. 209 210 """ 211 self.SSID_1 = ssid_1 212 self.SSID_2 = ssid_2 213 self.TEST = test 214 215 with cntc.ChromeNetworkingTestContext() as testing_context: 216 self.chrome_net = cnta.ChromeNetworkProvider(testing_context) 217 enabled_devices = self.chrome_net.get_enabled_devices() 218 if (self.chrome_net.WIFI_DEVICE not in enabled_devices): 219 self.chrome_net.enable_network_device( 220 self.chrome_net.WIFI_DEVICE) 221 self.chrome_net.scan_for_networks() 222 223 if test == 'all': 224 self._find_and_transition_wifi_networks_in_range() 225 self._enable_disable_wifi() 226 self._autoconnect_wifi() 227 elif test in ('findVerifyWiFiNetworks', 'transitionWiFiNetworks'): 228 self._find_and_transition_wifi_networks_in_range() 229 elif test == 'enableDisableWiFi': 230 self._enable_disable_wifi() 231 elif test == 'autoconnectWiFi': 232 self._autoconnect_wifi() 233