Add InjectSimulatedTPMForTest function for testing (#187)

This commit is contained in:
Brandon Weeks 2020-11-19 07:43:19 +01:00 committed by GitHub
parent 6bd14729ac
commit d90962df3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

22
attest/tpm_fake.go Normal file
View File

@ -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
}

View File

@ -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