|
2 | 2 | __copyright__ = "Copyright The ORAS Authors." |
3 | 3 | __license__ = "Apache-2.0" |
4 | 4 |
|
| 5 | +import hashlib |
5 | 6 | import os |
6 | 7 | import subprocess |
7 | 8 | from pathlib import Path |
|
17 | 18 | here = Path(__file__).resolve().parent |
18 | 19 |
|
19 | 20 |
|
| 21 | +def make_pull_client(monkeypatch, layer, content): |
| 22 | + """Create a registry client whose pull inputs do not require a live registry.""" |
| 23 | + client = oras.provider.Registry(insecure=True) |
| 24 | + monkeypatch.setattr(client, "get_container", lambda target: target) |
| 25 | + monkeypatch.setattr(client.auth, "load_configs", lambda *args, **kwargs: None) |
| 26 | + monkeypatch.setattr( |
| 27 | + client, |
| 28 | + "get_manifest", |
| 29 | + lambda container, allowed_media_type: {"layers": [layer]}, |
| 30 | + ) |
| 31 | + |
| 32 | + def download_blob(container, digest, outfile): |
| 33 | + Path(outfile).write_bytes(content) |
| 34 | + return outfile |
| 35 | + |
| 36 | + monkeypatch.setattr(client, "download_blob", download_blob) |
| 37 | + return client |
| 38 | + |
| 39 | + |
| 40 | +def test_digest_string_round_trip(): |
| 41 | + original = f"sha256:{hashlib.sha256(b'content').hexdigest()}" |
| 42 | + |
| 43 | + assert str(oras.provider.Digest(original)) == original |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("algorithm", ["sha256", "sha512"]) |
| 47 | +def test_pull_validates_registered_digest(monkeypatch, tmp_path, algorithm): |
| 48 | + content = b"verified content" |
| 49 | + digest = f"{algorithm}:{hashlib.new(algorithm, content).hexdigest()}" |
| 50 | + layer = { |
| 51 | + "mediaType": oras.defaults.default_blob_media_type, |
| 52 | + "size": len(content), |
| 53 | + "digest": digest, |
| 54 | + "annotations": {oras.defaults.annotation_title: "artifact.txt"}, |
| 55 | + } |
| 56 | + client = make_pull_client(monkeypatch, layer, content) |
| 57 | + |
| 58 | + files = client.pull("registry.example/repository:tag", outdir=str(tmp_path)) |
| 59 | + |
| 60 | + outfile = tmp_path / "artifact.txt" |
| 61 | + assert files == [str(outfile)] |
| 62 | + assert outfile.read_bytes() == content |
| 63 | + |
| 64 | + |
| 65 | +def test_pull_rejects_digest_mismatch_without_replacing_file(monkeypatch, tmp_path): |
| 66 | + expected_content = b"expected content" |
| 67 | + downloaded_content = b"corrupt! content" |
| 68 | + digest = f"sha256:{hashlib.sha256(expected_content).hexdigest()}" |
| 69 | + layer = { |
| 70 | + "mediaType": oras.defaults.default_blob_media_type, |
| 71 | + "size": len(downloaded_content), |
| 72 | + "digest": digest, |
| 73 | + "annotations": {oras.defaults.annotation_title: "artifact.txt"}, |
| 74 | + } |
| 75 | + client = make_pull_client(monkeypatch, layer, downloaded_content) |
| 76 | + outfile = tmp_path / "artifact.txt" |
| 77 | + outfile.write_bytes(b"existing content") |
| 78 | + |
| 79 | + with pytest.raises(ValueError) as error: |
| 80 | + client.pull("registry.example/repository:tag", outdir=str(tmp_path)) |
| 81 | + |
| 82 | + actual_digest = f"sha256:{hashlib.sha256(downloaded_content).hexdigest()}" |
| 83 | + assert str(error.value) == ( |
| 84 | + f"Downloaded blob digest mismatch: expected {digest}, got {actual_digest}." |
| 85 | + ) |
| 86 | + assert outfile.read_bytes() == b"existing content" |
| 87 | + assert not list(tmp_path.glob(".oras-*")) |
| 88 | + |
| 89 | + |
| 90 | +def test_pull_rejects_size_mismatch(monkeypatch, tmp_path): |
| 91 | + content = b"content" |
| 92 | + digest = f"sha256:{hashlib.sha256(content).hexdigest()}" |
| 93 | + layer = { |
| 94 | + "mediaType": oras.defaults.default_blob_media_type, |
| 95 | + "size": len(content) + 1, |
| 96 | + "digest": digest, |
| 97 | + "annotations": {oras.defaults.annotation_title: "artifact.txt"}, |
| 98 | + } |
| 99 | + client = make_pull_client(monkeypatch, layer, content) |
| 100 | + |
| 101 | + with pytest.raises(ValueError, match="Downloaded blob size mismatch"): |
| 102 | + client.pull("registry.example/repository:tag", outdir=str(tmp_path)) |
| 103 | + |
| 104 | + assert not (tmp_path / "artifact.txt").exists() |
| 105 | + assert not list(tmp_path.glob(".oras-*")) |
| 106 | + |
| 107 | + |
| 108 | +def test_pull_validates_directory_before_extraction(monkeypatch, tmp_path): |
| 109 | + expected_content = b"expected archive" |
| 110 | + downloaded_content = b"corrupted archive" |
| 111 | + digest = f"sha256:{hashlib.sha256(expected_content).hexdigest()}" |
| 112 | + layer = { |
| 113 | + "mediaType": oras.defaults.default_blob_dir_media_type, |
| 114 | + "size": len(downloaded_content), |
| 115 | + "digest": digest, |
| 116 | + "annotations": {oras.defaults.annotation_title: "artifact"}, |
| 117 | + } |
| 118 | + client = make_pull_client(monkeypatch, layer, downloaded_content) |
| 119 | + extracted = False |
| 120 | + |
| 121 | + def extract_targz(*args, **kwargs): |
| 122 | + nonlocal extracted |
| 123 | + extracted = True |
| 124 | + |
| 125 | + monkeypatch.setattr(oras.utils, "extract_targz", extract_targz) |
| 126 | + |
| 127 | + with pytest.raises(ValueError, match="Downloaded blob digest mismatch"): |
| 128 | + client.pull("registry.example/repository:tag", outdir=str(tmp_path)) |
| 129 | + |
| 130 | + assert not extracted |
| 131 | + assert not (tmp_path / "artifact").exists() |
| 132 | + assert not list(tmp_path.glob(".oras-*")) |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize( |
| 136 | + ("digest", "error"), |
| 137 | + [ |
| 138 | + ("sha256+b64u:YWJj", "Unsupported OCI digest algorithm"), |
| 139 | + (f"sha384:{'a' * 96}", "Unsupported OCI digest algorithm"), |
| 140 | + (f"sha256:{'A' * 64}", "Invalid sha256 digest encoding"), |
| 141 | + (f"sha256:{'a' * 63}", "Invalid sha256 digest encoding"), |
| 142 | + ("sha256:not!hex", "Invalid OCI digest"), |
| 143 | + ("sha256:", "Invalid OCI digest"), |
| 144 | + (f"SHA256:{'a' * 64}", "Invalid OCI digest"), |
| 145 | + ], |
| 146 | +) |
| 147 | +def test_pull_rejects_invalid_digest_encoding(monkeypatch, tmp_path, digest, error): |
| 148 | + layer = { |
| 149 | + "mediaType": oras.defaults.default_blob_media_type, |
| 150 | + "size": 0, |
| 151 | + "digest": digest, |
| 152 | + "annotations": {oras.defaults.annotation_title: "artifact.txt"}, |
| 153 | + } |
| 154 | + client = make_pull_client(monkeypatch, layer, b"") |
| 155 | + |
| 156 | + with pytest.raises(ValueError, match=error): |
| 157 | + client.pull("registry.example/repository:tag", outdir=str(tmp_path)) |
| 158 | + |
| 159 | + assert not (tmp_path / "artifact.txt").exists() |
| 160 | + |
| 161 | + |
20 | 162 | @pytest.mark.with_auth(False) |
21 | 163 | def test_annotated_registry_push(tmp_path, registry, credentials, target): |
22 | 164 | """ |
|
0 commit comments