Home | History | Annotate | Download | only in brillo_WifiInterfaceTest
      1 # Copyright 2015 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 import common
      8 from autotest_lib.client.common_lib import error
      9 from autotest_lib.client.common_lib.cros.network import iw_runner
     10 from autotest_lib.server import test
     11 
     12 
     13 class brillo_WifiInterfaceTest(test.test):
     14     """Verify that a Brillo device has its wifi properly configured."""
     15     version = 1
     16 
     17     def get_ifconfig_dict(self, ifconfig_output):
     18         """Convert output of ifconfig into a dictionary.
     19 
     20         @param ifconfig_output: List of ifconfig output lines.
     21 
     22         @return Dictionary mapping interface names (e.g. 'wlan0') to their list
     23                 of stripped output lines.
     24         """
     25         curr_iface = None
     26         ifconfig_dict = {}
     27         for line in ifconfig_output:
     28             if curr_iface is None:
     29                 curr_iface, line = line.split(None, 1)
     30                 ifconfig_dict[curr_iface] = []
     31 
     32             line = line.strip()
     33             if line:
     34                 ifconfig_dict[curr_iface].append(line)
     35             else:
     36                 curr_iface = None
     37 
     38         return ifconfig_dict
     39 
     40 
     41     def run_once(self, host=None, wifi_iface=None, wifi_ssid=None):
     42         """Check that a given wifi interface is properly configured.
     43 
     44         @param host: a host object representing the DUT.
     45         @param wifi_iface: Name of the wifi interface to test; None means we'll
     46                            try to detect at least one that works.
     47         @param wifi_ssid: Name of the SSID we want the interface to be
     48                           connected to; None means any.
     49 
     50         @raise TestFail: The test failed.
     51         """
     52         err_iface = ('No interface is' if wifi_iface is None
     53                       else 'Interface %s is not' % wifi_iface)
     54 
     55         # First check link status and SSID.
     56         iw = iw_runner.IwRunner(remote_host=host)
     57         active_ifaces = []
     58         try:
     59             iw_ifaces = [iface_tuple.if_name
     60                          for iface_tuple in iw.list_interfaces()]
     61             if wifi_iface is not None:
     62                 if wifi_iface not in iw_ifaces:
     63                     raise error.TestFail(
     64                             'Interface %s not listed by iw' % wifi_iface)
     65                 test_ifaces = [wifi_iface]
     66             else:
     67                 test_ifaces = iw_ifaces
     68 
     69             for iface in test_ifaces:
     70                 iface_ssid = iw.get_link_value(iface, 'SSID')
     71                 if (iface_ssid is not None and
     72                     (wifi_ssid is None or iface_ssid == wifi_ssid)):
     73                     active_ifaces.append(iface)
     74         except error.AutoservRunError:
     75             raise error.TestFail('Failed to run iw')
     76 
     77         if not active_ifaces:
     78             err_ssid = 'any SSID' if wifi_ssid is None else 'SSID ' + wifi_ssid
     79             raise error.TestFail('%s connected to %s' % (err_iface, err_ssid))
     80 
     81         logging.info('Active wifi interfaces: %s', ', '.join(active_ifaces))
     82 
     83         # Then check IPv4 connectivity.
     84         try:
     85             ifconfig_output = host.run_output('ifconfig').splitlines()
     86         except error.AutoservRunError:
     87             raise error.TestFail('Failed to run ifconfig')
     88 
     89         ifconfig_dict = self.get_ifconfig_dict(ifconfig_output)
     90         connected_ifaces = [iface for iface in active_ifaces
     91                             if any(['inet addr:' in line
     92                                     for line in ifconfig_dict.get(iface, [])])]
     93         if not connected_ifaces:
     94             raise error.TestFail('%s IPv4 connected' % err_iface)
     95 
     96         logging.info('IPv4 connected wifi interfaces: %s',
     97                      ', '.join(connected_ifaces))
     98