Search in sources :

Example 11 with Script

use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.

the class LexicalTest method testInternalLexicalFeatures.

@Test
public void testInternalLexicalFeatures() throws Exception {
    final String str = "42";
    final JexlFeatures f = new JexlFeatures();
    f.lexical(true);
    f.lexicalShade(true);
    final JexlEngine jexl = new JexlBuilder().features(f).create();
    final JexlScript e = jexl.createScript(str);
    final VarContext vars = new VarContext();
    final JexlOptions opts = vars.getEngineOptions();
    // so we can see the effect of features on options
    opts.setSharedInstance(true);
    final Script script = (Script) e;
    final JexlFeatures features = script.getFeatures();
    Assert.assertTrue(features.isLexical());
    Assert.assertTrue(features.isLexicalShade());
    final Object result = e.execute(vars);
    Assert.assertEquals(42, result);
    Assert.assertTrue(opts.isLexical());
    Assert.assertTrue(opts.isLexicalShade());
}
Also used : Script(org.apache.commons.jexl3.internal.Script) Test(org.junit.Test)

Example 12 with Script

use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.

the class JXLTTest method testDbgEscapes.

@Test
public void testDbgEscapes() throws Exception {
    String[] srcs = new String[] { "jexl:print('hello\\'\\nworld')", "'hello\\tworld'", "'hello\\nworld'", "'hello\\fworld'", "'hello\\rworld'" };
    for (String src : srcs) {
        JexlScript script = ENGINE.createScript(src);
        Debugger dbg = new Debugger();
        dbg.debug(script);
        String msrc = dbg.toString();
        Assert.assertEquals(src, msrc);
    }
}
Also used : TemplateDebugger(org.apache.commons.jexl3.internal.TemplateDebugger) Debugger(org.apache.commons.jexl3.internal.Debugger) Test(org.junit.Test)

Example 13 with Script

use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.

the class PermissionsTest method testParsePermissions1.

@Test
public void testParsePermissions1() {
    String[] src = new String[] { "java.lang.*", "java.math.*", "java.text.*", "java.util.*", "java.lang { Runtime {} }", "java.rmi {}", "java.io { File {} }", "java.nio { Path {} }", "org.apache.commons.jexl3.internal.introspection { " + "PermissionsTest { #level 0\n" + " Outer { #level 1\n" + " Inner { #level 2\n" + " callMeNot();" + " }" + " }" + " }" + " }" };
    Permissions p = (Permissions) JexlPermissions.parse(src);
    Map<String, Permissions.NoJexlPackage> nojexlmap = p.getPackages();
    Assert.assertNotNull(nojexlmap);
    Set<String> wildcards = p.getWildcards();
    Assert.assertEquals(4, wildcards.size());
    JexlEngine jexl = new JexlBuilder().permissions(p).safe(false).lexical(true).create();
    Method exit = getMethod(java.lang.Runtime.class, "exit");
    Assert.assertNotNull(exit);
    Assert.assertFalse(p.allow(exit));
    Method exec = getMethod(java.lang.Runtime.class, "getRuntime");
    Assert.assertNotNull(exec);
    Assert.assertFalse(p.allow(exec));
    Method callMeNot = getMethod(Outer.Inner.class, "callMeNot");
    Assert.assertNotNull(callMeNot);
    Assert.assertFalse(p.allow(callMeNot));
    JexlScript script = jexl.createScript("o.callMeNot()", "o");
    try {
        Object result = script.execute(null, new Outer.Inner());
        Assert.fail("callMeNot should be uncallable");
    } catch (JexlException.Method xany) {
        Assert.assertEquals("callMeNot", xany.getMethod());
    }
    Method uncallable = getMethod(Invisible.class, "uncallable");
    Assert.assertFalse(p.allow(uncallable));
    Package ip = Invisible.class.getPackage();
    Assert.assertFalse(p.allow(ip));
    script = jexl.createScript("o.uncallable()", "o");
    try {
        Object result = script.execute(null, new Invisible());
        Assert.fail("uncallable should be uncallable");
    } catch (JexlException.Method xany) {
        Assert.assertEquals("uncallable", xany.getMethod());
    }
}
Also used : JexlException(org.apache.commons.jexl3.JexlException) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlScript(org.apache.commons.jexl3.JexlScript) Method(java.lang.reflect.Method) JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlPermissions(org.apache.commons.jexl3.introspection.JexlPermissions) Invisible(org.apache.commons.jexl3.internal.introspection.nojexlpackage.Invisible) Test(org.junit.Test)

Example 14 with Script

use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.

the class SandboxTest method testGetBlock.

@Test
public void testGetBlock() throws Exception {
    final String expr = "foo.alias";
    JexlScript script = JEXL.createScript(expr, "foo");
    final Foo foo = new Foo("42");
    Object result;
    result = script.execute(null, foo);
    Assert.assertEquals(foo.alias, result);
    final JexlSandbox sandbox = new JexlSandbox();
    sandbox.block(Foo.class.getName()).read("alias");
    final JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create();
    script = sjexl.createScript(expr, "foo");
    try {
        result = script.execute(null, foo);
        Assert.fail("alias should not be accessible");
    } catch (final JexlException.Property xvar) {
        // ok, alias should not have been accessible
        LOGGER.debug(xvar.toString());
    }
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlException(org.apache.commons.jexl3.JexlException) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlScript(org.apache.commons.jexl3.JexlScript) Test(org.junit.Test)

Example 15 with Script

use of org.apache.commons.jexl3.internal.Script in project commons-jexl by apache.

the class SandboxTest method testMethodBlock.

@Test
public void testMethodBlock() throws Exception {
    final String expr = "foo.Quux()";
    JexlScript script = JEXL.createScript(expr, "foo");
    final Foo foo = new Foo("42");
    Object result;
    result = script.execute(null, foo);
    Assert.assertEquals(foo.Quux(), result);
    final JexlSandbox sandbox = new JexlSandbox();
    sandbox.block(Foo.class.getName()).execute("Quux");
    final JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create();
    script = sjexl.createScript(expr, "foo");
    try {
        result = script.execute(null, foo);
        Assert.fail("Quux should not be accessible");
    } catch (final JexlException.Method xmethod) {
        // ok, Quux should not have been accessible
        LOGGER.debug(xmethod.toString());
    }
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlException(org.apache.commons.jexl3.JexlException) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlScript(org.apache.commons.jexl3.JexlScript) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)27 JexlScript (org.apache.commons.jexl3.JexlScript)24 JexlEngine (org.apache.commons.jexl3.JexlEngine)17 JexlException (org.apache.commons.jexl3.JexlException)17 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)15 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)14 JexlContext (org.apache.commons.jexl3.JexlContext)8 JexlNode (org.apache.commons.jexl3.parser.JexlNode)8 MapContext (org.apache.commons.jexl3.MapContext)5 TemplateDebugger (org.apache.commons.jexl3.internal.TemplateDebugger)4 ASTJexlLambda (org.apache.commons.jexl3.parser.ASTJexlLambda)4 JexlFeatures (org.apache.commons.jexl3.JexlFeatures)3 JexlOptions (org.apache.commons.jexl3.JexlOptions)3 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)3 StringReader (java.io.StringReader)2 Map (java.util.Map)2 ScriptException (javax.script.ScriptException)2 JexlInfo (org.apache.commons.jexl3.JexlInfo)2 JxltEngine (org.apache.commons.jexl3.JxltEngine)2 Debugger (org.apache.commons.jexl3.internal.Debugger)2