Skip to content

Commit

Permalink
added matches regex to checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mattebit committed Aug 23, 2023
1 parent 47f0b19 commit d44cc15
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ The Checks tag is a list of Check elements, which can be defined with:
- `is in` the value is between a list of values
- `is not in` the value is not between a list of values
- `is subset of` used to check that a matched JSON array is a subset of the given array. Is subset means that all the values in the matched array are present in the given array.
- `matches regex` when using `check param` or `check` in json, you can use this tag to execute a regex to the matched value of the parameter or the value of the json key specified with the jsonpath
- `not matches regex` as `match regex` but the result is true when the regex doesn't match

Note that you can use `check regex` OR `check` OR `check param`.

Expand Down
46 changes: 43 additions & 3 deletions tool/src/main/java/migt/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ public Check(JSONObject json_check) throws ParsingException {
value_list.add(act_enc);
}
break;
case "matches regex":
this.op = MATCHES_REGEX;
this.op_val = json_check.getString("matches regex");
break;
case "not matches regex":
this.op = NOT_MATCHES_REGEX;
this.op_val = json_check.getString("not matches regex");
break;
}
} catch (JSONException e) {
throw new ParsingException("error in parsing check: " + e);
Expand Down Expand Up @@ -183,7 +191,7 @@ public void loader(Operation_API api) {
* @param input the input content
* @return the result of the check
*/
private boolean execute_regex(String input) {
private boolean execute_regex(String input) throws ParsingException {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
applicable = true;
Expand Down Expand Up @@ -426,6 +434,18 @@ private boolean execute_json() throws ParsingException {
throw new ParsingException("Matched single element in jwt, but should be an array when using IS SUBSET OF");

return value_list.containsAll(found_array);
case MATCHES_REGEX: {
if (value_is_array) throw new ParsingException("Check error: cannot execute a regex over a list");
Pattern p = Pattern.compile(op_val);
Matcher m = p.matcher(found);
return m.find();
}
case NOT_MATCHES_REGEX: {
if (value_is_array) throw new ParsingException("Check error: cannot execute a regex over a list");
Pattern p = Pattern.compile(op_val);
Matcher m = p.matcher(found);
return !m.find();
}
}

return false;
Expand All @@ -437,7 +457,7 @@ private boolean execute_json() throws ParsingException {
* @param val_to_check the value to check
* @return the result of the check
*/
public boolean do_check(String val_to_check) {
public boolean do_check(String val_to_check) throws ParsingException {
try {
if (this.op == null && val_to_check.length() != 0) {
// if it passed all the splits without errors, the param is present, but no checks are specified
Expand Down Expand Up @@ -473,6 +493,18 @@ public boolean do_check(String val_to_check) {
return value_list.contains(val_to_check); // TODO check
case IS_NOT_IN:
return !value_list.contains(val_to_check);
case MATCHES_REGEX: {
Pattern p = Pattern.compile(op_val);
Matcher m = p.matcher(val_to_check);
return m.find();
}
case NOT_MATCHES_REGEX: {
Pattern p = Pattern.compile(op_val);
Matcher m = p.matcher(val_to_check);
return !m.find();
}
default:
throw new ParsingException("Unsupported operand for Check in a message: " + op.toString());
}
} catch (ArrayIndexOutOfBoundsException e) {
//e.printStackTrace();
Expand Down Expand Up @@ -564,7 +596,9 @@ public enum CheckOps {
IS_NOT_PRESENT,
IS_IN,
IS_NOT_IN,
IS_SUBSET_OF;
IS_SUBSET_OF,
MATCHES_REGEX,
NOT_MATCHES_REGEX;

/**
* Function that given a String, returns the corresponding CheckOps enum's value
Expand All @@ -588,6 +622,12 @@ public static CheckOps fromString(String input) throws ParsingException {
return IS_IN;
case "is not in":
return IS_NOT_IN;
case "is subset of":
return IS_SUBSET_OF;
case "matches regex":
return MATCHES_REGEX;
case "not matches regex":
return NOT_MATCHES_REGEX;
default:
throw new ParsingException("invalid check operation");
}
Expand Down
56 changes: 56 additions & 0 deletions tool/src/test/java/Checks_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,60 @@ void test_check_json_array_is_subset_of_wrong() throws ParsingException {
c.execute(new ArrayList<Var>());
assertFalse(c.getResult());
}

@Test
@DisplayName("check")
void test_check_json_matches_regex() throws ParsingException {
String check_str = "{\n" +
" \"in\": \"header\",\n" +
" \"check\": \"$.pageInfo.pagePic\",\n" +
" \"matches regex\": \"example\"\n" +
"}";

Check c = initCheck_json(check_str);
c.execute(new ArrayList<Var>());
assertTrue(c.getResult());
}

@Test
@DisplayName("check")
void test_check_json_matches_regex_wrong() throws ParsingException {
String check_str = "{\n" +
" \"in\": \"header\",\n" +
" \"check\": \"$.pageInfo.pagePic\",\n" +
" \"matches regex\": \"exampsle\"\n" +
"}";

Check c = initCheck_json(check_str);
c.execute(new ArrayList<Var>());
assertFalse(c.getResult());
}

@Test
@DisplayName("check")
void test_check_json_not_matches_regex() throws ParsingException {
String check_str = "{\n" +
" \"in\": \"header\",\n" +
" \"check\": \"$.pageInfo.pagePic\",\n" +
" \"not matches regex\": \"exampsle\"\n" +
"}";

Check c = initCheck_json(check_str);
c.execute(new ArrayList<Var>());
assertTrue(c.getResult());
}

@Test
@DisplayName("check")
void test_check_json_not_matches_regex_wrong() throws ParsingException {
String check_str = "{\n" +
" \"in\": \"header\",\n" +
" \"check\": \"$.pageInfo.pagePic\",\n" +
" \"not matches regex\": \"example\"\n" +
"}";

Check c = initCheck_json(check_str);
c.execute(new ArrayList<Var>());
assertFalse(c.getResult());
}
}

0 comments on commit d44cc15

Please sign in to comment.