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.
  • Loading branch information
deeglaze committed Feb 22, 2024
1 parent 87a2ce5 commit 2dfc021
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 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,6 +172,18 @@ func TestReadAttestation(t *testing.T) {
}
}

func binAttestationDiff(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 TestTransform(t *testing.T) {
mu.Do(initDevice)
t.Run("bin", func(t *testing.T) {
Expand All @@ -186,8 +200,8 @@ func TestTransform(t *testing.T) {
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 := binAttestationDiff(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 2dfc021

Please sign in to comment.