| 1 | // Copyright 2020 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package pb |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | |
| 21 | "google.golang.org/protobuf/reflect/protodesc" |
| 22 | "google.golang.org/protobuf/reflect/protoreflect" |
| 23 | |
| 24 | proto2pb "github.com/google/cel-go/test/proto2pb" |
| 25 | proto3pb "github.com/google/cel-go/test/proto3pb" |
| 26 | descpb "google.golang.org/protobuf/types/descriptorpb" |
| 27 | dynamicpb "google.golang.org/protobuf/types/dynamicpb" |
| 28 | durationpb "google.golang.org/protobuf/types/known/durationpb" |
| 29 | tpb "google.golang.org/protobuf/types/known/timestamppb" |
| 30 | ) |
| 31 | |
| 32 | func TestDbCopy(t *testing.T) { |
| 33 | clone := DefaultDb.Copy() |
| 34 | if !reflect.DeepEqual(clone, DefaultDb) { |
| 35 | t.Error("db.Copy() did not result in equivalent objects.") |
| 36 | } |
| 37 | _, err := clone.RegisterMessage(&proto3pb.TestAllTypes{}) |
| 38 | if err != nil { |
| 39 | t.Fatalf("db.RegisterMessage() failed: %v", err) |
| 40 | } |
| 41 | if reflect.DeepEqual(clone, DefaultDb) { |
| 42 | t.Error("db.RegisterMessage() altered the default db as well") |
| 43 | } |
| 44 | |
| 45 | // modify the clone after deriving another copy. |
| 46 | clone2 := clone.Copy() |
| 47 | if !reflect.DeepEqual(clone, clone2) { |
| 48 | t.Error("db.Copy() did not result in equivalent objects.") |
| 49 | } |
| 50 | _, err = clone.RegisterMessage(&proto2pb.TestAllTypes{}) |
| 51 | if err != nil { |
| 52 | t.Fatalf("Db.RegisterMessage() failed: %v", err) |
| 53 | } |
| 54 | if reflect.DeepEqual(clone, clone2) { |
| 55 | t.Error("clone modification altered derived clone2 also") |
| 56 | } |
| 57 | |
| 58 | // modify the clone2 with some extensions and assert equivalence to clone3 |
| 59 | _, err = clone2.RegisterMessage(&proto2pb.TestAllTypes{}) |
| 60 | if err != nil { |
| 61 | t.Fatalf("Db.RegisterMessage() failed: %v", err) |
| 62 | } |
| 63 | _, err = clone2.RegisterMessage(&proto2pb.ExternalMessageType{}) |
| 64 | if err != nil { |
| 65 | t.Fatalf("Db.RegisterMessage() failed: %v", err) |
| 66 | } |
| 67 | clone3 := clone2.Copy() |
| 68 | if !reflect.DeepEqual(clone2, clone3) { |
| 69 | t.Error("db.Copy() did not result in equivalent objects.") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func BenchmarkDbCopy(b *testing.B) { |
| 74 | for i := 0; i < b.N; i++ { |
| 75 | NewDb().Copy() |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestProtoReflectRoundTrip(t *testing.T) { |
| 80 | msg := &proto3pb.TestAllTypes{SingleBool: true} |
| 81 | fdMap := CollectFileDescriptorSet(msg) |
| 82 | files := []*descpb.FileDescriptorProto{} |
| 83 | for _, fd := range fdMap { |
| 84 | files = append(files, protodesc.ToFileDescriptorProto(fd)) |
| 85 | } |
| 86 | // Round-tripping from a protoreflect.FileDescriptor to a FileDescriptorSet and back |
| 87 | // will result in completely independent copies of the protoreflect.FileDescriptor |
| 88 | // whose values are incompatible with each other. |
| 89 | // |
| 90 | // This test showcases what happens when the protoregistry.GlobalFiles values are |
| 91 | // used when a given protoreflect.FileDescriptor is linked into the binary. |
| 92 | fds := &descpb.FileDescriptorSet{File: files} |
| 93 | pbReg, err := protodesc.NewFiles(fds) |
| 94 | if err != nil { |
| 95 | t.Fatalf("protodesc.NewFiles() failed: %v", err) |
| 96 | } |
| 97 | pbdb := NewDb() |
| 98 | pbReg.RangeFiles(func(fd protoreflect.FileDescriptor) bool { |
| 99 | _, err := pbdb.RegisterDescriptor(fd) |
| 100 | if err != nil { |
| 101 | t.Fatalf("pbdb.RegisterDecriptor(%v) failed: %v", fd, err) |
| 102 | } |
| 103 | return true |
| 104 | }) |
| 105 | msgType, found := pbdb.DescribeType("google.expr.proto3.test.TestAllTypes") |
| 106 | if !found { |
| 107 | t.Fatal("pbdb.DescribeType(google.expr.proto3.test.TestAllTypes) failed") |
| 108 | } |
| 109 | boolField, found := msgType.FieldByName("single_bool") |
| 110 | if !found { |
| 111 | t.Fatal("msgType.FieldByName(single_bool) failed") |
| 112 | } |
| 113 | val, err := boolField.GetFrom(msg) |
| 114 | if err != nil { |
| 115 | t.Errorf("boolField.GetFrom(msg) failed: %v", err) |
| 116 | } |
| 117 | if val != true { |
| 118 | t.Errorf("got TestAllTypes.single_bool %v, wanted true", val) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestMerge(t *testing.T) { |
| 123 | timestampPB := &tpb.Timestamp{} |
| 124 | fileDesc := protodesc.ToFileDescriptorProto(timestampPB.ProtoReflect().Descriptor().ParentFile()) |
| 125 | fds := &descpb.FileDescriptorSet{} |
| 126 | fds.File = append(fds.File, fileDesc) |
| 127 | pbFiles, err := protodesc.NewFiles(fds) |
| 128 | if err != nil { |
| 129 | t.Fatalf("protodesc.NewFiles(%v) failed: %v", fds, err) |
| 130 | } |
| 131 | tsDesc, err := pbFiles.FindDescriptorByName(protoreflect.FullName("google.protobuf.Timestamp")) |
| 132 | if err != nil { |
| 133 | t.Fatalf("pbFiles.FindDescriptorByName() failed for Timestamp: %v", err) |
| 134 | } |
| 135 | tsMsgDesc := tsDesc.(protoreflect.MessageDescriptor) |
| 136 | tsType := dynamicpb.NewMessageType(tsMsgDesc) |
| 137 | dynTimestampPB := tsType.New() |
| 138 | dynTimestampPB.Set(tsMsgDesc.Fields().ByName(protoreflect.Name("seconds")), protoreflect.ValueOf(int64(123))) |
| 139 | err = Merge(timestampPB, dynTimestampPB.Interface()) |
| 140 | if err != nil { |
| 141 | t.Fatalf("Merge() failed: %v", err) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestMergeError(t *testing.T) { |
| 146 | timestampPB := &tpb.Timestamp{} |
| 147 | durationPB := &durationpb.Duration{} |
| 148 | err := Merge(timestampPB, durationPB) |
| 149 | if err == nil || err.Error() != "pb.Merge() arguments must be the same type. got: google.protobuf.Timestamp, google.protobuf.Duration" { |
| 150 | t.Fatalf("got err: %v, wanted bad argument error", err) |
| 151 | } |
| 152 | } |