1   package delight.nashornsandbox.tests;
2   
3   import delight.nashornsandbox.NashornSandbox;
4   import delight.nashornsandbox.NashornSandboxes;
5   import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;
6   import java.util.concurrent.ExecutorService;
7   import java.util.concurrent.Executors;
8   import org.eclipse.xtend2.lib.StringConcatenation;
9   import org.junit.Test;
10  
11  @SuppressWarnings("all")
12  public class TestLimitCPU {
13    @Test(expected = ScriptCPUAbuseException.class)
14    public void test() {
15      final NashornSandbox sandbox = NashornSandboxes.create();
16      try {
17        sandbox.setMaxCPUTime(50);
18        ExecutorService _newSingleThreadExecutor = Executors.newSingleThreadExecutor();
19        sandbox.setExecutor(_newSingleThreadExecutor);
20        StringConcatenation _builder = new StringConcatenation();
21        _builder.append("var x = 1;");
22        _builder.newLine();
23        _builder.append("while (true) {");
24        _builder.newLine();
25        _builder.append("\t");
26        _builder.append("x=x+1;");
27        _builder.newLine();
28        _builder.append("}");
29        _builder.newLine();
30        sandbox.eval(_builder.toString());
31      } finally {
32        ExecutorService _executor = sandbox.getExecutor();
33        _executor.shutdown();
34      }
35    }
36    
37    @Test(expected = ScriptCPUAbuseException.class)
38    public void test_evil_script() {
39      final NashornSandbox sandbox = NashornSandboxes.create();
40      try {
41        sandbox.setMaxCPUTime(50);
42        ExecutorService _newSingleThreadExecutor = Executors.newSingleThreadExecutor();
43        sandbox.setExecutor(_newSingleThreadExecutor);
44        StringConcatenation _builder = new StringConcatenation();
45        _builder.append("var x = 1;");
46        _builder.newLine();
47        _builder.append("while (true) { }");
48        _builder.newLine();
49        sandbox.eval(_builder.toString());
50      } finally {
51        ExecutorService _executor = sandbox.getExecutor();
52        _executor.shutdown();
53      }
54    }
55    
56    @Test
57    public void test_nice_script() {
58      final NashornSandbox sandbox = NashornSandboxes.create();
59      sandbox.setMaxCPUTime(50);
60      ExecutorService _newSingleThreadExecutor = Executors.newSingleThreadExecutor();
61      sandbox.setExecutor(_newSingleThreadExecutor);
62      StringConcatenation _builder = new StringConcatenation();
63      _builder.append("var x = 1;");
64      _builder.newLine();
65      _builder.append("for (var i=0;i<=1000;i++) {");
66      _builder.newLine();
67      _builder.append("\t");
68      _builder.append("x = x + i");
69      _builder.newLine();
70      _builder.append("}");
71      _builder.newLine();
72      sandbox.eval(_builder.toString());
73      ExecutorService _executor = sandbox.getExecutor();
74      _executor.shutdown();
75    }
76    
77    @Test(expected = ScriptCPUAbuseException.class)
78    public void test_only_while() {
79      final NashornSandbox sandbox = NashornSandboxes.create();
80      try {
81        sandbox.setMaxCPUTime(50);
82        ExecutorService _newSingleThreadExecutor = Executors.newSingleThreadExecutor();
83        sandbox.setExecutor(_newSingleThreadExecutor);
84        StringConcatenation _builder = new StringConcatenation();
85        _builder.append("while (true);");
86        _builder.newLine();
87        sandbox.eval(_builder.toString());
88      } finally {
89        ExecutorService _executor = sandbox.getExecutor();
90        _executor.shutdown();
91      }
92    }
93    
94    @Test(expected = ScriptCPUAbuseException.class)
95    public void test_while_plus_iteration() {
96      final NashornSandbox sandbox = NashornSandboxes.create();
97      try {
98        sandbox.setMaxCPUTime(50);
99        ExecutorService _newSingleThreadExecutor = Executors.newSingleThreadExecutor();
100       sandbox.setExecutor(_newSingleThreadExecutor);
101       StringConcatenation _builder = new StringConcatenation();
102       _builder.append("var x=0;");
103       _builder.newLine();
104       _builder.append("while (true) x++;");
105       _builder.newLine();
106       sandbox.eval(_builder.toString());
107     } finally {
108       ExecutorService _executor = sandbox.getExecutor();
109       _executor.shutdown();
110     }
111   }
112 }