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