proto: rearrange test messages
Move the test inputs for the wire marshaler and unmarshaler out of
decode_test.go and into a new file. Consolidate some tests for invalid
messages (UTF-8 validation failures, field numbers out of range) into
a single list of invalid messages. Break out the no-enforce-utf8 test
into a separate file, since it is both complicated and conditional on
legacy support.
Change-Id: Ide80fa9d3aec2b6d42a57e6f9265358aa5e661a7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/211557
Reviewed-by: Joe Tsai <joetsai@google.com>
diff --git a/proto/encode_test.go b/proto/encode_test.go
index 348d900..8f02d0c 100644
--- a/proto/encode_test.go
+++ b/proto/encode_test.go
@@ -12,7 +12,6 @@
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/internal/encoding/wire"
- "google.golang.org/protobuf/internal/flags"
"google.golang.org/protobuf/proto"
pref "google.golang.org/protobuf/reflect/protoreflect"
@@ -22,7 +21,7 @@
)
func TestEncode(t *testing.T) {
- for _, test := range testProtos {
+ for _, test := range testValidMessages {
for _, want := range test.decodeTo {
t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
opts := proto.MarshalOptions{
@@ -55,7 +54,7 @@
}
func TestEncodeDeterministic(t *testing.T) {
- for _, test := range testProtos {
+ for _, test := range testValidMessages {
for _, want := range test.decodeTo {
t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
opts := proto.MarshalOptions{
@@ -90,37 +89,8 @@
}
}
-func TestEncodeInvalidUTF8(t *testing.T) {
- for _, test := range invalidUTF8TestProtos {
- for _, want := range test.decodeTo {
- t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
- _, err := proto.Marshal(want)
- if err == nil {
- t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
- }
- })
- }
- }
-}
-
-func TestEncodeNoEnforceUTF8(t *testing.T) {
- for _, test := range noEnforceUTF8TestProtos {
- for _, want := range test.decodeTo {
- t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
- _, err := proto.Marshal(want)
- switch {
- case flags.ProtoLegacy && err != nil:
- t.Errorf("Marshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
- case !flags.ProtoLegacy && err == nil:
- t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
- }
- })
- }
- }
-}
-
func TestEncodeRequiredFieldChecks(t *testing.T) {
- for _, test := range testProtos {
+ for _, test := range testValidMessages {
if !test.partial {
continue
}
@@ -149,6 +119,26 @@
}
}
+func TestEncodeInvalidMessages(t *testing.T) {
+ for _, test := range testInvalidMessages {
+ for _, m := range test.decodeTo {
+ if !m.ProtoReflect().IsValid() {
+ continue
+ }
+ t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
+ t.Logf("%v %v", m, m.ProtoReflect().IsValid())
+ opts := proto.MarshalOptions{
+ AllowPartial: test.partial,
+ }
+ got, err := opts.Marshal(m)
+ if err == nil {
+ t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, marshalText(m))
+ }
+ })
+ }
+ }
+}
+
func TestEncodeOneofNilWrapper(t *testing.T) {
m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
b, err := proto.Marshal(m)