Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 5 | package proto_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
Damien Neil | 37cbbeb | 2020-02-24 11:10:10 -0800 | [diff] [blame] | 10 | "math" |
Damien Neil | 01c0e8d | 2019-11-12 12:33:12 -0800 | [diff] [blame] | 11 | "reflect" |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 12 | "testing" |
| 13 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 14 | "github.com/google/go-cmp/cmp" |
Joe Tsai | e0daf31 | 2020-02-25 12:51:10 -0800 | [diff] [blame] | 15 | |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 16 | "google.golang.org/protobuf/encoding/prototext" |
Joe Tsai | cd108d0 | 2020-02-14 18:08:02 -0800 | [diff] [blame] | 17 | "google.golang.org/protobuf/encoding/protowire" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 18 | "google.golang.org/protobuf/proto" |
Damien Neil | 01c0e8d | 2019-11-12 12:33:12 -0800 | [diff] [blame] | 19 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Damien Neil | 3016b73 | 2019-04-07 12:43:10 -0700 | [diff] [blame] | 20 | |
Damien Neil | 01c0e8d | 2019-11-12 12:33:12 -0800 | [diff] [blame] | 21 | orderpb "google.golang.org/protobuf/internal/testprotos/order" |
Joe Tsai | 9b22b93 | 2019-08-08 19:23:32 -0700 | [diff] [blame] | 22 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 23 | test3pb "google.golang.org/protobuf/internal/testprotos/test3" |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func TestEncode(t *testing.T) { |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 27 | for _, test := range testValidMessages { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 28 | for _, want := range test.decodeTo { |
| 29 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 30 | opts := proto.MarshalOptions{ |
| 31 | AllowPartial: test.partial, |
| 32 | } |
| 33 | wire, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 34 | if err != nil { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 35 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want)) |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | size := proto.Size(want) |
| 39 | if size != len(wire) { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 40 | t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), prototext.Format(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 43 | got := want.ProtoReflect().New().Interface() |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 44 | uopts := proto.UnmarshalOptions{ |
| 45 | AllowPartial: test.partial, |
| 46 | } |
| 47 | if err := uopts.Unmarshal(wire, got); err != nil { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 48 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 49 | return |
| 50 | } |
Joe Tsai | 96a4473 | 2020-01-03 19:52:28 -0800 | [diff] [blame] | 51 | if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 52 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 53 | } |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestEncodeDeterministic(t *testing.T) { |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 60 | for _, test := range testValidMessages { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 61 | for _, want := range test.decodeTo { |
| 62 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 63 | opts := proto.MarshalOptions{ |
| 64 | Deterministic: true, |
| 65 | AllowPartial: test.partial, |
| 66 | } |
| 67 | wire, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 68 | if err != nil { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 69 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 70 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 71 | wire2, err := opts.Marshal(want) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 72 | if err != nil { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 73 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 74 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 75 | if !bytes.Equal(wire, wire2) { |
| 76 | t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2)) |
| 77 | } |
| 78 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 79 | got := want.ProtoReflect().New().Interface() |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 80 | uopts := proto.UnmarshalOptions{ |
| 81 | AllowPartial: test.partial, |
| 82 | } |
| 83 | if err := uopts.Unmarshal(wire, got); err != nil { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 84 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 85 | return |
| 86 | } |
Joe Tsai | 96a4473 | 2020-01-03 19:52:28 -0800 | [diff] [blame] | 87 | if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 88 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want)) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 89 | } |
| 90 | }) |
| 91 | } |
| 92 | } |
| 93 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 94 | |
| 95 | func TestEncodeRequiredFieldChecks(t *testing.T) { |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 96 | for _, test := range testValidMessages { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 97 | if !test.partial { |
| 98 | continue |
| 99 | } |
| 100 | for _, m := range test.decodeTo { |
| 101 | t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) { |
| 102 | _, err := proto.Marshal(m) |
| 103 | if err == nil { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 104 | t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", prototext.Format(m)) |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 105 | } |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | } |
Damien Neil | 3016b73 | 2019-04-07 12:43:10 -0700 | [diff] [blame] | 110 | |
Joe Tsai | 9b22b93 | 2019-08-08 19:23:32 -0700 | [diff] [blame] | 111 | func TestEncodeAppend(t *testing.T) { |
Damien Neil | 3016b73 | 2019-04-07 12:43:10 -0700 | [diff] [blame] | 112 | want := []byte("prefix") |
| 113 | got := append([]byte(nil), want...) |
| 114 | got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{ |
| 115 | OptionalString: "value", |
| 116 | }) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | if !bytes.HasPrefix(got, want) { |
| 121 | t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want) |
| 122 | } |
| 123 | } |
Joe Tsai | 9b22b93 | 2019-08-08 19:23:32 -0700 | [diff] [blame] | 124 | |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 125 | func TestEncodeInvalidMessages(t *testing.T) { |
| 126 | for _, test := range testInvalidMessages { |
| 127 | for _, m := range test.decodeTo { |
| 128 | if !m.ProtoReflect().IsValid() { |
| 129 | continue |
| 130 | } |
| 131 | t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) { |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 132 | opts := proto.MarshalOptions{ |
| 133 | AllowPartial: test.partial, |
| 134 | } |
| 135 | got, err := opts.Marshal(m) |
| 136 | if err == nil { |
Joe Tsai | 74b1460 | 2020-01-06 15:44:09 -0800 | [diff] [blame] | 137 | t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, prototext.Format(m)) |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 138 | } |
| 139 | }) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Joe Tsai | 9b22b93 | 2019-08-08 19:23:32 -0700 | [diff] [blame] | 144 | func TestEncodeOneofNilWrapper(t *testing.T) { |
| 145 | m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)} |
| 146 | b, err := proto.Marshal(m) |
| 147 | if err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | if len(b) > 0 { |
| 151 | t.Errorf("Marshal return non-empty, want empty") |
| 152 | } |
| 153 | } |
Damien Neil | 1e5516a | 2019-09-27 14:31:10 -0700 | [diff] [blame] | 154 | |
| 155 | func TestMarshalAppendAllocations(t *testing.T) { |
| 156 | m := &test3pb.TestAllTypes{OptionalInt32: 1} |
| 157 | size := proto.Size(m) |
| 158 | const count = 1000 |
| 159 | b := make([]byte, size) |
| 160 | // AllocsPerRun returns an integral value. |
| 161 | marshalAllocs := testing.AllocsPerRun(count, func() { |
| 162 | _, err := proto.MarshalOptions{}.MarshalAppend(b[:0], m) |
| 163 | if err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | }) |
| 167 | b = nil |
| 168 | marshalAppendAllocs := testing.AllocsPerRun(count, func() { |
| 169 | var err error |
| 170 | b, err = proto.MarshalOptions{}.MarshalAppend(b, m) |
| 171 | if err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | }) |
| 175 | if marshalAllocs != marshalAppendAllocs { |
| 176 | t.Errorf("%v allocs/op when writing to a preallocated buffer", marshalAllocs) |
| 177 | t.Errorf("%v allocs/op when repeatedly appending to a slice", marshalAppendAllocs) |
| 178 | t.Errorf("expect amortized allocs/op to be identical") |
| 179 | } |
| 180 | } |
Damien Neil | 01c0e8d | 2019-11-12 12:33:12 -0800 | [diff] [blame] | 181 | |
| 182 | func TestEncodeOrder(t *testing.T) { |
| 183 | // We make no guarantees about the stability of wire marshal output. |
| 184 | // The order in which fields are marshaled may change over time. |
| 185 | // If deterministic marshaling is not enabled, it may change over |
| 186 | // successive calls to proto.Marshal in the same binary. |
| 187 | // |
| 188 | // Unfortunately, many users have come to rely on the specific current |
| 189 | // wire marshal output. Perhaps someday we will choose to deliberately |
| 190 | // change the marshal output; until that day comes, this test verifies |
| 191 | // that we don't unintentionally change it. |
| 192 | m := &orderpb.Message{ |
| 193 | Field_1: proto.String("one"), |
| 194 | Field_2: proto.String("two"), |
| 195 | Field_20: proto.String("twenty"), |
| 196 | Oneof_1: &orderpb.Message_Field_10{"ten"}, |
| 197 | } |
| 198 | proto.SetExtension(m, orderpb.E_Field_30, "thirty") |
| 199 | proto.SetExtension(m, orderpb.E_Field_31, "thirty-one") |
| 200 | proto.SetExtension(m, orderpb.E_Field_32, "thirty-two") |
| 201 | want := []pref.FieldNumber{ |
| 202 | 30, 31, 32, // extensions first, in number order |
| 203 | 1, 2, 20, // non-extension, non-oneof in number order |
| 204 | 10, // oneofs last, undefined order |
| 205 | } |
| 206 | |
| 207 | // Test with deterministic serialization, since fields are not sorted without |
| 208 | // it when -tags=protoreflect. |
| 209 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
| 210 | if err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | var got []pref.FieldNumber |
| 214 | for len(b) > 0 { |
Joe Tsai | cd108d0 | 2020-02-14 18:08:02 -0800 | [diff] [blame] | 215 | num, _, n := protowire.ConsumeField(b) |
Damien Neil | 01c0e8d | 2019-11-12 12:33:12 -0800 | [diff] [blame] | 216 | if n < 0 { |
Joe Tsai | cd108d0 | 2020-02-14 18:08:02 -0800 | [diff] [blame] | 217 | t.Fatal(protowire.ParseError(n)) |
Damien Neil | 01c0e8d | 2019-11-12 12:33:12 -0800 | [diff] [blame] | 218 | } |
| 219 | b = b[n:] |
| 220 | got = append(got, num) |
| 221 | } |
| 222 | if !reflect.DeepEqual(got, want) { |
| 223 | t.Errorf("unexpected field marshal order:\ngot: %v\nwant: %v\nmessage:\n%v", got, want, m) |
| 224 | } |
| 225 | } |
Damien Neil | 37cbbeb | 2020-02-24 11:10:10 -0800 | [diff] [blame] | 226 | |
| 227 | func TestEncodeLarge(t *testing.T) { |
| 228 | // Encode/decode a message large enough to overflow a 32-bit size cache. |
| 229 | t.Skip("too slow and memory-hungry to run all the time") |
| 230 | size := math.MaxUint32 + 1 |
| 231 | m := &testpb.TestAllTypes_NestedMessage{ |
| 232 | Corecursive: &testpb.TestAllTypes{ |
| 233 | OptionalBytes: make([]byte, size), |
| 234 | }, |
| 235 | } |
| 236 | b, err := proto.Marshal(m) |
| 237 | if err != nil { |
| 238 | t.Fatalf("Marshal: %v", err) |
| 239 | } |
| 240 | if got, want := len(b), proto.Size(m); got != want { |
| 241 | t.Fatalf("Size(m) = %v, but len(Marshal(m)) = %v", got, want) |
| 242 | } |
| 243 | if err := proto.Unmarshal(b, m); err != nil { |
| 244 | t.Fatalf("Unmarshal: %v", err) |
| 245 | } |
| 246 | if got, want := len(m.Corecursive.OptionalBytes), size; got != want { |
| 247 | t.Errorf("after round-trip marshal, got len(m.OptionalBytes) = %v, want %v", got, want) |
| 248 | } |
| 249 | } |
Joe Tsai | a732b3c | 2020-04-17 11:08:53 -0700 | [diff] [blame^] | 250 | |
| 251 | // TestEncodeEmpty tests for boundary conditions when producing an empty output. |
| 252 | // These tests are not necessarily a statement of proper behavior, |
| 253 | // but exist to detect accidental changes in behavior. |
| 254 | func TestEncodeEmpty(t *testing.T) { |
| 255 | for _, m := range []proto.Message{nil, (*testpb.TestAllTypes)(nil), &testpb.TestAllTypes{}} { |
| 256 | isValid := m != nil && m.ProtoReflect().IsValid() |
| 257 | |
| 258 | b, err := proto.Marshal(m) |
| 259 | if err != nil { |
| 260 | t.Errorf("proto.Marshal() = %v", err) |
| 261 | } |
| 262 | if isNil := b == nil; isNil == isValid { |
| 263 | t.Errorf("proto.Marshal() == nil: %v, want %v", isNil, !isValid) |
| 264 | } |
| 265 | |
| 266 | b, err = proto.MarshalOptions{}.Marshal(m) |
| 267 | if err != nil { |
| 268 | t.Errorf("proto.MarshalOptions{}.Marshal() = %v", err) |
| 269 | } |
| 270 | if isNil := b == nil; isNil == isValid { |
| 271 | t.Errorf("proto.MarshalOptions{}.Marshal() = %v, want %v", isNil, !isValid) |
| 272 | } |
| 273 | } |
| 274 | } |