nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineSecurityTest.java
changeset 27099 eceb216332cb
parent 26251 b93e4bd0c5de
child 27361 86c4ddb4797b
equal deleted inserted replaced
27098:2875b30458d3 27099:eceb216332cb
   166                 fail(exp.getMessage());
   166                 fail(exp.getMessage());
   167             }
   167             }
   168         }
   168         }
   169     }
   169     }
   170 
   170 
   171     @Test
       
   172     /**
       
   173      * Check that script can't implement sensitive package interfaces.
       
   174      */
       
   175     public void checkSensitiveInterfaceImplTest() throws ScriptException {
       
   176         if (System.getSecurityManager() == null) {
       
   177             // pass vacuously
       
   178             return;
       
   179         }
       
   180 
       
   181         final ScriptEngineManager m = new ScriptEngineManager();
       
   182         final ScriptEngine e = m.getEngineByName("nashorn");
       
   183         final Object[] holder = new Object[1];
       
   184         e.put("holder", holder);
       
   185         // put an empty script object into array
       
   186         e.eval("holder[0] = {}");
       
   187         // holder[0] is an object of some subclass of ScriptObject
       
   188         final Class<?> ScriptObjectClass = holder[0].getClass().getSuperclass();
       
   189         final Class<?> PropertyAccessClass = ScriptObjectClass.getInterfaces()[0];
       
   190         // implementation methods for PropertyAccess class
       
   191         e.eval("function set() {}; function get() {}; function getInt(){} " +
       
   192                "function getDouble(){}; function getLong() {}; " +
       
   193                "this.delete = function () {}; function has() {}; " +
       
   194                "function hasOwnProperty() {}");
       
   195 
       
   196         // get implementation of a restricted package interface
       
   197         try {
       
   198             log(Objects.toString(((Invocable)e).getInterface((Class<?>)PropertyAccessClass)));
       
   199             fail("should have thrown SecurityException");
       
   200         } catch (final Exception exp) {
       
   201             if (! (exp instanceof SecurityException)) {
       
   202                 fail("SecurityException expected, got " + exp);
       
   203             }
       
   204         }
       
   205     }
       
   206 
       
   207     // @bug 8032948: Nashorn linkages awry
   171     // @bug 8032948: Nashorn linkages awry
   208     public static class FakeProxy extends Proxy {
   172     public static class FakeProxy extends Proxy {
   209         public FakeProxy(final InvocationHandler ih) {
   173         public FakeProxy(final InvocationHandler ih) {
   210             super(ih);
   174             super(ih);
   211         }
   175         }