| 2865 | } |
| 2866 | |
| 2867 | func TestMemoryBudget(t *testing.T) { |
| 2868 | tests := []struct { |
| 2869 | code string |
| 2870 | max int |
| 2871 | }{ |
| 2872 | {`map(1..100, {map(1..100, {map(1..100, {0})})})`, -1}, |
| 2873 | {`len(1..10000000)`, -1}, |
| 2874 | {`1..100`, 100}, |
| 2875 | } |
| 2876 | |
| 2877 | for _, tt := range tests { |
| 2878 | t.Run(tt.code, func(t *testing.T) { |
| 2879 | program, err := expr.Compile(tt.code) |
| 2880 | require.NoError(t, err, "compile error") |
| 2881 | |
| 2882 | vm := vm.VM{} |
| 2883 | if tt.max > 0 { |
| 2884 | vm.MemoryBudget = uint(tt.max) |
| 2885 | } |
| 2886 | _, err = vm.Run(program, nil) |
| 2887 | require.Error(t, err, "run error") |
| 2888 | assert.Contains(t, err.Error(), "memory budget exceeded") |
| 2889 | }) |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | func TestIssue802(t *testing.T) { |
| 2894 | prog, err := expr.Compile(`arr[1:2][0]`) |