Skip to content
Snippets Groups Projects
Commit 521c85d8 authored by Jean-Romain Garnier's avatar Jean-Romain Garnier
Browse files

Implement test_macho_decompiler.py

parent a23c973f
No related branches found
No related tags found
1 merge request!82Rework macho comparator
......@@ -57,7 +57,7 @@ def skip_unless_radare2_command_exists(command):
def exclude_commands(monkeypatch, patterns):
excluded = list(Config().exclude_commands)
excluded += patterns
monkeypatch.setattr(Config(), "exclude_commands", patterns)
monkeypatch.setattr(Config(), "exclude_commands", excluded)
@pytest.fixture(scope="function", autouse=True)
......@@ -89,7 +89,7 @@ def test_obj_compare_non_existing(monkeypatch, obj1):
@skip_unless_module_exists("r2pipe")
@skip_unless_radare2_command_exists("pdgj")
def test_ghidra_diff(monkeypatch, obj1, obj2):
exclude_commands(monkeypatch, ["disass.*"])
exclude_commands(monkeypatch, ["^radare2 disass.*"])
obj_differences = obj1.compare(obj2).details[0].details
assert len(obj_differences) == 1
assert_diff(obj_differences[0], "elf_obj_ghidra_expected_diff")
......@@ -98,7 +98,7 @@ def test_ghidra_diff(monkeypatch, obj1, obj2):
@skip_unless_tools_exist("radare2")
@skip_unless_module_exists("r2pipe")
def test_radare2_diff(monkeypatch, obj1, obj2):
exclude_commands(monkeypatch, ["r2ghidra.*"])
exclude_commands(monkeypatch, ["^radare2 r2ghidra.*"])
obj_differences = obj1.compare(obj2).details[0].details
assert len(obj_differences) == 1
assert_diff(obj_differences[0], "elf_obj_radare2_expected_diff")
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2021 Jean-Romain Garnier <salsa@jean-romain.com>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffoscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
import pytest
from diffoscope.config import Config
from diffoscope.comparators.missing_file import MissingFile
from ..utils.data import load_fixture, assert_diff
from ..utils.tools import (
skipif,
tools_missing,
skip_unless_tools_exist,
skip_unless_module_exists,
)
def radare2_command_is_undefined(x):
if tools_missing("radare2"):
return True
try:
# Open any file with radare2 and try to execute the given command
# If it returns None, then the command doesn't exist
import r2pipe
r2 = r2pipe.open("/dev/null", flags=["-2"])
return r2.cmdj(x) is None
except ImportError:
return True
def skip_unless_radare2_command_exists(command):
return skipif(
radare2_command_is_undefined(command),
reason=f"radare2 didn't recognize {command} command",
tools=(f"{command}_radare2_command",),
)
def exclude_commands(monkeypatch, patterns):
excluded = list(Config().exclude_commands)
excluded += patterns
monkeypatch.setattr(Config(), "exclude_commands", excluded)
@pytest.fixture(scope="function", autouse=True)
def init_tests(request, monkeypatch):
# Ignore Mach-O tools that are already tested in test_macho.py
exclude_commands(monkeypatch, ["^llvm-readobj.*", "^llvm-objdump.*", "^lipo.*", "^otool.*", "^strings.*"])
obj1 = load_fixture("test1.macho")
obj2 = load_fixture("test2.macho")
@pytest.fixture
def obj_differences(obj1, obj2):
return obj1.compare(obj2).details
@skip_unless_tools_exist("radare2")
@skip_unless_module_exists("r2pipe")
@skip_unless_radare2_command_exists("pdgj")
def test_obj_compare_non_existing(monkeypatch, obj1):
monkeypatch.setattr(Config(), "new_file", True)
difference = obj1.compare(MissingFile("/nonexisting", obj1))
assert difference.source2 == "/nonexisting"
assert len(difference.details) > 0
@skip_unless_tools_exist("radare2")
@skip_unless_module_exists("r2pipe")
@skip_unless_radare2_command_exists("pdgj")
def test_ghidra_diff(monkeypatch, obj1, obj2):
exclude_commands(monkeypatch, ["^radare2 disass.*"])
obj_differences = obj1.compare(obj2).details[0].details
assert len(obj_differences) == 1
filenames = [
"macho_obj_ghidra_expected_diff_main",
]
for idx, diff in enumerate(obj_differences):
assert_diff(diff.details[0], filenames[idx])
@skip_unless_tools_exist("radare2")
@skip_unless_module_exists("r2pipe")
def test_radare2_diff(monkeypatch, obj1, obj2):
exclude_commands(monkeypatch, ["^radare2 r2ghidra.*"])
obj_differences = obj1.compare(obj2).details[0].details
assert len(obj_differences) == 2
filenames = [
"macho_obj_radare2_expected_diff_main",
"macho_obj_radare2_expected_diff_printf",
]
for idx, diff in enumerate(obj_differences):
assert_diff(diff.details[0], filenames[idx])
@@ -2,11 +2,11 @@
// WARNING: Variable defined which should be unmapped: var_4h
undefined8 main(void)
{
int64_t var_4h;
- // [00] -r-x section size 49 named 0.__TEXT.__text
- sym.imp.printf("%s %s\n", "17:31:50", "Wed Dec 2 17:31:49 2015");
+ // [00] -r-x section size 42 named 0.__TEXT.__text
+ sym.imp.printf(0x100000f8a, "15:52:34");
return 0;
}
@@ -1,23 +1,21 @@
-// This function contains 1 basic blocks and its 49 long.
+// This function contains 1 basic blocks and its 42 long.
function main () {
- loc_0x100000f20:
- push rbp //[00] -r-x section size 49 named 0.__TEXT.__text
+ loc_0x100000f40:
+ push rbp //[00] -r-x section size 42 named 0.__TEXT.__text
rbp = rsp
rsp -= 0x10
- rdi = rip + 0x43 //section.3.__TEXT.__cstring
- //0x100000f72 ; "%s %s\n"
- rsi = rip + 0x43 //str.17:31:50
- //0x100000f79 ; "17:31:50"
- rdx = rip + 0x45 //str.Wed_Dec__2_17:31:49_2015
- //0x100000f82 ; "Wed Dec 2 17:31:49 2015"
+ rdi = rip + 0x3b //section.3.__TEXT.__cstring
+ //0x100000f8a ; "%s\n"
+ rsi = rip + 0x38 //str.15:52:34
+ //0x100000f8e ; "15:52:34"
al = 0
- 0x100000f3f
- int printf("%s %s\n")
+ 0x100000f58
+ int printf("%s\n")
ecx = 0
dword [var_4h] = eax
eax = ecx
rsp += 0x10
//rsp ; rsp
return
(break)
@@ -1,8 +1,8 @@
// This function contains 1 basic blocks and its 6 long.
function sym.imp.printf () {
- loc_0x100000f52:
- //CALL XREF from main @ 0x100000f3f
- goto qword [reloc.printf] //[0x100001010:8]=0x100000f68 "h" ; "h\x0f" ; [01] -r-x section size 6 named 1.__TEXT.__stubs
+ loc_0x100000f6a:
+ //CALL XREF from main @ 0x100000f58
+ goto qword [reloc.printf] //[0x100001010:8]=0x100000f80 "h" ; [01] -r-x section size 6 named 1.__TEXT.__stubs
(break)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment