blob: c2fefc390dd31bc4a2f2ca35bf874b3fdfc580c7 [file] [log] [blame]
Damien Neilc37adef2019-04-01 13:49:56 -07001// 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 Neil99f24c32019-03-13 17:06:42 -07005package proto_test
6
7import (
8 "bytes"
9 "fmt"
Damien Neil01c0e8d2019-11-12 12:33:12 -080010 "reflect"
Damien Neil99f24c32019-03-13 17:06:42 -070011 "testing"
12
Damien Neil99f24c32019-03-13 17:06:42 -070013 "github.com/google/go-cmp/cmp"
Joe Tsai74b14602020-01-06 15:44:09 -080014 "google.golang.org/protobuf/encoding/prototext"
Damien Neil01c0e8d2019-11-12 12:33:12 -080015 "google.golang.org/protobuf/internal/encoding/wire"
Damien Neile89e6242019-05-13 23:55:40 -070016 "google.golang.org/protobuf/proto"
Damien Neil01c0e8d2019-11-12 12:33:12 -080017 pref "google.golang.org/protobuf/reflect/protoreflect"
Damien Neil3016b732019-04-07 12:43:10 -070018
Damien Neil01c0e8d2019-11-12 12:33:12 -080019 orderpb "google.golang.org/protobuf/internal/testprotos/order"
Joe Tsai9b22b932019-08-08 19:23:32 -070020 testpb "google.golang.org/protobuf/internal/testprotos/test"
Damien Neile89e6242019-05-13 23:55:40 -070021 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neil99f24c32019-03-13 17:06:42 -070022)
23
24func TestEncode(t *testing.T) {
Damien Neild0b07492019-12-16 12:59:13 -080025 for _, test := range testValidMessages {
Damien Neil99f24c32019-03-13 17:06:42 -070026 for _, want := range test.decodeTo {
27 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070028 opts := proto.MarshalOptions{
29 AllowPartial: test.partial,
30 }
31 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070032 if err != nil {
Joe Tsai74b14602020-01-06 15:44:09 -080033 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want))
Damien Neil61e93c72019-03-27 09:23:20 -070034 }
35
36 size := proto.Size(want)
37 if size != len(wire) {
Joe Tsai74b14602020-01-06 15:44:09 -080038 t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), prototext.Format(want))
Damien Neil99f24c32019-03-13 17:06:42 -070039 }
40
Joe Tsai378c1322019-04-25 23:48:08 -070041 got := want.ProtoReflect().New().Interface()
Damien Neil96c229a2019-04-03 12:17:24 -070042 uopts := proto.UnmarshalOptions{
43 AllowPartial: test.partial,
44 }
45 if err := uopts.Unmarshal(wire, got); err != nil {
Joe Tsai74b14602020-01-06 15:44:09 -080046 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want))
Damien Neil99f24c32019-03-13 17:06:42 -070047 return
48 }
Joe Tsai96a44732020-01-03 19:52:28 -080049 if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
Joe Tsai74b14602020-01-06 15:44:09 -080050 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want))
Damien Neil99f24c32019-03-13 17:06:42 -070051 }
52 })
53 }
54 }
55}
56
57func TestEncodeDeterministic(t *testing.T) {
Damien Neild0b07492019-12-16 12:59:13 -080058 for _, test := range testValidMessages {
Damien Neil99f24c32019-03-13 17:06:42 -070059 for _, want := range test.decodeTo {
60 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070061 opts := proto.MarshalOptions{
62 Deterministic: true,
63 AllowPartial: test.partial,
64 }
65 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070066 if err != nil {
Joe Tsai74b14602020-01-06 15:44:09 -080067 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want))
Damien Neil99f24c32019-03-13 17:06:42 -070068 }
Damien Neil96c229a2019-04-03 12:17:24 -070069 wire2, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070070 if err != nil {
Joe Tsai74b14602020-01-06 15:44:09 -080071 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want))
Damien Neil99f24c32019-03-13 17:06:42 -070072 }
Damien Neil99f24c32019-03-13 17:06:42 -070073 if !bytes.Equal(wire, wire2) {
74 t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
75 }
76
Joe Tsai378c1322019-04-25 23:48:08 -070077 got := want.ProtoReflect().New().Interface()
Damien Neil96c229a2019-04-03 12:17:24 -070078 uopts := proto.UnmarshalOptions{
79 AllowPartial: test.partial,
80 }
81 if err := uopts.Unmarshal(wire, got); err != nil {
Joe Tsai74b14602020-01-06 15:44:09 -080082 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want))
Damien Neil99f24c32019-03-13 17:06:42 -070083 return
84 }
Joe Tsai96a44732020-01-03 19:52:28 -080085 if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
Joe Tsai74b14602020-01-06 15:44:09 -080086 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want))
Damien Neil99f24c32019-03-13 17:06:42 -070087 }
88 })
89 }
90 }
91}
Damien Neil96c229a2019-04-03 12:17:24 -070092
93func TestEncodeRequiredFieldChecks(t *testing.T) {
Damien Neild0b07492019-12-16 12:59:13 -080094 for _, test := range testValidMessages {
Damien Neil96c229a2019-04-03 12:17:24 -070095 if !test.partial {
96 continue
97 }
98 for _, m := range test.decodeTo {
99 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
100 _, err := proto.Marshal(m)
101 if err == nil {
Joe Tsai74b14602020-01-06 15:44:09 -0800102 t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", prototext.Format(m))
Damien Neil96c229a2019-04-03 12:17:24 -0700103 }
104 })
105 }
106 }
107}
Damien Neil3016b732019-04-07 12:43:10 -0700108
Joe Tsai9b22b932019-08-08 19:23:32 -0700109func TestEncodeAppend(t *testing.T) {
Damien Neil3016b732019-04-07 12:43:10 -0700110 want := []byte("prefix")
111 got := append([]byte(nil), want...)
112 got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
113 OptionalString: "value",
114 })
115 if err != nil {
116 t.Fatal(err)
117 }
118 if !bytes.HasPrefix(got, want) {
119 t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
120 }
121}
Joe Tsai9b22b932019-08-08 19:23:32 -0700122
Damien Neild0b07492019-12-16 12:59:13 -0800123func TestEncodeInvalidMessages(t *testing.T) {
124 for _, test := range testInvalidMessages {
125 for _, m := range test.decodeTo {
126 if !m.ProtoReflect().IsValid() {
127 continue
128 }
129 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
Damien Neild0b07492019-12-16 12:59:13 -0800130 opts := proto.MarshalOptions{
131 AllowPartial: test.partial,
132 }
133 got, err := opts.Marshal(m)
134 if err == nil {
Joe Tsai74b14602020-01-06 15:44:09 -0800135 t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, prototext.Format(m))
Damien Neild0b07492019-12-16 12:59:13 -0800136 }
137 })
138 }
139 }
140}
141
Joe Tsai9b22b932019-08-08 19:23:32 -0700142func TestEncodeOneofNilWrapper(t *testing.T) {
143 m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
144 b, err := proto.Marshal(m)
145 if err != nil {
146 t.Fatal(err)
147 }
148 if len(b) > 0 {
149 t.Errorf("Marshal return non-empty, want empty")
150 }
151}
Damien Neil1e5516a2019-09-27 14:31:10 -0700152
153func TestMarshalAppendAllocations(t *testing.T) {
154 m := &test3pb.TestAllTypes{OptionalInt32: 1}
155 size := proto.Size(m)
156 const count = 1000
157 b := make([]byte, size)
158 // AllocsPerRun returns an integral value.
159 marshalAllocs := testing.AllocsPerRun(count, func() {
160 _, err := proto.MarshalOptions{}.MarshalAppend(b[:0], m)
161 if err != nil {
162 t.Fatal(err)
163 }
164 })
165 b = nil
166 marshalAppendAllocs := testing.AllocsPerRun(count, func() {
167 var err error
168 b, err = proto.MarshalOptions{}.MarshalAppend(b, m)
169 if err != nil {
170 t.Fatal(err)
171 }
172 })
173 if marshalAllocs != marshalAppendAllocs {
174 t.Errorf("%v allocs/op when writing to a preallocated buffer", marshalAllocs)
175 t.Errorf("%v allocs/op when repeatedly appending to a slice", marshalAppendAllocs)
176 t.Errorf("expect amortized allocs/op to be identical")
177 }
178}
Damien Neil01c0e8d2019-11-12 12:33:12 -0800179
180func TestEncodeOrder(t *testing.T) {
181 // We make no guarantees about the stability of wire marshal output.
182 // The order in which fields are marshaled may change over time.
183 // If deterministic marshaling is not enabled, it may change over
184 // successive calls to proto.Marshal in the same binary.
185 //
186 // Unfortunately, many users have come to rely on the specific current
187 // wire marshal output. Perhaps someday we will choose to deliberately
188 // change the marshal output; until that day comes, this test verifies
189 // that we don't unintentionally change it.
190 m := &orderpb.Message{
191 Field_1: proto.String("one"),
192 Field_2: proto.String("two"),
193 Field_20: proto.String("twenty"),
194 Oneof_1: &orderpb.Message_Field_10{"ten"},
195 }
196 proto.SetExtension(m, orderpb.E_Field_30, "thirty")
197 proto.SetExtension(m, orderpb.E_Field_31, "thirty-one")
198 proto.SetExtension(m, orderpb.E_Field_32, "thirty-two")
199 want := []pref.FieldNumber{
200 30, 31, 32, // extensions first, in number order
201 1, 2, 20, // non-extension, non-oneof in number order
202 10, // oneofs last, undefined order
203 }
204
205 // Test with deterministic serialization, since fields are not sorted without
206 // it when -tags=protoreflect.
207 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
208 if err != nil {
209 t.Fatal(err)
210 }
211 var got []pref.FieldNumber
212 for len(b) > 0 {
213 num, _, n := wire.ConsumeField(b)
214 if n < 0 {
215 t.Fatal(wire.ParseError(n))
216 }
217 b = b[n:]
218 got = append(got, num)
219 }
220 if !reflect.DeepEqual(got, want) {
221 t.Errorf("unexpected field marshal order:\ngot: %v\nwant: %v\nmessage:\n%v", got, want, m)
222 }
223}