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.server.cros.update_engine import update_engine_test
8
9class autoupdate_OmahaResponse(update_engine_test.UpdateEngineTest):
10    """
11    This server test is used just to get the URL of the payload to use. It
12    will then call into a client side test to test different things in
13    the omaha response (e.g switching between two urls, bad hash, bad SHA256).
14    """
15    version = 1
16
17    def cleanup(self):
18        super(autoupdate_OmahaResponse, self).cleanup()
19        self._host.reboot()
20
21    def run_once(self, job_repo_url=None, full_payload=True,
22                 running_at_desk=False, switch_urls=False, bad_sha256=False,
23                 bad_metadata_size=False, test_backoff=False, backoff=False):
24        self._job_repo_url = job_repo_url
25
26        # Reboot DUT if a previous test left update_engine not idle.
27        status = self._get_update_engine_status()
28        if self._UPDATE_STATUS_IDLE != status[self._CURRENT_OP]:
29            self._host.reboot()
30
31        # Figure out the payload to use for the current build.
32        payload = self._get_payload_url(full_payload=full_payload)
33        image_url = self._stage_payload_by_uri(payload)
34        file_info = self._get_staged_file_info(image_url)
35
36        if running_at_desk:
37            image_url = self._copy_payload_to_public_bucket(payload)
38            logging.info('We are running from a workstation. Putting URL on a '
39                         'public location: %s', image_url)
40
41        if switch_urls:
42            self._run_client_test_and_check_result('autoupdate_UrlSwitch',
43                                                   image_url=image_url,
44                                                   image_size=file_info['size'],
45                                                   sha256=file_info['sha256'])
46
47        if bad_sha256 or bad_metadata_size:
48            sha = 'blahblah' if bad_sha256 else file_info['sha256']
49            metadata = 123 if bad_metadata_size else None
50            self._run_client_test_and_check_result('autoupdate_BadMetadata',
51                                                   image_url=image_url,
52                                                   image_size=file_info['size'],
53                                                   sha256=sha,
54                                                   metadata_size=metadata)
55
56        if test_backoff:
57            self._run_client_test_and_check_result('autoupdate_Backoff',
58                                                   image_url=image_url,
59                                                   image_size=file_info['size'],
60                                                   sha256=file_info['sha256'],
61                                                   backoff=backoff)
62