Skip to content

Commit

Permalink
Use protocmp for binary proto comparison
Browse files Browse the repository at this point in the history
The report_test Transform test is flaky due to nondeterministic
serialization. Compare marshaled forms by unmarshaling and computing a
protobuf diff.

Similarly the CertsFromProto depends on map iteration order, which is
nondeterministic, so change the "bin" transform comparison as well.
  • Loading branch information
deeglaze committed Feb 22, 2024
1 parent 87a2ce5 commit 31a5719
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions tools/lib/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import (
"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
Expand Down Expand Up @@ -170,24 +172,53 @@ func TestReadAttestation(t *testing.T) {
}
}

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 !bytes.Equal(binout, input.bincerts) {
t.Fatalf("Transform(_, \"bin\") = %v, nil. Expect %v.", binout, input.bincerts)
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 !bytes.Equal(protoout, input.protocerts) {
t.Fatalf("Transform(_, \"proto\") = %v, nil. Expect %v.", protoout, input.protocerts)
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) {
Expand Down

0 comments on commit 31a5719

Please sign in to comment.