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