| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 4 |
import re |
| 5 |
from contextlib import nullcontext as does_not_raise |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
import auditwheel.policy |
| 11 |
from auditwheel.architecture import Architecture |
| 12 |
from auditwheel.lddtree import DynamicExecutable, DynamicLibrary, Platform |
| 13 |
from auditwheel.libc import Libc |
| 14 |
from auditwheel.policy import ( |
| 15 |
Policy, |
| 16 |
WheelPolicies, |
| 17 |
_validate_pep600_compliance, |
| 18 |
get_replace_platforms, |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
def ids(x): |
| 23 |
if isinstance(x, Libc): |
| 24 |
return x.name |
| 25 |
if isinstance(x, does_not_raise): |
| 26 |
return "NoError" |
| 27 |
if hasattr(x, "expected_exception"): |
| 28 |
return x.expected_exception |
| 29 |
return None |
| 30 |
|
| 31 |
|
| 32 |
def raises(exception, match=None, escape=True): |
| 33 |
if escape and match is not None: |
| 34 |
match = re.escape(match) |
| 35 |
return pytest.raises(exception, match=match) |
| 36 |
|
| 37 |
|
| 38 |
@pytest.mark.parametrize( |
| 39 |
("name", "expected"), |
| 40 |
[ |
| 41 |
("linux_aarch64", []), |
| 42 |
("manylinux1_ppc64le", ["linux_ppc64le"]), |
| 43 |
("manylinux2014_x86_64", ["linux_x86_64"]), |
| 44 |
("manylinux_2_24_x86_64", ["linux_x86_64"]), |
| 45 |
], |
| 46 |
) |
| 47 |
def test_replacement_platform(name, expected): |
| 48 |
assert get_replace_platforms(name) == expected |
| 49 |
|
| 50 |
|
| 51 |
def test_pep600_compliance(): |
| 52 |
_validate_pep600_compliance( |
| 53 |
[ |
| 54 |
{ |
| 55 |
"name": "manylinux1", |
| 56 |
"priority": 100, |
| 57 |
"symbol_versions": { |
| 58 |
"i686": {"CXXABI": ["1.3"]}, |
| 59 |
}, |
| 60 |
"lib_whitelist": ["libgcc_s.so.1"], |
| 61 |
}, |
| 62 |
{ |
| 63 |
"name": "manylinux2010", |
| 64 |
"priority": 90, |
| 65 |
"symbol_versions": { |
| 66 |
"i686": {"CXXABI": ["1.3", "1.3.1"]}, |
| 67 |
}, |
| 68 |
"lib_whitelist": ["libgcc_s.so.1", "libstdc++.so.6"], |
| 69 |
}, |
| 70 |
], |
| 71 |
) |
| 72 |
|
| 73 |
_validate_pep600_compliance( |
| 74 |
[ |
| 75 |
{ |
| 76 |
"name": "manylinux1", |
| 77 |
"priority": 100, |
| 78 |
"symbol_versions": { |
| 79 |
"i686": {"CXXABI": ["1.3"]}, |
| 80 |
"x86_64": {"CXXABI": ["1.3"]}, |
| 81 |
}, |
| 82 |
"lib_whitelist": ["libgcc_s.so.1"], |
| 83 |
}, |
| 84 |
{ |
| 85 |
"name": "manylinux2010", |
| 86 |
"priority": 90, |
| 87 |
"symbol_versions": { |
| 88 |
"i686": {"CXXABI": ["1.3", "1.3.1"]}, |
| 89 |
}, |
| 90 |
"lib_whitelist": ["libgcc_s.so.1", "libstdc++.so.6"], |
| 91 |
}, |
| 92 |
], |
| 93 |
) |
| 94 |
|
| 95 |
with pytest.raises(ValueError, match=r"manylinux2010_i686.*CXXABI.*1.3.2"): |
| 96 |
_validate_pep600_compliance( |
| 97 |
[ |
| 98 |
{ |
| 99 |
"name": "manylinux1", |
| 100 |
"priority": 100, |
| 101 |
"symbol_versions": { |
| 102 |
"i686": {"CXXABI": ["1.3", "1.3.2"]}, |
| 103 |
}, |
| 104 |
"lib_whitelist": ["libgcc_s.so.1"], |
| 105 |
}, |
| 106 |
{ |
| 107 |
"name": "manylinux2010", |
| 108 |
"priority": 90, |
| 109 |
"symbol_versions": { |
| 110 |
"i686": {"CXXABI": ["1.3", "1.3.1"]}, |
| 111 |
}, |
| 112 |
"lib_whitelist": ["libgcc_s.so.1", "libstdc++.so.6"], |
| 113 |
}, |
| 114 |
], |
| 115 |
) |
| 116 |
|
| 117 |
with pytest.raises(ValueError, match=r"manylinux2010.*libstdc\+\+\.so\.6"): |
| 118 |
_validate_pep600_compliance( |
| 119 |
[ |
| 120 |
{ |
| 121 |
"name": "manylinux1", |
| 122 |
"priority": 100, |
| 123 |
"symbol_versions": { |
| 124 |
"i686": {"CXXABI": ["1.3"]}, |
| 125 |
}, |
| 126 |
"lib_whitelist": ["libgcc_s.so.1", "libstdc++.so.6"], |
| 127 |
}, |
| 128 |
{ |
| 129 |
"name": "manylinux2010", |
| 130 |
"priority": 90, |
| 131 |
"symbol_versions": { |
| 132 |
"i686": {"CXXABI": ["1.3", "1.3.1"]}, |
| 133 |
}, |
| 134 |
"lib_whitelist": ["libgcc_s.so.1"], |
| 135 |
}, |
| 136 |
], |
| 137 |
) |
| 138 |
|
| 139 |
|
| 140 |
class TestPolicyAccess: |
| 141 |
def test_get_by_name(self): |
| 142 |
arch = Architecture.detect() |
| 143 |
policies = WheelPolicies(libc=Libc.GLIBC, arch=arch) |
| 144 |
assert policies.get_policy_by_name(f"manylinux_2_41_{arch}").priority == 51 |
| 145 |
assert policies.get_policy_by_name(f"manylinux_2_36_{arch}").priority == 56 |
| 146 |
if arch == Architecture.loongarch64: |
| 147 |
return |
| 148 |
assert policies.get_policy_by_name(f"manylinux_2_31_{arch}").priority == 61 |
| 149 |
if arch == Architecture.riscv64: |
| 150 |
return |
| 151 |
assert policies.get_policy_by_name(f"manylinux_2_27_{arch}").priority == 65 |
| 152 |
assert policies.get_policy_by_name(f"manylinux_2_24_{arch}").priority == 70 |
| 153 |
assert policies.get_policy_by_name(f"manylinux2014_{arch}").priority == 80 |
| 154 |
assert policies.get_policy_by_name(f"manylinux_2_17_{arch}").priority == 80 |
| 155 |
if arch not in {Architecture.x86_64, Architecture.i686}: |
| 156 |
return |
| 157 |
assert policies.get_policy_by_name(f"manylinux2010_{arch}").priority == 90 |
| 158 |
assert policies.get_policy_by_name(f"manylinux_2_12_{arch}").priority == 90 |
| 159 |
assert policies.get_policy_by_name(f"manylinux1_{arch}").priority == 100 |
| 160 |
assert policies.get_policy_by_name(f"manylinux_2_5_{arch}").priority == 100 |
| 161 |
|
| 162 |
def test_get_by_name_missing(self): |
| 163 |
policies = WheelPolicies(libc=Libc.GLIBC, arch=Architecture.x86_64) |
| 164 |
with pytest.raises(LookupError): |
| 165 |
policies.get_policy_by_name("nosuchpolicy") |
| 166 |
|
| 167 |
def test_get_by_name_duplicate(self): |
| 168 |
policies = WheelPolicies(libc=Libc.GLIBC, arch=Architecture.x86_64) |
| 169 |
policy = Policy("duplicate", (), 0, {}, frozenset(), {}) |
| 170 |
policies._policies = [policy, policy] |
| 171 |
with pytest.raises(RuntimeError): |
| 172 |
policies.get_policy_by_name("duplicate") |
| 173 |
|
| 174 |
|
| 175 |
class TestLddTreeExternalReferences: |
| 176 |
"""Tests for lddtree_external_references.""" |
| 177 |
|
| 178 |
@pytest.mark.parametrize("libc", [Libc.GLIBC, Libc.ANDROID]) |
| 179 |
def test_filter_libs(self, libc): |
| 180 |
"""Test the nested filter_libs function.""" |
| 181 |
filtered_libs = [ |
| 182 |
"ld-linux-x86_64.so.1", |
| 183 |
"ld64.so.1", |
| 184 |
"ld64.so.2", |
| 185 |
] |
| 186 |
unfiltered_libs = ["libfoo.so.1.0", "libbar.so.999.999.999"] |
| 187 |
(filtered_libs if libc == Libc.ANDROID else unfiltered_libs).append("libpython3.13.so") |
| 188 |
libs = filtered_libs + unfiltered_libs |
| 189 |
|
| 190 |
lddtree = DynamicExecutable( |
| 191 |
interpreter=None, |
| 192 |
libc=libc, |
| 193 |
path="/path/to/lib", |
| 194 |
realpath=Path("/path/to/lib"), |
| 195 |
platform=Platform( |
| 196 |
"", |
| 197 |
64, |
| 198 |
True, |
| 199 |
"EM_X86_64", |
| 200 |
Architecture.x86_64, |
| 201 |
None, |
| 202 |
None, |
| 203 |
), |
| 204 |
needed=tuple(libs), |
| 205 |
libraries={ |
| 206 |
lib: DynamicLibrary(lib, f"/path/to/{lib}", Path(f"/path/to/{lib}")) for lib in libs |
| 207 |
}, |
| 208 |
rpath=(), |
| 209 |
runpath=(), |
| 210 |
) |
| 211 |
policies = WheelPolicies( |
| 212 |
libc=libc, |
| 213 |
arch=Architecture.x86_64, |
| 214 |
wheel_fn=( |
| 215 |
Path("spam-0.1-py3-none-android_24_x86_64.whl") if libc == Libc.ANDROID else None |
| 216 |
), |
| 217 |
) |
| 218 |
full_external_refs = policies.lddtree_external_references( |
| 219 |
lddtree, |
| 220 |
Path("/path/to/wheel"), |
| 221 |
) |
| 222 |
|
| 223 |
# Assert that each policy only has the unfiltered libs. |
| 224 |
for policy in full_external_refs: |
| 225 |
if policy.startswith("linux_"): |
| 226 |
assert set(full_external_refs[policy].libs) == set() |
| 227 |
else: |
| 228 |
assert set(full_external_refs[policy].libs) == set(unfiltered_libs) |
| 229 |
|
| 230 |
|
| 231 |
@pytest.mark.parametrize( |
| 232 |
("libc", "musl_policy", "arch", "exception"), |
| 233 |
[ |
| 234 |
# valid |
| 235 |
(Libc.detect(), None, Architecture.detect(), does_not_raise()), |
| 236 |
(Libc.GLIBC, None, Architecture.x86_64, does_not_raise()), |
| 237 |
(Libc.MUSL, "musllinux_1_1", Architecture.x86_64, does_not_raise()), |
| 238 |
(Libc.GLIBC, None, Architecture.aarch64, does_not_raise()), |
| 239 |
# invalid |
| 240 |
( |
| 241 |
Libc.GLIBC, |
| 242 |
"musllinux_1_1", |
| 243 |
Architecture.x86_64, |
| 244 |
raises(ValueError, "'musl_policy' shall be None"), |
| 245 |
), |
| 246 |
( |
| 247 |
Libc.MUSL, |
| 248 |
"manylinux_1_1", |
| 249 |
Architecture.x86_64, |
| 250 |
raises(ValueError, "Invalid 'musl_policy'"), |
| 251 |
), |
| 252 |
(Libc.MUSL, "musllinux_5_1", Architecture.x86_64, raises(AssertionError)), |
| 253 |
(Libc.MUSL, None, Architecture.x86_64, does_not_raise()), |
| 254 |
], |
| 255 |
ids=ids, |
| 256 |
) |
| 257 |
def test_wheel_policies_args(libc, musl_policy, arch, exception): |
| 258 |
with exception: |
| 259 |
policies = WheelPolicies(libc=libc, musl_policy=musl_policy, arch=arch) |
| 260 |
assert policies.libc == libc |
| 261 |
assert policies.architecture == arch |
| 262 |
if musl_policy is not None: |
| 263 |
assert policies._musl_policy == musl_policy |
| 264 |
elif libc == Libc.MUSL: |
| 265 |
assert policies._musl_policy == "musllinux_1_2" |
| 266 |
|
| 267 |
|
| 268 |
def test_policy_checks_glibc(): |
| 269 |
policies = WheelPolicies(libc=Libc.GLIBC, arch=Architecture.x86_64) |
| 270 |
|
| 271 |
policy = policies.versioned_symbols_policy({"some_library.so": {"GLIBC_2.17"}}) |
| 272 |
assert policy > policies.linux |
| 273 |
policy = policies.versioned_symbols_policy({"some_library.so": {"GLIBC_999"}}) |
| 274 |
assert policy == policies.linux |
| 275 |
policy = policies.versioned_symbols_policy({"some_library.so": {"OPENSSL_1_1_0"}}) |
| 276 |
assert policy == policies.highest |
| 277 |
policy = policies.versioned_symbols_policy({"some_library.so": {"IAMALIBRARY"}}) |
| 278 |
assert policy == policies.highest |
| 279 |
assert policies.linux < policies.lowest < policies.highest |
| 280 |
|
| 281 |
|
| 282 |
@pytest.mark.parametrize( |
| 283 |
("wheel_level", "policy_level"), |
| 284 |
[(21, 21), (22, 21), (23, 21), (24, 24), (25, 24), (26, 26), (27, 27), (28, 27)], |
| 285 |
) |
| 286 |
@pytest.mark.parametrize(("arch"), [Architecture.arm64_v8a, Architecture.x86_64]) |
| 287 |
def test_android(wheel_level, policy_level, arch): |
| 288 |
for policy_json in json.loads( |
| 289 |
(Path(auditwheel.policy.__file__).parent / "android-policy.json").read_text(), |
| 290 |
): |
| 291 |
if policy_json["name"] == f"android_{policy_level}": |
| 292 |
whitelist = frozenset(policy_json["lib_whitelist"]) |
| 293 |
break |
| 294 |
else: |
| 295 |
raise AssertionError(policy_level) |
| 296 |
|
| 297 |
assert "libc.so" in whitelist |
| 298 |
assert ("libcamera2ndk.so" in whitelist) == (policy_level >= 24) |
| 299 |
assert ("libaaudio.so" in whitelist) == (policy_level >= 26) |
| 300 |
assert ("libneuralnetworks.so" in whitelist) == (policy_level >= 27) |
| 301 |
|
| 302 |
platform = f"android_{wheel_level}_{arch}" |
| 303 |
policies = WheelPolicies( |
| 304 |
libc=Libc.ANDROID, |
| 305 |
arch=arch.baseline, |
| 306 |
wheel_fn=Path(f"foo-1.0-py3-none-{platform}.whl"), |
| 307 |
) |
| 308 |
assert len(list(policies)) == 2 |
| 309 |
assert policies.linux.whitelist == frozenset() |
| 310 |
|
| 311 |
assert policies.lowest is policies.highest |
| 312 |
assert policies.lowest.whitelist == whitelist |
| 313 |
|
| 314 |
|
| 315 |
def test_android_no_filename(): |
| 316 |
with pytest.raises(ValueError, match="wheel_fn is required when selecting Android policies"): |
| 317 |
WheelPolicies(libc=Libc.ANDROID, arch=Architecture.x86_64) |
| 318 |
|
| 319 |
|
| 320 |
@pytest.mark.parametrize( |
| 321 |
("filename", "message"), |
| 322 |
[ |
| 323 |
( |
| 324 |
"foo-1.0-py3-none-android_21_arm64_v8a.android_22_arm64_v8a.whl", |
| 325 |
"Android wheels must have exactly one platform tag, got " |
| 326 |
"['android_21_arm64_v8a', 'android_22_arm64_v8a']", |
| 327 |
), |
| 328 |
( |
| 329 |
"foo-1.0-py3-none-android_20_x86_64.whl", |
| 330 |
"no Android policies match the platform tag 'android_20_x86_64'", |
| 331 |
), |
| 332 |
], |
| 333 |
) |
| 334 |
def test_android_error(filename, message): |
| 335 |
with pytest.raises(ValueError, match=re.escape(message)): |
| 336 |
WheelPolicies(libc=Libc.ANDROID, arch=Architecture.x86_64, wheel_fn=Path(filename)) |