blob: cd4a7ef8c49ff13665f4fc34077a0c1cb2fa99ee [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
Damien Neil99f24c32019-03-13 17:06:42 -070012 "github.com/google/go-cmp/cmp"
Damien Neile89e6242019-05-13 23:55:40 -070013 "google.golang.org/protobuf/proto"
Damien Neil3016b732019-04-07 12:43:10 -070014
Damien Neile89e6242019-05-13 23:55:40 -070015 test3pb "google.golang.org/protobuf/internal/testprotos/test3"
Damien Neil99f24c32019-03-13 17:06:42 -070016)
17
18func TestEncode(t *testing.T) {
19 for _, test := range testProtos {
20 for _, want := range test.decodeTo {
21 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil96c229a2019-04-03 12:17:24 -070022 opts := proto.MarshalOptions{
23 AllowPartial: test.partial,
24 }
25 wire, err := opts.Marshal(want)
Damien Neil99f24c32019-03-13 17:06:42 -070026 if err != nil {
Damien Neil61e93c72019-03-27 09:23:20 -070027 t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
28 }
29
30 size := proto.Size(want)
31 if size != len(wire) {
32 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 -070033 }
34
Joe Tsai378c1322019-04-25 23:48:08 -070035 got := want.ProtoReflect().New().Interface()
Damien Neil96c229a2019-04-03 12:17:24 -070036 uopts := proto.UnmarshalOptions{
37 AllowPartial: test.partial,
38 }
39 if err := uopts.Unmarshal(wire, got); err != nil {
Joe Tsai8d30bbe2019-05-16 15:53:25 -070040 t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070041 return
42 }
43
Damien Neil96c229a2019-04-03 12:17:24 -070044 if test.invalidExtensions {
45 // Equal doesn't work on messages containing invalid extension data.
46 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) {
57 for _, test := range testProtos {
58 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 }
84
Damien Neil96c229a2019-04-03 12:17:24 -070085 if test.invalidExtensions {
86 // Equal doesn't work on messages containing invalid extension data.
87 return
88 }
Damien Neile6f060f2019-04-23 17:11:02 -070089 if !proto.Equal(got, want) {
Damien Neil61e93c72019-03-27 09:23:20 -070090 t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
Damien Neil99f24c32019-03-13 17:06:42 -070091 }
92 })
93 }
94 }
95}
Damien Neil96c229a2019-04-03 12:17:24 -070096
Damien Neilbc310b52019-04-11 11:46:55 -070097func TestEncodeInvalidUTF8(t *testing.T) {
98 for _, test := range invalidUTF8TestProtos {
99 for _, want := range test.decodeTo {
100 t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
Damien Neil8c86fc52019-06-19 09:28:29 -0700101 _, err := proto.Marshal(want)
102 if err == nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700103 t.Errorf("Marshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
104 }
Damien Neilbc310b52019-04-11 11:46:55 -0700105 })
106 }
107 }
108}
109
Damien Neil96c229a2019-04-03 12:17:24 -0700110func TestEncodeRequiredFieldChecks(t *testing.T) {
111 for _, test := range testProtos {
112 if !test.partial {
113 continue
114 }
115 for _, m := range test.decodeTo {
116 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
117 _, err := proto.Marshal(m)
118 if err == nil {
119 t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
120 }
121 })
122 }
123 }
124}
Damien Neil3016b732019-04-07 12:43:10 -0700125
126func TestMarshalAppend(t *testing.T) {
127 want := []byte("prefix")
128 got := append([]byte(nil), want...)
129 got, err := proto.MarshalOptions{}.MarshalAppend(got, &test3pb.TestAllTypes{
130 OptionalString: "value",
131 })
132 if err != nil {
133 t.Fatal(err)
134 }
135 if !bytes.HasPrefix(got, want) {
136 t.Fatalf("MarshalAppend modified prefix: got %v, want prefix %v", got, want)
137 }
138}