Search in sources :

Example 1 with MyEquals

use of com.oracle.truffle.api.test.polyglot.HostAccessTest.MyEquals in project graal by oracle.

the class HostAccessExamplesTest method banAccessToReflection.

@Test
public void banAccessToReflection() throws Exception {
    // @formatter:off
    HostAccess config = HostAccess.newBuilder().allowPublicAccess(true).denyAccess(Class.class).denyAccess(Method.class).denyAccess(Field.class).denyAccess(Proxy.class).denyAccess(Object.class, false).build();
    try (Context context = Context.newBuilder().allowHostAccess(config).build()) {
        Value readValue = context.eval("sl", "" + "function readValue(x, y) {\n" + "  return x.equals(y);\n" + "}\n" + "function main() {\n" + "  return readValue;\n" + "}\n");
        MyEquals myEquals = new MyEquals();
        assertTrue("MyEquals.equals method is accessible", readValue.execute(myEquals, myEquals).asBoolean());
        Value res;
        try {
            res = readValue.execute(new Object());
        } catch (PolyglotException ex) {
            return;
        }
        fail("expecting no result: " + res);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Field(java.lang.reflect.Field) MyEquals(com.oracle.truffle.api.test.polyglot.HostAccessTest.MyEquals) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Aggregations

MyEquals (com.oracle.truffle.api.test.polyglot.HostAccessTest.MyEquals)1 Field (java.lang.reflect.Field)1 Context (org.graalvm.polyglot.Context)1 HostAccess (org.graalvm.polyglot.HostAccess)1 PolyglotException (org.graalvm.polyglot.PolyglotException)1 Value (org.graalvm.polyglot.Value)1 Test (org.junit.Test)1