| 1 | // Copyright 2022 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 | "testing" |
| 19 | |
| 20 | "google.golang.org/protobuf/proto" |
| 21 | "google.golang.org/protobuf/reflect/protodesc" |
| 22 | "google.golang.org/protobuf/reflect/protoreflect" |
| 23 | |
| 24 | "github.com/google/cel-go/checker/decls" |
| 25 | |
| 26 | proto2pb "github.com/google/cel-go/test/proto2pb" |
| 27 | proto3pb "github.com/google/cel-go/test/proto3pb" |
| 28 | exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 29 | descpb "google.golang.org/protobuf/types/descriptorpb" |
| 30 | ) |
| 31 | |
| 32 | func TestFileDescriptionGetExtensions(t *testing.T) { |
| 33 | pbdb := NewDb() |
| 34 | registerFileDescriptor(t, pbdb, &proto2pb.TestAllTypes{}) |
| 35 | registerFileDescriptor(t, pbdb, &proto2pb.ExternalMessageType{}) |
| 36 | ex, found := pbdb.DescribeType("google.expr.proto2.test.ExampleType") |
| 37 | if !found { |
| 38 | t.Fatal("ExampleType not found") |
| 39 | } |
| 40 | tests := []struct { |
| 41 | field string |
| 42 | fieldType *exprpb.Type |
| 43 | }{ |
| 44 | { |
| 45 | field: "google.expr.proto2.test.nested_example", |
| 46 | fieldType: decls.NewObjectType("google.expr.proto2.test.ExampleType"), |
| 47 | }, |
| 48 | { |
| 49 | field: "google.expr.proto2.test.int32_ext", |
| 50 | fieldType: decls.Int, |
| 51 | }, |
| 52 | { |
| 53 | field: "google.expr.proto2.test.ExtendedExampleType.extended_examples", |
| 54 | fieldType: decls.NewListType(decls.String), |
| 55 | }, |
| 56 | { |
| 57 | field: "google.expr.proto2.test.ExtendedExampleType.enum_ext", |
| 58 | fieldType: decls.Int, |
| 59 | }, |
| 60 | { |
| 61 | field: "google.expr.proto2.test.ExternalMessageType.int64_ext", |
| 62 | fieldType: decls.Int, |
| 63 | }, |
| 64 | } |
| 65 | for _, tst := range tests { |
| 66 | tc := tst |
| 67 | t.Run(tc.field, func(t *testing.T) { |
| 68 | field, found := ex.FieldByName(tc.field) |
| 69 | if !found { |
| 70 | t.Fatalf("%s extension not found", tc.field) |
| 71 | } |
| 72 | if !proto.Equal(field.CheckedType(), tc.fieldType) { |
| 73 | t.Errorf("Got %v, wanted %v", field.CheckedType(), tc.fieldType) |
| 74 | } |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestFileDescriptionGetTypes(t *testing.T) { |
| 80 | pbdb := NewDb() |
| 81 | fd, err := pbdb.RegisterMessage(&proto3pb.TestAllTypes{}) |
| 82 | if err != nil { |
| 83 | t.Error(err) |
| 84 | } |
| 85 | expected := []string{ |
| 86 | "google.expr.proto3.test.TestAllTypes", |
| 87 | "google.expr.proto3.test.TestAllTypes.NestedMessage", |
| 88 | "google.expr.proto3.test.TestAllTypes.MapStringStringEntry", |
| 89 | "google.expr.proto3.test.TestAllTypes.MapInt64NestedTypeEntry", |
| 90 | "google.expr.proto3.test.NestedTestAllTypes"} |
| 91 | if len(fd.GetTypeNames()) != len(expected) { |
| 92 | t.Errorf("got '%v', wanted '%v'", fd.GetTypeNames(), expected) |
| 93 | } |
| 94 | for _, tn := range fd.GetTypeNames() { |
| 95 | var found = false |
| 96 | for _, elem := range expected { |
| 97 | if elem == tn { |
| 98 | found = true |
| 99 | break |
| 100 | } |
| 101 | } |
| 102 | if !found { |
| 103 | t.Error("Unexpected type name", tn) |
| 104 | } |
| 105 | } |
| 106 | for _, typeName := range fd.GetTypeNames() { |
| 107 | td, found := fd.GetTypeDescription(typeName) |
| 108 | if !found { |
| 109 | t.Fatalf("fd.GetTypeDescription(%v) returned not found", typeName) |
| 110 | } |
| 111 | if td.Name() != typeName { |
| 112 | t.Error("Indexed type name not equal to descriptor type name", td, typeName) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func TestFileDescriptionGetEnumNames(t *testing.T) { |
| 118 | pbdb := NewDb() |
| 119 | fd, err := pbdb.RegisterMessage(&proto3pb.TestAllTypes{}) |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | expected := map[string]int32{ |
| 124 | "google.expr.proto3.test.TestAllTypes.NestedEnum.FOO": 0, |
| 125 | "google.expr.proto3.test.TestAllTypes.NestedEnum.BAR": 1, |
| 126 | "google.expr.proto3.test.TestAllTypes.NestedEnum.BAZ": 2, |
| 127 | "google.expr.proto3.test.GlobalEnum.GOO": 0, |
| 128 | "google.expr.proto3.test.GlobalEnum.GAR": 1, |
| 129 | "google.expr.proto3.test.GlobalEnum.GAZ": 2} |
| 130 | if len(expected) != len(fd.GetEnumNames()) { |
| 131 | t.Error("Count of enum names does not match expected'", |
| 132 | fd.GetEnumNames(), expected) |
| 133 | } |
| 134 | for _, enumName := range fd.GetEnumNames() { |
| 135 | if enumVal, found := expected[enumName]; found { |
| 136 | ed, found := fd.GetEnumDescription(enumName) |
| 137 | if !found { |
| 138 | t.Fatalf("fd.GetEnumDescription(%v) returned not found", enumName) |
| 139 | } |
| 140 | if ed.Value() != enumVal { |
| 141 | t.Errorf("Enum did not have expected value. %s got '%v', wanted '%v'", |
| 142 | enumName, ed.Value(), enumVal) |
| 143 | } |
| 144 | } else { |
| 145 | t.Errorf("Enum value not found for: %s", enumName) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestFileDescriptionGetImportedEnumNames(t *testing.T) { |
| 151 | pbdb := NewDb() |
| 152 | fdMap := CollectFileDescriptorSet(&proto3pb.TestAllTypes{}) |
| 153 | fileSet := make([]*descpb.FileDescriptorProto, 0, len(fdMap)) |
| 154 | for _, fd := range fdMap { |
| 155 | fileSet = append(fileSet, protodesc.ToFileDescriptorProto(fd)) |
| 156 | } |
| 157 | fds := &descpb.FileDescriptorSet{File: fileSet} |
| 158 | files, err := protodesc.NewFiles(fds) |
| 159 | if err != nil { |
| 160 | t.Fatalf("protodesc.NewFiles() failed: %v", err) |
| 161 | } |
| 162 | files.RangeFiles(func(fd protoreflect.FileDescriptor) bool { |
| 163 | t.Logf("registering file: %v", fd.Path()) |
| 164 | _, err = pbdb.RegisterDescriptor(fd) |
| 165 | if err != nil { |
| 166 | t.Fatalf("pbdb.RegisterDescriptor(%v) failed: %v", fd, err) |
| 167 | return false |
| 168 | } |
| 169 | return true |
| 170 | }) |
| 171 | imported := map[string]int32{ |
| 172 | "google.expr.proto3.test.ImportedGlobalEnum.IMPORT_FOO": 0, |
| 173 | "google.expr.proto3.test.ImportedGlobalEnum.IMPORT_BAR": 1, |
| 174 | "google.expr.proto3.test.ImportedGlobalEnum.IMPORT_BAZ": 2, |
| 175 | } |
| 176 | for enumName, value := range imported { |
| 177 | ed, found := pbdb.DescribeEnum(enumName) |
| 178 | if !found { |
| 179 | t.Fatalf("pbdb.DescribeEnum(%q) returned not found", enumName) |
| 180 | } |
| 181 | if ed.Value() != value { |
| 182 | t.Errorf("Got %v, wanted %v for enum %s", ed, value, enumName) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func registerFileDescriptor(t *testing.T, pbdb *Db, pbMsg proto.Message) { |
| 188 | fileDesc := pbMsg.ProtoReflect().Descriptor().ParentFile() |
| 189 | _, err := pbdb.RegisterDescriptor(fileDesc) |
| 190 | if err != nil { |
| 191 | t.Fatalf("pbdb.RegisterDescriptor(%v) failed: %v", fileDesc, err) |
| 192 | } |
| 193 | } |