Skip to content

Commit

Permalink
Add tpm_cmd and txt_cmd unit testing
Browse files Browse the repository at this point in the history
Signed-off-by: Sae86 <[email protected]>
  • Loading branch information
Sae86 committed Oct 25, 2023
1 parent a7df794 commit 01ef46d
Show file tree
Hide file tree
Showing 11 changed files with 150,542 additions and 5 deletions.
10 changes: 7 additions & 3 deletions chipsec/utilcmd/tpm_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from chipsec.hal import tpm_eventlog
from chipsec.hal import tpm
from chipsec.exceptions import TpmRuntimeError
from chipsec.testcase import ExitCode
from argparse import ArgumentParser


Expand Down Expand Up @@ -88,15 +89,18 @@ def tpm_state(self):
self._tpm.dump_intcap(self.locality)
self._tpm.dump_intenable(self.locality)

def run(self):
def set_up(self):
if self.func != self.tpm_parse:
try:
self._tpm = tpm.TPM(self.cs)
except TpmRuntimeError as msg:
self.logger.log(msg)
return

self.func()

def run(self):
try:
self.func()
except Exception:
self.ExitCode = ExitCode.ERROR

commands = {'tpm': TPMCommand}
10 changes: 8 additions & 2 deletions chipsec/utilcmd/txt_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from argparse import ArgumentParser
from chipsec.command import BaseCommand, toLoad
from chipsec.exceptions import HWAccessViolationError
from chipsec.testcase import ExitCode
import struct


Expand Down Expand Up @@ -66,10 +67,10 @@ def _log_register(self, reg_name):
value = self.cs.read_register(reg_name)
desc = reg_def["desc"]
if reg_def["type"] == "memory":
addr = int(reg_def["address"], 16) + int(reg_def["offset"], 16)
addr = reg_def["address"] + reg_def["offset"]
desc += ", at {:08X}".format(addr)
self.logger.log("[CHIPSEC] {} = 0x{:0{width}X} ({})".format(
reg_name, value, desc, width=int(reg_def['size'], 16) * 2))
reg_name, value, desc, width=reg_def['size'] * 2))

if 'FIELDS' in reg_def:
sorted_fields = sorted(reg_def['FIELDS'].items(), key=lambda field: int(field[1]['bit']))
Expand Down Expand Up @@ -162,5 +163,10 @@ def txt_state(self):
self._log_register("TXT_PCH_DIDVID")
self._log_register("INSMM")

def run(self):
try:
self.func()
except Exception:
self.ExitCode = ExitCode.ERROR

commands = {'txt': TXTCommand}
51 changes: 51 additions & 0 deletions tests/utilcmd/run_chipsec_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# CHIPSEC: Platform Security Assessment Framework
# Copyright (c) 2023, Intel Corporation
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; Version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Contact information:
# [email protected]
#


from unittest.mock import Mock

import chipsec.helper.replay.replayhelper as rph
from chipsec_util import ChipsecUtil, parse_args
import chipsec.logger


def run_chipsec_util(csu: ChipsecUtil, util_replay_file: str) -> bool:
csu._cs.init(csu._platform, csu._pch, csu._helper, not csu._no_driver, csu._load_config, csu._ignore_platform)
if util_replay_file:
csu._helper.config_file = util_replay_file
csu._helper._load()
comm = csu.commands[csu._cmd](csu._cmd_args, cs=csu._cs)
comm.parse_arguments()
comm.set_up()
comm.run()
comm.tear_down()
return comm.ExitCode

def setup_run_destroy_util(init_replay_file: str, util_name: str, util_args: str = "", util_replay_file: str = "") -> bool:
chipsec.logger._logger = Mock()
arg_str = f" {util_args}" if util_args else ""
cli_cmds = f"{util_name}{arg_str}".strip().split(' ')
par = parse_args(cli_cmds)
csu = ChipsecUtil(par, cli_cmds)
replayHelper = rph.ReplayHelper(init_replay_file)
csu._helper = replayHelper
retval = run_chipsec_util(csu, util_replay_file)
chipsec.logger._logger = chipsec.logger.Logger()
return retval
Loading

0 comments on commit 01ef46d

Please sign in to comment.