Skip to content

Commit

Permalink
Add value substitution to Moose event assertion action in test framework
Browse files Browse the repository at this point in the history
Signed-off-by: Martynas Gurskas <martynas.gurskas@nordsec.com>
  • Loading branch information
Lipt0nas committed Sep 13, 2024
1 parent e23234a commit 45c578a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
29 changes: 26 additions & 3 deletions test/drop_test/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import socket
import requests
import shutil
import re

from . import event, ffi
from .logger import logger
Expand All @@ -33,7 +34,7 @@ def to_num(s: str):
return s


compare_funcs = [
compare_ops = [
("*", lambda input, value: value is not None),
("<=", lambda input, value: value <= to_num(input)),
(">=", lambda input, value: value >= to_num(input)),
Expand All @@ -48,6 +49,20 @@ def to_num(s: str):
]


def substitute_transfer_id(input: str):
split = input.split(":")
if len(split) != 2:
raise Exception("Malformed transfer_id substitution expression")

return get_uuid(to_num(split[1]))


substitute_pattern = r"(\?\/)(.*)(\/)"
substitute_ops = [
("transfer_id", lambda input: substitute_transfer_id(input))
]


def compare_json_struct(expected: dict, actual: dict):
for key in expected:
if key is not int:
Expand All @@ -64,11 +79,19 @@ def compare_json_struct(expected: dict, actual: dict):
compare_json_struct(expected_value[i], actual_value[i])
else:
valid = False
for func in compare_funcs:
substitution = re.search(substitute_pattern, str(expected_value))
if substitution is not None:
inner = substitution.group(2)
for sub_entry in substitute_ops:
if inner.startswith(sub_entry[0]):
expected_value = sub_entry[1](inner)
break

for func in compare_ops:
signature = func[0]
op = func[1]
if str(expected_value).startswith(signature):
valid = op(expected_value[len(signature) :], actual_value)
valid = op(expected_value[len(signature):], actual_value)
break

if not valid:
Expand Down
52 changes: 26 additions & 26 deletions test/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -11623,7 +11623,7 @@
}""",
"""{
"type": "transfer_intent",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"path_ids": \""""
+ FILES["testfile-big"].id
+ """\",
Expand All @@ -11642,7 +11642,7 @@
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": 10240,
"path_id": \""""
Expand Down Expand Up @@ -11703,13 +11703,13 @@
}""",
"""{
"type": "transfer_intent_received",
"transfer_id": "*"
"transfer_id": "?/transfer_id:0/"
}""",
"""{
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": 10240,
"path_id": \""""
Expand Down Expand Up @@ -11780,7 +11780,7 @@
}""",
"""{
"type": "transfer_intent",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"path_ids": \"[~]"""
+ FILES["tiny-jpeg.jpg"].id
+ ","
Expand All @@ -11801,7 +11801,7 @@
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": 0,
"path_id": "*",
Expand All @@ -11811,7 +11811,7 @@
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": 0,
"path_id": "*",
Expand Down Expand Up @@ -11890,13 +11890,13 @@
}""",
"""{
"type": "transfer_intent_received",
"transfer_id": "*"
"transfer_id": "?/transfer_id:0/"
}""",
"""{
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": 0,
"path_id": "*",
Expand All @@ -11906,7 +11906,7 @@
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": 0,
"path_id": "*",
Expand Down Expand Up @@ -11965,7 +11965,7 @@
}""",
"""{
"type": "transfer_intent",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"path_ids": \""""
+ FILES["testfile-big"].id
+ """\",
Expand All @@ -11984,7 +11984,7 @@
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": 10240,
"path_id": \""""
Expand Down Expand Up @@ -12046,13 +12046,13 @@
}""",
"""{
"type": "transfer_intent_received",
"transfer_id": "*"
"transfer_id": "?/transfer_id:0/"
}""",
"""{
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">=0",
"transferred": 10240,
"path_id": \""""
Expand Down Expand Up @@ -12113,7 +12113,7 @@
}""",
"""{
"type": "transfer_intent",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"path_ids": \""""
+ FILES["testfile-big"].id
+ """\",
Expand All @@ -12132,7 +12132,7 @@
"type": "file",
"result": 0,
"phase": "paused",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": ">=0",
"path_id": \""""
Expand All @@ -12149,7 +12149,7 @@
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": 10240,
"path_id": \""""
Expand Down Expand Up @@ -12226,13 +12226,13 @@
}""",
"""{
"type": "transfer_intent_received",
"transfer_id": "*"
"transfer_id": "?/transfer_id:0/"
}""",
"""{
"type": "file",
"result": 0,
"phase": "paused",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">=0",
"transferred": ">=0",
"path_id": \""""
Expand All @@ -12251,7 +12251,7 @@
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">=0",
"transferred": 10240,
"path_id": \""""
Expand Down Expand Up @@ -12313,7 +12313,7 @@
}""",
"""{
"type": "transfer_intent",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"path_ids": \""""
+ FILES["testfile-big"].id
+ """\",
Expand All @@ -12332,7 +12332,7 @@
"type": "file",
"result": 28,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": ">=0",
"path_id": \""""
Expand Down Expand Up @@ -12389,13 +12389,13 @@
}""",
"""{
"type": "transfer_intent_received",
"transfer_id": "*"
"transfer_id": "?/transfer_id:0/"
}""",
"""{
"type": "file",
"result": 8,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">=0",
"transferred": ">=0",
"path_id": \""""
Expand Down Expand Up @@ -12542,7 +12542,7 @@
}""",
"""{
"type": "transfer_intent",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"path_ids": \""""
+ FILES["testfile-small"].id
+ """\",
Expand All @@ -12561,7 +12561,7 @@
"type": "file",
"result": 0,
"phase": "finished",
"transfer_id": "*",
"transfer_id": "?/transfer_id:0/",
"transfer_time": ">0",
"transferred": ">=0",
"path_id": \""""
Expand Down

0 comments on commit 45c578a

Please sign in to comment.