File: helper.go

package info (click to toggle)
golang-github-google-go-tpm 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: makefile: 13
file content (59 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Package testhelper provides some helper code for TPM transport tests.
package testhelper

import (
	"bytes"
	"encoding/binary"
	"errors"
	"testing"

	"github.com/google/go-tpm/tpm2"
	"github.com/google/go-tpm/tpm2/transport"
)

// RunTest checks that the connection to the given TPM seems to be working.
func RunTest(t *testing.T, skipErrs []error, tpmOpener func() (transport.TPMCloser, error)) {
	tpm, err := tpmOpener()
	for _, skipErr := range skipErrs {
		if errors.Is(err, skipErr) {
			t.Skipf("%v", err)
		}
	}
	if err != nil {
		t.Fatalf("Failed to open TPM: %v", err)
	}
	defer func(tpm transport.TPMCloser) {
		if err := tpm.Close(); err != nil {
			t.Fatalf("tpm.Close() = %v", err)
		}
	}(tpm)

	// Ping the TPM to ask it what the manufacturer is, as a basic consistency check.
	cap, err := tpm2.GetCapability{
		Capability:    tpm2.TPMCapTPMProperties,
		Property:      uint32(tpm2.TPMPTManufacturer),
		PropertyCount: 1,
	}.Execute(tpm)

	// We might run into one of the known "skip if this error" cases.
	for _, skipErr := range skipErrs {
		if errors.Is(err, skipErr) {
			t.Skipf("%v", err)
		}
	}
	if err != nil {
		t.Fatalf("GetCapability() = %v", err)
	}
	props, err := cap.CapabilityData.Data.TPMProperties()
	if err != nil {
		t.Fatalf("cap.TPMProperties() = %v", err)
	}
	if len(props.TPMProperty) != 1 {
		t.Fatalf("GetCapability() = %v properties, want 1", len(props.TPMProperty))
	}

	var idBuf bytes.Buffer
	idBuf.Grow(4)
	binary.Write(&idBuf, binary.BigEndian, props.TPMProperty[0].Value)
	t.Logf("Manufacturer ID: %q", idBuf.String())
}