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 import error
8from autotest_lib.server.cros.faft.cr50_test import Cr50Test
9
10
11class firmware_Cr50Open(Cr50Test):
12    """Verify cr50 open."""
13    version = 1
14
15    def initialize(self, host, cmdline_args, ccd_open_restricted, full_args):
16        """Initialize the test"""
17        super(firmware_Cr50Open, self).initialize(host, cmdline_args,
18                full_args)
19
20        self.ccd_open_restricted = ccd_open_restricted
21        self.fast_open(enable_testlab=True)
22        self.cr50.send_command('ccd reset')
23        self.cr50.set_ccd_level('lock')
24
25
26    def check_cr50_open(self, dev_mode):
27        """Verify you can't open ccd unless dev mode is enabled.
28
29        Make sure the ability to open ccd corresponds with the device being in
30        dev mode. When the device is in dev mode, open should be accessible from
31        the AP. When the device is in normal mode it shouldn't be accessible.
32        Open will never work from the console.
33
34        Args:
35            dev_mode: bool reflecting whether the device is in dev mode. If
36                    True, the device is in dev mode. If False, the device is in
37                    normal mode.
38        """
39        self.cr50.set_ccd_level('lock')
40        self.cr50.get_ccd_info()
41
42        #Make sure open doesn't work from the console.
43        try:
44            self.cr50.set_ccd_level('open')
45        except error.TestFail, e:
46            # If ccd open is limited, open should fail with access denied
47            #
48            # TODO: move logic to set_ccd_level.
49            if 'Access Denied' in str(e) and self.ccd_open_restricted:
50                logging.info('console ccd open successfully rejected')
51            else:
52                raise
53        else:
54            if self.ccd_open_restricted:
55                raise error.TestFail('Open should not be accessible from the '
56                                     'console')
57
58        self.cr50.set_ccd_level('lock')
59
60        #Make sure open only works from the AP when the device is in dev mode.
61        try:
62            self.ccd_open_from_ap()
63        except error.TestFail, e:
64            logging.info(e)
65            # ccd open should work if the device is in dev mode or ccd open
66            # isn't restricted. If open failed for some reason raise the error.
67            if dev_mode or not self.ccd_open_restricted:
68                raise
69
70
71    def run_once(self):
72        """Check open only works when the device is in dev mode."""
73        self.switcher.reboot_to_mode(to_mode='normal')
74        self.check_cr50_open(False)
75        self.switcher.reboot_to_mode(to_mode='dev')
76        self.check_cr50_open(True)
77