Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial fuzz tests comparing roundtrip to upstream Go proto #129

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions conformance/internal/conformance/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package conformance

import (
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"strings"
"testing"
)

func roundTripUpstream(b []byte) ([]byte, error) {
msg := &TestAllTypesProto3{}
if err := proto.Unmarshal(b, msg); err != nil {
return nil, err
}
res, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
return res, nil
}

func roundTripVtprotobuf(b []byte) ([]byte, error) {
msg := &TestAllTypesProto3{}
if err := msg.UnmarshalVT(b); err != nil {
return nil, err
}
res, err := msg.MarshalVT()
if err != nil {
return nil, err
}
return res, nil
}

func FuzzProto(f *testing.F) {
f.Fuzz(func(t *testing.T, b []byte) {
u, uerr := roundTripUpstream(b)
v, verr := roundTripVtprotobuf(b)
if verr != nil && strings.Contains(verr.Error(), "wrong wireType") {
t.Skip()
}
if uerr != nil && strings.Contains(uerr.Error(), "cannot parse invalid wire-format data") {
t.Skip()
}
if (uerr != nil) != (verr != nil) {
t.Fatalf("upstream err: %v (%v), vtprotobuf err: %v (%v)", uerr, u, verr, v)
}
us := &TestAllTypesProto3{}
_ = proto.Unmarshal(b, us)
us.unknownFields = nil

t.Logf("upstream : %v, %v", protojson.Format(us), prototext.Format(us))
vt := &TestAllTypesProto3{}
_ = vt.UnmarshalVT(b)
vt.unknownFields = nil
t.Logf("vtprotobuf: %v, %v", protojson.Format(vt), prototext.Format(vt))
require.Equal(t, us, vt)
//require.Equal(t, u, v)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("\x89\x89\x0000000000")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("z\x00")
Loading