blob: 539f704a52b001de91be66f8cb03f97f6bfb0b74 [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"
10 "reflect"
11 "testing"
12
13 protoV1 "github.com/golang/protobuf/proto"
Damien Neil99f24c32019-03-13 17:06:42 -070014 "github.com/google/go-cmp/cmp"
Damien Neile89e6242019-05-13 23:55:40 -070015 "google.golang.org/protobuf/proto"
16 pref "google.golang.org/protobuf/reflect/protoreflect"
Damien Neil3016b732019-04-07 12:43:10 -070017
Damien Neile89e6242019-05-13 23:55:40 -070018 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neil99f24c32019-03-13 17:06:42 -070019)
20
21func TestEncode(t *testing.T) {
22 for _, test := range testProtos {
23 for _, want := range test.decodeTo {
24 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070025 opts := proto.MarshalOptions{
26 AllowPartial: test.partial,
27 }
28 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070029 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070030 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
31 }
32
33 size := proto.Size(want)
34 if size != len(wire) {
35 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 -070036 }
37
Damien Neile6f060f2019-04-23 17:11:02 -070038 got := newMessage(want)
Damien Neil96c229a2019-04-03 12:17:24 -070039 uopts := proto.UnmarshalOptions{
40 AllowPartial: test.partial,
41 }
42 if err := uopts.Unmarshal(wire, got); err != nil {
Damien Neil99f24c32019-03-13 17:06:42 -070043 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
44 return
45 }
46
Damien Neil96c229a2019-04-03 12:17:24 -070047 if test.invalidExtensions {
48 // Equal doesn't work on messages containing invalid extension data.
49 return
50 }
Damien Neile6f060f2019-04-23 17:11:02 -070051 if !proto.Equal(got, want) {
Damien Neil99f24c32019-03-13 17:06:42 -070052 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message)))
53 }
54 })
55 }
56 }
57}
58
59func TestEncodeDeterministic(t *testing.T) {
60 for _, test := range testProtos {
61 for _, want := range test.decodeTo {
62 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070063 opts := proto.MarshalOptions{
64 Deterministic: true,
65 AllowPartial: test.partial,
66 }
67 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070068 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070069 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070070 }
Damien Neil96c229a2019-04-03 12:17:24 -070071 wire2, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070072 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070073 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070074 }
Damien Neil99f24c32019-03-13 17:06:42 -070075 if !bytes.Equal(wire, wire2) {
76 t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
77 }
78
Damien Neile6f060f2019-04-23 17:11:02 -070079 got := newMessage(want)
Damien Neil96c229a2019-04-03 12:17:24 -070080 uopts := proto.UnmarshalOptions{
81 AllowPartial: test.partial,
82 }
83 if err := uopts.Unmarshal(wire, got); err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070084 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070085 return
86 }
87
Damien Neil96c229a2019-04-03 12:17:24 -070088 if test.invalidExtensions {
89 // Equal doesn't work on messages containing invalid extension data.
90 return
91 }
Damien Neile6f060f2019-04-23 17:11:02 -070092 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070093 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070094 }
95 })
96 }
97 }
98}
Damien Neil96c229a2019-04-03 12:17:24 -070099
Damien Neilbc310b52019-04-11 11:46:55 -0700100func TestEncodeInvalidUTF8(t *testing.T) {
101 for _, test := range invalidUTF8TestProtos {
102 for _, want := range test.decodeTo {
103 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
104 wire, err := proto.Marshal(want)
105 if !isErrInvalidUTF8(err) {
106 t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
107 }
Damien Neile6f060f2019-04-23 17:11:02 -0700108 got := newMessage(want)
Damien Neilbc310b52019-04-11 11:46:55 -0700109 if err := proto.Unmarshal(wire, got); !isErrInvalidUTF8(err) {
110 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
111 return
112 }
Damien Neile6f060f2019-04-23 17:11:02 -0700113 if !proto.Equal(got, want) {
Damien Neilbc310b52019-04-11 11:46:55 -0700114 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
115 }
116 })
117 }
118 }
119}
120
Damien Neil96c229a2019-04-03 12:17:24 -0700121func TestEncodeRequiredFieldChecks(t *testing.T) {
122 for _, test := range testProtos {
123 if !test.partial {
124 continue
125 }
126 for _, m := range test.decodeTo {
127 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
128 _, err := proto.Marshal(m)
129 if err == nil {
130 t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
131 }
132 })
133 }
134 }
135}
Damien Neil3016b732019-04-07 12:43:10 -0700136
137func TestMarshalAppend(t *testing.T) {
138 want := []byte("prefix")
139 got := append([]byte(nil), want...)
140 got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
141 OptionalString: "value",
142 })
143 if err != nil {
144 t.Fatal(err)
145 }
146 if !bytes.HasPrefix(got, want) {
147 t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
148 }
149}
Damien Neile6f060f2019-04-23 17:11:02 -0700150
151// newMessage returns a new message with the same type and extension fields as m.
152func newMessage(m proto.Message) proto.Message {
153 n := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
154 m.ProtoReflect().KnownFields().ExtensionTypes().Range(func(xt pref.ExtensionType) bool {
155 n.ProtoReflect().KnownFields().ExtensionTypes().Register(xt)
156 return true
157 })
158 return n
159}