From 3e356513cd2a1af2f675384c5468b38cd61e1f31 Mon Sep 17 00:00:00 2001 From: Andrei KISARI Date: Tue, 17 Sep 2024 17:51:06 +0300 Subject: [PATCH] Fix the text parser used for tests. --- tests/core/core.py | 5 +++- tests/core/diff.py | 13 +++++----- tests/core/objects.py | 55 ++++++++++++++++++++++++++++++++----------- tests/core/output.py | 2 +- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/tests/core/core.py b/tests/core/core.py index 684decf..53b9831 100644 --- a/tests/core/core.py +++ b/tests/core/core.py @@ -150,7 +150,10 @@ class Core(object): 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", "") diff --git a/tests/core/diff.py b/tests/core/diff.py index 4e349b5..6fe7b65 100644 --- a/tests/core/diff.py +++ b/tests/core/diff.py @@ -25,7 +25,8 @@ class Diff(object): ] 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: """ @@ -33,11 +34,11 @@ class Diff(object): """ 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: diff --git a/tests/core/objects.py b/tests/core/objects.py index 901687d..cddd07d 100644 --- a/tests/core/objects.py +++ b/tests/core/objects.py @@ -105,31 +105,58 @@ class ShemuInstrux(DecodeShemuParser): """ 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() diff --git a/tests/core/output.py b/tests/core/output.py index ddfffd8..658810f 100644 --- a/tests/core/output.py +++ b/tests/core/output.py @@ -100,7 +100,7 @@ class OutputShemu(OutputParser): 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)