| 5245 | } |
| 5246 | |
| 5247 | func TestExpressionSizeLimitEarlyEnforcement(t *testing.T) { |
| 5248 | env, err := NewEnv(ParserExpressionSizeLimit(1000)) |
| 5249 | if err != nil { |
| 5250 | t.Fatalf("NewEnv() failed: %v", err) |
| 5251 | } |
| 5252 | |
| 5253 | tests := []struct { |
| 5254 | name string |
| 5255 | mode string |
| 5256 | }{ |
| 5257 | {name: "compile_rejects_oversized", mode: "compile"}, |
| 5258 | {name: "parse_rejects_oversized", mode: "parse"}, |
| 5259 | } |
| 5260 | |
| 5261 | for _, tc := range tests { |
| 5262 | t.Run(tc.name, func(t *testing.T) { |
| 5263 | payload := strings.Repeat("a", 10_000_000) |
| 5264 | |
| 5265 | var m1, m2 runtime.MemStats |
| 5266 | runtime.GC() |
| 5267 | runtime.ReadMemStats(&m1) |
| 5268 | |
| 5269 | switch tc.mode { |
| 5270 | case "compile": |
| 5271 | _, iss := env.Compile(payload) |
| 5272 | if iss == nil || iss.Err() == nil { |
| 5273 | t.Fatal("expected size limit error, got nil") |
| 5274 | } |
| 5275 | if !strings.Contains(iss.Err().Error(), "expression code point size exceeds limit") { |
| 5276 | t.Fatalf("unexpected error: %v", iss.Err()) |
| 5277 | } |
| 5278 | case "parse": |
| 5279 | _, iss := env.Parse(payload) |
| 5280 | if iss == nil || iss.Err() == nil { |
| 5281 | t.Fatal("expected size limit error, got nil") |
| 5282 | } |
| 5283 | if !strings.Contains(iss.Err().Error(), "expression code point size exceeds limit") { |
| 5284 | t.Fatalf("unexpected error: %v", iss.Err()) |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | runtime.ReadMemStats(&m2) |
| 5289 | allocDelta := (m2.TotalAlloc - m1.TotalAlloc) / (1024 * 1024) |
| 5290 | if allocDelta > 5 { |
| 5291 | t.Errorf("excessive memory allocation: %dMiB during %s (expected <5MiB with early enforcement)", |
| 5292 | allocDelta, tc.mode) |
| 5293 | } |
| 5294 | t.Logf("[%s] memory delta: %dMiB", tc.mode, allocDelta) |
| 5295 | }) |
| 5296 | } |
| 5297 | } |
| 5298 | |
| 5299 | func TestProgramEvalInvalidInput(t *testing.T) { |
| 5300 | env, err := NewEnv() |