| 1 |
import json |
| 2 |
import posixpath |
| 3 |
import zipfile |
| 4 |
from base64 import urlsafe_b64encode |
| 5 |
from hashlib import sha256 |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from installer.exceptions import InstallerError |
| 10 |
from installer.records import parse_record_file |
| 11 |
from installer.sources import WheelFile, WheelSource |
| 12 |
|
| 13 |
|
| 14 |
class TestWheelSource: |
| 15 |
def test_takes_two_arguments(self): |
| 16 |
WheelSource("distribution", "version") |
| 17 |
WheelSource(distribution="distribution", version="version") |
| 18 |
|
| 19 |
def test_correctly_computes_properties(self): |
| 20 |
source = WheelSource(distribution="distribution", version="version") |
| 21 |
|
| 22 |
assert source.data_dir == "distribution-version.data" |
| 23 |
assert source.dist_info_dir == "distribution-version.dist-info" |
| 24 |
|
| 25 |
def test_raises_not_implemented_error(self): |
| 26 |
source = WheelSource(distribution="distribution", version="version") |
| 27 |
|
| 28 |
with pytest.raises(NotImplementedError): |
| 29 |
_ = source.dist_info_filenames |
| 30 |
|
| 31 |
with pytest.raises(NotImplementedError): |
| 32 |
source.read_dist_info("METADATA") |
| 33 |
|
| 34 |
with pytest.raises(NotImplementedError): |
| 35 |
source.get_contents() |
| 36 |
|
| 37 |
with pytest.raises(NotImplementedError): |
| 38 |
source.validate_record() |
| 39 |
|
| 40 |
|
| 41 |
def replace_file_in_zip(path: str, filename: str, content: "str | None") -> None: |
| 42 |
"""Helper function for replacing a file in the zip. |
| 43 |
|
| 44 |
Exists because ZipFile doesn't support remove. |
| 45 |
""" |
| 46 |
files = {} |
| 47 |
|
| 48 |
with zipfile.ZipFile(path) as archive: |
| 49 |
for file in archive.namelist(): |
| 50 |
if file == filename: |
| 51 |
if content is None: |
| 52 |
continue |
| 53 |
files[file] = content.encode() |
| 54 |
else: |
| 55 |
files[file] = archive.read(file) |
| 56 |
|
| 57 |
with zipfile.ZipFile(path, mode="w") as archive: |
| 58 |
for name, content in files.items(): |
| 59 |
archive.writestr(name, content) |
| 60 |
|
| 61 |
|
| 62 |
class TestWheelFile: |
| 63 |
def test_rejects_not_okay_name(self, tmp_path): |
| 64 |
|
| 65 |
path = tmp_path / "not_a_valid_name.whl" |
| 66 |
with zipfile.ZipFile(str(path), "w"): |
| 67 |
pass |
| 68 |
|
| 69 |
with ( |
| 70 |
pytest.raises(ValueError, match=r"Not a valid wheel filename: .+"), |
| 71 |
WheelFile.open(str(path)), |
| 72 |
): |
| 73 |
pass |
| 74 |
|
| 75 |
def test_provides_correct_dist_info_filenames(self, fancy_wheel): |
| 76 |
with WheelFile.open(fancy_wheel) as source: |
| 77 |
assert sorted(source.dist_info_filenames) == [ |
| 78 |
"METADATA", |
| 79 |
"RECORD", |
| 80 |
"WHEEL", |
| 81 |
"entry_points.txt", |
| 82 |
"top_level.txt", |
| 83 |
] |
| 84 |
|
| 85 |
def test_correctly_reads_from_dist_info_files(self, fancy_wheel): |
| 86 |
files = {} |
| 87 |
with zipfile.ZipFile(fancy_wheel) as archive: |
| 88 |
for file in archive.namelist(): |
| 89 |
if ".dist-info" not in file: |
| 90 |
continue |
| 91 |
files[posixpath.basename(file)] = archive.read(file).decode("utf-8") |
| 92 |
|
| 93 |
got_files = {} |
| 94 |
with WheelFile.open(fancy_wheel) as source: |
| 95 |
for file in files: |
| 96 |
got_files[file] = source.read_dist_info(file) |
| 97 |
|
| 98 |
assert got_files == files |
| 99 |
|
| 100 |
def test_provides_correct_contents(self, fancy_wheel): |
| 101 |
|
| 102 |
files = {} |
| 103 |
with zipfile.ZipFile(fancy_wheel) as archive: |
| 104 |
for file in archive.namelist(): |
| 105 |
if file[-1:] == "/": |
| 106 |
continue |
| 107 |
files[file] = archive.read(file) |
| 108 |
|
| 109 |
expected_record_lines = ( |
| 110 |
files["fancy-1.0.0.dist-info/RECORD"].decode("utf-8").splitlines() |
| 111 |
) |
| 112 |
expected_records = list(parse_record_file(expected_record_lines)) |
| 113 |
|
| 114 |
|
| 115 |
got_records = [] |
| 116 |
got_files = {} |
| 117 |
with WheelFile.open(fancy_wheel) as source: |
| 118 |
for record_elements, stream, is_executable in source.get_contents(): |
| 119 |
got_records.append(record_elements) |
| 120 |
got_files[record_elements[0]] = stream.read() |
| 121 |
assert not is_executable |
| 122 |
|
| 123 |
assert sorted(got_records) == sorted(expected_records) |
| 124 |
assert got_files == files |
| 125 |
|
| 126 |
def test_finds_dist_info(self, fancy_wheel): |
| 127 |
denorm = fancy_wheel.rename(fancy_wheel.parent / "Fancy-1.0.0-py3-none-any.whl") |
| 128 |
|
| 129 |
denorm = fancy_wheel.parent / "Fancy-1.0.0-py3-none-any.whl" |
| 130 |
with WheelFile.open(denorm) as source: |
| 131 |
assert source.dist_info_filenames |
| 132 |
|
| 133 |
def test_requires_dist_info_name_match(self, fancy_wheel): |
| 134 |
misnamed = fancy_wheel.rename( |
| 135 |
fancy_wheel.parent / "misnamed-1.0.0-py3-none-any.whl" |
| 136 |
) |
| 137 |
|
| 138 |
misnamed = fancy_wheel.parent / "misnamed-1.0.0-py3-none-any.whl" |
| 139 |
with pytest.raises(InstallerError) as ctx, WheelFile.open(misnamed) as source: |
| 140 |
_ = source.dist_info_filenames |
| 141 |
|
| 142 |
error = ctx.value |
| 143 |
assert error.filename == str(misnamed) |
| 144 |
assert error.dist_info == "fancy-1.0.0.dist-info" |
| 145 |
assert "" in error.reason |
| 146 |
assert error.dist_info in str(error) |
| 147 |
|
| 148 |
def test_enforces_single_dist_info(self, fancy_wheel): |
| 149 |
with zipfile.ZipFile(fancy_wheel, "a") as archive: |
| 150 |
archive.writestr( |
| 151 |
"name-1.0.0.dist-info/random.txt", |
| 152 |
b"This is a random file.", |
| 153 |
) |
| 154 |
|
| 155 |
with ( |
| 156 |
pytest.raises(InstallerError) as ctx, |
| 157 |
WheelFile.open(fancy_wheel) as source, |
| 158 |
): |
| 159 |
_ = source.dist_info_filenames |
| 160 |
|
| 161 |
error = ctx.value |
| 162 |
assert error.filename == str(fancy_wheel) |
| 163 |
assert error.dist_info == str(["fancy-1.0.0.dist-info", "name-1.0.0.dist-info"]) |
| 164 |
assert "exactly one .dist-info" in error.reason |
| 165 |
assert error.dist_info in str(error) |
| 166 |
|
| 167 |
def test_rejects_no_record_on_validate(self, fancy_wheel): |
| 168 |
|
| 169 |
replace_file_in_zip( |
| 170 |
fancy_wheel, |
| 171 |
filename="fancy-1.0.0.dist-info/RECORD", |
| 172 |
content=None, |
| 173 |
) |
| 174 |
with ( |
| 175 |
WheelFile.open(fancy_wheel) as source, |
| 176 |
pytest.raises( |
| 177 |
WheelFile.validation_error, match="Unable to retrieve `RECORD`" |
| 178 |
), |
| 179 |
): |
| 180 |
source.validate_record(validate_contents=False) |
| 181 |
|
| 182 |
def test_rejects_invalid_record_entry(self, fancy_wheel): |
| 183 |
with WheelFile.open(fancy_wheel) as source: |
| 184 |
record_file_contents = source.read_dist_info("RECORD") |
| 185 |
|
| 186 |
replace_file_in_zip( |
| 187 |
fancy_wheel, |
| 188 |
filename="fancy-1.0.0.dist-info/RECORD", |
| 189 |
content="\n".join( |
| 190 |
line.replace("sha256=", "") for line in record_file_contents |
| 191 |
), |
| 192 |
) |
| 193 |
with ( |
| 194 |
WheelFile.open(fancy_wheel) as source, |
| 195 |
pytest.raises( |
| 196 |
WheelFile.validation_error, |
| 197 |
match="Unable to retrieve `RECORD`", |
| 198 |
), |
| 199 |
): |
| 200 |
source.validate_record() |
| 201 |
|
| 202 |
def test_rejects_record_missing_file_on_validate(self, fancy_wheel): |
| 203 |
with WheelFile.open(fancy_wheel) as source: |
| 204 |
record_file_contents = source.read_dist_info("RECORD") |
| 205 |
|
| 206 |
|
| 207 |
new_record_file_contents = "\n".join(record_file_contents.split("\n")[2:]) |
| 208 |
replace_file_in_zip( |
| 209 |
fancy_wheel, |
| 210 |
filename="fancy-1.0.0.dist-info/RECORD", |
| 211 |
content=new_record_file_contents, |
| 212 |
) |
| 213 |
with ( |
| 214 |
WheelFile.open(fancy_wheel) as source, |
| 215 |
pytest.raises(WheelFile.validation_error, match="not mentioned in RECORD"), |
| 216 |
): |
| 217 |
source.validate_record(validate_contents=False) |
| 218 |
|
| 219 |
def test_rejects_record_missing_hash(self, fancy_wheel): |
| 220 |
with WheelFile.open(fancy_wheel) as source: |
| 221 |
record_file_contents = source.read_dist_info("RECORD") |
| 222 |
|
| 223 |
new_record_file_contents = "\n".join( |
| 224 |
line.split(",")[0] + ",," |
| 225 |
for line in record_file_contents.split("\n") |
| 226 |
) |
| 227 |
replace_file_in_zip( |
| 228 |
fancy_wheel, |
| 229 |
filename="fancy-1.0.0.dist-info/RECORD", |
| 230 |
content=new_record_file_contents, |
| 231 |
) |
| 232 |
with ( |
| 233 |
WheelFile.open(fancy_wheel) as source, |
| 234 |
pytest.raises( |
| 235 |
WheelFile.validation_error, |
| 236 |
match=r"hash / size of (.+) is not included in RECORD", |
| 237 |
), |
| 238 |
): |
| 239 |
source.validate_record(validate_contents=False) |
| 240 |
|
| 241 |
def test_accept_wheel_with_signature_file(self, fancy_wheel): |
| 242 |
with WheelFile.open(fancy_wheel) as source: |
| 243 |
record_file_contents = source.read_dist_info("RECORD") |
| 244 |
hash_b64_nopad = ( |
| 245 |
urlsafe_b64encode(sha256(record_file_contents.encode()).digest()) |
| 246 |
.decode("utf-8") |
| 247 |
.rstrip("=") |
| 248 |
) |
| 249 |
jws_content = json.dumps({"hash": f"sha256={hash_b64_nopad}"}) |
| 250 |
with zipfile.ZipFile(fancy_wheel, "a") as archive: |
| 251 |
archive.writestr("fancy-1.0.0.dist-info/RECORD.jws", jws_content) |
| 252 |
with WheelFile.open(fancy_wheel) as source: |
| 253 |
source.validate_record() |
| 254 |
|
| 255 |
def test_reject_signature_file_in_record(self, fancy_wheel): |
| 256 |
with WheelFile.open(fancy_wheel) as source: |
| 257 |
record_file_contents = source.read_dist_info("RECORD") |
| 258 |
record_hash_nopad = ( |
| 259 |
urlsafe_b64encode(sha256(record_file_contents.encode()).digest()) |
| 260 |
.decode("utf-8") |
| 261 |
.rstrip("=") |
| 262 |
) |
| 263 |
jws_content = json.dumps({"hash": f"sha256={record_hash_nopad}"}) |
| 264 |
with zipfile.ZipFile(fancy_wheel, "a") as archive: |
| 265 |
archive.writestr("fancy-1.0.0.dist-info/RECORD.jws", jws_content) |
| 266 |
|
| 267 |
|
| 268 |
jws_content = jws_content.encode() |
| 269 |
jws_hash_nopad = ( |
| 270 |
urlsafe_b64encode(sha256(jws_content).digest()).decode("utf-8").rstrip("=") |
| 271 |
) |
| 272 |
replace_file_in_zip( |
| 273 |
fancy_wheel, |
| 274 |
filename="fancy-1.0.0.dist-info/RECORD", |
| 275 |
content=record_file_contents.rstrip("\n") |
| 276 |
+ f"\nfancy-1.0.0.dist-info/RECORD.jws,sha256={jws_hash_nopad},{len(jws_content)}\n", |
| 277 |
) |
| 278 |
with ( |
| 279 |
WheelFile.open(fancy_wheel) as source, |
| 280 |
pytest.raises( |
| 281 |
WheelFile.validation_error, |
| 282 |
match=r"digital signature file (.+) is incorrectly contained in RECORD.", |
| 283 |
), |
| 284 |
): |
| 285 |
source.validate_record(validate_contents=False) |
| 286 |
|
| 287 |
def test_rejects_record_contain_self_hash(self, fancy_wheel): |
| 288 |
with WheelFile.open(fancy_wheel) as source: |
| 289 |
record_file_contents = source.read_dist_info("RECORD") |
| 290 |
|
| 291 |
new_record_file_lines = [] |
| 292 |
for line in record_file_contents.split("\n"): |
| 293 |
if not line: |
| 294 |
continue |
| 295 |
filename, hash_, size = line.split(",") |
| 296 |
if filename.split("/")[-1] == "RECORD": |
| 297 |
hash_ = "sha256=pREiHcl39jRySUXMCOrwmSsnOay8FB7fOJP5mZQ3D3A" |
| 298 |
size = str(len(record_file_contents)) |
| 299 |
new_record_file_lines.append(f"{filename},{hash_},{size}") |
| 300 |
|
| 301 |
replace_file_in_zip( |
| 302 |
fancy_wheel, |
| 303 |
filename="fancy-1.0.0.dist-info/RECORD", |
| 304 |
content="\n".join(new_record_file_lines), |
| 305 |
) |
| 306 |
with ( |
| 307 |
WheelFile.open(fancy_wheel) as source, |
| 308 |
pytest.raises( |
| 309 |
WheelFile.validation_error, |
| 310 |
match=r"RECORD file incorrectly contains hash / size.", |
| 311 |
), |
| 312 |
): |
| 313 |
source.validate_record(validate_contents=False) |
| 314 |
|
| 315 |
def test_rejects_record_validation_failed(self, fancy_wheel): |
| 316 |
with WheelFile.open(fancy_wheel) as source: |
| 317 |
record_file_contents = source.read_dist_info("RECORD") |
| 318 |
|
| 319 |
new_record_file_lines = [] |
| 320 |
for line in record_file_contents.split("\n"): |
| 321 |
if not line: |
| 322 |
continue |
| 323 |
filename, hash_, size = line.split(",") |
| 324 |
if filename.split("/")[-1] != "RECORD": |
| 325 |
hash_ = "sha256=pREiHcl39jRySUXMCOrwmSsnOay8FB7fOJP5mZQ3D3A" |
| 326 |
new_record_file_lines.append(f"{filename},{hash_},{size}") |
| 327 |
|
| 328 |
replace_file_in_zip( |
| 329 |
fancy_wheel, |
| 330 |
filename="fancy-1.0.0.dist-info/RECORD", |
| 331 |
content="\n".join(new_record_file_lines), |
| 332 |
) |
| 333 |
with ( |
| 334 |
WheelFile.open(fancy_wheel) as source, |
| 335 |
pytest.raises( |
| 336 |
WheelFile.validation_error, |
| 337 |
match=r"hash / size of (.+) didn't match RECORD", |
| 338 |
), |
| 339 |
): |
| 340 |
source.validate_record() |
| 341 |
|
| 342 |
def test_rejects_record_containing_unknown_hash(self, fancy_wheel): |
| 343 |
with WheelFile.open(fancy_wheel) as source: |
| 344 |
record_file_contents = source.read_dist_info("RECORD") |
| 345 |
|
| 346 |
new_record_file_contents = record_file_contents.replace("sha256=", "sha=") |
| 347 |
replace_file_in_zip( |
| 348 |
fancy_wheel, |
| 349 |
filename="fancy-1.0.0.dist-info/RECORD", |
| 350 |
content=new_record_file_contents, |
| 351 |
) |
| 352 |
|
| 353 |
with ( |
| 354 |
WheelFile.open(fancy_wheel) as source, |
| 355 |
pytest.raises( |
| 356 |
WheelFile.validation_error, |
| 357 |
match=( |
| 358 |
r"In .+, entry in RECORD file for .+ is invalid: " |
| 359 |
"invalid hash algorithm 'sha'" |
| 360 |
), |
| 361 |
), |
| 362 |
): |
| 363 |
source.validate_record(validate_contents=True) |