2018-12-07 14:38:17 -08:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
2020-02-20 10:30:38 -08:00
|
|
|
// Use of this source code is governed by a BSD-style
|
2018-12-07 14:38:17 -08:00
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-12-17 11:16:16 -08:00
|
|
|
package proto_test
|
2018-12-07 14:38:17 -08:00
|
|
|
|
|
|
|
import (
|
2020-03-17 16:49:25 -07:00
|
|
|
"bytes"
|
2018-12-07 14:38:17 -08:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-14 12:44:37 -07:00
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
2019-05-13 23:55:40 -07:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-07-13 00:44:41 -07:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2020-02-14 18:13:14 -08:00
|
|
|
"google.golang.org/protobuf/testing/protopack"
|
2019-02-27 21:46:29 -08:00
|
|
|
|
2020-07-24 14:25:43 -07:00
|
|
|
"google.golang.org/protobuf/internal/errors"
|
2019-05-13 23:55:40 -07:00
|
|
|
testpb "google.golang.org/protobuf/internal/testprotos/test"
|
|
|
|
test3pb "google.golang.org/protobuf/internal/testprotos/test3"
|
2018-12-07 14:38:17 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecode(t *testing.T) {
|
2019-12-16 12:59:13 -08:00
|
|
|
for _, test := range testValidMessages {
|
|
|
|
if len(test.decodeTo) == 0 {
|
|
|
|
t.Errorf("%v: no test message types", test.desc)
|
|
|
|
}
|
2018-12-07 14:38:17 -08:00
|
|
|
for _, want := range test.decodeTo {
|
|
|
|
t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
|
2020-01-28 14:53:44 -08:00
|
|
|
opts := test.unmarshalOptions
|
|
|
|
opts.AllowPartial = test.partial
|
2018-12-07 14:38:17 -08:00
|
|
|
wire := append(([]byte)(nil), test.wire...)
|
2018-12-17 11:16:16 -08:00
|
|
|
got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
|
2019-04-03 12:17:24 -07:00
|
|
|
if err := opts.Unmarshal(wire, got); err != nil {
|
2020-01-06 15:44:09 -08:00
|
|
|
t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want))
|
2018-12-07 14:38:17 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-17 16:49:25 -07:00
|
|
|
// Aliasing check: Unmarshal shouldn't modify the original wire
|
|
|
|
// bytes, and modifying the original wire bytes shouldn't affect
|
|
|
|
// the unmarshaled message.
|
|
|
|
if !bytes.Equal(test.wire, wire) {
|
|
|
|
t.Errorf("Unmarshal unexpectedly modified its input")
|
|
|
|
}
|
2018-12-07 14:38:17 -08:00
|
|
|
for i := range wire {
|
|
|
|
wire[i] = 0
|
|
|
|
}
|
2020-01-03 19:52:28 -08:00
|
|
|
if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
|
2020-01-06 15:44:09 -08:00
|
|
|
t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want))
|
2018-12-07 14:38:17 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 12:17:24 -07:00
|
|
|
func TestDecodeRequiredFieldChecks(t *testing.T) {
|
2019-12-16 12:59:13 -08:00
|
|
|
for _, test := range testValidMessages {
|
2019-04-03 12:17:24 -07: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 14:53:44 -08:00
|
|
|
opts := test.unmarshalOptions
|
|
|
|
opts.AllowPartial = false
|
2019-04-03 12:17:24 -07:00
|
|
|
got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
|
|
|
|
if err := proto.Unmarshal(test.wire, got); err == nil {
|
2020-01-06 15:44:09 -08:00
|
|
|
t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", prototext.Format(got))
|
2019-04-03 12:17:24 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:59:13 -08: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 11:46:55 -07:00
|
|
|
}
|
2019-07-13 00:44:41 -07:00
|
|
|
for _, want := range test.decodeTo {
|
|
|
|
t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
|
2020-01-28 14:53:44 -08:00
|
|
|
opts := test.unmarshalOptions
|
|
|
|
opts.AllowPartial = test.partial
|
2019-12-16 12:59:13 -08:00
|
|
|
got := want.ProtoReflect().New().Interface()
|
|
|
|
if err := opts.Unmarshal(test.wire, got); err == nil {
|
2020-01-06 15:44:09 -08:00
|
|
|
t.Errorf("Unmarshal unexpectedly succeeded\ninput bytes: [%x]\nMessage:\n%v", test.wire, prototext.Format(got))
|
2020-07-24 14:25:43 -07:00
|
|
|
} else if !errors.Is(err, proto.Error) {
|
|
|
|
t.Errorf("Unmarshal error is not a proto.Error: %v", err)
|
2019-07-13 00:44:41 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 15:13:00 -07:00
|
|
|
func TestDecodeZeroLengthBytes(t *testing.T) {
|
|
|
|
// Verify that proto3 bytes fields don't give the mistaken
|
|
|
|
// impression that they preserve presence.
|
2020-02-14 18:13:14 -08:00
|
|
|
wire := protopack.Message{
|
2020-04-28 14:44:38 -07:00
|
|
|
protopack.Tag{94, protopack.BytesType}, protopack.Bytes(nil),
|
2019-08-02 15:13:00 -07:00
|
|
|
}.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-08 19:23:32 -07:00
|
|
|
func TestDecodeOneofNilWrapper(t *testing.T) {
|
2020-02-14 18:13:14 -08:00
|
|
|
wire := protopack.Message{
|
|
|
|
protopack.Tag{111, protopack.VarintType}, protopack.Varint(1111),
|
2019-08-08 19:23:32 -07:00
|
|
|
}.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 14:54:35 -08: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{}
|
2020-02-14 18:13:14 -08:00
|
|
|
b := protopack.Message{
|
|
|
|
protopack.Tag{45, protopack.BytesType}, protopack.Bytes(nil),
|
2019-12-05 14:54:35 -08:00
|
|
|
}.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 11:16:16 -08:00
|
|
|
func build(m proto.Message, opts ...buildOpt) proto.Message {
|
2018-12-07 14:38:17 -08:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(m)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-12-17 11:16:16 -08:00
|
|
|
type buildOpt func(proto.Message)
|
2018-12-07 14:38:17 -08:00
|
|
|
|
2019-07-14 18:51:46 -07:00
|
|
|
func unknown(raw protoreflect.RawFields) buildOpt {
|
2018-12-17 11:16:16 -08:00
|
|
|
return func(m proto.Message) {
|
2019-04-25 23:48:08 -07:00
|
|
|
m.ProtoReflect().SetUnknown(raw)
|
2019-04-23 17:11:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:45:59 -07:00
|
|
|
func extend(desc protoreflect.ExtensionType, value interface{}) buildOpt {
|
2018-12-17 11:16:16 -08:00
|
|
|
return func(m proto.Message) {
|
2019-08-02 16:58:08 -07:00
|
|
|
proto.SetExtension(m, desc, value)
|
2018-12-07 14:38:17 -08:00
|
|
|
}
|
|
|
|
}
|