1# Copyright (c) 2010,2013 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
6from autotest_lib.client.bin import test, utils
7from autotest_lib.client.common_lib import error
8
9class kernel_TPMPing(test.test):
10  """See control file for doc"""
11  version = 2
12
13  def run_once(self):
14    tpm_version = utils.system_output("tpm_version")
15    if tpm_version.find("Version Info") == -1:
16      raise error.TestFail("Invalid tpm_version output:\n%s\n" % tpm_version)
17    else:
18      logging.info(tpm_version)
19
20    # This autotest is not compatible with kernel version < 3.8
21    version = utils.system_output('/bin/uname -r').strip()
22    logging.info(version)
23
24    # If the "[gentle shutdown]" string  followed by 'Linux Version'
25    # is missing from the /var/log/messages,
26    # we forgot to carry over an important patch.
27    if version >= '3.8':
28      result = utils.system_output('awk \'/Linux version [0-9]+\./ '
29                                   '{gentle=0;} /\[gentle shutdown\]/ '
30                                   '{gentle=1;} END {print gentle}\' '
31                                   '$(ls -t /var/log/messages* | tac)',
32                                    ignore_status=True)
33
34      # We only care about the most recent instance of the TPM driver message.
35      if result == '0':
36        raise error.TestFail('no \'gentle shutdown\' TPM driver init message')
37    else:
38      logging.info('Bypassing the test as kernel version is < 3.8')
39