metron-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ceste...@apache.org
Subject metron git commit: METRON-1171: Better validation for the SUBSTRING stellar function closes apache/incubator-metron#745
Date Sun, 10 Sep 2017 01:33:35 GMT
Repository: metron
Updated Branches:
  refs/heads/master 269d42e92 -> 382816062


METRON-1171: Better validation for the SUBSTRING stellar function closes apache/incubator-metron#745


Project: http://git-wip-us.apache.org/repos/asf/metron/repo
Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/38281606
Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/38281606
Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/38281606

Branch: refs/heads/master
Commit: 3828160625c6617db99d9a640fdb6b84d4f91f4f
Parents: 269d42e
Author: cstella <cestella@gmail.com>
Authored: Sat Sep 9 21:33:13 2017 -0400
Committer: cstella <cestella@gmail.com>
Committed: Sat Sep 9 21:33:13 2017 -0400

----------------------------------------------------------------------
 .../stellar/dsl/functions/StringFunctions.java  | 20 ++++++++++++++++----
 .../dsl/functions/StringFunctionsTest.java      | 20 +++++++++++++++++++-
 2 files changed, 35 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/metron/blob/38281606/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/StringFunctions.java
----------------------------------------------------------------------
diff --git a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/StringFunctions.java
b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/StringFunctions.java
index 99c8fe7..7f4f396 100644
--- a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/StringFunctions.java
+++ b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/StringFunctions.java
@@ -336,13 +336,25 @@ public class StringFunctions {
     public Object apply(List<Object> strings) {
 
       if(strings == null || strings.size() < 2 ) {
-        throw new IllegalArgumentException("[SUBSTRING] required 2 arguments: the input and
the start position (inclusive)");
+        throw new IllegalArgumentException("SUBSTRING requires (at least) 2 arguments: the
input and the start position (inclusive)");
       }
-      String var = strings.get(0) == null?null: (String) strings.get(0);
-      Integer start = strings.get(1) == null?null:(Integer)strings.get(1);
+      Object varObj = strings.get(0);
+      if(varObj != null && !(varObj instanceof String)) {
+        throw new IllegalArgumentException("SUBSTRING input must be a String");
+      }
+      String var = varObj == null?null: (String) varObj;
+      Object startObj = strings.get(1);
+      if(startObj != null && !(startObj instanceof Number)) {
+        throw new IllegalArgumentException("SUBSTRING start must be an Number");
+      }
+      Integer start = startObj == null?null:((Number)startObj).intValue();
       Integer end = null;
       if(strings.size() > 2) {
-         end = strings.get(2) == null ? null : (Integer) strings.get(2);
+        Object endObj = strings.get(2);
+        if(endObj != null && !(endObj instanceof Number)) {
+          throw new IllegalArgumentException("SUBSTRING end must be an Number");
+        }
+        end = endObj == null ? null : ((Number) endObj).intValue();
       }
       if(var == null || start == null) {
         return null;

http://git-wip-us.apache.org/repos/asf/metron/blob/38281606/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/StringFunctionsTest.java
----------------------------------------------------------------------
diff --git a/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/StringFunctionsTest.java
b/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/StringFunctionsTest.java
index 2b1115e..cb33eaa 100644
--- a/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/StringFunctionsTest.java
+++ b/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/StringFunctionsTest.java
@@ -422,7 +422,25 @@ public class StringFunctionsTest {
 
   @Test(expected=ParseException.class)
   public void testSubstring_invalidEmpty() throws Exception {
-    Assert.assertEquals("metron", (String) run("SUBSTRING()", new HashMap<>()));
+    Assert.assertEquals("metron", run("SUBSTRING()", new HashMap<>()));
+  }
+
+  @Test(expected=ParseException.class)
+  public void testSubstring_invalidWrongTypeStart() throws Exception {
+    Map<String, Object> variables = ImmutableMap.of("s", "apache metron");
+    Assert.assertEquals("metron", (String) run("SUBSTRING(s, '7')", variables));
+  }
+
+  @Test(expected=ParseException.class)
+  public void testSubstring_invalidWrongTypeEnd() throws Exception {
+    Map<String, Object> variables = ImmutableMap.of("s", "apache metron");
+    Assert.assertEquals("metron", (String) run("SUBSTRING(s, 7, '9')", variables));
+  }
+
+  @Test(expected=ParseException.class)
+  public void testSubstring_invalidWrongTypeInput() throws Exception {
+    Map<String, Object> variables = ImmutableMap.of("s", 7);
+    Assert.assertEquals("metron", (String) run("SUBSTRING(s, 7, '9')", variables));
   }
 
   /**


Mime
View raw message