| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package ext |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"google.golang.org/protobuf/proto" |
| 22 |
|
| 23 |
"github.com/google/cel-go/cel" |
| 24 |
"github.com/google/cel-go/common/types" |
| 25 |
"github.com/google/cel-go/common/types/ref" |
| 26 |
"github.com/google/cel-go/common/types/traits" |
| 27 |
"github.com/google/cel-go/test" |
| 28 |
|
| 29 |
proto2pb "github.com/google/cel-go/test/proto2pb" |
| 30 |
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" |
| 31 |
) |
| 32 |
|
| 33 |
func TestProtos(t *testing.T) { |
| 34 |
var protosTests = []struct { |
| 35 |
expr string |
| 36 |
}{ |
| 37 |
|
| 38 |
{expr: `proto.getExt(ExampleType{}, google.expr.proto2.test.int32_ext) == 0`}, |
| 39 |
{expr: `!proto.hasExt(ExampleType{}, google.expr.proto2.test.int32_wrapper_ext)`}, |
| 40 |
{expr: `proto.getExt(ExampleType{}, google.expr.proto2.test.int32_wrapper_ext) == null`}, |
| 41 |
{expr: `!proto.hasExt(ExampleType{}, google.expr.proto2.test.nested_example)`}, |
| 42 |
{expr: `proto.getExt(ExampleType{}, google.expr.proto2.test.nested_example) == ExampleType{}`}, |
| 43 |
{expr: `proto.getExt(ExampleType{}, google.expr.proto2.test.ExtendedExampleType.extended_examples) == []`}, |
| 44 |
{expr: `proto.getExt(ExampleType{}, google.expr.proto2.test.ExtendedExampleType.enum_ext) == GlobalEnum.GOO`}, |
| 45 |
{expr: "ExampleType{`in`: 64}.`in` == 64"}, |
| 46 |
|
| 47 |
|
| 48 |
{expr: `proto.getExt(msg, google.expr.proto2.test.int32_ext) == 42`}, |
| 49 |
{expr: "msg.`google.expr.proto2.test.int32_ext` == 42"}, |
| 50 |
{expr: `proto.getExt(msg, google.expr.proto2.test.int32_wrapper_ext) == 21`}, |
| 51 |
{expr: "msg.`google.expr.proto2.test.int32_wrapper_ext` == 21"}, |
| 52 |
{expr: `proto.hasExt(msg, google.expr.proto2.test.nested_example)`}, |
| 53 |
{expr: "has(msg.`google.expr.proto2.test.nested_example`)"}, |
| 54 |
{expr: `proto.getExt(msg, google.expr.proto2.test.nested_example) == ExampleType{name: 'nested'}`}, |
| 55 |
{expr: "msg.`google.expr.proto2.test.nested_example` == ExampleType{name: 'nested'}"}, |
| 56 |
{expr: `proto.getExt(msg, google.expr.proto2.test.ExtendedExampleType.extended_examples) == ['example1', 'example2']`}, |
| 57 |
{expr: "msg.`google.expr.proto2.test.ExtendedExampleType.extended_examples` == ['example1', 'example2']"}, |
| 58 |
{expr: `proto.getExt(msg, google.expr.proto2.test.ExtendedExampleType.enum_ext) == GlobalEnum.GAZ`}, |
| 59 |
{expr: "msg.`google.expr.proto2.test.ExtendedExampleType.enum_ext` == GlobalEnum.GAZ"}, |
| 60 |
} |
| 61 |
|
| 62 |
env := testProtosEnv(t) |
| 63 |
for i, tst := range protosTests { |
| 64 |
tc := tst |
| 65 |
t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) { |
| 66 |
var asts []*cel.Ast |
| 67 |
pAst, iss := env.Parse(tc.expr) |
| 68 |
if iss.Err() != nil { |
| 69 |
t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err()) |
| 70 |
} |
| 71 |
asts = append(asts, pAst) |
| 72 |
cAst, iss := env.Check(pAst) |
| 73 |
if iss.Err() != nil { |
| 74 |
t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err()) |
| 75 |
} |
| 76 |
asts = append(asts, cAst) |
| 77 |
|
| 78 |
for _, ast := range asts { |
| 79 |
prg, err := env.Program(ast) |
| 80 |
if err != nil { |
| 81 |
t.Fatal(err) |
| 82 |
} |
| 83 |
out, _, err := prg.Eval(map[string]any{"msg": msgWithExtensions()}) |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
if out.Value() != true { |
| 88 |
t.Errorf("got %v, wanted true for expr: %s", out.Value(), tc.expr) |
| 89 |
} |
| 90 |
} |
| 91 |
}) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func TestProtosNonMatch(t *testing.T) { |
| 96 |
var protosTests = []struct { |
| 97 |
expr string |
| 98 |
}{ |
| 99 |
|
| 100 |
|
| 101 |
{ |
| 102 |
expr: `msg.getExt("google.expr.proto2.test.int32_ext", 0) == 42`, |
| 103 |
}, |
| 104 |
|
| 105 |
{ |
| 106 |
expr: `dyn(msg).getExt("google.expr.proto2.test.int32_ext", 0) == 42`, |
| 107 |
}, |
| 108 |
|
| 109 |
{ |
| 110 |
expr: `msg.hasExt("google.expr.proto2.test.int32_ext", 0)`, |
| 111 |
}, |
| 112 |
} |
| 113 |
env := testProtosEnv(t, |
| 114 |
cel.Function("getExt", |
| 115 |
cel.MemberOverload("msg_getExt_field_default", |
| 116 |
[]*cel.Type{cel.DynType, cel.StringType, cel.DynType}, |
| 117 |
cel.DynType), |
| 118 |
cel.SingletonFunctionBinding(func(args ...ref.Val) ref.Val { |
| 119 |
msg := args[0] |
| 120 |
field := args[1] |
| 121 |
indexer := msg.(traits.Indexer) |
| 122 |
return indexer.Get(field) |
| 123 |
})), |
| 124 |
cel.Function("hasExt", |
| 125 |
cel.MemberOverload("msg_hasExt_field_any", |
| 126 |
[]*cel.Type{cel.DynType, cel.StringType, cel.DynType}, |
| 127 |
cel.BoolType), |
| 128 |
cel.SingletonFunctionBinding(func(args ...ref.Val) ref.Val { |
| 129 |
return types.True |
| 130 |
}))) |
| 131 |
for i, tst := range protosTests { |
| 132 |
tc := tst |
| 133 |
t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) { |
| 134 |
var asts []*cel.Ast |
| 135 |
pAst, iss := env.Parse(tc.expr) |
| 136 |
if iss.Err() != nil { |
| 137 |
t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err()) |
| 138 |
} |
| 139 |
asts = append(asts, pAst) |
| 140 |
cAst, iss := env.Check(pAst) |
| 141 |
if iss.Err() != nil { |
| 142 |
t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err()) |
| 143 |
} |
| 144 |
asts = append(asts, cAst) |
| 145 |
|
| 146 |
for _, ast := range asts { |
| 147 |
prg, err := env.Program(ast) |
| 148 |
if err != nil { |
| 149 |
t.Fatal(err) |
| 150 |
} |
| 151 |
out, _, err := prg.Eval(map[string]any{"msg": msgWithExtensions()}) |
| 152 |
if err != nil { |
| 153 |
t.Fatal(err) |
| 154 |
} |
| 155 |
if out.Value() != true { |
| 156 |
t.Errorf("got %v, wanted true for expr: %s", out.Value(), tc.expr) |
| 157 |
} |
| 158 |
} |
| 159 |
}) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func TestProtosParseErrors(t *testing.T) { |
| 164 |
var protosTests = []struct { |
| 165 |
expr string |
| 166 |
err string |
| 167 |
}{ |
| 168 |
{ |
| 169 |
expr: `proto.getExt(ExtendedExampleType{}, enum_ext)`, |
| 170 |
err: `ERROR: <input>:1:37: invalid extension field |
| 171 |
| proto.getExt(ExtendedExampleType{}, enum_ext) |
| 172 |
| ....................................^`, |
| 173 |
}, |
| 174 |
{ |
| 175 |
expr: `proto.hasExt(ExtendedExampleType{}, call().enum_ext)`, |
| 176 |
err: `ERROR: <input>:1:43: invalid extension field |
| 177 |
| proto.hasExt(ExtendedExampleType{}, call().enum_ext) |
| 178 |
| ..........................................^`, |
| 179 |
}, |
| 180 |
{ |
| 181 |
expr: `proto.getExt(ExtendedExampleType{}, has(google.expr.proto2.test.int32_ext))`, |
| 182 |
err: `ERROR: <input>:1:40: invalid extension field |
| 183 |
| proto.getExt(ExtendedExampleType{}, has(google.expr.proto2.test.int32_ext)) |
| 184 |
| .......................................^`, |
| 185 |
}, |
| 186 |
{ |
| 187 |
expr: `ExampleType{}.in`, |
| 188 |
err: `ERROR: <input>:1:15: Syntax error: no viable alternative at input '.in' |
| 189 |
| ExampleType{}.in |
| 190 |
| ..............^ |
| 191 |
ERROR: <input>:1:17: Syntax error: mismatched input '<EOF>' expecting {'[', '{', '(', '.', '-', '!', 'true', 'false', 'null', NUM_FLOAT, NUM_INT, NUM_UINT, STRING, BYTES, IDENTIFIER} |
| 192 |
| ExampleType{}.in |
| 193 |
| ................^`, |
| 194 |
}, |
| 195 |
} |
| 196 |
env := testProtosEnv(t) |
| 197 |
for i, tst := range protosTests { |
| 198 |
tc := tst |
| 199 |
t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) { |
| 200 |
ast, iss := env.Parse(tc.expr) |
| 201 |
if iss.Err() != nil { |
| 202 |
if !test.Compare(iss.Err().Error(), tc.err) { |
| 203 |
t.Errorf("got parse error %v, wanted error %s for expr: %s", iss.Err(), tc.err, tc.expr) |
| 204 |
} |
| 205 |
} else { |
| 206 |
t.Fatalf("got %v, wanted parse error", ast) |
| 207 |
} |
| 208 |
}) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
func testProtosEnv(t *testing.T, opts ...cel.EnvOption) *cel.Env { |
| 214 |
t.Helper() |
| 215 |
baseOpts := []cel.EnvOption{ |
| 216 |
cel.Container("google.expr.proto2.test"), |
| 217 |
cel.Types(&proto2pb.ExampleType{}, &proto2pb.ExternalMessageType{}), |
| 218 |
cel.Variable("msg", cel.ObjectType("google.expr.proto2.test.ExampleType")), |
| 219 |
cel.EnableIdentifierEscapeSyntax(), |
| 220 |
Protos(), |
| 221 |
} |
| 222 |
env, err := cel.NewEnv(append(baseOpts, opts...)...) |
| 223 |
if err != nil { |
| 224 |
t.Fatalf("cel.NewEnv(Protos()) failed: %v", err) |
| 225 |
} |
| 226 |
return env |
| 227 |
} |
| 228 |
|
| 229 |
func TestProtosWithExtension(t *testing.T) { |
| 230 |
env := testProtosEnv(t) |
| 231 |
_, err := env.Extend(Protos()) |
| 232 |
if err != nil { |
| 233 |
t.Fatalf("env.Extend(Protos()) failed: %v", err) |
| 234 |
} |
| 235 |
_, iss := env.Compile("proto.getExt(ExampleType{}, google.expr.proto2.test.int32_ext) == 0") |
| 236 |
if iss.Err() != nil { |
| 237 |
t.Errorf("env.Compile() failed: %v", iss.Err()) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func TestProtosVersion(t *testing.T) { |
| 242 |
_, err := cel.NewEnv(Protos(ProtosVersion(0))) |
| 243 |
if err != nil { |
| 244 |
t.Fatalf("ProtosVersion(0) failed: %v", err) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
func msgWithExtensions() *proto2pb.ExampleType { |
| 250 |
msg := &proto2pb.ExampleType{ |
| 251 |
Name: proto.String("example0"), |
| 252 |
} |
| 253 |
proto.SetExtension(msg, proto2pb.E_Int32Ext, int32(42)) |
| 254 |
proto.SetExtension(msg, proto2pb.E_Int32WrapperExt, wrapperspb.Int32(21)) |
| 255 |
proto.SetExtension(msg, proto2pb.E_NestedExample, &proto2pb.ExampleType{Name: proto.String("nested")}) |
| 256 |
proto.SetExtension(msg, proto2pb.E_ExtendedExampleType_EnumExt, proto2pb.GlobalEnum_GAZ) |
| 257 |
proto.SetExtension(msg, proto2pb.E_ExtendedExampleType_ExtendedExamples, []string{"example1", "example2"}) |
| 258 |
return msg |
| 259 |
} |