(t *testing.T)
| 566 | } |
| 567 | |
| 568 | func TestValidateBindNestingLimit(t *testing.T) { |
| 569 | env, err := cel.NewEnv( |
| 570 | Bindings(), |
| 571 | cel.ASTValidators(cel.ValidateBindNestingLimit(2)), |
| 572 | ) |
| 573 | if err != nil { |
| 574 | t.Fatalf("cel.NewEnv() failed: %v", err) |
| 575 | } |
| 576 | tests := []struct { |
| 577 | expr string |
| 578 | iss string |
| 579 | }{ |
| 580 | { |
| 581 | expr: `cel.bind(a, 1, a + 1)`, |
| 582 | }, |
| 583 | { |
| 584 | expr: `cel.bind(a, 1, cel.bind(b, 2, a + b))`, |
| 585 | }, |
| 586 | { |
| 587 | // two cel.binds, but in separate branches |
| 588 | expr: `cel.bind(a, 1, a) + cel.bind(b, 2, b)`, |
| 589 | }, |
| 590 | { |
| 591 | // empty iteration range comprehension (e.g. cel.bind) does not count against comprehension limit, |
| 592 | // but counts against cel.bind nesting limit. |
| 593 | expr: `[1, 2, 3].exists(i, cel.bind(a, i, cel.bind(b, a, a + b) > 0))`, |
| 594 | }, |
| 595 | { |
| 596 | // three cel.binds, three levels deep |
| 597 | expr: `cel.bind(a, 1, cel.bind(b, 2, cel.bind(c, 3, a + b + c)))`, |
| 598 | iss: ` |
| 599 | ERROR: <input>:1:39: cel.bind exceeds nesting limit |
| 600 | | cel.bind(a, 1, cel.bind(b, 2, cel.bind(c, 3, a + b + c))) |
| 601 | | ......................................^`, |
| 602 | }, |
| 603 | { |
| 604 | // three cel.binds, three levels deep with non-comprehension ancestor (+) |
| 605 | expr: `cel.bind(a, 1, cel.bind(b, 2, 1 + cel.bind(c, 3, a + b + c)))`, |
| 606 | iss: ` |
| 607 | ERROR: <input>:1:43: cel.bind exceeds nesting limit |
| 608 | | cel.bind(a, 1, cel.bind(b, 2, 1 + cel.bind(c, 3, a + b + c))) |
| 609 | | ..........................................^`, |
| 610 | }, |
| 611 | } |
| 612 | for _, tst := range tests { |
| 613 | tc := tst |
| 614 | t.Run(tc.expr, func(t *testing.T) { |
| 615 | _, iss := env.Compile(tc.expr) |
| 616 | if tc.iss != "" { |
| 617 | if iss.Err() == nil { |
| 618 | t.Fatalf("env.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss) |
| 619 | } |
| 620 | if !test.Compare(iss.Err().Error(), tc.iss) { |
| 621 | t.Fatalf("env.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss) |
| 622 | } |
| 623 | return |
| 624 | } |
| 625 | if iss.Err() != nil { |
nothing calls this directly
no test coverage detected