(t *testing.T)
| 1364 | } |
| 1365 | |
| 1366 | func TestVM_Limits(t *testing.T) { |
| 1367 | tests := []struct { |
| 1368 | name string |
| 1369 | expr string |
| 1370 | memoryBudget uint |
| 1371 | maxNodes uint |
| 1372 | env map[string]any |
| 1373 | expectError string |
| 1374 | }{ |
| 1375 | { |
| 1376 | name: "nested arithmetic allowed with max nodes and memory budget", |
| 1377 | expr: createNestedArithmeticExpr(t, 100), |
| 1378 | env: map[string]any{"a": 1}, |
| 1379 | maxNodes: 1000, |
| 1380 | memoryBudget: 1, // arithmetic expressions not counted towards memory budget |
| 1381 | }, |
| 1382 | { |
| 1383 | name: "nested arithmetic blocked by max nodes", |
| 1384 | expr: createNestedArithmeticExpr(t, 10000), |
| 1385 | env: map[string]any{"a": 1}, |
| 1386 | maxNodes: 100, |
| 1387 | memoryBudget: 1, // arithmetic expressions not counted towards memory budget |
| 1388 | expectError: "compilation failed: expression exceeds maximum allowed nodes", |
| 1389 | }, |
| 1390 | { |
| 1391 | name: "nested map blocked by memory budget", |
| 1392 | expr: createNestedMapExpr(t, 100), |
| 1393 | env: map[string]any{}, |
| 1394 | maxNodes: 1000, |
| 1395 | memoryBudget: 10, // Small memory budget to trigger limit |
| 1396 | expectError: "memory budget exceeded", |
| 1397 | }, |
| 1398 | } |
| 1399 | |
| 1400 | for _, test := range tests { |
| 1401 | t.Run(test.name, func(t *testing.T) { |
| 1402 | var options []expr.Option |
| 1403 | options = append(options, expr.Env(test.env)) |
| 1404 | if test.maxNodes > 0 { |
| 1405 | options = append(options, func(c *conf.Config) { |
| 1406 | c.MaxNodes = test.maxNodes |
| 1407 | }) |
| 1408 | } |
| 1409 | |
| 1410 | program, err := expr.Compile(test.expr, options...) |
| 1411 | if err != nil { |
| 1412 | if test.expectError != "" && strings.Contains(err.Error(), test.expectError) { |
| 1413 | return |
| 1414 | } |
| 1415 | t.Fatal(err) |
| 1416 | } |
| 1417 | |
| 1418 | testVM := &vm.VM{ |
| 1419 | MemoryBudget: test.memoryBudget, |
| 1420 | } |
| 1421 | |
| 1422 | _, err = testVM.Run(program, test.env) |
| 1423 |
nothing calls this directly
no test coverage detected
searching dependent graphs…