1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#
# 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])
|