Debian Codegraph @

tests/unit/test_elfutils.py

raw
1 from __future__ import annotations
2
3 from typing import Any
4 from unittest.mock import Mock, patch
5
6 import pytest
7 from elftools.common.exceptions import ELFError
8
9 from auditwheel.elfutils import (
10 elf_file_filter,
11 elf_find_ucs2_symbols,
12 elf_find_versioned_symbols,
13 elf_read_dt_needed,
14 elf_references_pyfpe_jbuf,
15 get_undefined_symbols,
16 )
17
18
19 class MockSymbol(dict[str, Any]):
20 """Mock representing a Symbol in ELFTools."""
21
22 def __init__(self, name: str, **kwargs): # type: ignore[no-untyped-def]
23 super().__init__(**kwargs)
24 self._name = name
25
26 @property
27 def name(self):
28 return self._name
29
30
31 @patch("auditwheel.elfutils.ELFFile")
32 class TestElfReadDt:
33 def test_missing_section(self, elffile_mock, tmp_path):
34 fake = tmp_path / "fake.so"
35
36 # GIVEN
37 fake.touch()
38 elffile_mock.return_value.get_section_by_name.return_value = None
39
40 # THEN
41 with pytest.raises(ValueError, match=r"^Could not find soname.*"):
42 # WHEN
43 elf_read_dt_needed(fake)
44
45 def test_needed_libs(self, elffile_mock, tmp_path):
46 fake = tmp_path / "fake.so"
47
48 # GIVEN
49 fake.touch()
50 section_mock = Mock()
51 tag1 = Mock(needed="libz.so")
52 tag1.entry.d_tag = "DT_NEEDED"
53 tag2 = Mock(needed="libfoo.so")
54 tag2.entry.d_tag = "DT_NEEDED"
55 section_mock.iter_tags.return_value = [tag1, tag2]
56 elffile_mock.return_value.get_section_by_name.return_value = section_mock
57
58 # WHEN
59 needed = elf_read_dt_needed(fake)
60
61 # THEN
62 assert len(needed) == 2
63 assert "libz.so" in needed
64 assert "libfoo.so" in needed
65
66
67 @patch("auditwheel.elfutils.ELFFile")
68 class TestElfFileFilter:
69 def test_filter(self, elffile_mock, tmp_path): # noqa: ARG002
70 file1 = tmp_path / "file1.so"
71 file2 = tmp_path / "file2.so"
72 file1.touch()
73 file2.touch()
74 result = elf_file_filter([file1, file2])
75 assert len(list(result)) == 2
76
77 def test_some_py_files(self, elffile_mock, tmp_path): # noqa: ARG002
78 file1 = tmp_path / "file1.py"
79 file2 = tmp_path / "file2.so"
80 file3 = tmp_path / "file3.py"
81 file1.touch()
82 file2.touch()
83 file3.touch()
84 result = elf_file_filter([file1, file2, file3])
85 assert len(list(result)) == 1
86
87 def test_not_elf(self, elffile_mock, tmp_path):
88 file1 = tmp_path / "file1.notelf"
89 file2 = tmp_path / "file2.notelf"
90
91 # GIVEN
92 file1.touch()
93 file2.touch()
94 elffile_mock.side_effect = ELFError
95
96 # WHEN
97 result = elf_file_filter([file1, file2])
98
99 # THEN
100 assert len(list(result)) == 0
101
102
103 class TestElfFindVersionedSymbols:
104 def test_find_symbols(self):
105 # GIVEN
106 elf = Mock()
107 verneed = Mock()
108 verneed.configure_mock(name="foo-lib")
109 veraux = Mock()
110 veraux.configure_mock(name="foo-lib")
111 elf.get_section_by_name.return_value.iter_versions.return_value = ((verneed, [veraux]),)
112
113 # WHEN
114 symbols = list(elf_find_versioned_symbols(elf))
115
116 # THEN
117 assert symbols == [("foo-lib", "foo-lib")]
118
119 @pytest.mark.parametrize("ld_name", ["ld-linux", "ld64.so.2", "ld64.so.1"])
120 def test_only_ld_linux(self, ld_name):
121 # GIVEN
122 elf = Mock()
123 verneed = Mock()
124 verneed.configure_mock(name=ld_name)
125 veraux = Mock()
126 veraux.configure_mock(name="foo-lib")
127 elf.get_section_by_name.return_value.iter_versions.return_value = ((verneed, [veraux]),)
128
129 # WHEN
130 symbols = list(elf_find_versioned_symbols(elf))
131
132 # THEN
133 assert len(symbols) == 0
134
135 def test_empty_section(self):
136 # GIVEN
137 elf = Mock()
138 elf.get_section_by_name.return_value = None
139
140 # WHEN
141 symbols = list(elf_find_versioned_symbols(elf))
142
143 # THEN
144 assert len(symbols) == 0
145
146
147 class TestFindUcs2Symbols:
148 def test_missing_dynsym_section(self):
149 # GIVEN
150 elf = Mock()
151 elf.get_section_by_name.return_value = None
152
153 # THEN
154 result = list(elf_find_ucs2_symbols(elf))
155 assert result == []
156
157 def test_elf_find_ucs2_symbols(self):
158 # GIVEN
159 elf = Mock()
160
161 asunicode = MockSymbol(
162 "PyUnicodeUCS2_AsUnicode",
163 st_shndx="SHN_UNDEF",
164 st_info={"type": "STT_FUNC"},
165 )
166 elf_symbols = (asunicode, Mock())
167 elf_symbols[1].name = "foobar"
168 elf.get_section_by_name.return_value.iter_symbols.return_value = elf_symbols
169
170 # WHEN
171 symbols = list(elf_find_ucs2_symbols(elf))
172
173 # THEN
174 assert len(symbols) == 1
175 assert symbols[0] == "PyUnicodeUCS2_AsUnicode"
176
177 def test_elf_find_ucs2_symbols_no_symbol(self):
178 # GIVEN
179 elf = Mock()
180
181 elf_symbols = (MockSymbol("FooSymbol"),)
182 elf.get_section_by_name.return_value.iter_symbols.return_value = elf_symbols
183
184 # WHEN/THEN
185 symbols = list(elf_find_ucs2_symbols(elf))
186 assert len(symbols) == 0
187
188
189 class TestElfReferencesPyPFE:
190 def test_elf_references_pyfpe_jbuf(self):
191 # GIVEN
192 elf = Mock()
193 symbols = (
194 MockSymbol(
195 "PyFPE_jbuf",
196 st_shndx="SHN_UNDEF",
197 st_info={"type": "STT_FUNC"},
198 ),
199 )
200
201 elf.get_section_by_name.return_value.iter_symbols.return_value = symbols
202
203 # WHEN/THEN
204 assert elf_references_pyfpe_jbuf(elf) is True
205
206 def test_elf_references_pyfpe_jbuf_false(self):
207 # GIVEN
208 elf = Mock()
209 symbols = (
210 MockSymbol(
211 "SomeSymbol",
212 st_shndx="SHN_UNDEF",
213 st_info={"type": "STT_FUNC"},
214 ),
215 )
216
217 elf.get_section_by_name.return_value.iter_symbols.return_value = symbols
218
219 # WHEN/THEN
220 assert elf_references_pyfpe_jbuf(elf) is False
221
222 def test_elf_references_pyfpe_jbuf_no_section(self):
223 # GIVEN
224 elf = Mock()
225
226 # WHEN
227 elf.get_section_by_name.return_value = None
228
229 # WHEN/THEN
230 assert elf_references_pyfpe_jbuf(elf) is False
231
232
233 @patch("auditwheel.elfutils.ELFFile")
234 class TestGetUndefinedSymbols:
235 def test_missing_dynsym_section(self, elffile_mock, tmp_path):
236 fake = tmp_path / "fake.so"
237
238 # GIVEN
239 fake.touch()
240 elffile_mock.return_value.get_section_by_name.return_value = None
241
242 # THEN
243 result = get_undefined_symbols(fake)
244 assert result == set()