Skip to content

Commit

Permalink
Add InjectSimulatedTPMForTest function for testing (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonweeks authored Nov 19, 2020
1 parent 6bd1472 commit d90962d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions attest/tpm_fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package attest

import (
"io"
)

var (
testLog = []byte{0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x21, 0x0, 0x0, 0x0, 0x53, 0x70, 0x65, 0x63, 0x20, 0x49, 0x44,
0x20, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x30, 0x33, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x2, 0x0, 0x2, 0x1, 0x0, 0x0, 0x0, 0xb, 0x0, 0x20, 0x0, 0x0}
)

type fakeCmdChannel struct {
io.ReadWriteCloser
}

// MeasurementLog implements CommandChannelTPM20.
func (cc *fakeCmdChannel) MeasurementLog() ([]byte, error) {
return testLog, nil
}
10 changes: 10 additions & 0 deletions attest/tpm_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ const (
// This will be initialized if we build with CGO (needed for TPM 1.2 support).
var getTPM12Impl func() (*TPM, error)

// InjectSimulatedTPMForTest returns a fake TPM that interfaces with
// the provided simulated TPM. This method should be used for testing
// only.
func InjectSimulatedTPMForTest(rwc io.ReadWriteCloser) *TPM {
return &TPM{tpm: &wrappedTPM20{
interf: TPMInterfaceCommandChannel,
rwc: &fakeCmdChannel{rwc},
}}
}

func probeSystemTPMs() ([]probedTPM, error) {
var tpms []probedTPM

Expand Down

0 comments on commit d90962d

Please sign in to comment.