Skip to content

Apache Lucene open-source search software

LUCENE-5207: add some checks and tests for illegal stuff

Robert Muir591a3b1ca18d
diff --git a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
index 9dd2f67d7bd..f02d24f8fd8 100644
--- a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
+++ b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
@@ -712,6 +712,14 @@ public class JavascriptCompiler {
if (!Modifier.isStatic(method.getModifiers())) {
throw new IllegalArgumentException(method + " is not static.");
}
+ if (!Modifier.isPublic(method.getModifiers())) {
+ throw new IllegalArgumentException(method + " is not public.");
+ }
+ for (Class<?> clazz : method.getParameterTypes()) {
+ if (!clazz.equals(double.class)) {
+ throw new IllegalArgumentException(method + " must take only double parameters");
+ }
+ }
if (method.getReturnType() != double.class) {
throw new IllegalArgumentException(method + " does not return a double.");
}
diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
index 23d0b162a48..6c18dae3531 100644
--- a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
+++ b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
@@ -85,4 +85,60 @@ public class TestCustomFunctions extends LuceneTestCase {
Expression expr = JavascriptCompiler.compile("foo() + bar(3)", functions, getClass().getClassLoader());
assertEquals(11, expr.evaluate(0, null), DELTA);
}
+
+ public static String bogusReturnType() { return "bogus!"; }
+
+ /** wrong return type: must be double */
+ public void testWrongReturnType() throws Exception {
+ Map<String,Method> functions = new HashMap<String,Method>();
+ functions.put("foo", getClass().getMethod("bogusReturnType"));
+ try {
+ JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("does not return a double"));
+ }
+ }
+
+ public static double bogusParameterType(String s) { return 0; }
+
+ /** wrong param type: must be doubles */
+ public void testWrongParameterType() throws Exception {
+ Map<String,Method> functions = new HashMap<String,Method>();
+ functions.put("foo", getClass().getMethod("bogusParameterType", String.class));
+ try {
+ JavascriptCompiler.compile("foo(2)", functions, getClass().getClassLoader());
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("must take only double parameters"));
+ }
+ }
+
+ public double nonStaticMethod() { return 0; }
+
+ /** wrong modifiers: must be static */
+ public void testWrongNotStatic() throws Exception {
+ Map<String,Method> functions = new HashMap<String,Method>();
+ functions.put("foo", getClass().getMethod("nonStaticMethod"));
+ try {
+ JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("is not static"));
+ }
+ }
+
+ static double nonPublicMethod() { return 0; }
+
+ /** wrong modifiers: must be public */
+ public void testWrongNotPublic() throws Exception {
+ Map<String,Method> functions = new HashMap<String,Method>();
+ functions.put("foo", getClass().getDeclaredMethod("nonPublicMethod"));
+ try {
+ JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("is not public"));
+ }
+ }
}