Skip to content

Commit

Permalink
feat: save JSON Arrays and use them in checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mattebit committed Jan 4, 2024
1 parent 51aa03b commit 71a1bcd
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 123 deletions.
103 changes: 68 additions & 35 deletions tool/src/main/java/migt/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,48 @@ public Check(JSONObject json_check) throws ParsingException {
break;
case "is in":
this.op = CheckOps.IS_IN;
JSONArray jsonArr = json_check.getJSONArray("is in");
Iterator<Object> it = jsonArr.iterator();

while (it.hasNext()) {
String act_enc = (String) it.next();
value_list.add(act_enc);
if (json_check.has("use variable")) {
// inside "is in" a string with the name of the var is expected
this.op_val = json_check.getString("is in");
} else {
JSONArray jsonArr = json_check.getJSONArray("is in");
Iterator<Object> it = jsonArr.iterator();

while (it.hasNext()) {
String act_enc = (String) it.next();
value_list.add(act_enc);
}
}
break;
case "is not in":
this.op = CheckOps.IS_NOT_IN;
JSONArray jsonArr2 = json_check.getJSONArray("is not in");
Iterator<Object> it2 = jsonArr2.iterator();

while (it2.hasNext()) {
String act_enc = (String) it2.next();
value_list.add(act_enc);
if (json_check.has("use variable")) {
// inside "is not in" a string with the name of the var is expected
this.op_val = json_check.getString("is not in");
} else {
JSONArray jsonArr2 = json_check.getJSONArray("is not in");
Iterator<Object> it2 = jsonArr2.iterator();

while (it2.hasNext()) {
String act_enc = (String) it2.next();
value_list.add(act_enc);
}
}
break;
case "is subset of":
this.op = IS_SUBSET_OF;
JSONArray jsonArr3 = json_check.getJSONArray("is subset of");
Iterator<Object> it3 = jsonArr3.iterator();

while (it3.hasNext()) {
String act_enc = (String) it3.next();
value_list.add(act_enc);
if (json_check.has("use variable")) {
// inside "is subset of" a string with the name of the var is expected
this.op_val = json_check.getString("is subset of");
} else {
JSONArray jsonArr3 = json_check.getJSONArray("is subset of");
Iterator<Object> it3 = jsonArr3.iterator();

while (it3.hasNext()) {
String act_enc = (String) it3.next();
value_list.add(act_enc);
}
}
break;
case "json schema compliant":
Expand Down Expand Up @@ -231,7 +247,14 @@ private boolean execute_regex(String input) throws ParsingException {
* @throws ParsingException if something wrong is found wrt the language
*/
private boolean execute_http(HTTPReqRes message,
boolean isRequest) throws ParsingException {
boolean isRequest,
List<Var> vars) throws ParsingException {

if (use_variable) {
Var v = Tools.getVariableByName(op_val, vars);
op_val = v.get_value_string();
}

String msg_str = "";
if (this.in == null) {
throw new ParsingException("from tag in checks is null");
Expand Down Expand Up @@ -328,13 +351,34 @@ private String url_decode(String string) {
* @return the result of the execution
* @throws ParsingException if something wrong is found wrt the language
*/
private boolean execute_json() throws ParsingException {
private boolean execute_json(List<Var> vars) throws ParsingException {
DecodeOperation_API tmp = ((DecodeOperation_API) this.imported_api);

if (isParamCheck) {
throw new ParsingException("Cannot execute a 'check param' in a json, please use 'check'");
}

Var v = null;

if (use_variable) {
// Substitute to the op_val variable (that contains the name), the value of the variable
v = Tools.getVariableByName(op_val, vars);

// TODO: check for variable type and check operation compatibility
switch (v.getType()) {
case STRING:
op_val = v.get_value_string();
break;
case JSON_ARRAY: // if the variable is a json array, substitute the value_list
for (Object el : ((JSONArray) v.value).toList()) {
value_list.add(el.toString()); // TODO check for unwanted scenarios when converting to string
}
break;
default:
throw new ParsingException("Invalid variable type to be used in check");
}
}

String j = "";

switch (in) {
Expand Down Expand Up @@ -578,11 +622,8 @@ public boolean execute(HTTPReqRes message,
boolean isRequest,
List<Var> vars) throws ParsingException {

if (use_variable) {
// Substitute to the op_val variable (that contains the name), the value of the variable
op_val = Tools.getVariableByName(op_val, vars).value;
}
result = execute_http(message, isRequest);

result = execute_http(message, isRequest, vars);
return result;
}

Expand All @@ -592,26 +633,18 @@ public boolean execute(HTTPReqRes message,
* @param vars the variables of the actual operation (test)
*/
public void execute(List<Var> vars) throws ParsingException {
if (use_variable) {
// Substitute to the op_val variable (that contains the name), the value of the variable
op_val = Tools.getVariableByName(op_val, vars).value;

// URL-decode variable value
// when a string contains a "+" character then, it is replaced with a space.
op_val = url_decode(op_val);
}

if (imported_api instanceof Operation_API) {
// If is inside a standard Operation
result = execute_http(
((Operation_API) imported_api).message,
((Operation_API) imported_api).is_request
((Operation_API) imported_api).is_request,
vars
);
} else if (imported_api instanceof DecodeOperation_API) {
// if inside a decode operation
switch (((DecodeOperation_API) imported_api).type) {
case JWT:
result = execute_json();
result = execute_json(vars);
break;
case NONE:
//TODO
Expand Down
21 changes: 4 additions & 17 deletions tool/src/main/java/migt/EditOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,15 @@ public void execute_decodeOperation_API(List<Var> vars) throws ParsingException
String to_save = XML.getTagValaue(edited_xml,
xml_action_name,
xml_occurrency);
Var v = new Var();
v.name = save_as;
v.isMessage = false;
v.value = to_save;
Var v = new Var(save_as, to_save);
vars.add(v);
break;
}
case SAVE_ATTR:
String to_save = XML.getTagAttributeValue(edited_xml,
xml_tag, xml_action_name,
xml_occurrency);
Var v = new Var();
v.name = save_as;
v.isMessage = false;
v.value = to_save;
Var v = new Var(save_as, to_save);
vars.add(v);
break;
}
Expand Down Expand Up @@ -395,10 +389,7 @@ public void execute_decodeOperation_API(List<Var> vars) throws ParsingException
break;
}

Var v = new Var();
v.name = save_as;
v.isMessage = false;
v.value = val;
Var v = new Var(save_as, val);
vars.add(v);
break;
}
Expand Down Expand Up @@ -538,11 +529,7 @@ public void execute(List<Var> vars) throws ParsingException {
// If a variable value has to be used, read the value of the variable at execution time
if (!use.equals("")) {
Var v = getVariableByName(use, vars);
if (!v.isMessage) {
value = v.value;
} else {
throw new ParsingException("Error while using variable, expected text var, got message var");
}
value = v.get_value_string();
}

if (imported_api instanceof DecodeOperation_API) {
Expand Down
4 changes: 1 addition & 3 deletions tool/src/main/java/migt/ExecuteTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ public void run() {
continue;
}
case SET_VAR: {
Var v = new Var();
v.name = action.elem;
v.value = action.content;
Var v = new Var(action.elem, action.content);
listener.onSetVar(v);
continue;
}
Expand Down
22 changes: 8 additions & 14 deletions tool/src/main/java/migt/MessageOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public MessageOperation(JSONObject message_op_json) throws ParsingException {
*/
public static String getAdding(MessageOperation m, List<Var> vars) throws ParsingException {
if (!m.use.isEmpty()) {
return getVariableByName(m.use, vars).value;
Var v = getVariableByName(m.use, vars);
return v.get_value_string();
} else {

return m.to;
Expand Down Expand Up @@ -336,26 +337,22 @@ public Operation execute(Operation op) throws ParsingException {
}
}

Var v = new Var();
v.name = mop.save_as;
v.isMessage = false;
v.value = value;
Var v = new Var(mop.save_as, value);
op.api.vars.add(v);
break;
}
case BODY: {
String tmp = new String(op.api.message.getBody(op.api.is_request), StandardCharsets.UTF_8);
pattern = Pattern.compile(mop.what);
matcher = pattern.matcher(tmp);
Var v = new Var();
Var v = null;

while (matcher.find()) {
v.name = mop.save_as;
v.isMessage = false;
v.value = matcher.group();
v = new Var(mop.save_as, matcher.group());
break;
}
op.api.vars.add(v);
if (v != null)
op.api.vars.add(v);
break;
}
case URL: {
Expand All @@ -378,10 +375,7 @@ public Operation execute(Operation op) throws ParsingException {
matched.split("=")[1] :
matched;

Var v = new Var();
v.name = mop.save_as;
v.isMessage = false;
v.value = value;
Var v = new Var(mop.save_as, value);
op.api.vars.add(v);
}
break;
Expand Down
44 changes: 21 additions & 23 deletions tool/src/main/java/migt/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,52 +270,50 @@ public List<Var> executeSessionOps(Test t, //TODO add this to the input api of O

switch (sop.action) {
case SAVE:
Var v = new Var();
v.name = sop.as;
v.isMessage = false;
v.value = "";
String value = "";
switch (sop.target) {
case TRACK:
for (SessionTrackAction sa : t.getSession(sop.from_session).track
.getStasFromMarkers(sop.at, sop.to, sop.is_from_included, sop.is_to_included)) {
v.value += sa.toString() + "\n";
value += sa.toString() + "\n";
}
break;
case LAST_ACTION:
v.value = session.last_action.toString();
value = session.last_action.toString();
break;
case LAST_ACTION_ELEM:
v.value = session.last_action.elem;
value = session.last_action.elem;
break;
case LAST_ACTION_ELEM_PARENT:
v.value = findParentDiv(session.last_action.elem);
value = findParentDiv(session.last_action.elem);
break;
case LAST_CLICK:
v.value = session.last_click.toString();
value = session.last_click.toString();
break;
case LAST_CLICK_ELEM:
v.value = session.last_click.elem;
value = session.last_click.elem;
break;
case LAST_CLICK_ELEM_PARENT:
v.value = findParentDiv(session.last_click.elem);
value = findParentDiv(session.last_click.elem);
break;
case LAST_OPEN:
v.value = session.last_open.toString();
value = session.last_open.toString();
break;
case LAST_OPEN_ELEM:
v.value = session.last_open.elem;
value = session.last_open.elem;
break;
case LAST_URL:
v.value = session.last_url;
value = session.last_url;
break;
case ALL_ASSERT:
for (SessionTrackAction sa : t.getSession(sop.from_session).track.getTrack()) {
if (sa.isAssert) {
v.value += sa + "\n";
value += sa + "\n";
}
}
break;
}
Var v = new Var(sop.as, value);
updated_vars.add(v);
break;

Expand Down Expand Up @@ -430,9 +428,9 @@ public void execute() {
if (api.is_request) {
if (!replace_request_name.equals("")) {
try {
Var v = getVariableByName(replace_request_name, api.vars);
processed_message = v.get_value_message();
applicable = true;
processed_message = getVariableByName(replace_request_name, api.vars).message;
//return op;
} catch (ParsingException e) {
e.printStackTrace();
applicable = false;
Expand All @@ -442,9 +440,9 @@ public void execute() {
} else {
if (!replace_response_name.equals("")) {
try {
Var v = getVariableByName(replace_response_name, api.vars);
processed_message = v.get_value_message();
applicable = true;
processed_message = getVariableByName(replace_response_name, api.vars).message;
//return op;
} catch (ParsingException e) {
e.printStackTrace();
applicable = false;
Expand Down Expand Up @@ -491,10 +489,10 @@ public void execute() {
}

if (!save_name.equals("")) {
Var v = new Var();
v.name = save_name;
v.isMessage = true;
v.message = api.is_request ? api.message.getRequest() : api.message.getResponse();
Var v = new Var(
save_name,
api.is_request ? api.message.getRequest() : api.message.getResponse()
);
api.vars.add(v);
}
}
Expand Down
6 changes: 5 additions & 1 deletion tool/src/main/java/migt/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ public void logTest(String log_folder) {
test_log_content += "Variables: \n";

for (Var v : vars) {
test_log_content += v.name = v.value + "\n";
try {
test_log_content += v.name = v.value + "\n";
} catch (Exception e) {
e.printStackTrace();
}
}

File test_log = new File(test_log_path);
Expand Down
Loading

0 comments on commit 71a1bcd

Please sign in to comment.