• 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
7import socket
8
9from autotest_lib.client.common_lib.cros import tpm_utils
10from autotest_lib.client.cros.enterprise.network_config import NetworkConfig
11from autotest_lib.server import autotest
12from autotest_lib.server import site_linux_system
13from autotest_lib.server.cros.network import hostap_config
14from autotest_lib.server.cros.network import wifi_cell_test_base
15
16
17class policy_GlobalNetworkSettingsServer(wifi_cell_test_base.WiFiCellTestBase):
18    version = 1
19
20
21    def cleanup(self):
22        """Clear TPM and catch errant socket exceptions."""
23        try:
24            super(policy_GlobalNetworkSettingsServer, self).cleanup()
25        except socket.error as e:
26            # Some of the WiFi components are closed when the DUT reboots,
27            # and a socket error is raised when cleanup tries to close them
28            # again.
29            logging.info(e)
30
31        tpm_utils.ClearTPMIfOwned(self.host)
32        self.host.reboot()
33
34
35    def run_client_test(self, gnc_settings, policy_network_config,
36                        user_network_config):
37        """
38        Run the client side test.
39
40        Pickle the NetworkConfig objects so they can be passed to the client
41        test.
42
43        @param gnc_settings: GlobalNetworkConfiguration policies to enable.
44        @param policy_network_config: NetworkConfig of the policy-defined
45            network.
46        @param user_network_config: NetworkConfig of the user-defined
47            network.
48
49        """
50        client_at = autotest.Autotest(self.host)
51        client_at.run_test(
52            'policy_GlobalNetworkSettings',
53            gnc_settings=gnc_settings,
54            policy_network_pickle=pickle.dumps(policy_network_config),
55            user_network_pickle=pickle.dumps(user_network_config),
56            check_client_result=True)
57
58
59    def run_once(self, host=None, gnc_settings=None):
60        """
61        Set up an AP for a WiFi authentication type then run the client test.
62
63        @param host: A host object representing the DUT.
64        @param gnc_settings: GlobalNetworkConfiguration policies to enable.
65
66        """
67        self.host = host
68
69        # Clear TPM to ensure that client test can enroll device.
70        tpm_utils.ClearTPMIfOwned(self.host)
71
72        self.context.router.require_capabilities(
73                [site_linux_system.LinuxSystem.CAPABILITY_MULTI_AP])
74        self.context.router.deconfig()
75
76        # Configure 2 open APs.
77        for ssid, channel in [('Policy_Network', 5), ('User_Network', 149)]:
78            n_mode = hostap_config.HostapConfig.MODE_11N_MIXED
79            ap_config = hostap_config.HostapConfig(channel=channel,
80                                                   mode=n_mode,
81                                                   ssid=ssid)
82            self.context.configure(ap_config, multi_interface=True)
83
84        policy_network_config = NetworkConfig(
85                                    self.context.router.get_ssid(instance=0))
86        user_network_config = NetworkConfig(
87                                  self.context.router.get_ssid(instance=1))
88
89        if gnc_settings.get('AllowOnlyPolicyNetworksToAutoconnect'):
90            policy_network_config.autoconnect = True
91
92        # Run the client test with both the policy and user networks available.
93        self.run_client_test(gnc_settings, policy_network_config,
94                             user_network_config)
95
96        # The AllowOnlyPolicyNetworksToConnectIfAvailable policy behaves
97        # differently depending on what networks are available. For this test,
98        # take down the policy network and run the client test again.
99        if gnc_settings.get('AllowOnlyPolicyNetworksToConnectIfAvailable'):
100            self.context.router.deconfig_aps(instance=0)
101
102            self.run_client_test(gnc_settings, policy_network_config,
103                                 user_network_config)
104
105        self.context.router.deconfig()
106