1# Copyright (c) 2013 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
7from autotest_lib.client.common_lib import utils
8from autotest_lib.client.common_lib.cros.network import xmlrpc_datatypes
9from autotest_lib.server import test
10from autotest_lib.server.cros.network import wifi_test_context_manager
11
12class WiFiCellTestBase(test.test):
13    """An abstract base class for autotests in WiFi cells.
14
15    WiFiCell tests refer to participants in the test as client, router, and
16    server.  The client is just the DUT and the router is a nearby AP which we
17    configure in various ways to test the ability of the client to connect.
18    There is a third entity called a server which is distinct from the autotest
19    server.  In WiFiTests, the server is a host which the client can only talk
20    to over the WiFi network.
21
22    WiFiTests have a notion of the control network vs the WiFi network.  The
23    control network refers to the network between the machine running the
24    autotest server and the various machines involved in the test.  The WiFi
25    network is the subnet(s) formed by WiFi routers between the server and the
26    client.
27
28    """
29
30    @property
31    def context(self):
32        """@return the WiFi context for this test."""
33        return self._wifi_context
34
35
36    def parse_additional_arguments(self, commandline_args, additional_params):
37        """Parse additional arguments for use in test.
38
39        Subclasses should override this method do any other commandline parsing
40        and setting grabbing that they need to do.  For test clarity, do not
41        parse additional settings in the body of run_once.
42
43        @param commandline_args dict of argument key, value pairs.
44        @param additional_params object defined by test control file.
45
46        """
47        pass
48
49
50    def warmup(self, host, raw_cmdline_args, additional_params=None,
51            pcap_as_router=False):
52        """
53        Use the additional_params argument to pass in custom test data from
54        control file to reuse test logic.  This object will be passed down via
55        parse_additional_arguments.
56
57        @param host host object representing the client DUT.
58        @param raw_cmdline_args raw input from autotest.
59        @param additional_params object passed in from control file.
60        @param pcap_as_router True if the test requires the pcap to be
61                configured as a router.
62
63        """
64        cmdline_args = utils.args_to_dict(raw_cmdline_args)
65        logging.info('Running wifi test with commandline arguments: %r',
66                     cmdline_args)
67        self._wifi_context = wifi_test_context_manager.WiFiTestContextManager(
68                self.__class__.__name__,
69                host,
70                cmdline_args,
71                self.debugdir)
72
73        self._wifi_context.setup(pcap_as_router=pcap_as_router)
74        self.parse_additional_arguments(cmdline_args, additional_params)
75
76        msg = '======= WiFi autotest setup complete. Starting test... ======='
77        self._wifi_context.client.shill_debug_log(msg)
78
79
80    def cleanup(self):
81        msg = '======= WiFi autotest complete. Cleaning up... ======='
82        self._wifi_context.client.shill_debug_log(msg)
83        # If we fail during initialization, we might not have a context.
84        if hasattr(self, '_wifi_context'):
85            self._wifi_context.teardown()
86
87
88    def configure_and_connect_to_ap(self, ap_config):
89        """
90        Configure the router as an AP with the given config and connect
91        the DUT to it.
92
93        @param ap_config HostapConfig object.
94
95        @return name of the configured AP
96        """
97        self.context.configure(ap_config)
98        ap_ssid = self.context.router.get_ssid()
99        assoc_params = xmlrpc_datatypes.AssociationParameters(ssid=ap_ssid)
100        self.context.assert_connect_wifi(assoc_params)
101        return ap_ssid
102