1# Copyright (c) 2011 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 datetime
6from autotest_lib.client.bin import test
7from autotest_lib.client.common_lib import error, smogcheck_tpm, smogcheck_util
8
9
10class hardware_TPMtspi(test.test):
11    version = 1
12
13    def setup(self):
14        smogcheck_util.enableI2C()
15
16    def _prepareTpmController(self):
17        """Prepare a TpmController instance for use.
18
19        Returns:
20          an operational TpmControler instance, ready to use.
21        """
22        try:
23            return smogcheck_tpm.TpmController()
24        except smogcheck_tpm.SmogcheckError, e:
25            raise error.TestFail('Error creating a TpmController: %s', e)
26
27    def run_once(self):
28        self.tpm_obj = self._prepareTpmController()
29
30        start_time = datetime.datetime.now()
31        try:
32            self.tpm_obj.setupContext()
33            self.tpm_obj.getTpmVersion()
34            self.tpm_obj.runTpmSelfTest()
35
36            # TODO(tgao): uncomment to enable.
37            #self.tpm_obj.takeTpmOwnership()
38
39            # TODO(tgao): uncomment to enable.
40            #self.tpm_obj.clearTpm()
41
42            # TODO(tgao): uncomment to enable.
43            #self.tpm_obj.setTpmActive('status')
44
45            # TODO(tgao): uncomment to enable.
46            #self.tpm_obj.setTpmActive('deactivate')
47
48            # TODO(tgao): uncomment to enable.
49            #self.tpm_obj.setTpmActive('activate')
50
51            # TODO(tgao): uncomment to enable.
52            #self.tpm_obj.setTpmActive('temp')
53
54            # TODO(tgao): uncomment to enable.
55            #self.tpm_obj.setTpmClearable('status')
56
57            # TODO(tgao): uncomment to enable.
58            #self.tpm_obj.setTpmClearable('owner')
59
60            # TODO(tgao): uncomment to enable.
61            #self.tpm_obj.setTpmClearable('force')
62
63        except smogcheck_tpm.SmogcheckError, e:
64            raise error.TestFail('Error: %r' % e)
65        finally:
66            # Close TPM context
67            if self.tpm_obj.closeContext():
68                raise error.TestFail('Error closing tspi context')
69
70        end_time = datetime.datetime.now()
71        smogcheck_util.computeTimeElapsed(end_time, start_time)
72