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