blob: 339249c7ae87fc43b11d12ea9579da1c8fe712c8 [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"
Damien Neil01c0e8d2019-11-12 12:33:12 -080014 "google.golang.org/protobuf/internal/encoding/wire"
Damien Neile89e6242019-05-13 23:55:40 -070015 "google.golang.org/protobuf/proto"
Damien Neil01c0e8d2019-11-12 12:33:12 -080016 pref "google.golang.org/protobuf/reflect/protoreflect"
Damien Neil3016b732019-04-07 12:43:10 -070017
Damien Neil01c0e8d2019-11-12 12:33:12 -080018 orderpb "google.golang.org/protobuf/internal/testprotos/order"
Joe Tsai9b22b932019-08-08 19:23:32 -070019 testpb "google.golang.org/protobuf/internal/testprotos/test"
Damien Neile89e6242019-05-13 23:55:40 -070020 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neil99f24c32019-03-13 17:06:42 -070021)
22
23func TestEncode(t *testing.T) {
Damien Neild0b07492019-12-16 12:59:13 -080024 for _, test := range testValidMessages {
Damien Neil99f24c32019-03-13 17:06:42 -070025 for _, want := range test.decodeTo {
26 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070027 opts := proto.MarshalOptions{
28 AllowPartial: test.partial,
29 }
30 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070031 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070032 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
33 }
34
35 size := proto.Size(want)
36 if size != len(wire) {
37 t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070038 }
39
Joe Tsai378c1322019-04-25 23:48:08 -070040 got := want.ProtoReflect().New().Interface()
Damien Neil96c229a2019-04-03 12:17:24 -070041 uopts := proto.UnmarshalOptions{
42 AllowPartial: test.partial,
43 }
44 if err := uopts.Unmarshal(wire, got); err != nil {
Joe Tsai8d30bbe2019-05-16 15:53:25 -070045 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070046 return
47 }
Joe Tsai96a44732020-01-03 19:52:28 -080048 if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
Joe Tsai8d30bbe2019-05-16 15:53:25 -070049 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070050 }
51 })
52 }
53 }
54}
55
56func TestEncodeDeterministic(t *testing.T) {
Damien Neild0b07492019-12-16 12:59:13 -080057 for _, test := range testValidMessages {
Damien Neil99f24c32019-03-13 17:06:42 -070058 for _, want := range test.decodeTo {
59 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070060 opts := proto.MarshalOptions{
61 Deterministic: true,
62 AllowPartial: test.partial,
63 }
64 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070065 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070066 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070067 }
Damien Neil96c229a2019-04-03 12:17:24 -070068 wire2, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070069 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070070 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070071 }
Damien Neil99f24c32019-03-13 17:06:42 -070072 if !bytes.Equal(wire, wire2) {
73 t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
74 }
75
Joe Tsai378c1322019-04-25 23:48:08 -070076 got := want.ProtoReflect().New().Interface()
Damien Neil96c229a2019-04-03 12:17:24 -070077 uopts := proto.UnmarshalOptions{
78 AllowPartial: test.partial,
79 }
80 if err := uopts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070081 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070082 return
83 }
Joe Tsai96a44732020-01-03 19:52:28 -080084 if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
Damien Neil61e93c72019-03-27 09:23:20 -070085 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070086 }
87 })
88 }
89 }
90}
Damien Neil96c229a2019-04-03 12:17:24 -070091
92func TestEncodeRequiredFieldChecks(t *testing.T) {
Damien Neild0b07492019-12-16 12:59:13 -080093 for _, test := range testValidMessages {
Damien Neil96c229a2019-04-03 12:17:24 -070094 if !test.partial {
95 continue
96 }
97 for _, m := range test.decodeTo {
98 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
99 _, err := proto.Marshal(m)
100 if err == nil {
101 t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
102 }
103 })
104 }
105 }
106}
Damien Neil3016b732019-04-07 12:43:10 -0700107
Joe Tsai9b22b932019-08-08 19:23:32 -0700108func TestEncodeAppend(t *testing.T) {
Damien Neil3016b732019-04-07 12:43:10 -0700109 want := []byte("prefix")
110 got := append([]byte(nil), want...)
111 got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
112 OptionalString: "value",
113 })
114 if err != nil {
115 t.Fatal(err)
116 }
117 if !bytes.HasPrefix(got, want) {
118 t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
119 }
120}
Joe Tsai9b22b932019-08-08 19:23:32 -0700121
Damien Neild0b07492019-12-16 12:59:13 -0800122func TestEncodeInvalidMessages(t *testing.T) {
123 for _, test := range testInvalidMessages {
124 for _, m := range test.decodeTo {
125 if !m.ProtoReflect().IsValid() {
126 continue
127 }
128 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
Damien Neild0b07492019-12-16 12:59:13 -0800129 opts := proto.MarshalOptions{
130 AllowPartial: test.partial,
131 }
132 got, err := opts.Marshal(m)
133 if err == nil {
134 t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, marshalText(m))
135 }
136 })
137 }
138 }
139}
140
Joe Tsai9b22b932019-08-08 19:23:32 -0700141func TestEncodeOneofNilWrapper(t *testing.T) {
142 m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
143 b, err := proto.Marshal(m)
144 if err != nil {
145 t.Fatal(err)
146 }
147 if len(b) > 0 {
148 t.Errorf("Marshal return non-empty, want empty")
149 }
150}
Damien Neil1e5516a2019-09-27 14:31:10 -0700151
152func TestMarshalAppendAllocations(t *testing.T) {
153 m := &test3pb.TestAllTypes{OptionalInt32: 1}
154 size := proto.Size(m)
155 const count = 1000
156 b := make([]byte, size)
157 // AllocsPerRun returns an integral value.
158 marshalAllocs := testing.AllocsPerRun(count, func() {
159 _, err := proto.MarshalOptions{}.MarshalAppend(b[:0], m)
160 if err != nil {
161 t.Fatal(err)
162 }
163 })
164 b = nil
165 marshalAppendAllocs := testing.AllocsPerRun(count, func() {
166 var err error
167 b, err = proto.MarshalOptions{}.MarshalAppend(b, m)
168 if err != nil {
169 t.Fatal(err)
170 }
171 })
172 if marshalAllocs != marshalAppendAllocs {
173 t.Errorf("%v allocs/op when writing to a preallocated buffer", marshalAllocs)
174 t.Errorf("%v allocs/op when repeatedly appending to a slice", marshalAppendAllocs)
175 t.Errorf("expect amortized allocs/op to be identical")
176 }
177}
Damien Neil01c0e8d2019-11-12 12:33:12 -0800178
179func TestEncodeOrder(t *testing.T) {
180 // We make no guarantees about the stability of wire marshal output.
181 // The order in which fields are marshaled may change over time.
182 // If deterministic marshaling is not enabled, it may change over
183 // successive calls to proto.Marshal in the same binary.
184 //
185 // Unfortunately, many users have come to rely on the specific current
186 // wire marshal output. Perhaps someday we will choose to deliberately
187 // change the marshal output; until that day comes, this test verifies
188 // that we don't unintentionally change it.
189 m := &orderpb.Message{
190 Field_1: proto.String("one"),
191 Field_2: proto.String("two"),
192 Field_20: proto.String("twenty"),
193 Oneof_1: &orderpb.Message_Field_10{"ten"},
194 }
195 proto.SetExtension(m, orderpb.E_Field_30, "thirty")
196 proto.SetExtension(m, orderpb.E_Field_31, "thirty-one")
197 proto.SetExtension(m, orderpb.E_Field_32, "thirty-two")
198 want := []pref.FieldNumber{
199 30, 31, 32, // extensions first, in number order
200 1, 2, 20, // non-extension, non-oneof in number order
201 10, // oneofs last, undefined order
202 }
203
204 // Test with deterministic serialization, since fields are not sorted without
205 // it when -tags=protoreflect.
206 b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
207 if err != nil {
208 t.Fatal(err)
209 }
210 var got []pref.FieldNumber
211 for len(b) > 0 {
212 num, _, n := wire.ConsumeField(b)
213 if n < 0 {
214 t.Fatal(wire.ParseError(n))
215 }
216 b = b[n:]
217 got = append(got, num)
218 }
219 if !reflect.DeepEqual(got, want) {
220 t.Errorf("unexpected field marshal order:\ngot: %v\nwant: %v\nmessage:\n%v", got, want, m)
221 }
222}