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