• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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 error
8from autotest_lib.client.cros.enterprise import enterprise_policy_base
9from autotest_lib.client.cros.enterprise import enterprise_network_api
10from autotest_lib.client.cros.enterprise import network_config
11
12
13class policy_WiFiAutoconnect(
14        enterprise_policy_base.EnterprisePolicyTest):
15    version = 1
16
17
18    def cleanup(self):
19        """Re-enable ethernet after the test is completed."""
20        if hasattr(self, 'net_api'):
21            self.net_api.chrome_net_context.enable_network_device('Ethernet')
22        super(policy_WiFiAutoconnect, self).cleanup()
23
24
25    def test_wifi_autoconnect(self, ssid, autoconnect):
26        """
27        Verifies the behavior of the autoconnect portion of network policy.
28
29        @param ssid: Service set identifier for wireless local area network.
30        @param autoconnect: Whether policy autoconnects to network.
31
32        @raises error.TestFail: When device's behavior does not match policy.
33
34        """
35        if not autoconnect:
36            if self.net_api.is_network_connected(ssid):
37                raise error.TestFail('Device autoconnected to %s, but '
38                                     'autoconnect = False.'
39                                     % ssid)
40            self.net_api.connect_to_network(ssid)
41
42        if not self.net_api.is_network_connected(ssid):
43            raise error.TestFail('Did not connect to network (%s)' % ssid)
44
45
46    def run_once(self, autoconnect=False, ssid=''):
47        """
48        Setup and run the test configured for the specified test case.
49
50        @param ssid: Service set identifier for wireless local area network.
51        @param autoconnect: Value of "AutoConnect" setting. Options are True,
52                            False, or None
53
54        """
55        network = network_config.NetworkConfig(ssid,
56                                               autoconnect=autoconnect)
57
58        self.setup_case(
59            user_policies={
60                'OpenNetworkConfiguration': network.policy()
61            },
62            extension_paths=[
63                enterprise_network_api.NETWORK_TEST_EXTENSION_PATH
64            ]
65        )
66
67        self.net_api = enterprise_network_api.\
68                ChromeEnterpriseNetworkContext(self.cr)
69
70        network_available = self.net_api.is_network_in_range(
71                network.ssid,
72                wait_time=self.net_api.SHORT_TIMEOUT)
73        if not network_available:
74            raise error.TestError('SSID %s not available within %s seconds'
75                                  % (network.ssid, self.net_api.SHORT_TIMEOUT))
76
77        # Disable ethernet so device will default to WiFi
78        self.net_api.disable_network_device('Ethernet')
79
80        self.test_wifi_autoconnect(ssid, autoconnect)
81