(t *testing.T)
| 2149 | } |
| 2150 | |
| 2151 | func TestInterpreter_RegexProgramSizeLimit(t *testing.T) { |
| 2152 | tcConst := testCase{ |
| 2153 | expr: `'hello'.matches('(a|b)*[0-9]+')`, |
| 2154 | } |
| 2155 | _, _, err := program(t, &tcConst, RegexProgramSizeLimit(5)) |
| 2156 | if err == nil { |
| 2157 | t.Fatalf("expected program creation error for constant regex exceeding limit") |
| 2158 | } |
| 2159 | if !strings.Contains(err.Error(), "regex program size 8 exceeds limit of 5") { |
| 2160 | t.Errorf("got error %v, wanted error containing 'regex program size 8 exceeds limit of 5'", err) |
| 2161 | } |
| 2162 | |
| 2163 | tcDyn := testCase{ |
| 2164 | expr: `'hello'.matches(pattern)`, |
| 2165 | vars: []*decls.VariableDecl{ |
| 2166 | decls.NewVariable("pattern", types.StringType), |
| 2167 | }, |
| 2168 | in: map[string]any{ |
| 2169 | "pattern": "(a|b)*[0-9]+", |
| 2170 | }, |
| 2171 | } |
| 2172 | prg, frame, err := program(t, &tcDyn, RegexProgramSizeLimit(5)) |
| 2173 | if err != nil { |
| 2174 | t.Fatalf("program() failed: %v", err) |
| 2175 | } |
| 2176 | out := prg.Exec(frame) |
| 2177 | frame.Close() |
| 2178 | if !types.IsError(out) || !strings.Contains(out.(*types.Err).String(), "regex program size 8 exceeds limit of 5") { |
| 2179 | t.Errorf("got %v, wanted regex program size limit error", out) |
| 2180 | } |
| 2181 | |
| 2182 | tcValid := testCase{ |
| 2183 | expr: `'hello'.matches(pattern)`, |
| 2184 | vars: []*decls.VariableDecl{ |
| 2185 | decls.NewVariable("pattern", types.StringType), |
| 2186 | }, |
| 2187 | in: map[string]any{ |
| 2188 | "pattern": "el*", |
| 2189 | }, |
| 2190 | out: true, |
| 2191 | } |
| 2192 | prgValid, frameValid, err := program(t, &tcValid, RegexProgramSizeLimit(5)) |
| 2193 | if err != nil { |
| 2194 | t.Fatalf("program() failed: %v", err) |
| 2195 | } |
| 2196 | outValid := prgValid.Exec(frameValid) |
| 2197 | frameValid.Close() |
| 2198 | if outValid != types.True { |
| 2199 | t.Errorf("got %v, wanted true", outValid) |
| 2200 | } |
| 2201 | |
| 2202 | // Non-regex function should not be modified by RegexProgramSizeLimit decorator |
| 2203 | tcOther := testCase{ |
| 2204 | expr: `'hello'.contains('e')`, |
| 2205 | } |
| 2206 | prgOther, frameOther, err := program(t, &tcOther, RegexProgramSizeLimit(5)) |
| 2207 | if err != nil { |
| 2208 | t.Fatalf("program() failed: %v", err) |
nothing calls this directly
no test coverage detected