File: report_test.go

package info (click to toggle)
golang-github-google-go-sev-guest 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 820 kB
  • sloc: asm: 9; makefile: 3
file content (233 lines) | stat: -rw-r--r-- 6,807 bytes parent folder | download
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
// Copyright 2024 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 report

import (
	"bytes"
	"fmt"
	"os"
	"path"
	"sync"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-sev-guest/abi"
	"github.com/google/go-sev-guest/client"
	spb "github.com/google/go-sev-guest/proto/sevsnp"
	test "github.com/google/go-sev-guest/testing"
	"google.golang.org/protobuf/encoding/prototext"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/testing/protocmp"
)

var qp client.QuoteProvider
var mu sync.Once

type reports struct {
	attestation *spb.Attestation
	bincerts    []byte
	binreport   []byte
	protocerts  []byte
	protoreport []byte
	textcerts   []byte
	textreport  []byte
}

var input *reports

func initDevice() {
	now := time.Date(2022, time.May, 3, 9, 0, 0, 0, time.UTC)
	tests := test.TestCases()
	ones32 := make([]byte, 32)
	for i := range ones32 {
		ones32[i] = 1
	}
	opts := &test.DeviceOptions{Now: now, Product: abi.DefaultSevProduct()}
	tcqp, err := test.TcQuoteProvider(tests, opts)
	if err != nil {
		panic(fmt.Sprintf("failed to create test device: %v", err))
	}
	qp = tcqp

	var zeros [abi.ReportDataSize]byte
	bincerts, err := qp.GetRawQuote(zeros)
	if err != nil {
		panic(fmt.Errorf("mock failed to quote: %v", err))
	}
	if len(bincerts) < abi.ReportSize+abi.CertTableEntrySize {
		panic("mock failed to return cert table")
	}
	binreport := bincerts[:abi.ReportSize]
	attestation, err := ParseAttestation(bincerts, "bin")
	if err != nil {
		panic(fmt.Errorf("marshal failure: %v", err))
	}
	protocerts, err := proto.Marshal(attestation)
	if err != nil {
		panic(fmt.Errorf("marshal failure: %v", err))
	}
	protoreport, err := proto.Marshal(attestation.Report)
	if err != nil {
		panic(fmt.Errorf("marshal failure: %v", err))
	}
	textcerts, err := prototext.MarshalOptions{Multiline: true, Indent: "  "}.Marshal(attestation)
	if err != nil {
		panic(fmt.Errorf("marshal failure: %v", err))
	}
	textreport, err := prototext.MarshalOptions{Multiline: true, Indent: "  "}.Marshal(attestation.Report)
	if err != nil {
		panic(fmt.Errorf("marshal failure: %v", err))
	}
	input = &reports{
		attestation: attestation,
		bincerts:    bincerts,
		binreport:   binreport,
		protocerts:  protocerts,
		protoreport: protoreport,
		textcerts:   textcerts,
		textreport:  textreport,
	}
}

func TestParseAttestation(t *testing.T) {
	mu.Do(initDevice)
	type testcase struct {
		input  []byte
		inform string
	}
	good := []testcase{
		{input.bincerts, "bin"},
		{input.binreport, "bin"},
		{input.protocerts, "proto"},
		{input.protoreport, "proto"},
		{input.textcerts, "textproto"},
		{input.textreport, "textproto"},
	}
	bad := []testcase{
		{input.bincerts, "proto"},
		{input.textcerts, "bin"},
		{input.protoreport, "textproto"},
	}
	for _, tc := range good {
		if _, err := ParseAttestation(tc.input, tc.inform); err != nil {
			t.Fatalf("ParseAttestation(_, %q) = _, %v. Expect nil", tc.inform, err)
		}
	}
	for _, tc := range bad {
		if _, err := ParseAttestation(tc.input, tc.inform); err == nil {
			t.Fatalf("ParseAttestation(_, %q) = _, nil. Expected an error", tc.inform)
		}
	}
}

func TestReadAttestation(t *testing.T) {
	mu.Do(initDevice)
	type testcase struct {
		input  []byte
		inform string
	}
	good := []testcase{
		{input.bincerts, "bin"},
		{input.binreport, "bin"},
		{input.protocerts, "proto"},
		{input.protoreport, "proto"},
		{input.textcerts, "textproto"},
		{input.textreport, "textproto"},
	}
	bad := []testcase{
		{input.bincerts, "proto"},
		{input.textcerts, "bin"},
		{input.protoreport, "textproto"},
	}
	for _, tc := range good {
		p := path.Join(t.TempDir(), "input")
		if err := os.WriteFile(p, tc.input, 0644); err != nil {
			t.Fatalf("Could not write test file %q: %v", p, err)
		}
		if _, err := ReadAttestation(p, tc.inform); err != nil {
			t.Fatalf("ParseAttestation(_, %q) = _, %v. Expect nil", tc.inform, err)
		}
	}
	for _, tc := range bad {
		p := path.Join(t.TempDir(), "input")
		if err := os.WriteFile(p, tc.input, 0644); err != nil {
			t.Fatalf("Could not write test file %q: %v", p, err)
		}
		if _, err := ReadAttestation(p, tc.inform); err == nil {
			t.Fatalf("ReadAttestation(_, %q) = _, nil. Expected an error", tc.inform)
		}
	}
}

func protoAttestationDiff(left, right []byte) string {
	leftp := &spb.Attestation{}
	rightp := &spb.Attestation{}
	if err := proto.Unmarshal(left, leftp); err != nil {
		return fmt.Sprintf("left parse: %v", err)
	}
	if err := proto.Unmarshal(right, rightp); err != nil {
		return fmt.Sprintf("right parse: %v", err)
	}
	return cmp.Diff(leftp, rightp, protocmp.Transform())
}

func binAttestationDiff(left, right []byte) string {
	if diff := cmp.Diff(left[:abi.ReportSize], right[:abi.ReportSize]); diff != "" {
		return fmt.Sprintf("Report diff: %s", diff)
	}
	leftcerts := left[abi.ReportSize:]
	rightcerts := right[abi.ReportSize:]
	leftt := new(abi.CertTable)
	rightt := new(abi.CertTable)
	if err := leftt.Unmarshal(leftcerts); err != nil {
		return "bad left"
	}
	if err := rightt.Unmarshal(rightcerts); err != nil {
		return "bad right"
	}
	return cmp.Diff(leftt.Proto(), rightt.Proto(), protocmp.Transform())
}

func TestTransform(t *testing.T) {
	mu.Do(initDevice)
	t.Run("bin", func(t *testing.T) {
		binout, err := Transform(input.attestation, "bin")
		if err != nil {
			t.Fatalf("Transform(_, \"bin\") = _, %v. Expect nil.", err)
		}
		if diff := binAttestationDiff(binout, input.bincerts); diff != "" {
			t.Fatalf("Transform(_, \"bin\") = %v, nil. Expect %v.\nDiff: %s", binout, input.bincerts, diff)
		}
	})
	t.Run("proto", func(t *testing.T) {
		protoout, err := Transform(input.attestation, "proto")
		if err != nil {
			t.Fatalf("Transform(_, \"proto\") = _, %v. Expect nil.", err)
		}
		if diff := protoAttestationDiff(protoout, input.protocerts); diff != "" {
			t.Fatalf("Transform(_, \"proto\") = %v, nil. Expect %v.\nDiff: %s", protoout, input.protocerts, diff)
		}
	})
	t.Run("textproto", func(t *testing.T) {
		textout, err := Transform(input.attestation, "textproto")
		if err != nil {
			t.Fatalf("Transform(_, \"textproto\") = _, %v. Expect nil.", err)
		}
		if !bytes.Equal(textout, input.textcerts) {
			t.Fatalf("Transform(_, \"textproto\") = %v, nil. Expect %v.", string(textout), string(input.textcerts))
		}
	})
}