blob: bfeff6a658fceb0d549967f974b754f014a934f4 [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 Neil99f24c32019-03-13 17:06:42 -070010 "testing"
11
12 protoV1 "github.com/golang/protobuf/proto"
Damien Neil99f24c32019-03-13 17:06:42 -070013 "github.com/google/go-cmp/cmp"
Damien Neile89e6242019-05-13 23:55:40 -070014 "google.golang.org/protobuf/proto"
Damien Neil3016b732019-04-07 12:43:10 -070015
Damien Neile89e6242019-05-13 23:55:40 -070016 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neil99f24c32019-03-13 17:06:42 -070017)
18
19func TestEncode(t *testing.T) {
20 for _, test := range testProtos {
21 for _, want := range test.decodeTo {
22 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070023 opts := proto.MarshalOptions{
24 AllowPartial: test.partial,
25 }
26 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070027 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070028 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
29 }
30
31 size := proto.Size(want)
32 if size != len(wire) {
33 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 -070034 }
35
Joe Tsai378c1322019-04-25 23:48:08 -070036 got := want.ProtoReflect().New().Interface()
Damien Neil96c229a2019-04-03 12:17:24 -070037 uopts := proto.UnmarshalOptions{
38 AllowPartial: test.partial,
39 }
40 if err := uopts.Unmarshal(wire, got); err != nil {
Damien Neil99f24c32019-03-13 17:06:42 -070041 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
42 return
43 }
44
Damien Neil96c229a2019-04-03 12:17:24 -070045 if test.invalidExtensions {
46 // Equal doesn't work on messages containing invalid extension data.
47 return
48 }
Damien Neile6f060f2019-04-23 17:11:02 -070049 if !proto.Equal(got, want) {
Damien Neil99f24c32019-03-13 17:06:42 -070050 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message)))
51 }
52 })
53 }
54 }
55}
56
57func TestEncodeDeterministic(t *testing.T) {
58 for _, test := range testProtos {
59 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 {
Damien Neil61e93c72019-03-27 09:23:20 -070067 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(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 {
Damien Neil61e93c72019-03-27 09:23:20 -070071 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(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 {
Damien Neil61e93c72019-03-27 09:23:20 -070082 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070083 return
84 }
85
Damien Neil96c229a2019-04-03 12:17:24 -070086 if test.invalidExtensions {
87 // Equal doesn't work on messages containing invalid extension data.
88 return
89 }
Damien Neile6f060f2019-04-23 17:11:02 -070090 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070091 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070092 }
93 })
94 }
95 }
96}
Damien Neil96c229a2019-04-03 12:17:24 -070097
Damien Neilbc310b52019-04-11 11:46:55 -070098func TestEncodeInvalidUTF8(t *testing.T) {
99 for _, test := range invalidUTF8TestProtos {
100 for _, want := range test.decodeTo {
101 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
102 wire, err := proto.Marshal(want)
103 if !isErrInvalidUTF8(err) {
104 t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
105 }
Joe Tsai378c1322019-04-25 23:48:08 -0700106 got := want.ProtoReflect().New().Interface()
Damien Neilbc310b52019-04-11 11:46:55 -0700107 if err := proto.Unmarshal(wire, got); !isErrInvalidUTF8(err) {
108 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
109 return
110 }
Damien Neile6f060f2019-04-23 17:11:02 -0700111 if !proto.Equal(got, want) {
Damien Neilbc310b52019-04-11 11:46:55 -0700112 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
113 }
114 })
115 }
116 }
117}
118
Damien Neil96c229a2019-04-03 12:17:24 -0700119func TestEncodeRequiredFieldChecks(t *testing.T) {
120 for _, test := range testProtos {
121 if !test.partial {
122 continue
123 }
124 for _, m := range test.decodeTo {
125 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
126 _, err := proto.Marshal(m)
127 if err == nil {
128 t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
129 }
130 })
131 }
132 }
133}
Damien Neil3016b732019-04-07 12:43:10 -0700134
135func TestMarshalAppend(t *testing.T) {
136 want := []byte("prefix")
137 got := append([]byte(nil), want...)
138 got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
139 OptionalString: "value",
140 })
141 if err != nil {
142 t.Fatal(err)
143 }
144 if !bytes.HasPrefix(got, want) {
145 t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
146 }
147}