From 8b41c438e15d41ac178c37db42a8f110916ef714 Mon Sep 17 00:00:00 2001 From: Chris Coulson Date: Wed, 31 Jul 2024 10:12:53 +0100 Subject: [PATCH] testutil: Add ResetTPMSimulatorNoStartup to TPMSimulatorTest suite This is like the existing ResetTPMSimulator except it omits the TPM2_Startup command after resetting the simulator. --- testutil/suites.go | 23 ++++++++++++++++------- testutil/suites_test.go | 16 ++++++++++++++++ testutil/tpm.go | 21 ++++++++++++++++++--- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/testutil/suites.go b/testutil/suites.go index 75b784b..d2ea09d 100644 --- a/testutil/suites.go +++ b/testutil/suites.go @@ -473,19 +473,28 @@ func (b *TPMSimulatorTest) Mssim(c *C) *mssim.Transport { return transport.(*mssim.Transport) } -// ResetTPMSimulator issues a Shutdown -> Reset -> Startup cycle of the TPM simulator -// and causes the test to fail if it is not successful. +// ResetTPMSimulator issues a Shutdown(Clear) -> Reset -> Startup(Clear) cycle of the TPM +// simulator and causes the test to fail if it is not successful. func (b *TPMSimulatorTest) ResetTPMSimulator(c *C) { b.Transport.disableCommandLogging = true defer func() { b.Transport.disableCommandLogging = false }() - c.Check(resetTPMSimulator(b.TPM, b.Mssim(c)), IsNil) + c.Check(resetTPMSimulator(b.TPM, b.Mssim(c), true), IsNil) +} + +// ResetTPMSimulatorNoStartup issues a Shutdown(Clear) -> Reset cycle of the TPM simulator +// and causes the test to fail if it is not successful. +func (b *TPMSimulatorTest) ResetTPMSimulatorNoStartup(c *C) { + b.Transport.disableCommandLogging = true + defer func() { b.Transport.disableCommandLogging = false }() + + c.Check(resetTPMSimulator(b.TPM, b.Mssim(c), false), IsNil) } -// ResetAndClearTPMSimulatorUsingPlatformHierarchy issues a Shutdown -> Reset -> -// Startup cycle of the TPM simulator which ensures that the platform hierarchy is -// enabled, and then enables the TPM2_Clear command and clears the TPM using the -// platform hierarchy. +// ResetAndClearTPMSimulatorUsingPlatformHierarchy issues a Shutdown(Clear) -> Reset -> +// Startup(Clear) cycle of the TPM simulator which ensures that the platform hierarchy is +// enabled, and then enables the TPM2_Clear command and clears the TPM using the platform +// hierarchy. func (b *TPMSimulatorTest) ResetAndClearTPMSimulatorUsingPlatformHierarchy(c *C) { b.ResetTPMSimulator(c) b.ClearTPMUsingPlatformHierarchy(c) diff --git a/testutil/suites_test.go b/testutil/suites_test.go index 5a48283..e101453 100644 --- a/testutil/suites_test.go +++ b/testutil/suites_test.go @@ -10,6 +10,7 @@ import ( . "gopkg.in/check.v1" "github.com/canonical/go-tpm2" + "github.com/canonical/go-tpm2/internal/testutil" internal_testutil "github.com/canonical/go-tpm2/internal/testutil" "github.com/canonical/go-tpm2/mssim" "github.com/canonical/go-tpm2/mu" @@ -707,6 +708,21 @@ func (s *tpmSimulatorTestSuiteProper) TestResetTPMSimulator(c *C) { c.Check(currentTime.ClockInfo.ResetCount, Equals, origCurrentTime.ClockInfo.ResetCount+1) } +func (s *tpmSimulatorTestSuiteProper) TestResetTPMSimulatorNoStartup(c *C) { + origCurrentTime, err := s.TPM.ReadClock() + c.Assert(err, IsNil) + + s.ResetTPMSimulatorNoStartup(c) + + _, err = s.TPM.ReadClock() + c.Check(tpm2.IsTPMError(err, tpm2.ErrorInitialize, tpm2.CommandReadClock), testutil.IsTrue) + c.Check(s.TPM.Startup(tpm2.StartupClear), IsNil) + + currentTime, err := s.TPM.ReadClock() + c.Assert(err, IsNil) + c.Check(currentTime.ClockInfo.ResetCount, Equals, origCurrentTime.ClockInfo.ResetCount+1) +} + func (s *tpmSimulatorTestSuiteProper) TestResetAndClearTPMSimulatorUsingPlatformHierarchy(c *C) { s.ResetTPMSimulator(c) // Increment reset count so we can detect the clea c.Check(s.TPM.ClearControl(s.TPM.PlatformHandleContext(), true, nil), IsNil) diff --git a/testutil/tpm.go b/testutil/tpm.go index f909bd8..b499008 100644 --- a/testutil/tpm.go +++ b/testutil/tpm.go @@ -1011,23 +1011,38 @@ func ClearTPMUsingPlatformHierarchyT(t *testing.T, tpm *tpm2.TPMContext) { } } -func resetTPMSimulator(tpm *tpm2.TPMContext, transport *mssim.Transport) error { +func resetTPMSimulator(tpm *tpm2.TPMContext, transport *mssim.Transport, startup bool) error { if err := tpm.Shutdown(tpm2.StartupClear); err != nil { return err } if err := transport.Reset(); err != nil { return fmt.Errorf("resetting the simulator failed: %v", err) } + if !startup { + return nil + } return tpm.Startup(tpm2.StartupClear) } -// ResetTPMSimulatorT issues a Shutdown -> Reset -> Startup cycle of the TPM simulator. +// ResetTPMSimulatorT issues a Shutdown(Clear) -> Reset -> Startup(Clear) cycle of the +// TPM simulator. func ResetTPMSimulatorT(t *testing.T, tpm *tpm2.TPMContext, transport *Transport) { mssim, ok := transport.Unwrap().(*mssim.Transport) if !ok { t.Fatal("not a simulator") } - if err := resetTPMSimulator(tpm, mssim); err != nil { + if err := resetTPMSimulator(tpm, mssim, true); err != nil { + t.Fatal(err) + } +} + +// ResetTPMSimulatorNoStartupT issues a Shutdown(Clear) -> Reset cycle of the TPM simulator. +func ResetTPMSimulatorNoStartupT(t *testing.T, tpm *tpm2.TPMContext, transport *Transport) { + mssim, ok := transport.Unwrap().(*mssim.Transport) + if !ok { + t.Fatal("not a simulator") + } + if err := resetTPMSimulator(tpm, mssim, false); err != nil { t.Fatal(err) } }