2018-12-07 22:38:17 +00:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
2020-02-20 18:30:38 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
2018-12-07 22:38:17 +00:00
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-12-17 19:16:16 +00:00
|
|
|
package proto_test
|
2018-12-07 22:38:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-14 19:44:37 +00:00
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
2019-05-14 06:55:40 +00:00
|
|
|
"google.golang.org/protobuf/internal/encoding/pack"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2019-07-13 07:44:41 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2019-02-28 05:46:29 +00:00
|
|
|
|
2019-05-14 06:55:40 +00:00
|
|
|
testpb "google.golang.org/protobuf/internal/testprotos/test"
|
|
|
|
test3pb "google.golang.org/protobuf/internal/testprotos/test3"
|
2018-12-07 22:38:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecode(t *testing.T) {
|
2019-12-16 20:59:13 +00:00
|
|
|
for _, test := range testValidMessages {
|
|
|
|
if len(test.decodeTo) == 0 {
|
|
|
|
t.Errorf("%v: no test message types", test.desc)
|
|
|
|
}
|
2018-12-07 22:38:17 +00:00
|
|
|
for _, want := range test.decodeTo {
|
|
|
|
t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
|
2020-01-28 22:53:44 +00:00
|
|
|
opts := test.unmarshalOptions
|
|
|
|
opts.AllowPartial = test.partial
|
2018-12-07 22:38:17 +00:00
|
|
|
wire := append(([]byte)(nil), test.wire...)
|
2018-12-17 19:16:16 +00:00
|
|
|
got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
|
2019-04-03 19:17:24 +00:00
|
|
|
if err := opts.Unmarshal(wire, got); err != nil {
|
2020-01-06 23:44:09 +00:00
|
|
|
t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want))
|
2018-12-07 22:38:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aliasing check: Modifying the original wire bytes shouldn't
|
|
|
|
// affect the unmarshaled message.
|
|
|
|
for i := range wire {
|
|
|
|
wire[i] = 0
|
|
|
|
}
|
2020-01-04 03:52:28 +00:00
|
|
|
if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
|
2020-01-06 23:44:09 +00:00
|
|
|
t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want))
|
2018-12-07 22:38:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:17:24 +00:00
|
|
|
func TestDecodeRequiredFieldChecks(t *testing.T) {
|
2019-12-16 20:59:13 +00:00
|
|
|
for _, test := range testValidMessages {
|
2019-04-03 19:17:24 +00:00
|
|
|
if !test.partial {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, m := range test.decodeTo {
|
|
|
|
t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
|
2020-01-28 22:53:44 +00:00
|
|
|
opts := test.unmarshalOptions
|
|
|
|
opts.AllowPartial = false
|
2019-04-03 19:17:24 +00:00
|
|
|
got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
|
|
|
|
if err := proto.Unmarshal(test.wire, got); err == nil {
|
2020-01-06 23:44:09 +00:00
|
|
|
t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", prototext.Format(got))
|
2019-04-03 19:17:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 20:59:13 +00:00
|
|
|
func TestDecodeInvalidMessages(t *testing.T) {
|
|
|
|
for _, test := range testInvalidMessages {
|
|
|
|
if len(test.decodeTo) == 0 {
|
|
|
|
t.Errorf("%v: no test message types", test.desc)
|
2019-04-11 18:46:55 +00:00
|
|
|
}
|
2019-07-13 07:44:41 +00:00
|
|
|
for _, want := range test.decodeTo {
|
|
|
|
t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
|
2020-01-28 22:53:44 +00:00
|
|
|
opts := test.unmarshalOptions
|
|
|
|
opts.AllowPartial = test.partial
|
2019-12-16 20:59:13 +00:00
|
|
|
got := want.ProtoReflect().New().Interface()
|
|
|
|
if err := opts.Unmarshal(test.wire, got); err == nil {
|
2020-01-06 23:44:09 +00:00
|
|
|
t.Errorf("Unmarshal unexpectedly succeeded\ninput bytes: [%x]\nMessage:\n%v", test.wire, prototext.Format(got))
|
2019-07-13 07:44:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 22:13:00 +00:00
|
|
|
func TestDecodeZeroLengthBytes(t *testing.T) {
|
|
|
|
// Verify that proto3 bytes fields don't give the mistaken
|
|
|
|
// impression that they preserve presence.
|
|
|
|
wire := pack.Message{
|
|
|
|
pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
|
|
|
|
}.Marshal()
|
|
|
|
m := &test3pb.TestAllTypes{}
|
|
|
|
if err := proto.Unmarshal(wire, m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if m.OptionalBytes != nil {
|
|
|
|
t.Errorf("unmarshal zero-length proto3 bytes field: got %v, want nil", m.OptionalBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 02:23:32 +00:00
|
|
|
func TestDecodeOneofNilWrapper(t *testing.T) {
|
|
|
|
wire := pack.Message{
|
|
|
|
pack.Tag{111, pack.VarintType}, pack.Varint(1111),
|
|
|
|
}.Marshal()
|
|
|
|
m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
|
|
|
|
if err := proto.Unmarshal(wire, m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got := m.GetOneofUint32(); got != 1111 {
|
|
|
|
t.Errorf("GetOneofUint32() = %v, want %v", got, 1111)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:54:35 +00:00
|
|
|
func TestDecodeEmptyBytes(t *testing.T) {
|
|
|
|
// There's really nothing wrong with a nil entry in a [][]byte,
|
|
|
|
// but we take care to produce non-nil []bytes for zero-length
|
|
|
|
// byte strings, so test for it.
|
|
|
|
m := &testpb.TestAllTypes{}
|
|
|
|
b := pack.Message{
|
|
|
|
pack.Tag{45, pack.BytesType}, pack.Bytes(nil),
|
|
|
|
}.Marshal()
|
|
|
|
if err := proto.Unmarshal(b, m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if m.RepeatedBytes[0] == nil {
|
|
|
|
t.Errorf("unmarshaling repeated bytes field containing zero-length value: Got nil bytes, want non-nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:16:16 +00:00
|
|
|
func build(m proto.Message, opts ...buildOpt) proto.Message {
|
2018-12-07 22:38:17 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(m)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:16:16 +00:00
|
|
|
type buildOpt func(proto.Message)
|
2018-12-07 22:38:17 +00:00
|
|
|
|
2019-07-15 01:51:46 +00:00
|
|
|
func unknown(raw protoreflect.RawFields) buildOpt {
|
2018-12-17 19:16:16 +00:00
|
|
|
return func(m proto.Message) {
|
2019-04-26 06:48:08 +00:00
|
|
|
m.ProtoReflect().SetUnknown(raw)
|
2019-04-24 00:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 22:45:59 +00:00
|
|
|
func extend(desc protoreflect.ExtensionType, value interface{}) buildOpt {
|
2018-12-17 19:16:16 +00:00
|
|
|
return func(m proto.Message) {
|
2019-08-02 23:58:08 +00:00
|
|
|
proto.SetExtension(m, desc, value)
|
2018-12-07 22:38:17 +00:00
|
|
|
}
|
|
|
|
}
|