Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 1 | // 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 Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 5 | package prototext_test |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "testing" |
| 9 | |
| 10 | protoV1 "github.com/golang/protobuf/proto" |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/encoding/prototext" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/internal/impl" |
| 13 | pimpl "google.golang.org/protobuf/internal/impl" |
| 14 | "google.golang.org/protobuf/internal/scalar" |
| 15 | "google.golang.org/protobuf/proto" |
| 16 | preg "google.golang.org/protobuf/reflect/protoregistry" |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 17 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 18 | "google.golang.org/protobuf/encoding/testprotos/pb2" |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 19 | "google.golang.org/protobuf/types/known/anypb" |
| 20 | "google.golang.org/protobuf/types/known/durationpb" |
| 21 | "google.golang.org/protobuf/types/known/emptypb" |
| 22 | "google.golang.org/protobuf/types/known/structpb" |
| 23 | "google.golang.org/protobuf/types/known/timestamppb" |
| 24 | "google.golang.org/protobuf/types/known/wrapperspb" |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func TestRoundTrip(t *testing.T) { |
| 28 | tests := []struct { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 29 | desc string |
| 30 | resolver *preg.Types |
| 31 | message proto.Message |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 32 | }{{ |
| 33 | desc: "well-known type fields set to empty messages", |
| 34 | message: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 35 | OptBool: &wrapperspb.BoolValue{}, |
| 36 | OptInt32: &wrapperspb.Int32Value{}, |
| 37 | OptInt64: &wrapperspb.Int64Value{}, |
| 38 | OptUint32: &wrapperspb.UInt32Value{}, |
| 39 | OptUint64: &wrapperspb.UInt64Value{}, |
| 40 | OptFloat: &wrapperspb.FloatValue{}, |
| 41 | OptDouble: &wrapperspb.DoubleValue{}, |
| 42 | OptString: &wrapperspb.StringValue{}, |
| 43 | OptBytes: &wrapperspb.BytesValue{}, |
| 44 | OptDuration: &durationpb.Duration{}, |
| 45 | OptTimestamp: ×tamppb.Timestamp{}, |
| 46 | OptStruct: &structpb.Struct{}, |
| 47 | OptList: &structpb.ListValue{}, |
| 48 | OptValue: &structpb.Value{}, |
| 49 | OptEmpty: &emptypb.Empty{}, |
| 50 | OptAny: &anypb.Any{}, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 51 | }, |
| 52 | }, { |
| 53 | desc: "well-known type scalar fields", |
| 54 | message: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 55 | OptBool: &wrapperspb.BoolValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 56 | Value: true, |
| 57 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 58 | OptInt32: &wrapperspb.Int32Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 59 | Value: -42, |
| 60 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 61 | OptInt64: &wrapperspb.Int64Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 62 | Value: -42, |
| 63 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 64 | OptUint32: &wrapperspb.UInt32Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 65 | Value: 0xff, |
| 66 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 67 | OptUint64: &wrapperspb.UInt64Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 68 | Value: 0xffff, |
| 69 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 70 | OptFloat: &wrapperspb.FloatValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 71 | Value: 1.234, |
| 72 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 73 | OptDouble: &wrapperspb.DoubleValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 74 | Value: 1.23e308, |
| 75 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 76 | OptString: &wrapperspb.StringValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 77 | Value: "谷歌", |
| 78 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 79 | OptBytes: &wrapperspb.BytesValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 80 | Value: []byte("\xe8\xb0\xb7\xe6\xad\x8c"), |
| 81 | }, |
| 82 | }, |
| 83 | }, { |
| 84 | desc: "well-known type time-related fields", |
| 85 | message: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 86 | OptDuration: &durationpb.Duration{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 87 | Seconds: -3600, |
| 88 | Nanos: -123, |
| 89 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 90 | OptTimestamp: ×tamppb.Timestamp{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 91 | Seconds: 1257894000, |
| 92 | Nanos: 123, |
| 93 | }, |
| 94 | }, |
| 95 | }, { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 96 | desc: "Struct field and different Value types", |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 97 | message: &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 98 | OptStruct: &structpb.Struct{ |
| 99 | Fields: map[string]*structpb.Value{ |
| 100 | "bool": &structpb.Value{ |
| 101 | Kind: &structpb.Value_BoolValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 102 | BoolValue: true, |
| 103 | }, |
| 104 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 105 | "double": &structpb.Value{ |
| 106 | Kind: &structpb.Value_NumberValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 107 | NumberValue: 3.1415, |
| 108 | }, |
| 109 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 110 | "null": &structpb.Value{ |
| 111 | Kind: &structpb.Value_NullValue{ |
| 112 | NullValue: structpb.NullValue_NULL_VALUE, |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 113 | }, |
| 114 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 115 | "string": &structpb.Value{ |
| 116 | Kind: &structpb.Value_StringValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 117 | StringValue: "string", |
| 118 | }, |
| 119 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 120 | "struct": &structpb.Value{ |
| 121 | Kind: &structpb.Value_StructValue{ |
| 122 | StructValue: &structpb.Struct{ |
| 123 | Fields: map[string]*structpb.Value{ |
| 124 | "bool": &structpb.Value{ |
| 125 | Kind: &structpb.Value_BoolValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 126 | BoolValue: false, |
| 127 | }, |
| 128 | }, |
| 129 | }, |
| 130 | }, |
| 131 | }, |
| 132 | }, |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 133 | "list": &structpb.Value{ |
| 134 | Kind: &structpb.Value_ListValue{ |
| 135 | ListValue: &structpb.ListValue{ |
| 136 | Values: []*structpb.Value{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 137 | { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 138 | Kind: &structpb.Value_BoolValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 139 | BoolValue: false, |
| 140 | }, |
| 141 | }, |
| 142 | { |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 143 | Kind: &structpb.Value_StringValue{ |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 144 | StringValue: "hello", |
| 145 | }, |
| 146 | }, |
| 147 | }, |
| 148 | }, |
| 149 | }, |
| 150 | }, |
| 151 | }, |
| 152 | }, |
| 153 | }, |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 154 | }, { |
| 155 | desc: "Any field without registered type", |
| 156 | resolver: preg.NewTypes(), |
| 157 | message: func() proto.Message { |
| 158 | m := &pb2.Nested{ |
| 159 | OptString: scalar.String("embedded inside Any"), |
| 160 | OptNested: &pb2.Nested{ |
| 161 | OptString: scalar.String("inception"), |
| 162 | }, |
| 163 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 164 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 165 | if err != nil { |
| 166 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 167 | } |
| 168 | return &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 169 | OptAny: &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 170 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 171 | Value: b, |
| 172 | }, |
| 173 | } |
| 174 | }(), |
| 175 | }, { |
| 176 | desc: "Any field with registered type", |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 177 | resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})), |
| 178 | message: func() *pb2.KnownTypes { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 179 | m := &pb2.Nested{ |
| 180 | OptString: scalar.String("embedded inside Any"), |
| 181 | OptNested: &pb2.Nested{ |
| 182 | OptString: scalar.String("inception"), |
| 183 | }, |
| 184 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 185 | b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 186 | if err != nil { |
| 187 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 188 | } |
| 189 | return &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 190 | OptAny: &anypb.Any{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 191 | TypeUrl: string(m.ProtoReflect().Descriptor().FullName()), |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 192 | Value: b, |
| 193 | }, |
| 194 | } |
| 195 | }(), |
| 196 | }, { |
| 197 | desc: "Any field containing Any message", |
| 198 | resolver: func() *preg.Types { |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 199 | mt1 := impl.Export{}.MessageTypeOf(&pb2.Nested{}) |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 200 | mt2 := impl.Export{}.MessageTypeOf(&anypb.Any{}) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 201 | return preg.NewTypes(mt1, mt2) |
| 202 | }(), |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 203 | message: func() *pb2.KnownTypes { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 204 | m1 := &pb2.Nested{ |
| 205 | OptString: scalar.String("message inside Any of another Any field"), |
| 206 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 207 | b1, err := proto.MarshalOptions{Deterministic: true}.Marshal(m1) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 208 | if err != nil { |
| 209 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 210 | } |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 211 | m2 := &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 212 | TypeUrl: "pb2.Nested", |
| 213 | Value: b1, |
| 214 | } |
Herbie Ong | 4d1c3be | 2019-03-15 18:22:36 -0700 | [diff] [blame] | 215 | b2, err := proto.MarshalOptions{Deterministic: true}.Marshal(m2) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 216 | if err != nil { |
| 217 | t.Fatalf("error in binary marshaling message for Any.value: %v", err) |
| 218 | } |
| 219 | return &pb2.KnownTypes{ |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 220 | OptAny: &anypb.Any{ |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 221 | TypeUrl: "google.protobuf.Any", |
| 222 | Value: b2, |
| 223 | }, |
| 224 | } |
| 225 | }(), |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 226 | }} |
| 227 | |
| 228 | for _, tt := range tests { |
| 229 | tt := tt |
| 230 | t.Run(tt.desc, func(t *testing.T) { |
| 231 | t.Parallel() |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 232 | b, err := prototext.MarshalOptions{Resolver: tt.resolver}.Marshal(tt.message) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 233 | if err != nil { |
| 234 | t.Errorf("Marshal() returned error: %v\n\n", err) |
| 235 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 236 | |
| 237 | gotMessage := new(pb2.KnownTypes) |
Joe Tsai | cdb7773 | 2019-05-14 16:05:06 -0700 | [diff] [blame] | 238 | err = prototext.UnmarshalOptions{Resolver: tt.resolver}.Unmarshal(b, gotMessage) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 239 | if err != nil { |
| 240 | t.Errorf("Unmarshal() returned error: %v\n\n", err) |
| 241 | } |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 242 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 243 | if !protoV1.Equal(gotMessage, tt.message.(protoV1.Message)) { |
Herbie Ong | 7065195 | 2018-12-13 14:19:50 -0800 | [diff] [blame] | 244 | t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", gotMessage, tt.message) |
| 245 | } |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 246 | }) |
| 247 | } |
| 248 | } |