You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/07/10 16:54:03 UTC

[5/6] incubator-tinkerpop git commit: Add more tests for variable filtering in GremlinGroovyScriptEngine sandbox.

Add more tests for variable filtering in GremlinGroovyScriptEngine sandbox.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/2af2c3dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/2af2c3dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/2af2c3dd

Branch: refs/heads/compiler-customizer
Commit: 2af2c3ddecd188bbf400acf910c2f73fcf21da84
Parents: 6af00c6
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 10 09:23:00 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 10 09:23:12 2015 -0400

----------------------------------------------------------------------
 .../BlockSomeVariablesSandboxExtension.groovy   | 41 +++++++++++++++++
 ...mlinGroovyScriptEngineSandboxCustomTest.java | 46 +++++++++++++++++++-
 .../groovy/jsr223/SandboxExtension.groovy       | 13 +++---
 3 files changed, 92 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2af2c3dd/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy
new file mode 100644
index 0000000..fcbbc51
--- /dev/null
+++ b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.groovy.jsr223
+
+import org.apache.tinkerpop.gremlin.structure.Graph
+import org.codehaus.groovy.ast.ClassNode
+import org.codehaus.groovy.ast.expr.VariableExpression
+
+import java.util.function.BiPredicate
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class BlockSomeVariablesSandboxExtension extends SandboxExtension {
+    BlockSomeVariablesSandboxExtension() {
+        gIsAlwaysGraphTraversalSource = false
+        graphIsAlwaysGraphInstance = false
+
+        // variable names must have a length of 3 and they can't be Graph instances
+        variableFilter = (BiPredicate<VariableExpression, Map<String,ClassNode>>) { v, m ->
+            def varType = m[v.name].getTypeClass()
+            v.name.length() > 3 && !Graph.isAssignableFrom(varType)
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2af2c3dd/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
index b9f8fcf..648e688 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
@@ -37,7 +37,6 @@ import static org.junit.Assert.fail;
  */
 public class GremlinGroovyScriptEngineSandboxCustomTest extends AbstractGremlinTest {
     @Test
-    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
     public void shouldEvalGVariableAsSomethingOtherThanGraphTraversalSource() throws Exception {
         final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(SandboxExtension.class.getName());
         try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
@@ -60,7 +59,6 @@ public class GremlinGroovyScriptEngineSandboxCustomTest extends AbstractGremlinT
     }
 
     @Test
-    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
     public void shouldEvalGraphVariableAsSomethingOtherThanGraph() throws Exception {
         final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(SandboxExtension.class.getName());
         try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
@@ -81,4 +79,48 @@ public class GremlinGroovyScriptEngineSandboxCustomTest extends AbstractGremlinT
             assertEquals(2, engine.eval("graph+1", bindings));
         }
     }
+
+    @Test
+    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+    public void shouldNotEvalBecauseSandboxIsConfiguredToNotAcceptGraphInstances() throws Exception {
+        final CompilerCustomizerProvider customSandbox = new CompileStaticCustomizerProvider(
+                BlockSomeVariablesSandboxExtension.class.getName());
+        try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(customSandbox)) {
+            final Bindings bindings = engine.createBindings();
+            bindings.put("graph", graph);
+            engine.eval("graph.vertices()", bindings);
+            fail("Should have a compile error because sandbox does not allow Graph");
+        } catch (Exception ex) {
+            assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+            assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
+        }
+    }
+
+    @Test
+    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+    public void shouldNotEvalBecauseSandboxIsConfiguredToNotAcceptShortVarNames() throws Exception {
+        final CompilerCustomizerProvider customSandbox = new CompileStaticCustomizerProvider(
+                BlockSomeVariablesSandboxExtension.class.getName());
+        try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(customSandbox)) {
+            final Bindings bindings = engine.createBindings();
+            bindings.put("g", 1);
+            engine.eval("g+1", bindings);
+            fail("Should have a compile error because sandbox wants names > 3");
+        } catch (Exception ex) {
+            assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+            assertThat(ex.getMessage(), containsString("The variable [g] is undeclared."));
+        }
+    }
+
+    @Test
+    @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+    public void shouldEvalAsVariableRequirementsAreInRangeOfSandbox() throws Exception {
+        final CompilerCustomizerProvider customSandbox = new CompileStaticCustomizerProvider(
+                BlockSomeVariablesSandboxExtension.class.getName());
+        try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(customSandbox)) {
+            final Bindings bindings = engine.createBindings();
+            bindings.put("graph", 1);
+            assertEquals(2, engine.eval("graph+1", bindings));
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2af2c3dd/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy
index d62ccb3..be0cbca 100644
--- a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy
@@ -73,12 +73,13 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
 
             final Map<String,ClassNode> varTypes = (Map<String,ClassNode>) GremlinGroovyScriptEngine.COMPILE_OPTIONS.get()
                     .get(GremlinGroovyScriptEngine.COMPILE_OPTIONS_VAR_TYPES)
-            if (varTypes.containsKey(var.name) && variableFilter.test(var, varTypes)
-                || (var.name == "graph" && !graphIsAlwaysGraphInstance
-                     || var.name == "g" && !gIsAlwaysGraphTraversalSource)) {
-                storeType(var, varTypes.get(var.name))
-                handled = true
-                return
+            if (varTypes.containsKey(var.name) && variableFilter.test(var, varTypes))  {
+                if (!(var.name in ["graph",  "g"]) || (var.name == "graph" && !graphIsAlwaysGraphInstance
+                         || var.name == "g" && !gIsAlwaysGraphTraversalSource)) {
+                    storeType(var, varTypes.get(var.name))
+                    handled = true
+                    return
+                }
             }
         }