• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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 pickle
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros.enterprise import enterprise_policy_base
10from autotest_lib.client.cros.enterprise import enterprise_network_api
11
12
13class policy_WiFiPrecedence(enterprise_policy_base.EnterprisePolicyTest):
14    version = 1
15
16
17    def cleanup(self):
18        """Re-enable ethernet after the test is completed."""
19        if hasattr(self, 'net_api'):
20            self.net_api.chrome_net_context.enable_network_device('Ethernet')
21        super(policy_WiFiPrecedence, self).cleanup()
22
23
24    def test_precedence(self, network1, network2, precedence, test):
25        """
26        Ensure DUT connects to network with higher precedence.
27
28        DUT is given 2 network configs and must connect to the one specified
29        by |precedence|.
30
31        @param network1: A NetworkConfig object representing a network.
32        @param network2: A NetworkConfig object representing a network.
33        @param precedence: The int 1 or 2 that indicates
34            which network should autoconnect.
35        @param test: Name of the test being run.
36
37        @raises error.TestFail: If DUT does not connect to the |precedence|
38            network.
39
40        """
41        if test == 'managed_vs_unmanaged':
42            # Connect and disconnect from the unmanaged network so the network
43            # is a "remembered" network on the DUT.
44            self.net_api.connect_to_network(network2.ssid)
45            self.net_api.disconnect_from_network(network2.ssid)
46
47        # If the networks are the same, ignore the precedence checks.
48        if network1.ssid != network2.ssid:
49            if (self.net_api.is_network_connected(network1.ssid) and
50                    precedence == 2):
51                raise error.TestFail(
52                        'DUT autoconnected to network1, but '
53                        'should have preferred network2.')
54            elif (self.net_api.is_network_connected(network2.ssid) and
55                  precedence == 1):
56                raise error.TestFail(
57                        'DUT autoconnected to network2, but '
58                        'should have preferred network1.')
59
60        if (not self.net_api.is_network_connected(network1.ssid) and
61              not self.net_api.is_network_connected(network2.ssid)):
62            raise error.TestFail('DUT did not connect to a network.')
63
64
65    def run_once(self, network1_pickle=None, network2_pickle=None,
66                 precedence=None, test=None):
67        """
68        Setup and run the test configured for the specified test case.
69
70        @param network_pickle1: A pickled version of a NetworkConfig
71            object representing network1.
72        @param network_pickle2: A pickled version of a NetworkConfig
73            object representing network2.
74        @param precedence: The int 1 or 2 that indicates which network
75            should autoconnect.
76        @param test: Name of the test being run.
77
78        @raises error.TestFail: If DUT does not connect to the |precedence|
79            network.
80
81        """
82        if network1_pickle is None or network2_pickle is None:
83            raise error.TestError('network1 and network2 cannot be None.')
84
85        network1 = pickle.loads(network1_pickle)
86        network2 = pickle.loads(network2_pickle)
87
88        network_policy = network1.policy()
89
90        device_policy = {}
91        if test == 'device_vs_user':
92            device_policy['device_policies'] = {
93                'DeviceOpenNetworkConfiguration': network2.policy()}
94        elif test != 'managed_vs_unmanaged':
95            # Concatenate the network policies.
96            network_policy['NetworkConfigurations'].append(
97                    network2.policy()['NetworkConfigurations'][0])
98
99        self.setup_case(
100            user_policies={
101                'OpenNetworkConfiguration': network_policy
102            },
103            extension_paths=[
104                enterprise_network_api.NETWORK_TEST_EXTENSION_PATH
105            ],
106            enroll=bool(device_policy),
107            **device_policy
108        )
109
110        self.net_api = enterprise_network_api.\
111            ChromeEnterpriseNetworkContext(self.cr)
112        # Disable ethernet so device will default to WiFi.
113        self.net_api.disable_network_device('Ethernet')
114
115        self.test_precedence(network1, network2, precedence, test)
116