-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathabi_test.go
More file actions
402 lines (390 loc) · 12.3 KB
/
abi_test.go
File metadata and controls
402 lines (390 loc) · 12.3 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package abi
import (
"bytes"
"encoding/binary"
"testing"
pb "github.com/google/go-tdx-guest/proto/tdx"
test "github.com/google/go-tdx-guest/testing/testdata"
"google.golang.org/protobuf/proto"
)
// TODO: Use errors.Is() instead of string comparisons
func TestQuoteToProto(t *testing.T) {
clone := func(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}
tcs := []struct {
name string
rawQuote []byte
wantErr string
}{
{
name: "empty quote",
rawQuote: []byte{},
wantErr: ErrRawQuoteEmpty.Error(),
},
{
name: "v4 quote",
rawQuote: test.RawQuote,
wantErr: "",
},
{
name: "v5 quote",
rawQuote: test.RawQuoteV5,
wantErr: "",
},
{
name: "v5 quote too small",
rawQuote: test.RawQuoteV5[:100],
wantErr: "raw quote size is 100 bytes, a V5 TDX quote should have size a minimum size of 1026 bytes", // QuoteMinSizeV5
},
{
name: "v5 quote unsupported body type",
rawQuote: func() []byte {
q := clone(test.RawQuoteV5)
q[quoteBodyStart] = 1 // TdQuoteBodyType = 1, little-endian uint16
q[quoteBodyStart+1] = 0
return q
}(),
wantErr: "parsing TD Quote Body Descriptor failed: unsupported TD quote body type , got 1",
},
{
name: "v5 quote body size too large",
rawQuote: func() []byte {
q := clone(test.RawQuoteV5)
// modify the body size to be larger than expected (648)
quoteBodySizeBytes := quoteHeaderSizeV5 + quoteBodyTypeSizeV5
binary.LittleEndian.PutUint32(q[quoteBodySizeBytes:], uint32(999))
return q
}(),
wantErr: "TD quote body size is 999, a V5 TDX1.5 quote should have size 648",
},
{
name: "incorrect TDX version",
rawQuote: func() []byte {
q := clone(test.RawQuoteV5)
binary.LittleEndian.PutUint16(q[quoteHeaderSizeV5:quoteHeaderSizeV5+quoteBodyTypeSizeV5], uint16(2))
return q
}(),
wantErr: "TD quote body size is 648, a V5 TDX1.0 quote should have size 584",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
_, err := QuoteToProto(tc.rawQuote)
if tc.wantErr == "" {
if err != nil {
t.Errorf("QuoteToProto() returned error %v, want nil", err)
}
return
}
if err == nil || err.Error() != tc.wantErr {
t.Errorf("QuoteToProto() returned error %v, want %v", err, tc.wantErr)
}
})
}
}
func TestQuoteToAbiBytes(t *testing.T) {
quote, err := QuoteToProto(test.RawQuote)
if err != nil {
t.Fatal(err)
}
rawQuote, err := QuoteToAbiBytes(quote)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(test.RawQuote, rawQuote) {
t.Errorf("raw quote bytes got %v. Expected %v", rawQuote, test.RawQuote)
}
quoteV5, err := QuoteToProto(test.RawQuoteV5)
if err != nil {
t.Fatal(err)
}
rawQuoteV5, err := QuoteToAbiBytes(quoteV5)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(test.RawQuoteV5, rawQuoteV5) {
t.Errorf("raw quote v5 bytes got %v. Expected %v", rawQuoteV5, test.RawQuoteV5)
}
}
func TestCheckQuote(t *testing.T) {
quoteV4, err := QuoteToProto(test.RawQuote)
if err != nil {
t.Fatalf("failed to parse RawQuote: %v", err)
}
quoteV5, err := QuoteToProto(test.RawQuoteV5)
if err != nil {
t.Fatalf("failed to parse RawQuoteV5: %v", err)
}
tcs := []struct {
name string
quote any
wantErr string
}{
{
name: "empty quoteV4",
quote: &pb.QuoteV4{},
wantErr: "QuoteV4 Header error: header is nil",
},
{
name: "empty quoteV5",
quote: &pb.QuoteV5{},
wantErr: "quoteV5 Header error: header is nil",
},
{
name: "correct quoteV4",
quote: quoteV4,
wantErr: "",
},
{
name: "correct quoteV5",
quote: quoteV5,
wantErr: "",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := CheckQuote(tc.quote)
if tc.wantErr == "" {
if err != nil {
t.Errorf("CheckQuote() returned error %v, want nil", err)
}
return
}
if err == nil || err.Error() != tc.wantErr {
t.Errorf("CheckQuote() returned error %v, want %v", err, tc.wantErr)
}
})
}
}
func TestNilToAbiBytesConversions(t *testing.T) {
tcs := []struct {
name string
call func() ([]byte, error)
want error
}{
{
name: "QuoteToAbiBytes",
call: func() ([]byte, error) { return QuoteToAbiBytes(nil) },
want: ErrQuoteNil,
},
{
name: "signedDataToAbiBytes",
call: func() ([]byte, error) { return signedDataToAbiBytes(nil) },
want: ErrQuoteV4AuthDataNil,
},
{
name: "certificationDataToAbiBytes",
call: func() ([]byte, error) { return certificationDataToAbiBytes(nil) },
want: ErrCertificationDataNil,
},
{
name: "qeReportCertificationDataToAbiBytes",
call: func() ([]byte, error) { return qeReportCertificationDataToAbiBytes(nil) },
want: ErrQeReportCertificationDataNil,
},
{
name: "qeAuthDataToAbiBytes",
call: func() ([]byte, error) { return qeAuthDataToAbiBytes(nil) },
want: ErrQeAuthDataNil,
},
{
name: "pckCertificateChainToAbiBytes",
call: func() ([]byte, error) { return pckCertificateChainToAbiBytes(nil) },
want: ErrPckCertChainNil,
},
{
name: "TdQuoteBodyToAbiBytes",
call: func() ([]byte, error) { return TdQuoteBodyToAbiBytes(nil) },
want: ErrTDQuoteBodyNil,
},
{
name: "HeaderToAbiBytes",
call: func() ([]byte, error) { return HeaderToAbiBytes(nil) },
want: ErrHeaderNil,
},
{
name: "EnclaveReportToAbiBytes",
call: func() ([]byte, error) { return EnclaveReportToAbiBytes(nil) },
want: ErrQeReportNil,
},
{
name: "TdQuoteBodyDescriptorToAbiBytes",
call: func() ([]byte, error) { return TdQuoteBodyDescriptorToAbiBytes(nil) },
want: ErrTDQuoteBodyDescriptorNil,
},
{
name: "tdQuoteBodyV5ToAbiBytes",
call: func() ([]byte, error) { return tdQuoteBodyV5ToAbiBytes(nil, tdxVersion15BodyType) },
want: ErrQuoteV5Nil,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
if _, err := tc.call(); err != tc.want {
t.Errorf("%s() returned error %v, want %v", tc.name, err, tc.want)
}
})
}
}
func TestInvalidConversionsToAbiBytes(t *testing.T) {
quoteV5, err := QuoteToProto(test.RawQuoteV5)
if err != nil {
t.Fatalf("failed to parse RawQuoteV5: %v", err)
}
tdQuoteBodyV5 := quoteV5.(*pb.QuoteV5).GetTdQuoteBodyDescriptor().GetTdQuoteBodyV5()
tcs := []struct {
name string
call func() ([]byte, error)
wantErr string
}{
{
name: "QuoteV4HeaderNil",
call: func() ([]byte, error) { return QuoteToAbiBytes(&pb.QuoteV4{}) },
wantErr: "QuoteV4 invalid: QuoteV4 Header error: header is nil",
},
{
name: "Ecdsa256BitQuoteV4AuthDataNil",
call: func() ([]byte, error) { return signedDataToAbiBytes(&pb.Ecdsa256BitQuoteV4AuthData{}) },
wantErr: "QuoteV4 AuthData invalid: signature size is 0 bytes. Expected 64 bytes",
},
{
name: "CertificationDataEmpty",
call: func() ([]byte, error) { return certificationDataToAbiBytes(&pb.CertificationData{}) },
wantErr: "certification data invalid: certification data type invalid, got 0, expected 6",
},
{
name: "CertificationDataType7",
call: func() ([]byte, error) {
return certificationDataToAbiBytes(&pb.CertificationData{CertificateDataType: 7})
},
wantErr: "certification data invalid: certification data type invalid, got 7, expected 6",
},
{
name: "CertificationDataQeReportNil",
call: func() ([]byte, error) {
return certificationDataToAbiBytes(&pb.CertificationData{CertificateDataType: qeReportCertificationDataType, Size: 2})
},
wantErr: "certification data invalid: QE Report certification data error: QE Report certification data is nil",
},
{
name: "QEReportCertificationDataEmpty",
call: func() ([]byte, error) { return qeReportCertificationDataToAbiBytes(&pb.QEReportCertificationData{}) },
wantErr: "QE Report certification data invalid: QE Report error: QE Report is nil",
},
{
name: "QeAuthDataEmpty",
call: func() ([]byte, error) { return qeAuthDataToAbiBytes(&pb.QeAuthData{ParsedDataSize: 1}) },
wantErr: "QE AuthData invalid: parsed data size is 0 bytes. Expected 1 bytes",
},
{
name: "PCKCertificateChainDataEmpty",
call: func() ([]byte, error) { return pckCertificateChainToAbiBytes(&pb.PCKCertificateChainData{}) },
wantErr: "PCK certificate chain data invalid: PCK certificate chain data type invalid, got 0, expected 5",
},
{
name: "PCKCertificateChainDataType7",
call: func() ([]byte, error) {
return pckCertificateChainToAbiBytes(&pb.PCKCertificateChainData{CertificateDataType: 7})
},
wantErr: "PCK certificate chain data invalid: PCK certificate chain data type invalid, got 7, expected 5",
},
{
name: "PCKCertificateChainDataSize0",
call: func() ([]byte, error) {
return pckCertificateChainToAbiBytes(&pb.PCKCertificateChainData{CertificateDataType: pckReportCertificationDataType, Size: 2})
},
wantErr: "PCK certificate chain data invalid: PCK certificate chain size is 0. Expected size 2",
},
{
name: "TDQuoteBodyEmpty",
call: func() ([]byte, error) { return TdQuoteBodyToAbiBytes(&pb.TDQuoteBody{}) },
wantErr: "TD quote body invalid: teeTcbSvn size is 0 bytes. Expected 16 bytes",
},
{
name: "HeaderEmpty",
call: func() ([]byte, error) { return HeaderToAbiBytes(&pb.Header{}) },
wantErr: "header invalid: version 0 not supported",
},
{
name: "HeaderV1",
call: func() ([]byte, error) { return HeaderToAbiBytes(&pb.Header{Version: 1}) },
wantErr: "header invalid: version 1 not supported",
},
{
name: "HeaderAttestationKeyType1",
call: func() ([]byte, error) {
return HeaderToAbiBytes(&pb.Header{Version: QuoteVersionV4, AttestationKeyType: 1})
},
wantErr: "header invalid: attestation key type not supported",
},
{
name: "HeaderTeeType1",
call: func() ([]byte, error) {
return HeaderToAbiBytes(&pb.Header{Version: QuoteVersionV4, AttestationKeyType: AttestationKeyType, TeeType: 0x01})
},
wantErr: "header invalid: TEE type is not TDX",
},
{
name: "EnclaveReportEmpty",
call: func() ([]byte, error) { return EnclaveReportToAbiBytes(&pb.EnclaveReport{}) },
wantErr: "QE Report invalid: cpuSvn size is 0 bytes. Expected 16 bytes",
},
{
name: "QuoteV5HeaderNil",
call: func() ([]byte, error) { return QuoteToAbiBytes(&pb.QuoteV5{}) },
wantErr: "quoteV5 invalid: quoteV5 Header error: header is nil",
},
{
name: "TDQuoteBodyV5Version5",
call: func() ([]byte, error) { return tdQuoteBodyV5ToAbiBytes(&pb.TDQuoteBodyV5{}, 5) },
wantErr: "td quote body V5 invalid: tdx version 5 is not supported",
},
{
name: "TDQuoteBodyV5Empty",
call: func() ([]byte, error) { return tdQuoteBodyV5ToAbiBytes(&pb.TDQuoteBodyV5{}, tdxVersion10BodyType) },
wantErr: "td quote body V5 invalid: teeTcbSvn size is 0 bytes. Expected 16 bytes",
},
{
name: "TdQuoteBodyV5Svn2ForTdx10",
call: func() ([]byte, error) { return tdQuoteBodyV5ToAbiBytes(tdQuoteBodyV5, tdxVersion10BodyType) },
wantErr: "td quote body V5 invalid: teeTcbSvn2 is not expected to be set for TDX version 1.0",
},
{
name: "TdQuoteBodyV5Svn2InvalidSize",
call: func() ([]byte, error) {
body := proto.Clone(tdQuoteBodyV5).(*pb.TDQuoteBodyV5)
body.TeeTcbSvn2 = []byte{1}
return tdQuoteBodyV5ToAbiBytes(body, tdxVersion15BodyType)
},
wantErr: "td quote body V5 invalid: teeTcbSvn2 size is 1 bytes. Expected 16 bytes",
},
{
name: "TDQuoteBodyDescriptorEmpty",
call: func() ([]byte, error) { return TdQuoteBodyDescriptorToAbiBytes(&pb.TDQuoteBodyDescriptor{}) },
wantErr: "td quote body descriptor invalid: unsupported TD quote body type , got 0",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
if _, err := tc.call(); err == nil || err.Error() != tc.wantErr {
t.Errorf("%s() returned error %v, want %v", tc.name, err, tc.wantErr)
}
})
}
}