| 2780 | } |
| 2781 | |
| 2782 | func TestRegexProgramSizeLimit(t *testing.T) { |
| 2783 | env, err := NewEnv( |
| 2784 | Variable("pattern", StringType), |
| 2785 | RegexProgramSizeLimit(5), |
| 2786 | ) |
| 2787 | if err != nil { |
| 2788 | t.Fatalf("NewEnv failed: %v", err) |
| 2789 | } |
| 2790 | |
| 2791 | tests := []struct { |
| 2792 | name string |
| 2793 | expr string |
| 2794 | progOpts []ProgramOption |
| 2795 | vars any |
| 2796 | want ref.Val |
| 2797 | compileErr string |
| 2798 | progErr string |
| 2799 | evalErr string |
| 2800 | }{ |
| 2801 | { |
| 2802 | name: "constant_regex_exceeds_limit_ast_validation", |
| 2803 | expr: `"123 abc 456".matches('(a|b)*[0-9]+')`, |
| 2804 | compileErr: "regex program size 8 exceeds limit of 5", |
| 2805 | }, |
| 2806 | { |
| 2807 | name: "dynamic_regex_exceeds_limit_runtime", |
| 2808 | expr: `"123 abc 456".matches(pattern)`, |
| 2809 | vars: map[string]any{"pattern": "(a|b)*[0-9]+"}, |
| 2810 | evalErr: "regex program size 8 exceeds limit of 5", |
| 2811 | }, |
| 2812 | { |
| 2813 | name: "dynamic_regex_within_limit", |
| 2814 | expr: `"123 abc 456".matches(pattern)`, |
| 2815 | vars: map[string]any{"pattern": "[0-9]+"}, |
| 2816 | want: types.True, |
| 2817 | }, |
| 2818 | { |
| 2819 | name: "constant_counted_repetition_exceeds_limit_ast_validation", |
| 2820 | expr: `"123 abc 456".matches('[a-z]{8}')`, |
| 2821 | compileErr: "regex program size 10 exceeds limit of 5", |
| 2822 | }, |
| 2823 | { |
| 2824 | name: "dynamic_counted_repetition_within_limit", |
| 2825 | expr: `"aa bb".matches(pattern)`, |
| 2826 | vars: map[string]any{"pattern": "a{2}"}, |
| 2827 | want: types.True, |
| 2828 | }, |
| 2829 | } |
| 2830 | |
| 2831 | for _, tc := range tests { |
| 2832 | t.Run(tc.name, func(tt *testing.T) { |
| 2833 | ast, iss := env.Compile(tc.expr) |
| 2834 | if tc.compileErr != "" { |
| 2835 | if iss.Err() == nil { |
| 2836 | tt.Fatalf("env.Compile(%s) succeeded, wanted error %s", tc.expr, tc.compileErr) |
| 2837 | } |
| 2838 | if !strings.Contains(iss.Err().Error(), tc.compileErr) { |
| 2839 | tt.Errorf("got compile error %v, wanted error containing %s", iss.Err(), tc.compileErr) |