1# Copyright (c) 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
6import time
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib.cros.network import xmlrpc_datatypes
10from autotest_lib.server import site_linux_system
11from autotest_lib.server.cros.network import hostap_config
12from autotest_lib.server.cros.network import wifi_cell_test_base
13from autotest_lib.server.cros.network import wifi_client
14
15
16class network_WiFi_DisconnectReason(wifi_cell_test_base.WiFiCellTestBase):
17    """Verify the client disconnects from an AP and read (but not verify)
18    the supplicant DisconnectReason for various scenarios."""
19    version = 1
20
21    INITIAL_CHANNEL = 64
22    ALT_CHANNEL = 6
23    CHANNEL_SWITCH_ATTEMPTS = 5
24    CHANNEL_SWITCH_WAIT_TIME_SEC = 3
25
26    def run_once(self, disconnect_trigger, req_capabilities=[]):
27        """Sets up a router, connects to it, pings it and disables it to trigger
28        disconnect."""
29        configuration = hostap_config.HostapConfig(
30                channel=self.INITIAL_CHANNEL,
31                mode=hostap_config.HostapConfig.MODE_11A,
32                spectrum_mgmt_required=True)
33        self.context.router.require_capabilities(req_capabilities)
34        self.context.configure(configuration)
35
36        if site_linux_system.LinuxSystem.CAPABILITY_MULTI_AP in req_capabilities:
37            # prep alternate Access Point
38            alt_ap_config = hostap_config.HostapConfig(
39                    channel=self.ALT_CHANNEL,
40                    mode=hostap_config.HostapConfig.MODE_11N_MIXED)
41            self.context.configure(alt_ap_config, multi_interface=True)
42            alt_assoc_params = xmlrpc_datatypes.AssociationParameters()
43            alt_assoc_params.ssid = self.context.router.get_ssid(instance=1)
44
45        assoc_params = xmlrpc_datatypes.AssociationParameters()
46        assoc_params.ssid = self.context.router.get_ssid(instance=0)
47        self.context.assert_connect_wifi(assoc_params)
48        self.context.assert_ping_from_dut()
49
50        with self.context.client.assert_disconnect_event():
51            if disconnect_trigger == 'AP gone':
52                self.context.router.deconfig()
53            elif disconnect_trigger == 'deauth client':
54                self.context.router.deauth_client(self.context.client.wifi_mac)
55            elif disconnect_trigger == 'AP send channel switch':
56                for attempt in range(self.CHANNEL_SWITCH_ATTEMPTS):
57                    self.context.router.send_management_frame_on_ap(
58                            'channel_switch',
59                            self.ALT_CHANNEL)
60                    time.sleep(self.CHANNEL_SWITCH_WAIT_TIME_SEC)
61            elif disconnect_trigger == 'switch AP':
62                self.context.assert_connect_wifi(alt_assoc_params)
63            elif disconnect_trigger == 'disable client wifi':
64                self.context.client.set_device_enabled(
65                        self.context.client.wifi_if, False)
66            else:
67                raise error.TestError('unknown test mode: %s' % disconnect_trigger)
68            time.sleep(wifi_client.DISCONNECT_WAIT_TIME_SECONDS)
69
70        disconnect_reasons = self.context.client.get_disconnect_reasons()
71        if disconnect_reasons is None or len(disconnect_reasons) == 0:
72            raise error.TestFail('supplicant DisconnectReason not logged')
73        for entry in disconnect_reasons:
74            logging.info("DisconnectReason: %s", entry);
75