MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestStringCostLimitEnforced

Function TestStringCostLimitEnforced

ext/strings_test.go:965–986  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

963}
964
965func TestStringCostLimitEnforced(t *testing.T) {
966 env, err := cel.NewEnv(Strings())
967 if err != nil {
968 t.Fatalf("cel.NewEnv() failed: %v", err)
969 }
970 // Chained replaces that produce exponential output.
971 // Without cost tracking, this would be charged ~6. With tracking, the cost
972 // scales with output size and should exceed any reasonable limit.
973 expr := `"A".replace("", "AAAAAAAAAA").replace("", "AAAAAAAAAA").replace("", "AAAAAAAAAA").replace("", "AAAAAAAAAA").replace("", "AAAAAAAAAA").replace("", "AAAAAAAAAA")`
974 ast, iss := env.Compile(expr)
975 if iss.Err() != nil {
976 t.Fatalf("env.Compile() failed: %v", iss.Err())
977 }
978 prg, err := env.Program(ast, cel.CostLimit(1000), cel.CostTracking(nil))
979 if err != nil {
980 t.Fatalf("env.Program() failed: %v", err)
981 }
982 _, _, err = prg.Eval(cel.NoVars())
983 if err == nil {
984 t.Error("expected cost limit exceeded error for exponential string growth, got nil")
985 }
986}

Callers

nothing calls this directly

Calls 10

CompileMethod · 0.95
ProgramMethod · 0.95
NewEnvFunction · 0.92
CostLimitFunction · 0.92
CostTrackingFunction · 0.92
NoVarsFunction · 0.92
StringsFunction · 0.85
ErrMethod · 0.80
EvalMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected