Skip to content

Commit afdf0b4

Browse files
authored
Merge pull request #113 from deeglaze/notdetreport
Use protocmp for binary proto comparison
2 parents 015b767 + 31a5719 commit afdf0b4

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

tools/lib/report/report_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import (
2323
"testing"
2424
"time"
2525

26+
"github.com/google/go-cmp/cmp"
2627
"github.com/google/go-sev-guest/abi"
2728
"github.com/google/go-sev-guest/client"
2829
spb "github.com/google/go-sev-guest/proto/sevsnp"
2930
test "github.com/google/go-sev-guest/testing"
3031
"google.golang.org/protobuf/encoding/prototext"
3132
"google.golang.org/protobuf/proto"
33+
"google.golang.org/protobuf/testing/protocmp"
3234
)
3335

3436
var qp client.QuoteProvider
@@ -170,24 +172,53 @@ func TestReadAttestation(t *testing.T) {
170172
}
171173
}
172174

175+
func protoAttestationDiff(left, right []byte) string {
176+
leftp := &spb.Attestation{}
177+
rightp := &spb.Attestation{}
178+
if err := proto.Unmarshal(left, leftp); err != nil {
179+
return fmt.Sprintf("left parse: %v", err)
180+
}
181+
if err := proto.Unmarshal(right, rightp); err != nil {
182+
return fmt.Sprintf("right parse: %v", err)
183+
}
184+
return cmp.Diff(leftp, rightp, protocmp.Transform())
185+
}
186+
187+
func binAttestationDiff(left, right []byte) string {
188+
if diff := cmp.Diff(left[:abi.ReportSize], right[:abi.ReportSize]); diff != "" {
189+
return fmt.Sprintf("Report diff: %s", diff)
190+
}
191+
leftcerts := left[abi.ReportSize:]
192+
rightcerts := right[abi.ReportSize:]
193+
leftt := new(abi.CertTable)
194+
rightt := new(abi.CertTable)
195+
if err := leftt.Unmarshal(leftcerts); err != nil {
196+
return "bad left"
197+
}
198+
if err := rightt.Unmarshal(rightcerts); err != nil {
199+
return "bad right"
200+
}
201+
return cmp.Diff(leftt.Proto(), rightt.Proto(), protocmp.Transform())
202+
}
203+
173204
func TestTransform(t *testing.T) {
174205
mu.Do(initDevice)
175206
t.Run("bin", func(t *testing.T) {
176207
binout, err := Transform(input.attestation, "bin")
177208
if err != nil {
178209
t.Fatalf("Transform(_, \"bin\") = _, %v. Expect nil.", err)
179210
}
180-
if !bytes.Equal(binout, input.bincerts) {
181-
t.Fatalf("Transform(_, \"bin\") = %v, nil. Expect %v.", binout, input.bincerts)
211+
if diff := binAttestationDiff(binout, input.bincerts); diff != "" {
212+
t.Fatalf("Transform(_, \"bin\") = %v, nil. Expect %v.\nDiff: %s", binout, input.bincerts, diff)
182213
}
183214
})
184215
t.Run("proto", func(t *testing.T) {
185216
protoout, err := Transform(input.attestation, "proto")
186217
if err != nil {
187218
t.Fatalf("Transform(_, \"proto\") = _, %v. Expect nil.", err)
188219
}
189-
if !bytes.Equal(protoout, input.protocerts) {
190-
t.Fatalf("Transform(_, \"proto\") = %v, nil. Expect %v.", protoout, input.protocerts)
220+
if diff := protoAttestationDiff(protoout, input.protocerts); diff != "" {
221+
t.Fatalf("Transform(_, \"proto\") = %v, nil. Expect %v.\nDiff: %s", protoout, input.protocerts, diff)
191222
}
192223
})
193224
t.Run("textproto", func(t *testing.T) {

0 commit comments

Comments
 (0)