Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the text parser used for tests. #104

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tests/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def run(self, elem: dict, d_path: str) -> type(None):
ldiff = list()
diffobj = diff.Diff()
for it in range(0, len(outresult)):
ldiff.append(diffobj.diffobj(outresult[it], outtest[it]))
td = diffobj.diffobj(outresult[it], outtest[it])
ldiff.append(td[:])
td = diffobj.diffobj(outtest[it], outresult[it])
ldiff.append(td[:])

result = result.replace("\r", "")
subprocess = subresult.replace("\r", "")
Expand Down
13 changes: 7 additions & 6 deletions tests/core/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ def diff(self, dict1: dict, dict2: dict) -> type(None):
]
self._diff.append(result)
else:
result = [" - KEY %s : " % (key), " + KEY %s : " % (key)]
result = [" - KEY %s : %s" % (key, dict1[key]), " + KEY %s : " % (key)]
self._diff.append(result)

def diffobj(self, actual: object, expected: object) -> list:
"""
Each object contains a list of dictionaries. For each dictionary, the function checks if they are is any difference.
"""
self._diff.clear()
if len(actual) != len(expected):
self._diff.append("invalid dimensions")
return None

for it in range(0, len(actual)):
self.diff(actual[it]._data, expected[it]._data)
result = [" - ACTUAL DIM = %d" % (len(actual)), "+ EXPECTED DIM = %d" % (len(expected))]
self._diff.append(result)
else:
for it in range(0, len(actual)):
self.diff(actual[it]._data, expected[it]._data)

if self._diff:
for elem in self._diff:
Expand Down
55 changes: 41 additions & 14 deletions tests/core/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,58 @@ def process(self) -> type(None):
"""
Parses the emulation result generated by disasmtool and stores it in a dictionary as key:value pair as follows:
{
"AX": "0x0000000000000000",
"CX": "0x0000000000000000",
"DX": "0x0000000000000000",
"BX": "0x0000000000000000",
"BP": "0x0000000000000000",
"SI": "0x0000000000000000",
"RAX": "0x0000000000000000",
"RCX": "0x0000000000000000",
"RDX": "0x0000000000000000",
"RBX": "0x0000000000000000",
"RBP": "0x0000000000000000",
"RSI": "0x0000000000000000",
...
"28": "0x0000000000000000",
"29": "0x0000000000000000",
"30": "0x0000000000000000",
"31": "0x0000000000000000",
"IP": "0x0000000000200000",
"GS": "0x0000000000000202"
"R28": "0x0000000000000000",
"R29": "0x0000000000000000",
"R30": "0x0000000000000000",
"R31": "0x0000000000000000",
"RIP": "0x0000000000200000",
"RFLAGS": "0x0000000000000202"
}
"""
self._obj = self._obj.split("\n")
line = self.rdline()
cnt = 0
while line:
# X0 = 0x0000000000000000 X1 = 0x0000000000000000 X2 = 0x0000000000000000 X3 = 0x0000000000000000
if " = " in line:
tokens = re.findall(r"\w\w\s*=\s*0x\d{16}", line)
tokens = re.findall(r"\w+\s*=\s*[0x]*[\da-f]{4,16}", line)
for token in tokens:
key = token.lstrip().rstrip().split("=")[0].lstrip().rstrip()
val = token.lstrip().rstrip().split("=")[1].lstrip().rstrip()
self._data[key] = val
if "IP: " in line or "PC: " in line:
tokens = re.findall(r"\w\w\s*:\s*0x[\da-f]{16}", line)
key = tokens[0].lstrip().rstrip().split(":")[0].lstrip().rstrip()
val = tokens[0].lstrip().rstrip().split(":")[1].lstrip().rstrip()
self._data["%s-%s" % (key, "INFO")] = val

tokens = line.split(' ')
key = "InstructionBytes"
val = tokens[1].lstrip().rstrip()
self._data[key] = val

key = "InstructionText"
val = " ".join(tokens[2:]).lstrip().rstrip()
self._data[key] = val
elif "Detection: " in line:
tokens = re.findall(r"\w+\s*:\s*0x\d{16}", line)
key = tokens[0].lstrip().rstrip().split(":")[0].lstrip().rstrip()
val = tokens[0].lstrip().rstrip().split(":")[1].lstrip().rstrip()
self._data["%s-%d" % (key, cnt)] = val
cnt += 1
elif ":" in line:
tokens = re.findall(r"\w\w\s*:\s*\d{1}", line)
for token in tokens:
key = token.lstrip().rstrip().split(":")[0].lstrip().rstrip()
val = token.lstrip().rstrip().split(":")[1].lstrip().rstrip()
self._data[key] = val

line = self.rdline()


Expand Down
2 changes: 1 addition & 1 deletion tests/core/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def rd(self, data) -> type(None):
for line in data.split("\n"):
if line.startswith("Emulation ") or "SHEMU" in line:
continue
if re.search("^PC:", line) or re.search("^IP:", line):
if re.search(r"^\s*\wAX =", line) or re.search("^\s*X0\s*= ", line):
if local:
local = local.replace("\r", "")
ostr.append(local)
Expand Down
Loading