| 1 | // Copyright 2023 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 ext |
| 16 | |
| 17 | import ( |
| 18 | "math" |
| 19 | "reflect" |
| 20 | "strings" |
| 21 | "testing" |
| 22 | |
| 23 | "github.com/google/cel-go/cel" |
| 24 | "github.com/google/cel-go/checker" |
| 25 | "github.com/google/cel-go/common/types" |
| 26 | "github.com/google/cel-go/common/types/ref" |
| 27 | |
| 28 | proto3pb "github.com/google/cel-go/test/proto3pb" |
| 29 | ) |
| 30 | |
| 31 | func TestSets(t *testing.T) { |
| 32 | tests := []struct { |
| 33 | expr string |
| 34 | vars []cel.EnvOption |
| 35 | in map[string]any |
| 36 | hints map[string]uint64 |
| 37 | estimatedCost checker.CostEstimate |
| 38 | actualCost uint64 |
| 39 | }{ |
| 40 | // set containment |
| 41 | { |
| 42 | expr: `sets.contains(x, [1, 2, 3])`, |
| 43 | vars: []cel.EnvOption{cel.Variable("x", cel.ListType(cel.IntType))}, |
| 44 | in: map[string]any{"x": []int64{5, 4, 3, 2, 1}}, |
| 45 | hints: map[string]uint64{"x": 10}, |
| 46 | // min cost is input 'x' length 0, 10 for list creation, 2 for arg costs |
| 47 | // max cost is input 'x' length 10, 10 for list creation, 2 for arg costs |
| 48 | estimatedCost: checker.CostEstimate{Min: 12, Max: 42}, |
| 49 | // actual cost is 'x' length 5 * list literal length 3, 10 for list creation, 2 for arg cost |
| 50 | actualCost: 27, |
| 51 | }, |
| 52 | { |
| 53 | expr: `sets.contains(x, [1, 1, 1, 1, 1])`, |
| 54 | vars: []cel.EnvOption{cel.Variable("x", cel.ListType(cel.IntType))}, |
| 55 | in: map[string]any{"x": []int64{5, 4, 3, 2, 1}}, |
| 56 | // min cost is input 'x' length 0, 10 for list creation, 2 for arg costs |
| 57 | // max cost is effectively infinite due to missing size hint for 'x' |
| 58 | estimatedCost: checker.CostEstimate{Min: 12, Max: math.MaxUint64}, |
| 59 | // actual cost is 'x' length 5 * list literal length 5, 10 for list creation, 2 for arg cost |
| 60 | actualCost: 37, |
| 61 | }, |
| 62 | { |
| 63 | expr: `sets.contains([], [])`, |
| 64 | estimatedCost: checker.FixedCostEstimate(21), |
| 65 | actualCost: 21, |
| 66 | }, |
| 67 | { |
| 68 | expr: `sets.contains([1], [])`, |
| 69 | estimatedCost: checker.FixedCostEstimate(21), |
| 70 | actualCost: 21, |
| 71 | }, |
| 72 | { |
| 73 | expr: `sets.contains([1], [1])`, |
| 74 | estimatedCost: checker.FixedCostEstimate(22), |
| 75 | actualCost: 22, |
| 76 | }, |
| 77 | { |
| 78 | expr: `sets.contains([1], [1, 1])`, |
| 79 | estimatedCost: checker.FixedCostEstimate(23), |
| 80 | actualCost: 23, |
| 81 | }, |
| 82 | { |
| 83 | expr: `sets.contains([1, 1], [1])`, |
| 84 | estimatedCost: checker.FixedCostEstimate(23), |
| 85 | actualCost: 23, |
| 86 | }, |
| 87 | { |
| 88 | expr: `sets.contains([2, 1], [1])`, |
| 89 | estimatedCost: checker.FixedCostEstimate(23), |
| 90 | actualCost: 23, |
| 91 | }, |
| 92 | { |
| 93 | expr: `sets.contains([1, 2, 3, 4], [2, 3])`, |
| 94 | estimatedCost: checker.FixedCostEstimate(29), |
| 95 | actualCost: 29, |
| 96 | }, |
| 97 | { |
| 98 | expr: `sets.contains([1], [1.0, 1])`, |
| 99 | estimatedCost: checker.FixedCostEstimate(23), |
| 100 | actualCost: 23, |
| 101 | }, |
| 102 | { |
| 103 | expr: `sets.contains([1, 2], [2u, 2.0])`, |
| 104 | estimatedCost: checker.FixedCostEstimate(25), |
| 105 | actualCost: 25, |
| 106 | }, |
| 107 | { |
| 108 | expr: `sets.contains([1, 2u], [2, 2.0])`, |
| 109 | estimatedCost: checker.FixedCostEstimate(25), |
| 110 | actualCost: 25, |
| 111 | }, |
| 112 | { |
| 113 | expr: `sets.contains([1, 2.0, 3u], [1.0, 2u, 3])`, |
| 114 | estimatedCost: checker.FixedCostEstimate(30), |
| 115 | actualCost: 30, |
| 116 | }, |
| 117 | { |
| 118 | expr: `sets.contains([[1], [2, 3]], [[2, 3.0]])`, |
| 119 | // 10 for each list creation, top-level list sizes are 2, 1 |
| 120 | estimatedCost: checker.FixedCostEstimate(53), |
| 121 | actualCost: 53, |
| 122 | }, |
| 123 | { |
| 124 | expr: `!sets.contains([1], [2])`, |
| 125 | estimatedCost: checker.FixedCostEstimate(23), |
| 126 | actualCost: 23, |
| 127 | }, |
| 128 | { |
| 129 | expr: `!sets.contains([1], [1, 2])`, |
| 130 | estimatedCost: checker.FixedCostEstimate(24), |
| 131 | actualCost: 24, |
| 132 | }, |
| 133 | { |
| 134 | expr: `!sets.contains([1], ["1", 1])`, |
| 135 | estimatedCost: checker.FixedCostEstimate(24), |
| 136 | actualCost: 24, |
| 137 | }, |
| 138 | { |
| 139 | expr: `!sets.contains([1], [1.1, 1u])`, |
| 140 | estimatedCost: checker.FixedCostEstimate(24), |
| 141 | actualCost: 24, |
| 142 | }, |
| 143 | |
| 144 | // set equivalence (note the cost factor is higher as it's basically two contains checks) |
| 145 | { |
| 146 | expr: `sets.equivalent([], [])`, |
| 147 | estimatedCost: checker.FixedCostEstimate(21), |
| 148 | actualCost: 21, |
| 149 | }, |
| 150 | { |
| 151 | expr: `sets.equivalent([1], [1])`, |
| 152 | estimatedCost: checker.FixedCostEstimate(23), |
| 153 | actualCost: 23, |
| 154 | }, |
| 155 | { |
| 156 | expr: `sets.equivalent([1], [1, 1])`, |
| 157 | estimatedCost: checker.FixedCostEstimate(25), |
| 158 | actualCost: 25, |
| 159 | }, |
| 160 | { |
| 161 | expr: `sets.equivalent([1, 1], [1])`, |
| 162 | estimatedCost: checker.FixedCostEstimate(25), |
| 163 | actualCost: 25, |
| 164 | }, |
| 165 | { |
| 166 | expr: `sets.equivalent([1], [1u, 1.0])`, |
| 167 | estimatedCost: checker.FixedCostEstimate(25), |
| 168 | actualCost: 25, |
| 169 | }, |
| 170 | { |
| 171 | expr: `sets.equivalent([1], [1u, 1.0])`, |
| 172 | estimatedCost: checker.FixedCostEstimate(25), |
| 173 | actualCost: 25, |
| 174 | }, |
| 175 | { |
| 176 | expr: `sets.equivalent([1, 2, 3], [3u, 2.0, 1])`, |
| 177 | estimatedCost: checker.FixedCostEstimate(39), |
| 178 | actualCost: 39, |
| 179 | }, |
| 180 | { |
| 181 | expr: `sets.equivalent([[1.0], [2, 3]], [[1], [2, 3.0]])`, |
| 182 | estimatedCost: checker.FixedCostEstimate(69), |
| 183 | actualCost: 69, |
| 184 | }, |
| 185 | { |
| 186 | expr: `!sets.equivalent([2, 1], [1])`, |
| 187 | estimatedCost: checker.FixedCostEstimate(26), |
| 188 | actualCost: 26, |
| 189 | }, |
| 190 | { |
| 191 | expr: `!sets.equivalent([1], [1, 2])`, |
| 192 | estimatedCost: checker.FixedCostEstimate(26), |
| 193 | actualCost: 26, |
| 194 | }, |
| 195 | { |
| 196 | expr: `!sets.equivalent([1, 2], [2u, 2, 2.0])`, |
| 197 | estimatedCost: checker.FixedCostEstimate(34), |
| 198 | actualCost: 34, |
| 199 | }, |
| 200 | { |
| 201 | expr: `!sets.equivalent([1, 2], [1u, 2, 2.3])`, |
| 202 | estimatedCost: checker.FixedCostEstimate(34), |
| 203 | actualCost: 34, |
| 204 | }, |
| 205 | |
| 206 | // set intersection |
| 207 | { |
| 208 | expr: `sets.intersects([1], [1])`, |
| 209 | estimatedCost: checker.FixedCostEstimate(22), |
| 210 | actualCost: 22, |
| 211 | }, |
| 212 | { |
| 213 | expr: `sets.intersects([1], [1, 1])`, |
| 214 | estimatedCost: checker.FixedCostEstimate(23), |
| 215 | actualCost: 23, |
| 216 | }, |
| 217 | { |
| 218 | expr: `sets.intersects([1, 1], [1])`, |
| 219 | estimatedCost: checker.FixedCostEstimate(23), |
| 220 | actualCost: 23, |
| 221 | }, |
| 222 | { |
| 223 | expr: `sets.intersects([2, 1], [1])`, |
| 224 | estimatedCost: checker.FixedCostEstimate(23), |
| 225 | actualCost: 23, |
| 226 | }, |
| 227 | { |
| 228 | expr: `sets.intersects([1], [1, 2])`, |
| 229 | estimatedCost: checker.FixedCostEstimate(23), |
| 230 | actualCost: 23, |
| 231 | }, |
| 232 | { |
| 233 | expr: `sets.intersects([1], [1.0, 2])`, |
| 234 | estimatedCost: checker.FixedCostEstimate(23), |
| 235 | actualCost: 23, |
| 236 | }, |
| 237 | { |
| 238 | expr: `sets.intersects([1, 2], [2u, 2, 2.0])`, |
| 239 | estimatedCost: checker.FixedCostEstimate(27), |
| 240 | actualCost: 27, |
| 241 | }, |
| 242 | { |
| 243 | expr: `sets.intersects([1, 2], [1u, 2, 2.3])`, |
| 244 | estimatedCost: checker.FixedCostEstimate(27), |
| 245 | actualCost: 27, |
| 246 | }, |
| 247 | { |
| 248 | expr: `sets.intersects([[1], [2, 3]], [[1, 2], [2, 3.0]])`, |
| 249 | estimatedCost: checker.FixedCostEstimate(65), |
| 250 | actualCost: 65, |
| 251 | }, |
| 252 | { |
| 253 | expr: `!sets.intersects([], [])`, |
| 254 | estimatedCost: checker.FixedCostEstimate(22), |
| 255 | actualCost: 22, |
| 256 | }, |
| 257 | { |
| 258 | expr: `!sets.intersects([1], [])`, |
| 259 | estimatedCost: checker.FixedCostEstimate(22), |
| 260 | actualCost: 22, |
| 261 | }, |
| 262 | { |
| 263 | expr: `!sets.intersects([1], [2])`, |
| 264 | estimatedCost: checker.FixedCostEstimate(23), |
| 265 | actualCost: 23, |
| 266 | }, |
| 267 | { |
| 268 | expr: `!sets.intersects([1], ["1", 2])`, |
| 269 | estimatedCost: checker.FixedCostEstimate(24), |
| 270 | actualCost: 24, |
| 271 | }, |
| 272 | { |
| 273 | expr: `!sets.intersects([1], [1.1, 2u])`, |
| 274 | estimatedCost: checker.FixedCostEstimate(24), |
| 275 | actualCost: 24, |
| 276 | }, |
| 277 | } |
| 278 | |
| 279 | for _, tst := range tests { |
| 280 | tc := tst |
| 281 | t.Run(tc.expr, func(t *testing.T) { |
| 282 | env := testSetsEnv(t, tc.vars...) |
| 283 | var asts []*cel.Ast |
| 284 | pAst, iss := env.Parse(tc.expr) |
| 285 | if iss.Err() != nil { |
| 286 | t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err()) |
| 287 | } |
| 288 | asts = append(asts, pAst) |
| 289 | cAst, iss := env.Check(pAst) |
| 290 | if iss.Err() != nil { |
| 291 | t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err()) |
| 292 | } |
| 293 | |
| 294 | testCheckCost(t, env, cAst, tc.hints, tc.estimatedCost) |
| 295 | asts = append(asts, cAst) |
| 296 | for _, ast := range asts { |
| 297 | testEvalWithCost(t, env, ast, tc.in, tc.actualCost) |
| 298 | } |
| 299 | }) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | func TestSetsMembershipRewriter(t *testing.T) { |
| 304 | tests := []struct { |
| 305 | expr string |
| 306 | optimized string |
| 307 | opts []cel.EnvOption |
| 308 | in map[string]any |
| 309 | out ref.Val |
| 310 | }{ |
| 311 | { |
| 312 | expr: `a in [1, 2, 3, 4]`, |
| 313 | optimized: `a in {1: true, 2: true, 3: true, 4: true}`, |
| 314 | opts: []cel.EnvOption{ |
| 315 | cel.Variable("a", cel.IntType), |
| 316 | }, |
| 317 | in: map[string]any{ |
| 318 | "a": 3, |
| 319 | }, |
| 320 | out: types.True, |
| 321 | }, |
| 322 | { |
| 323 | expr: `a in ['1', '2', '3', 4]`, |
| 324 | optimized: `a in {"1": true, "2": true, "3": true, 4: true}`, |
| 325 | opts: []cel.EnvOption{ |
| 326 | cel.Variable("a", cel.IntType), |
| 327 | }, |
| 328 | in: map[string]any{ |
| 329 | "a": 3, |
| 330 | }, |
| 331 | out: types.False, |
| 332 | }, |
| 333 | { |
| 334 | expr: `a in [1u, '2', '3', 4]`, |
| 335 | optimized: `a in {1u: true, "2": true, "3": true, 4: true}`, |
| 336 | opts: []cel.EnvOption{ |
| 337 | cel.Variable("a", cel.IntType), |
| 338 | }, |
| 339 | in: map[string]any{ |
| 340 | "a": 4, |
| 341 | }, |
| 342 | out: types.True, |
| 343 | }, |
| 344 | { |
| 345 | expr: `a in [1u, 2.0, '3', 4]`, |
| 346 | optimized: `a in [1u, 2.0, "3", 4]`, |
| 347 | opts: []cel.EnvOption{ |
| 348 | cel.Variable("a", cel.IntType), |
| 349 | }, |
| 350 | in: map[string]any{ |
| 351 | "a": 4, |
| 352 | }, |
| 353 | out: types.True, |
| 354 | }, |
| 355 | { |
| 356 | expr: `a in [b, 32]`, |
| 357 | optimized: `a in [b, 32]`, |
| 358 | opts: []cel.EnvOption{ |
| 359 | cel.Variable("a", cel.IntType), |
| 360 | cel.Variable("b", cel.IntType), |
| 361 | }, |
| 362 | in: map[string]any{ |
| 363 | "a": 4, |
| 364 | "b": 4, |
| 365 | }, |
| 366 | out: types.True, |
| 367 | }, |
| 368 | { |
| 369 | expr: `a in {b: c}`, |
| 370 | optimized: `a in {b: c}`, |
| 371 | opts: []cel.EnvOption{ |
| 372 | cel.Variable("a", cel.IntType), |
| 373 | cel.Variable("b", cel.IntType), |
| 374 | cel.Variable("c", cel.IntType), |
| 375 | }, |
| 376 | in: map[string]any{ |
| 377 | "a": 4, |
| 378 | "b": 42, |
| 379 | "c": 123, |
| 380 | }, |
| 381 | out: types.False, |
| 382 | }, |
| 383 | { |
| 384 | expr: `a in {3: true}`, |
| 385 | optimized: `a in {3: true}`, |
| 386 | opts: []cel.EnvOption{ |
| 387 | cel.Variable("a", cel.IntType), |
| 388 | }, |
| 389 | in: map[string]any{ |
| 390 | "a": 4, |
| 391 | }, |
| 392 | out: types.False, |
| 393 | }, |
| 394 | { |
| 395 | expr: `a in ["hello", "world"].map(i, i in ["goodbye", "world"], i + i)`, |
| 396 | optimized: `a in ["hello", "world"].map(i, i in {"goodbye": true, "world": true}, i + i)`, |
| 397 | opts: []cel.EnvOption{ |
| 398 | cel.Variable("a", cel.StringType), |
| 399 | }, |
| 400 | in: map[string]any{ |
| 401 | "a": "worldworld", |
| 402 | }, |
| 403 | out: types.True, |
| 404 | }, |
| 405 | { |
| 406 | expr: `a in [test.GlobalEnum.GOO, test.GlobalEnum.GAR, test.GlobalEnum.GAZ]`, |
| 407 | optimized: `a in {0: true, 1: true, 2: true}`, |
| 408 | opts: []cel.EnvOption{ |
| 409 | cel.Container("google.expr.proto3"), |
| 410 | cel.Variable("a", cel.IntType), |
| 411 | cel.Types(&proto3pb.TestAllTypes{}), |
| 412 | }, |
| 413 | in: map[string]any{ |
| 414 | "a": proto3pb.GlobalEnum_GAZ, |
| 415 | }, |
| 416 | out: types.True, |
| 417 | }, |
| 418 | } |
| 419 | for _, tst := range tests { |
| 420 | tc := tst |
| 421 | t.Run(tc.expr, func(t *testing.T) { |
| 422 | env := testSetsEnv(t, tc.opts...) |
| 423 | var asts []*cel.Ast |
| 424 | a, iss := env.Compile(tc.expr) |
| 425 | if iss.Err() != nil { |
| 426 | t.Fatalf("env.Compile(%v) failed: %v", tc.expr, iss.Err()) |
| 427 | } |
| 428 | asts = append(asts, a) |
| 429 | setsOpt, err := NewSetMembershipOptimizer() |
| 430 | if err != nil { |
| 431 | t.Fatalf("NewSetMembershipOptimizer() failed with error: %v", err) |
| 432 | } |
| 433 | opt, err := cel.NewStaticOptimizer(setsOpt) |
| 434 | if err != nil { |
| 435 | t.Fatalf("cel.NewStaticOptimizer() failed with error: %v", err) |
| 436 | } |
| 437 | optAST, iss := opt.Optimize(env, a) |
| 438 | if iss.Err() != nil { |
| 439 | t.Fatalf("opt.Optimize() failed: %v", iss.Err()) |
| 440 | } |
| 441 | optExpr, err := cel.AstToString(optAST) |
| 442 | if err != nil { |
| 443 | t.Fatalf("cel.AstToString() failed :%v", err) |
| 444 | } |
| 445 | if tc.optimized != optExpr { |
| 446 | t.Errorf("got %v, wanted optimized expr %v", optExpr, tc.optimized) |
| 447 | } |
| 448 | asts = append(asts, optAST) |
| 449 | |
| 450 | for _, ast := range asts { |
| 451 | prg, err := env.Program(ast) |
| 452 | if err != nil { |
| 453 | t.Fatalf("env.Program() failed: %v", err) |
| 454 | } |
| 455 | in := tc.in |
| 456 | if in == nil { |
| 457 | in = map[string]any{} |
| 458 | } |
| 459 | out, _, err := prg.Eval(in) |
| 460 | if err != nil { |
| 461 | t.Fatalf("prg.Eval() failed: %v", err) |
| 462 | } |
| 463 | if out != tc.out { |
| 464 | t.Errorf("prg.Eval() got %v, wanted %v for expr: %s", out, tc.out, tc.expr) |
| 465 | } |
| 466 | } |
| 467 | }) |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | func TestSetsVersion(t *testing.T) { |
| 472 | _, err := cel.NewEnv(Sets(SetsVersion(0))) |
| 473 | if err != nil { |
| 474 | t.Fatalf("SetsVersion(0) failed: %v", err) |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | func testSetsEnv(t *testing.T, opts ...cel.EnvOption) *cel.Env { |
| 479 | t.Helper() |
| 480 | baseOpts := []cel.EnvOption{cel.EnableMacroCallTracking(), Sets()} |
| 481 | env, err := cel.NewEnv(append(baseOpts, opts...)...) |
| 482 | if err != nil { |
| 483 | t.Fatalf("cel.NewEnv(Sets()) failed: %v", err) |
| 484 | } |
| 485 | return env |
| 486 | } |
| 487 | |
| 488 | func testCheckCost(t *testing.T, env *cel.Env, ast *cel.Ast, hints map[string]uint64, wantEst checker.CostEstimate) { |
| 489 | t.Helper() |
| 490 | if len(hints) == 0 { |
| 491 | hints = map[string]uint64{} |
| 492 | } |
| 493 | est, err := env.EstimateCost(ast, testCostHintEstimator{hints: hints}) |
| 494 | if err != nil { |
| 495 | t.Fatalf("env.EstimateCost() failed: %v", err) |
| 496 | } |
| 497 | if !reflect.DeepEqual(est, wantEst) { |
| 498 | t.Errorf("env.EstimateCost() got %v, wanted %v", est, wantEst) |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | func testEvalWithCost(t *testing.T, env *cel.Env, ast *cel.Ast, in any, wantCost uint64) { |
| 503 | t.Helper() |
| 504 | prgOpts := []cel.ProgramOption{} |
| 505 | if ast.IsChecked() { |
| 506 | prgOpts = append(prgOpts, cel.CostTracking(nil)) |
| 507 | } |
| 508 | prg, err := env.Program(ast, prgOpts...) |
| 509 | if err != nil { |
| 510 | t.Fatalf("env.Program() failed: %v", err) |
| 511 | } |
| 512 | var data any = cel.NoVars() |
| 513 | if in != nil { |
| 514 | data = in |
| 515 | } |
| 516 | out, det, err := prg.Eval(data) |
| 517 | if err != nil { |
| 518 | t.Fatal(err) |
| 519 | } else if out.Value() != true { |
| 520 | t.Errorf("got %v, wanted true", out.Value()) |
| 521 | } |
| 522 | if det.ActualCost() != nil && *det.ActualCost() != wantCost { |
| 523 | t.Errorf("prg.Eval() had cost %v, wanted %v", *det.ActualCost(), wantCost) |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | type testCostHintEstimator struct { |
| 528 | hints map[string]uint64 |
| 529 | } |
| 530 | |
| 531 | func (tc testCostHintEstimator) EstimateSize(element checker.AstNode) *checker.SizeEstimate { |
| 532 | if l, ok := tc.hints[strings.Join(element.Path(), ".")]; ok { |
| 533 | return &checker.SizeEstimate{Min: 0, Max: l} |
| 534 | } |
| 535 | return nil |
| 536 | } |
| 537 | |
| 538 | func (testCostHintEstimator) EstimateCallCost(function, overloadID string, target *checker.AstNode, args []checker.AstNode) *checker.CallEstimate { |
| 539 | return nil |
| 540 | } |