File: test_macho.py

package info (click to toggle)
diffoscope 297%2Bdeb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie-proposed-updates
  • size: 11,304 kB
  • sloc: python: 17,173; sh: 114; makefile: 97; xml: 36; javascript: 2
file content (140 lines) | stat: -rw-r--r-- 4,977 bytes parent folder | download | duplicates (4)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2015 Jérémy Bobbio <lunar@debian.org>
# Copyright © 2015 Clemens Lang <cal@macports.org>
# Copyright © 2016-2021 Chris Lamb <lamby@debian.org>
# 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.macho import MachoFile
from diffoscope.comparators.missing_file import MissingFile

from ..utils.data import load_fixture, assert_diff
from ..utils.tools import skip_unless_tools_exist
from .test_rlib import llvm_version


obj1 = load_fixture("test1.macho")
obj2 = load_fixture("test2.macho")


@pytest.fixture(scope="function", autouse=True)
def init_tests(request, monkeypatch):
    # Ignore radare2 commands so decompiling is skipped
    # See test_macho_decompiler.py for tests related to decompiler
    monkeypatch.setattr(Config(), "exclude_commands", ["^radare2.*"])


def test_obj_identification(obj1):
    assert isinstance(obj1, MachoFile)


@skip_unless_tools_exist("llvm-readobj", "llvm-objdump")
def test_obj_no_differences(obj1):
    difference = obj1.compare(obj1)
    assert difference is None


@pytest.fixture
def obj_differences(obj1, obj2):
    return obj1.compare(obj2).details


@skip_unless_tools_exist("otool", "lipo")
def test_otool_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("otool", "lipo")
def test_otool_diff(obj_differences):
    assert len(obj_differences) == 2
    assert_diff(obj_differences[0], "macho_otool_expected_diff_strings")

    arch_differences = obj_differences[-1].details
    assert len(arch_differences) == 6

    filenames = [
        "macho_otool_expected_diff_headers",
        "macho_otool_expected_diff_libraries",
        "macho_otool_expected_diff_indirect_symbols",
        "macho_otool_expected_diff__text",
        "macho_otool_expected_diff__cstring",
        "macho_otool_expected_diff__data",
    ]
    for idx, diff in enumerate(arch_differences):
        assert_diff(diff, filenames[idx])


@skip_unless_tools_exist("llvm-readobj", "llvm-objdump")
def test_llvm_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("llvm-readobj", "llvm-objdump")
def test_llvm_diff(obj_differences):
    if llvm_version() < "13":
        diff_symbols = "macho_llvm_expected_diff_symbols_llvm_11"
    else:
        diff_symbols = "macho_llvm_expected_diff_symbols"

    # Headers
    assert len(obj_differences) == 8
    filenames = [
        "macho_llvm_expected_diff_strings",
        "macho_llvm_expected_diff_file_headers",
        "macho_llvm_expected_diff_needed_libs",
        diff_symbols,
        "macho_llvm_expected_diff_dyn_symbols",
        "macho_llvm_expected_diff_relocations",
        "macho_llvm_expected_diff_dyn_relocations",
    ]
    for idx, diff in enumerate(obj_differences[:-1]):
        assert_diff(diff, filenames[idx])

    # Sections
    arch_differences = obj_differences[-1].details
    assert len(arch_differences) == 7
    if llvm_version() < "16":
        filenames = [
            "macho_llvm_expected_diff__text",
            "macho_llvm_expected_diff__stubs",
            "macho_llvm_expected_diff__stub_helper",
            "macho_llvm_expected_diff__cstring",
            "macho_llvm_expected_diff__unwind_info",
            "macho_llvm_expected_diff__eh_frame",
            "macho_llvm_expected_diff__la_symbol_ptr",
        ]
    else:
        filenames = [
            "macho_llvm_expected_diff__text_16",
            "macho_llvm_expected_diff__stubs_16",
            "macho_llvm_expected_diff__stub_helper_16",
            "macho_llvm_expected_diff__cstring",
            "macho_llvm_expected_diff__unwind_info",
            "macho_llvm_expected_diff__eh_frame",
            "macho_llvm_expected_diff__la_symbol_ptr",
        ]
    for idx, diff in enumerate(arch_differences):
        assert_diff(diff, filenames[idx])