File: test_elfutils.py

package info (click to toggle)
python-auditwheel 6.6.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 980 kB
  • sloc: python: 6,165; ansic: 304; cpp: 66; sh: 28; makefile: 25; f90: 12
file content (259 lines) | stat: -rw-r--r-- 7,071 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from __future__ import annotations

from typing import Any
from unittest.mock import Mock, patch

import pytest
from elftools.common.exceptions import ELFError

from auditwheel.elfutils import (
    elf_file_filter,
    elf_find_ucs2_symbols,
    elf_find_versioned_symbols,
    elf_read_dt_needed,
    elf_read_rpaths,
    elf_references_pyfpe_jbuf,
    get_undefined_symbols,
)


class MockSymbol(dict[str, Any]):
    """Mock representing a Symbol in ELFTools."""

    def __init__(self, name: str, **kwargs):  # type: ignore[no-untyped-def]
        super().__init__(**kwargs)
        self._name = name

    @property
    def name(self):
        return self._name


@patch("auditwheel.elfutils.ELFFile")
class TestElfReadDt:
    def test_missing_section(self, elffile_mock, tmp_path):
        fake = tmp_path / "fake.so"

        # GIVEN
        fake.touch()
        elffile_mock.return_value.get_section_by_name.return_value = None

        # THEN
        with pytest.raises(ValueError, match=r"^Could not find soname.*"):
            # WHEN
            elf_read_dt_needed(fake)

    def test_needed_libs(self, elffile_mock, tmp_path):
        fake = tmp_path / "fake.so"

        # GIVEN
        fake.touch()
        section_mock = Mock()
        tag1 = Mock(needed="libz.so")
        tag1.entry.d_tag = "DT_NEEDED"
        tag2 = Mock(needed="libfoo.so")
        tag2.entry.d_tag = "DT_NEEDED"
        section_mock.iter_tags.return_value = [tag1, tag2]
        elffile_mock.return_value.get_section_by_name.return_value = section_mock

        # WHEN
        needed = elf_read_dt_needed(fake)

        # THEN
        assert len(needed) == 2
        assert "libz.so" in needed
        assert "libfoo.so" in needed


@patch("auditwheel.elfutils.ELFFile")
class TestElfFileFilter:
    def test_filter(self, elffile_mock, tmp_path):  # noqa: ARG002
        file1 = tmp_path / "file1.so"
        file2 = tmp_path / "file2.so"
        file1.touch()
        file2.touch()
        result = elf_file_filter([file1, file2])
        assert len(list(result)) == 2

    def test_some_py_files(self, elffile_mock, tmp_path):  # noqa: ARG002
        file1 = tmp_path / "file1.py"
        file2 = tmp_path / "file2.so"
        file3 = tmp_path / "file3.py"
        file1.touch()
        file2.touch()
        file3.touch()
        result = elf_file_filter([file1, file2, file3])
        assert len(list(result)) == 1

    def test_not_elf(self, elffile_mock, tmp_path):
        file1 = tmp_path / "file1.notelf"
        file2 = tmp_path / "file2.notelf"

        # GIVEN
        file1.touch()
        file2.touch()
        elffile_mock.side_effect = ELFError

        # WHEN
        result = elf_file_filter([file1, file2])

        # THEN
        assert len(list(result)) == 0


class TestElfFindVersionedSymbols:
    def test_find_symbols(self):
        # GIVEN
        elf = Mock()
        verneed = Mock()
        verneed.configure_mock(name="foo-lib")
        veraux = Mock()
        veraux.configure_mock(name="foo-lib")
        elf.get_section_by_name.return_value.iter_versions.return_value = ((verneed, [veraux]),)

        # WHEN
        symbols = list(elf_find_versioned_symbols(elf))

        # THEN
        assert symbols == [("foo-lib", "foo-lib")]

    @pytest.mark.parametrize("ld_name", ["ld-linux", "ld64.so.2", "ld64.so.1"])
    def test_only_ld_linux(self, ld_name):
        # GIVEN
        elf = Mock()
        verneed = Mock()
        verneed.configure_mock(name=ld_name)
        veraux = Mock()
        veraux.configure_mock(name="foo-lib")
        elf.get_section_by_name.return_value.iter_versions.return_value = ((verneed, [veraux]),)

        # WHEN
        symbols = list(elf_find_versioned_symbols(elf))

        # THEN
        assert len(symbols) == 0

    def test_empty_section(self):
        # GIVEN
        elf = Mock()
        elf.get_section_by_name.return_value = None

        # WHEN
        symbols = list(elf_find_versioned_symbols(elf))

        # THEN
        assert len(symbols) == 0


class TestFindUcs2Symbols:
    def test_missing_dynsym_section(self):
        # GIVEN
        elf = Mock()
        elf.get_section_by_name.return_value = None

        # THEN
        result = list(elf_find_ucs2_symbols(elf))
        assert result == []

    def test_elf_find_ucs2_symbols(self):
        # GIVEN
        elf = Mock()

        asunicode = MockSymbol(
            "PyUnicodeUCS2_AsUnicode",
            st_shndx="SHN_UNDEF",
            st_info={"type": "STT_FUNC"},
        )
        elf_symbols = (asunicode, Mock())
        elf_symbols[1].name = "foobar"
        elf.get_section_by_name.return_value.iter_symbols.return_value = elf_symbols

        # WHEN
        symbols = list(elf_find_ucs2_symbols(elf))

        # THEN
        assert len(symbols) == 1
        assert symbols[0] == "PyUnicodeUCS2_AsUnicode"

    def test_elf_find_ucs2_symbols_no_symbol(self):
        # GIVEN
        elf = Mock()

        elf_symbols = (MockSymbol("FooSymbol"),)
        elf.get_section_by_name.return_value.iter_symbols.return_value = elf_symbols

        # WHEN/THEN
        symbols = list(elf_find_ucs2_symbols(elf))
        assert len(symbols) == 0


class TestElfReferencesPyPFE:
    def test_elf_references_pyfpe_jbuf(self):
        # GIVEN
        elf = Mock()
        symbols = (
            MockSymbol(
                "PyFPE_jbuf",
                st_shndx="SHN_UNDEF",
                st_info={"type": "STT_FUNC"},
            ),
        )

        elf.get_section_by_name.return_value.iter_symbols.return_value = symbols

        # WHEN/THEN
        assert elf_references_pyfpe_jbuf(elf) is True

    def test_elf_references_pyfpe_jbuf_false(self):
        # GIVEN
        elf = Mock()
        symbols = (
            MockSymbol(
                "SomeSymbol",
                st_shndx="SHN_UNDEF",
                st_info={"type": "STT_FUNC"},
            ),
        )

        elf.get_section_by_name.return_value.iter_symbols.return_value = symbols

        # WHEN/THEN
        assert elf_references_pyfpe_jbuf(elf) is False

    def test_elf_references_pyfpe_jbuf_no_section(self):
        # GIVEN
        elf = Mock()

        # WHEN
        elf.get_section_by_name.return_value = None

        # WHEN/THEN
        assert elf_references_pyfpe_jbuf(elf) is False


@patch("auditwheel.elfutils.ELFFile")
class TestElfReadRpaths:
    def test_missing_dynamic_section(self, elffile_mock, tmp_path):
        fake = tmp_path / "fake.so"

        # GIVEN
        fake.touch()
        elffile_mock.return_value.get_section_by_name.return_value = None

        # THEN
        result = elf_read_rpaths(fake)
        assert result == {"rpaths": [], "runpaths": []}


@patch("auditwheel.elfutils.ELFFile")
class TestGetUndefinedSymbols:
    def test_missing_dynsym_section(self, elffile_mock, tmp_path):
        fake = tmp_path / "fake.so"

        # GIVEN
        fake.touch()
        elffile_mock.return_value.get_section_by_name.return_value = None

        # THEN
        result = get_undefined_symbols(fake)
        assert result == set()