Debian Codegraph @

tpm2/transport/tcp/tcp_test.go

raw
1 package tcp
2
3 import (
4 "bytes"
5 "errors"
6 "flag"
7 "testing"
8
9 "github.com/google/go-tpm/tpm2"
10 )
11
12 var (
13 cmdAddr = flag.String("cmd_addr", "", "command port (e.g., 'localhost:2321')")
14 platAddr = flag.String("plat_addr", "", "platform port (e.g., 'localhost:2322')")
15 )
16
17 // The tests in this file are skipped unless the flags above are provided.
18 // To run the tests:
19 // Fetch the TPM reference code at https://github.com/trustedcomputinggroup/tpm
20 // Build the simulator per the instructions for your platform
21 // In one shell, run the simulator, e.g., TPMCmd/Simulator/src/tpm2-simulator
22 // In the other, run the tests, e.g.:
23 // go test --cmd_addr localhost:2321 --plat_addr localhost:2322
24
25 // Helper to open the TPM based on command-line flags passed to the test, or skip.
26 func getTPM(t *testing.T, powerOnStartUp bool) *TPM {
27 t.Helper()
28 flag.Parse()
29
30 if *cmdAddr == "" || *platAddr == "" {
31 t.Skipf("TPM simulator not provided, skipping test")
32 }
33
34 tpm, err := Open(Config{
35 CommandAddress: *cmdAddr,
36 PlatformAddress: *platAddr,
37 })
38 if err != nil {
39 t.Fatalf("Open() = %v", err)
40 }
41
42 if err := tpm.PowerOff(); err != nil {
43 t.Fatalf("PowerOff() = %v", err)
44 }
45
46 if powerOnStartUp {
47 if err := tpm.PowerOn(); err != nil {
48 t.Fatalf("PowerOn() = %v", err)
49 }
50 _, err := tpm2.Startup{
51 StartupType: tpm2.TPMSUClear,
52 }.Execute(tpm)
53 if err != nil {
54 t.Fatalf("Startup() = %v", err)
55 }
56 }
57
58 return tpm
59 }
60
61 // Helper to let us easily test that closing the TPM doesn't return any errors.
62 func closeTPM(t *testing.T, tpm *TPM) {
63 t.Helper()
64 if err := tpm.Close(); err != nil {
65 t.Fatalf("Close() = %v", err)
66 }
67 }
68
69 func TestPowerOnOff(t *testing.T) {
70 tpm := getTPM(t, false)
71 defer closeTPM(t, tpm)
72
73 // The simulator starts out powered off, but maybe the simulator was
74 // running before we started this test.
75 // Ensure it is off at the start of the test.
76 if err := tpm.PowerOff(); err != nil {
77 t.Fatalf("PowerOff() = %v", err)
78 }
79
80 // Check that we return the expected error for a powered-off TPM.
81 _, err := tpm2.Startup{
82 StartupType: tpm2.TPMSUClear,
83 }.Execute(tpm)
84 if !errors.Is(err, ErrEmptyResponse) {
85 t.Fatalf("Startup() before PowerOn() = %v, want %v", err, ErrEmptyResponse)
86 }
87
88 // Power on the TPM.
89 if err := tpm.PowerOn(); err != nil {
90 t.Fatalf("PowerOn() = %v", err)
91 }
92
93 // Check that the TPM now reports it needs to be started up.
94 _, err = tpm2.GetRandom{
95 BytesRequested: 16,
96 }.Execute(tpm)
97 if !errors.Is(err, tpm2.TPMRCInitialize) {
98 t.Errorf("GetRandom() = %v, want %v", err, tpm2.TPMRCInitialize)
99 }
100
101 _, err = tpm2.Startup{
102 StartupType: tpm2.TPMSUClear,
103 }.Execute(tpm)
104 if err != nil {
105 t.Errorf("Startup() = %v", err)
106 }
107
108 rnd, err := tpm2.GetRandom{
109 BytesRequested: 16,
110 }.Execute(tpm)
111 if err != nil {
112 t.Errorf("GetRandom() = %v", err)
113 }
114 if bytes.Equal(rnd.RandomBytes.Buffer, make([]byte, 16)) {
115 t.Errorf("GetRandom() = %x, expected random bytes", rnd.RandomBytes.Buffer)
116 }
117 }
118
119 // Helper to perform a restart ("warm reboot") or reset ("cold reboot") of the TPM.
120 func rebootTPM(t *testing.T, tpm *TPM, shutdownType tpm2.TPMSU) {
121 t.Helper()
122
123 _, err := tpm2.Shutdown{
124 ShutdownType: shutdownType,
125 }.Execute(tpm)
126 if err != nil {
127 t.Fatalf("Shutdown() = %v", err)
128 }
129 if err := tpm.Reset(); err != nil {
130 t.Fatalf("Reset() = %v", err)
131 }
132 _, err = tpm2.Startup{
133 StartupType: shutdownType,
134 }.Execute(tpm)
135 if err != nil {
136 t.Fatalf("Startup() = %v", err)
137 }
138 }
139
140 func TestResetRestart(t *testing.T) {
141 tpm := getTPM(t, true)
142 defer closeTPM(t, tpm)
143
144 clock1, err := tpm2.ReadClock{}.Execute(tpm)
145 if err != nil {
146 t.Fatalf("ReadClock() = %v", err)
147 }
148
149 // Perform a TPM Restart (SU_STATE)
150 rebootTPM(t, tpm, tpm2.TPMSUState)
151
152 clock2, err := tpm2.ReadClock{}.Execute(tpm)
153 if err != nil {
154 t.Fatalf("ReadClock() = %v", err)
155 }
156
157 // Perform a TPM Reset (SU_CLEAR)
158 rebootTPM(t, tpm, tpm2.TPMSUClear)
159
160 clock3, err := tpm2.ReadClock{}.Execute(tpm)
161 if err != nil {
162 t.Fatalf("ReadClock() = %v", err)
163 }
164
165 // Restart should increment restartCount and leave resetCount alone.
166 if clock2.CurrentTime.ClockInfo.RestartCount != clock1.CurrentTime.ClockInfo.RestartCount+1 {
167 t.Errorf("restartCount after Restart was %v, want %v",
168 clock2.CurrentTime.ClockInfo.RestartCount,
169 clock1.CurrentTime.ClockInfo.RestartCount+1)
170 }
171 if clock2.CurrentTime.ClockInfo.ResetCount != clock1.CurrentTime.ClockInfo.ResetCount {
172 t.Errorf("resetCount after Restart was %v, want %v",
173 clock2.CurrentTime.ClockInfo.ResetCount,
174 clock1.CurrentTime.ClockInfo.ResetCount)
175 }
176
177 // Reset should reset restartCount to 0 and increment resetCount.
178 if clock3.CurrentTime.ClockInfo.RestartCount != 0 {
179 t.Errorf("restartCount after Reset was %v, want 0",
180 clock3.CurrentTime.ClockInfo.RestartCount)
181 }
182 if clock3.CurrentTime.ClockInfo.ResetCount != clock2.CurrentTime.ClockInfo.ResetCount+1 {
183 t.Errorf("resetCount after Reset was %v, want %v",
184 clock3.CurrentTime.ClockInfo.ResetCount,
185 clock2.CurrentTime.ClockInfo.ResetCount+1)
186 }
187 }