test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java

Print this page
rev 753 : 8033924: Default permissions are not given for eval code
Reviewed-by: lagergren, jlaskey


 543         final ScriptEngineManager m = new ScriptEngineManager();
 544         final ScriptEngine e = m.getEngineByName("nashorn");
 545         final boolean[] reached = new boolean[1];
 546         final Runnable r = (Runnable)Proxy.newProxyInstance(
 547             ScriptEngineTest.class.getClassLoader(),
 548             new Class[] { Runnable.class },
 549             new InvocationHandler() {
 550                 @Override
 551                 public Object invoke(Object p, Method m, Object[] a) {
 552                     reached[0] = true;
 553                     return null;
 554                 }
 555             });
 556 
 557         e.put("r", r);
 558         e.eval("r.run()");
 559 
 560         assertTrue(reached[0]);
 561     }
 562 









































 563     private static final String LINE_SEPARATOR = System.getProperty("line.separator");
 564 
 565     // Returns String that would be the result of calling PrintWriter.println
 566     // of the given String. (This is to handle platform specific newline).
 567     private static String println(final String str) {
 568         return str + LINE_SEPARATOR;
 569     }
 570 }


 543         final ScriptEngineManager m = new ScriptEngineManager();
 544         final ScriptEngine e = m.getEngineByName("nashorn");
 545         final boolean[] reached = new boolean[1];
 546         final Runnable r = (Runnable)Proxy.newProxyInstance(
 547             ScriptEngineTest.class.getClassLoader(),
 548             new Class[] { Runnable.class },
 549             new InvocationHandler() {
 550                 @Override
 551                 public Object invoke(Object p, Method m, Object[] a) {
 552                     reached[0] = true;
 553                     return null;
 554                 }
 555             });
 556 
 557         e.put("r", r);
 558         e.eval("r.run()");
 559 
 560         assertTrue(reached[0]);
 561     }
 562 
 563     // properties that can be read by any code
 564     private static String[] propNames = {
 565         "java.version",
 566         "java.vendor",
 567         "java.vendor.url",
 568         "java.class.version",
 569         "os.name",
 570         "os.version",
 571         "os.arch",
 572         "file.separator",
 573         "path.separator",
 574         "line.separator",
 575         "java.specification.version",
 576         "java.specification.vendor",
 577         "java.specification.name",
 578         "java.vm.specification.version",
 579         "java.vm.specification.vendor",
 580         "java.vm.specification.name",
 581         "java.vm.version",
 582         "java.vm.vendor",
 583         "java.vm.name"
 584     };
 585 
 586     // @bug 8033924: Default permissions are not given for eval code
 587     @Test
 588     public void checkPropertyReadPermissions() throws ScriptException {
 589         final ScriptEngineManager m = new ScriptEngineManager();
 590         final ScriptEngine e = m.getEngineByName("nashorn");
 591 
 592         for (final String name : propNames) {
 593             checkProperty(e, name);
 594         }
 595     }
 596 
 597     private static void checkProperty(final ScriptEngine e, final String name)
 598         throws ScriptException {
 599         String value = System.getProperty(name);
 600         e.put("name", name);
 601         assertEquals(value, e.eval("java.lang.System.getProperty(name)"));
 602     }
 603 
 604     private static final String LINE_SEPARATOR = System.getProperty("line.separator");
 605 
 606     // Returns String that would be the result of calling PrintWriter.println
 607     // of the given String. (This is to handle platform specific newline).
 608     private static String println(final String str) {
 609         return str + LINE_SEPARATOR;
 610     }
 611 }