OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 '''Unittest wrapper for tpm_handler.py.''' |
| 7 |
| 8 import os |
| 9 import shutil |
| 10 import subprocess |
| 11 import tempfile |
| 12 import unittest |
| 13 |
| 14 import tpm_handler |
| 15 |
| 16 CONF_FILE_TEMPLATE = ''' |
| 17 # Trousers daemon, which talks to the TPM (or the TPM emulator). |
| 18 |
| 19 stop on starting halt or starting reboot |
| 20 |
| 21 pre-start script |
| 22 tpmc clear || logger "tpmc clear: status $?" |
| 23 tpmc enable || logger "tpmc enable: status $?" |
| 24 tpmc act || logger "tpmc act: status $?" |
| 25 %s tpmc block || logger "tpmc block: status $?" |
| 26 %s tpmc pplock || logger "tpmc pplock: status $?" |
| 27 |
| 28 fi |
| 29 end script |
| 30 |
| 31 exec /usr/sbin/tcsd |
| 32 ''' |
| 33 |
| 34 class CrosIfMock(object): |
| 35 MOCKED_COMMANDS = { |
| 36 'initctl status tcsd': 'tcsd start/running, process 3757', |
| 37 'tpmc read 0x1007 0xa': '1 0 1 1 1 0 0 0 0 0', |
| 38 'tpmc read 0x1008 0xd': '1 4c 57 52 47 1 2 1 0 0 0 0 0', |
| 39 'stop tcsd': 'tcsd stop/waiting', |
| 40 'tpmc write 0x1008 0x01 0x4c 0x57 0x52 0x47 0x01 0x00 ' + |
| 41 '0x01 0x00 0x00 0x00 0x00 0x00' : '', |
| 42 } |
| 43 |
| 44 def __init__(self): |
| 45 self.last_cmd = None |
| 46 self.state_dir = tempfile.mkdtemp() |
| 47 |
| 48 def run_shell_command_get_output(self, cmd): |
| 49 self.last_cmd = cmd |
| 50 return (self.MOCKED_COMMANDS[cmd],) |
| 51 |
| 52 def run_shell_command(self, cmd): |
| 53 self.last_cmd = cmd |
| 54 assert(cmd in self.MOCKED_COMMANDS) |
| 55 |
| 56 def state_dir_file(self, file_name): |
| 57 return os.path.join(self.state_dir, file_name) |
| 58 |
| 59 def __del__(self): |
| 60 shutil.rmtree(self.state_dir) |
| 61 |
| 62 class TestTpmHandler(unittest.TestCase): |
| 63 def setUp(self): |
| 64 self.tpmh = tpm_handler.TpmHandler() |
| 65 self.tpmh.init(CrosIfMock()) |
| 66 |
| 67 def test_boot_version(self): |
| 68 self.assertEqual(self.tpmh.get_fw_version(), 257) |
| 69 |
| 70 def test_read_kernel_version(self): |
| 71 self.assertEqual(self.tpmh.get_kernel_version(), 513) |
| 72 |
| 73 def test_write_kernel_version(self): |
| 74 self.tpmh.set_kernel_version(1) |
| 75 |
| 76 def test_config_file_handling(self): |
| 77 '''Verify that config file gets modified/restored properly.''' |
| 78 conf_file, conf_file_name = tempfile.mkstemp() |
| 79 os.write(conf_file, CONF_FILE_TEMPLATE % ('', '')) |
| 80 os.close(conf_file) |
| 81 self.tpmh.enable_write_access(conf_file_name) |
| 82 self.assertEqual(open(conf_file_name).read(), CONF_FILE_TEMPLATE % ( |
| 83 tpm_handler.COMMENT_PATTERN, tpm_handler.COMMENT_PATTERN)) |
| 84 self.tpmh.disable_write_access(conf_file_name) |
| 85 self.assertEqual(open(conf_file_name).read(), |
| 86 CONF_FILE_TEMPLATE % ('', '')) |
| 87 os.remove(conf_file_name) |
| 88 |
| 89 if __name__ == '__main__': |
| 90 unittest.main() |
OLD | NEW |