Skip to content

Commit

Permalink
encoding/prototext: add proto editions and fuzz tests
Browse files Browse the repository at this point in the history
Change-Id: I2afc5ae83bf68600def3568e1d3ad51ef00e7671
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/566395
Auto-Submit: Lasse Folger <lassefolger@google.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
lfolger committed Feb 23, 2024
1 parent 2caa6b0 commit 055c812
Show file tree
Hide file tree
Showing 6 changed files with 1,312 additions and 498 deletions.
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go/testdata/import_public/a.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package goproto.protoc.import_public;
// Same Go package.
import public "cmd/protoc-gen-go/testdata/import_public/b.proto";

// Different Go package.
// Different Go package.
import public "cmd/protoc-gen-go/testdata/import_public/sub/a.proto";

option go_package = "google.golang.org/protobuf/cmd/protoc-gen-go/testdata/import_public";
Expand Down
198 changes: 198 additions & 0 deletions encoding/prototext/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
pb2 "google.golang.org/protobuf/internal/testprotos/textpb2"
pb3 "google.golang.org/protobuf/internal/testprotos/textpb3"
pbeditions "google.golang.org/protobuf/internal/testprotos/textpbeditions"
"google.golang.org/protobuf/types/known/anypb"
)

Expand Down Expand Up @@ -70,6 +71,62 @@ opt_string: ""
OptBytes: []byte{},
OptString: proto.String(""),
},
}, {
desc: "protoeditions explicit scalars set to zero values",
inputMessage: &pbeditions.Scalars{},
inputText: `opt_bool: false
opt_int32: 0
opt_int64: 0
opt_uint32: 0
opt_uint64: 0
opt_sint32: 0
opt_sint64: 0
opt_fixed32: 0
opt_fixed64: 0
opt_sfixed32: 0
opt_sfixed64: 0
opt_float: 0
opt_double: 0
opt_bytes: ""
opt_string: ""
`,
wantMessage: &pbeditions.Scalars{
OptBool: proto.Bool(false),
OptInt32: proto.Int32(0),
OptInt64: proto.Int64(0),
OptUint32: proto.Uint32(0),
OptUint64: proto.Uint64(0),
OptSint32: proto.Int32(0),
OptSint64: proto.Int64(0),
OptFixed32: proto.Uint32(0),
OptFixed64: proto.Uint64(0),
OptSfixed32: proto.Int32(0),
OptSfixed64: proto.Int64(0),
OptFloat: proto.Float32(0),
OptDouble: proto.Float64(0),
OptBytes: []byte{},
OptString: proto.String(""),
},
}, {
desc: "protoeditions implicit scalars set to zero values",
inputMessage: &pbeditions.ImplicitScalars{},
inputText: `s_bool: false
s_int32: 0
s_int64: 0
s_uint32: 0
s_uint64: 0
s_sint32: 0
s_sint64: 0
s_fixed32: 0
s_fixed64: 0
s_sfixed32: 0
s_sfixed64: 0
s_float: 0
s_double: 0
s_bytes: ""
s_string: ""
`,
wantMessage: &pbeditions.ImplicitScalars{},
}, {
desc: "proto3 scalars set to zero values",
inputMessage: &pb3.Scalars{},
Expand Down Expand Up @@ -150,6 +207,38 @@ opt_string: "谷歌"
OptBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
OptString: proto.String("谷歌"),
},
}, {
desc: "protoeditions explicit scalars",
inputMessage: &pbeditions.Scalars{},
inputText: `opt_bool: true
opt_int32: 255
opt_int64: 3735928559
opt_uint32: 0xff
opt_uint64: 0xdeadbeef
opt_sint32: -1001
opt_sint64: - 0xffff
opt_fixed64: 64
opt_sfixed32: - 32
opt_float: 1.234
opt_double: 1.23e+100
opt_bytes: "\xe8\xb0\xb7\xe6\xad\x8c"
opt_string: "谷歌"
`,
wantMessage: &pbeditions.Scalars{
OptBool: proto.Bool(true),
OptInt32: proto.Int32(0xff),
OptInt64: proto.Int64(0xdeadbeef),
OptUint32: proto.Uint32(0xff),
OptUint64: proto.Uint64(0xdeadbeef),
OptSint32: proto.Int32(-1001),
OptSint64: proto.Int64(-0xffff),
OptFixed64: proto.Uint64(64),
OptSfixed32: proto.Int32(-32),
OptFloat: proto.Float32(1.234),
OptDouble: proto.Float64(1.23e100),
OptBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
OptString: proto.String("谷歌"),
},
}, {
desc: "case sensitive",
inputMessage: &pb3.Scalars{},
Expand Down Expand Up @@ -195,6 +284,13 @@ s_string: "谷歌"
wantMessage: &pb2.Scalars{
OptString: proto.String("abc\xff"),
},
}, {
desc: "protoeditions unvalidated string with invalid UTF-8",
inputMessage: &pbeditions.Scalars{},
inputText: `opt_string: "abc\xff"`,
wantMessage: &pbeditions.Scalars{
OptString: proto.String("abc\xff"),
},
}, {
desc: "proto3 string with invalid UTF-8",
inputMessage: &pb3.Scalars{},
Expand All @@ -215,6 +311,11 @@ s_string: "谷歌"
umo: prototext.UnmarshalOptions{DiscardUnknown: true},
inputMessage: &pb2.Scalars{},
inputText: `unknown_field:123 1000:"hello"`,
}, {
desc: "protoeditions message contains discarded unknown field",
umo: prototext.UnmarshalOptions{DiscardUnknown: true},
inputMessage: &pbeditions.Scalars{},
inputText: `unknown_field:123 1000:"hello"`,
}, {
desc: "proto3 message contains discarded unknown field",
umo: prototext.UnmarshalOptions{DiscardUnknown: true},
Expand Down Expand Up @@ -408,6 +509,39 @@ opt_nested_enum: UNO
OptEnum: pb2.Enum_ONE.Enum(),
OptNestedEnum: pb2.Enums_UNO.Enum(),
},
}, {
desc: "protoeditions closed enum",
inputMessage: &pbeditions.Enums{},
inputText: `
opt_enum: ONE
opt_nested_enum: UNO
`,
wantMessage: &pbeditions.Enums{
OptEnum: pbeditions.Enum_ONE.Enum(),
OptNestedEnum: pbeditions.Enums_UNO.Enum(),
},
}, {
desc: "protoeditions open enum",
inputMessage: &pbeditions.Enums{},
inputText: `
implicit_enum: EINS
implicit_nested_enum: ZEHN
`,
wantMessage: &pbeditions.Enums{
ImplicitEnum: pbeditions.OpenEnum_EINS,
ImplicitNestedEnum: pbeditions.Enums_ZEHN,
},
}, {
desc: "protoeditions enum numeric value",
inputMessage: &pbeditions.Enums{},
inputText: `
implicit_enum: 1
implicit_nested_enum: 10
`,
wantMessage: &pbeditions.Enums{
ImplicitEnum: pbeditions.OpenEnum_EINS,
ImplicitNestedEnum: pbeditions.Enums_ZEHN,
},
}, {
desc: "proto2 enum set to numeric values",
inputMessage: &pb2.Enums{},
Expand Down Expand Up @@ -482,6 +616,17 @@ OptGroup: {}
OptNested: &pb2.Nested{},
Optgroup: &pb2.Nests_OptGroup{},
},
}, {
desc: "protoeditions nested empty messages",
inputMessage: &pbeditions.Nests{},
inputText: `
opt_nested: {}
OptGroup: {}
`,
wantMessage: &pbeditions.Nests{
OptNested: &pbeditions.Nested{},
Optgroup: &pbeditions.Nests_OptGroup{},
},
}, {
desc: "message fields with no field separator",
inputMessage: &pb2.Nests{},
Expand All @@ -493,11 +638,27 @@ OptGroup {}
OptNested: &pb2.Nested{},
Optgroup: &pb2.Nests_OptGroup{},
},
}, {
desc: "message fields with no field separator",
inputMessage: &pbeditions.Nests{},
inputText: `
opt_nested {}
OptGroup {}
`,
wantMessage: &pbeditions.Nests{
OptNested: &pbeditions.Nested{},
Optgroup: &pbeditions.Nests_OptGroup{},
},
}, {
desc: "group field name",
inputMessage: &pb2.Nests{},
inputText: `optgroup: {}`,
wantErr: "unknown field: optgroup",
}, {
desc: "delimited encoded message field name",
inputMessage: &pbeditions.Nests{},
inputText: `optgroup: {}`,
wantErr: "unknown field: optgroup",
}, {
desc: "proto2 nested messages",
inputMessage: &pb2.Nests{},
Expand All @@ -517,6 +678,25 @@ opt_nested: {
},
},
},
}, {
desc: "protoeditions delimited encoded nested messages",
inputMessage: &pbeditions.Nests{},
inputText: `
opt_nested: {
opt_string: "nested message"
opt_nested: {
opt_string: "another nested message"
}
}
`,
wantMessage: &pbeditions.Nests{
OptNested: &pbeditions.Nested{
OptString: proto.String("nested message"),
OptNested: &pbeditions.Nested{
OptString: proto.String("another nested message"),
},
},
},
}, {
desc: "proto3 nested empty message",
inputMessage: &pb3.Nests{},
Expand Down Expand Up @@ -711,6 +891,24 @@ RptGroup: {}
{},
},
},
}, {
desc: "repeated delimited encoded message fields",
inputMessage: &pbeditions.Nests{},
inputText: `
RptGroup: {
rpt_string: "hello"
rpt_string: "world"
}
RptGroup: {}
`,
wantMessage: &pbeditions.Nests{
Rptgroup: []*pbeditions.Nests_RptGroup{
{
RptString: []string{"hello", "world"},
},
{},
},
},
}, {
desc: "repeated message fields without field separator",
inputMessage: &pb2.Nests{},
Expand Down
Loading

0 comments on commit 055c812

Please sign in to comment.