(t *testing.T)
| 2114 | } |
| 2115 | |
| 2116 | func TestInterpreter_RegexProgramSizeLimit(t *testing.T) { |
| 2117 | tcConst := testCase{ |
| 2118 | expr: `'hello'.matches('(a|b)*[0-9]+')`, |
| 2119 | } |
| 2120 | _, _, err := program(t, &tcConst, RegexProgramSizeLimit(5)) |
| 2121 | if err == nil { |
| 2122 | t.Fatalf("expected program creation error for constant regex exceeding limit") |
| 2123 | } |
| 2124 | if !strings.Contains(err.Error(), "regex program size 8 exceeds limit of 5") { |
| 2125 | t.Errorf("got error %v, wanted error containing 'regex program size 8 exceeds limit of 5'", err) |
| 2126 | } |
| 2127 | |
| 2128 | tcDyn := testCase{ |
| 2129 | expr: `'hello'.matches(pattern)`, |
| 2130 | vars: []*decls.VariableDecl{ |
| 2131 | decls.NewVariable("pattern", types.StringType), |
| 2132 | }, |
| 2133 | in: map[string]any{ |
| 2134 | "pattern": "(a|b)*[0-9]+", |
| 2135 | }, |
| 2136 | } |
| 2137 | prg, frame, err := program(t, &tcDyn, RegexProgramSizeLimit(5)) |
| 2138 | if err != nil { |
| 2139 | t.Fatalf("program() failed: %v", err) |
| 2140 | } |
| 2141 | out := prg.Exec(frame) |
| 2142 | frame.Close() |
| 2143 | if !types.IsError(out) || !strings.Contains(out.(*types.Err).String(), "regex program size 8 exceeds limit of 5") { |
| 2144 | t.Errorf("got %v, wanted regex program size limit error", out) |
| 2145 | } |
| 2146 | |
| 2147 | tcValid := testCase{ |
| 2148 | expr: `'hello'.matches(pattern)`, |
| 2149 | vars: []*decls.VariableDecl{ |
| 2150 | decls.NewVariable("pattern", types.StringType), |
| 2151 | }, |
| 2152 | in: map[string]any{ |
| 2153 | "pattern": "el*", |
| 2154 | }, |
| 2155 | out: true, |
| 2156 | } |
| 2157 | prgValid, frameValid, err := program(t, &tcValid, RegexProgramSizeLimit(5)) |
| 2158 | if err != nil { |
| 2159 | t.Fatalf("program() failed: %v", err) |
| 2160 | } |
| 2161 | outValid := prgValid.Exec(frameValid) |
| 2162 | frameValid.Close() |
| 2163 | if outValid != types.True { |
| 2164 | t.Errorf("got %v, wanted true", outValid) |
| 2165 | } |
| 2166 | |
| 2167 | // Non-regex function should not be modified by RegexProgramSizeLimit decorator |
| 2168 | tcOther := testCase{ |
| 2169 | expr: `'hello'.contains('e')`, |
| 2170 | } |
| 2171 | prgOther, frameOther, err := program(t, &tcOther, RegexProgramSizeLimit(5)) |
| 2172 | if err != nil { |
| 2173 | t.Fatalf("program() failed: %v", err) |
nothing calls this directly
no test coverage detected