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());
}
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);
}
}
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());
}
}
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());
}
}
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());
}
}
Aggregations