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.cros import tpm_utils
8from autotest_lib.server.cros.update_engine import update_engine_test
9
10
11class policy_AUServer(update_engine_test.UpdateEngineTest):
12    """
13    This server test is used just to get the URL of the payload to use. It
14    will then call into a client side test to test different things in
15    the Omaha response.
16    """
17    version = 1
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        super(policy_AUServer, self).cleanup()
31        self.clear_tpm_if_owned()
32        self._host.reboot()
33
34
35    def run_once(self, client_test, case, full_payload=True,
36                 job_repo_url=None, running_at_desk=False):
37        """
38        Starting point of this test.
39
40        Note: base class sets host as self._host.
41
42        @param client_test: the name of the Client test to run.
43        @param case: the case to run for the given Client test.
44        @param full_payload: whether the update should be full or incremental.
45        @param job_repo_url: url provided at runtime (or passed in locally
46                             when running at workstation).
47        @param running_at_desk: indicates test is run from a workstation.
48
49        """
50        self._job_repo_url = job_repo_url
51
52        # Clear TPM to ensure that client test can enroll device.
53        self.clear_tpm_if_owned()
54
55        # Figure out the payload to use for the current build.
56        payload = self._get_payload_url(full_payload=full_payload)
57        image_url = self._stage_payload_by_uri(payload)
58        file_info = self._get_staged_file_info(image_url)
59
60        if running_at_desk:
61            image_url = self._copy_payload_to_public_bucket(payload)
62            logging.info('We are running from a workstation. Putting URL on a '
63                         'public location: %s', image_url)
64
65        logging.info('url: %s', image_url)
66        logging.info('file_info: %s', file_info)
67
68        self._run_client_test_and_check_result(client_test,
69                                               case=case,
70                                               image_url=image_url,
71                                               image_size=file_info['size'],
72                                               sha256=file_info['sha256'])
73