blob: 8f02d0c0a28144e765bd1131a0b3d4ff530f1d01 [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 }
Damien Neile6f060f2019-04-23 17:11:02 -070048 if !proto.Equal(got, want) {
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 }
Damien Neile6f060f2019-04-23 17:11:02 -070084 if !proto.Equal(got, want) {
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) {
129 t.Logf("%v %v", m, m.ProtoReflect().IsValid())
130 opts := proto.MarshalOptions{
131 AllowPartial: test.partial,
132 }
133 got, err := opts.Marshal(m)
134 if err == nil {
135 t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, marshalText(m))
136 }
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}