1# Copyright 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
5import logging
6import time
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.bin import test
10from autotest_lib.client.cros.networking.chrome_testing \
11        import chrome_networking_test_context as cntc
12from autotest_lib.client.cros.networking.chrome_testing \
13        import chrome_networking_test_api as cnta
14from autotest_lib.client.common_lib.cros.network \
15         import chrome_net_constants
16
17class network_RoamWifiEndToEnd(test.test):
18    """
19    Tests the following with chrome.networkingPrivate APIs:
20        1. Tests that the DUT can see and connect to the configured AP.
21        2. Tests the DUT can Roam/Failover between networks with same SSID.
22
23    """
24    version = 1
25
26
27    def _find_configured_network(self):
28        """Check if the required network is in the networks_found list.
29
30        @return Service list of the required network; None if network not found
31
32        """
33        networks = self.chrome_networking.get_wifi_networks()
34
35        for service in networks:
36            if service['Name'] == self.SSID:
37                return service
38        return None
39
40
41    def _find_and_connect_to_configured_network(self):
42        """Find all networks in range and connect to the required network."""
43
44        network = self._find_configured_network()
45
46        if network is None:
47            raise error.TestFail('Network with ssid=%s is not found', self.SSID)
48        self.chrome_networking.connect_to_network(network)
49
50
51    def _verify_device_roamed(self):
52        """Verify that the device has roamed between networks.
53
54        @raises error.TestFail if connect to second AP has failed.
55
56        """
57        # Give the DUT some time to roam to the other AP and connect.
58        time.sleep(self.chrome_networking.SHORT_TIMEOUT)
59        required_network_service_list = self._find_configured_network()
60
61        # Check if the network exists and it is in connected state.
62        if required_network_service_list is None:
63            raise error.TestFail('Network with ssid=%s is not found', self.SSID)
64
65        if required_network_service_list['ConnectionState'] != 'Connected':
66            raise error.TestFail(
67                'DUT failed to roam/connect to the second network')
68
69        if required_network_service_list['ConnectionState'] == 'Connected':
70            logging.info('DUT successfully roamed to the other Open network')
71
72
73    def run_once(self, ssid, test):
74        """Run the test.
75
76        @param ssid: SSID of the APs.
77        @param test: Set by the server test control file depending on the test
78                that is being run.
79
80        """
81        self.SSID = ssid
82
83        with cntc.ChromeNetworkingTestContext() as testing_context:
84            self.chrome_networking = cnta.ChromeNetworkProvider(testing_context)
85            enabled_devices = self.chrome_networking.get_enabled_devices()
86            if (self.chrome_networking.WIFI_DEVICE not in
87                    enabled_devices):
88                self.chrome_networking.enable_network_device(
89                    self.chrome_networking.WIFI_DEVICE)
90
91            self.chrome_networking.scan_for_networks()
92            if test == chrome_net_constants.OPEN_CONNECT:
93                self._find_and_connect_to_configured_network()
94            elif test == chrome_net_constants.OPEN_ROAM:
95                self._verify_device_roamed()
96