Skip to content

Commit

Permalink
testutil: Add ResetTPMSimulatorNoStartup to TPMSimulatorTest suite
Browse files Browse the repository at this point in the history
This is like the existing ResetTPMSimulator except it omits the
TPM2_Startup command after resetting the simulator.
  • Loading branch information
chrisccoulson committed Jul 31, 2024
1 parent 82ffe76 commit 8b41c43
Showing 3 changed files with 50 additions and 10 deletions.
23 changes: 16 additions & 7 deletions testutil/suites.go
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions testutil/suites_test.go
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 18 additions & 3 deletions testutil/tpm.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 8b41c43

Please sign in to comment.