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.server import autotest
10from autotest_lib.server import site_linux_system
11from autotest_lib.client.common_lib.cros import tpm_utils
12from autotest_lib.server.cros.network import wifi_cell_test_base
13
14
15class policy_WiFiPrecedenceServer(wifi_cell_test_base.WiFiCellTestBase):
16    version = 1
17
18
19    def clear_tpm_if_owned(self):
20        """Clear the TPM only if device is already owned."""
21        tpm_status = tpm_utils.TPMStatus(self.host)
22        logging.info('TPM status: %s', tpm_status)
23        if tpm_status['Owned']:
24            logging.info('Clearing TPM because this device is owned.')
25            tpm_utils.ClearTPMOwnerRequest(self.host)
26
27
28    def cleanup(self):
29        """Cleanup for this test."""
30        try:
31            super(policy_WiFiPrecedenceServer, self).cleanup()
32        except socket.error as e:
33            # Some of the WiFi components are closed when the DUT reboots,
34            # and a socket error is raised when cleanup tries to close them
35            # again.
36            logging.info(e)
37
38        if self.test == 'device_vs_user':
39            self.clear_tpm_if_owned()
40            self.host.reboot()
41
42
43    def run_once(self, host=None, ap_configs=None, network1_config=None,
44                 network2_config=None, precedence=None, test=None):
45        """
46        Set up the APs then run the client side tests.
47
48        Clears the TPM because because the client test needs to enroll.
49
50        @param host: A host object representing the DUT.
51        @param ap_configs: List containing HostapConfig objects to setup APs.
52        @param network1_config: NetworkConfig object for the client-side
53            configuration of network1.
54        @param network1_config: NetworkConfig object for the client-side
55            configuration of network2.
56        @param precedence: One of 1 or 2: which of the APs the
57            DUT should connect to.
58
59        """
60        self.context.router.require_capabilities(
61                [site_linux_system.LinuxSystem.CAPABILITY_MULTI_AP])
62        self.context.router.deconfig()
63        for ap_config in ap_configs:
64            self.context.configure(ap_config, multi_interface=True)
65
66        self.host = host
67        self.test = test
68
69        # Clear TPM to ensure that client test can enroll device.
70        if self.test == 'device_vs_user':
71            self.clear_tpm_if_owned()
72
73        client_at = autotest.Autotest(self.host)
74
75        client_at.run_test(
76                'policy_WiFiPrecedence',
77                # The config objects must be pickled before they can be
78                # passed to the client test.
79                network1_pickle=pickle.dumps(network1_config),
80                network2_pickle=pickle.dumps(network2_config),
81                precedence=precedence,
82                test=self.test,
83                check_client_result=True)
84
85        self.context.router.deconfig()
86