Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1 | // Copyright 2018 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 Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 5 | package proto_test |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | "testing" |
| 11 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/encoding/prototext" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 13 | "google.golang.org/protobuf/internal/encoding/pack" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 14 | "google.golang.org/protobuf/proto" |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 15 | "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 16 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 17 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
| 18 | test3pb "google.golang.org/protobuf/internal/testprotos/test3" |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 19 | ) |
| 20 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 21 | func TestDecode(t *testing.T) { |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 22 | for _, test := range testValidMessages { |
| 23 | if len(test.decodeTo) == 0 { |
| 24 | t.Errorf("%v: no test message types", test.desc) |
| 25 | } |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 26 | for _, want := range test.decodeTo { |
| 27 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
Damien Neil | a60e709 | 2020-01-28 14:53:44 -0800 | [diff] [blame^] | 28 | opts := test.unmarshalOptions |
| 29 | opts.AllowPartial = test.partial |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 30 | wire := append(([]byte)(nil), test.wire...) |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 31 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 32 | if err := opts.Unmarshal(wire, got); err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 33 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 34 | return |
| 35 | } |
| 36 | |
| 37 | // Aliasing check: Modifying the original wire bytes shouldn't |
| 38 | // affect the unmarshaled message. |
| 39 | for i := range wire { |
| 40 | wire[i] = 0 |
| 41 | } |
Joe Tsai | 96a4473 | 2020-01-03 19:52:28 -0800 | [diff] [blame] | 42 | if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 43 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want)) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 44 | } |
| 45 | }) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 50 | func TestDecodeRequiredFieldChecks(t *testing.T) { |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 51 | for _, test := range testValidMessages { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 52 | if !test.partial { |
| 53 | continue |
| 54 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 55 | for _, m := range test.decodeTo { |
| 56 | t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) { |
Damien Neil | a60e709 | 2020-01-28 14:53:44 -0800 | [diff] [blame^] | 57 | opts := test.unmarshalOptions |
| 58 | opts.AllowPartial = false |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 59 | got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message) |
| 60 | if err := proto.Unmarshal(test.wire, got); err == nil { |
| 61 | t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got)) |
| 62 | } |
| 63 | }) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 68 | func TestDecodeInvalidMessages(t *testing.T) { |
| 69 | for _, test := range testInvalidMessages { |
| 70 | if len(test.decodeTo) == 0 { |
| 71 | t.Errorf("%v: no test message types", test.desc) |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 72 | } |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 73 | for _, want := range test.decodeTo { |
| 74 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
Damien Neil | a60e709 | 2020-01-28 14:53:44 -0800 | [diff] [blame^] | 75 | opts := test.unmarshalOptions |
| 76 | opts.AllowPartial = test.partial |
Damien Neil | d0b0749 | 2019-12-16 12:59:13 -0800 | [diff] [blame] | 77 | got := want.ProtoReflect().New().Interface() |
| 78 | if err := opts.Unmarshal(test.wire, got); err == nil { |
| 79 | t.Errorf("Unmarshal unexpectedly succeeded\ninput bytes: [%x]\nMessage:\n%v", test.wire, marshalText(got)) |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Damien Neil | 8003f08 | 2019-08-02 15:13:00 -0700 | [diff] [blame] | 86 | func TestDecodeZeroLengthBytes(t *testing.T) { |
| 87 | // Verify that proto3 bytes fields don't give the mistaken |
| 88 | // impression that they preserve presence. |
| 89 | wire := pack.Message{ |
| 90 | pack.Tag{15, pack.BytesType}, pack.Bytes(nil), |
| 91 | }.Marshal() |
| 92 | m := &test3pb.TestAllTypes{} |
| 93 | if err := proto.Unmarshal(wire, m); err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if m.OptionalBytes != nil { |
| 97 | t.Errorf("unmarshal zero-length proto3 bytes field: got %v, want nil", m.OptionalBytes) |
| 98 | } |
| 99 | } |
| 100 | |
Joe Tsai | 9b22b93 | 2019-08-08 19:23:32 -0700 | [diff] [blame] | 101 | func TestDecodeOneofNilWrapper(t *testing.T) { |
| 102 | wire := pack.Message{ |
| 103 | pack.Tag{111, pack.VarintType}, pack.Varint(1111), |
| 104 | }.Marshal() |
| 105 | m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)} |
| 106 | if err := proto.Unmarshal(wire, m); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | if got := m.GetOneofUint32(); got != 1111 { |
| 110 | t.Errorf("GetOneofUint32() = %v, want %v", got, 1111) |
| 111 | } |
| 112 | } |
| 113 | |
Damien Neil | 5366f82 | 2019-12-05 14:54:35 -0800 | [diff] [blame] | 114 | func TestDecodeEmptyBytes(t *testing.T) { |
| 115 | // There's really nothing wrong with a nil entry in a [][]byte, |
| 116 | // but we take care to produce non-nil []bytes for zero-length |
| 117 | // byte strings, so test for it. |
| 118 | m := &testpb.TestAllTypes{} |
| 119 | b := pack.Message{ |
| 120 | pack.Tag{45, pack.BytesType}, pack.Bytes(nil), |
| 121 | }.Marshal() |
| 122 | if err := proto.Unmarshal(b, m); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if m.RepeatedBytes[0] == nil { |
| 126 | t.Errorf("unmarshaling repeated bytes field containing zero-length value: Got nil bytes, want non-nil") |
| 127 | } |
| 128 | } |
| 129 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 130 | func build(m proto.Message, opts ...buildOpt) proto.Message { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 131 | for _, opt := range opts { |
| 132 | opt(m) |
| 133 | } |
| 134 | return m |
| 135 | } |
| 136 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 137 | type buildOpt func(proto.Message) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 138 | |
Joe Tsai | 74615a3 | 2019-07-14 18:51:46 -0700 | [diff] [blame] | 139 | func unknown(raw protoreflect.RawFields) buildOpt { |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 140 | return func(m proto.Message) { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 141 | m.ProtoReflect().SetUnknown(raw) |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 145 | func extend(desc protoreflect.ExtensionType, value interface{}) buildOpt { |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 146 | return func(m proto.Message) { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 147 | proto.SetExtension(m, desc, value) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 150 | |
| 151 | func marshalText(m proto.Message) string { |
Joe Tsai | f2c4ddc | 2019-09-19 21:28:52 -0700 | [diff] [blame] | 152 | if m == nil { |
| 153 | return "<nil>\n" |
| 154 | } |
Joe Tsai | cd4a31e | 2019-09-14 19:14:24 -0700 | [diff] [blame] | 155 | b, _ := prototext.MarshalOptions{ |
| 156 | AllowPartial: true, |
| 157 | EmitUnknown: true, |
| 158 | Indent: "\t", |
| 159 | }.Marshal(m) |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 160 | return string(b) |
| 161 | } |