| 1 | #!/usr/bin/env python3 |
| 2 | import concurrent.futures |
| 3 | import importlib.util |
| 4 | import io |
| 5 | import contextlib |
| 6 | import ctypes.util |
| 7 | import datetime |
| 8 | import json |
| 9 | import os |
| 10 | from pathlib import Path |
| 11 | from types import SimpleNamespace |
| 12 | import tempfile |
| 13 | import threading |
| 14 | import time |
| 15 | import unittest |
| 16 | import urllib.error |
| 17 | import urllib.request |
| 18 | from unittest.mock import patch |
| 19 | |
| 20 | spec = importlib.util.spec_from_file_location('mirror', Path(__file__).resolve().parents[1] / 'service/zig-mirror/build/mirror.py') |
| 21 | mirror = importlib.util.module_from_spec(spec) |
| 22 | spec.loader.exec_module(mirror) |
| 23 | |
| 24 | |
| 25 | class IndexTests(unittest.TestCase): |
| 26 | def setUp(self): |
| 27 | cache = patch.object(mirror, '_index_cache', (float('-inf'), {})) |
| 28 | cache.start() |
| 29 | self.addCleanup(cache.stop) |
| 30 | mirror.stable_time.cache_clear() |
| 31 | |
| 32 | def test_index_coalesces_concurrent_reads_expires_and_forces_refresh(self): |
| 33 | snapshot = {'0.17.0': {'docs': 'https://ziglang.org/documentation/0.17.0/'}} |
| 34 | def response(*args, **kwargs): |
| 35 | time.sleep(.01) |
| 36 | return io.BytesIO(json.dumps(snapshot).encode()) |
| 37 | with patch.object(mirror.urllib.request, 'urlopen', side_effect=response) as request, patch.object(mirror.time, 'monotonic', return_value=0) as clock: |
| 38 | with concurrent.futures.ThreadPoolExecutor(max_workers=4) as workers: |
| 39 | entries = list(workers.map(lambda _: mirror.index(), range(8))) |
| 40 | self.assertTrue(all(entry == snapshot for entry in entries)) |
| 41 | self.assertEqual(request.call_count, 1) |
| 42 | clock.return_value = 3599 |
| 43 | mirror.index() |
| 44 | self.assertEqual(request.call_count, 1) |
| 45 | clock.return_value = 3600 |
| 46 | mirror.index() |
| 47 | self.assertEqual(request.call_count, 2) |
| 48 | mirror.index(refresh=True) |
| 49 | self.assertEqual(request.call_count, 3) |
| 50 | self.assertTrue(all(call.args[0] == 'https://ziglang.org/download/index.json' for call in request.call_args_list)) |
| 51 | |
| 52 | def test_new_release_miss_refreshes_stale_index_before_publication(self): |
| 53 | release = {'date': '2027-04-01', 'docs': 'https://ziglang.org/documentation/0.19.0/'} |
| 54 | responses = [{}, {'0.19.0': release}] |
| 55 | with tempfile.TemporaryDirectory() as root, patch.object(mirror.urllib.request, 'urlopen', side_effect=lambda *args, **kwargs: io.BytesIO(json.dumps(responses.pop(0)).encode())) as request: |
| 56 | mirror.index() |
| 57 | cache = mirror.Mirror(root) |
| 58 | with patch.object(mirror.shutil, 'disk_usage', return_value=SimpleNamespace(free=64 * 1024**3)), patch.object(mirror, 'fetch', side_effect=lambda url, target, limit: target.write_bytes(b'fixture')), patch.object(mirror.subprocess, 'run', return_value=SimpleNamespace(returncode=0)): |
| 59 | artifact = cache.ensure('zig-x86_64-linux-0.19.0.tar.xz') |
| 60 | self.assertEqual(request.call_count, 2) |
| 61 | self.assertIn('[Language Reference](' + release['docs'] + ')', (artifact.parent / 'preadme.md').read_text()) |
| 62 | expected = int(datetime.datetime(2027, 4, 1, tzinfo=datetime.timezone.utc).timestamp()) * 10**9 |
| 63 | self.assertEqual(artifact.stat().st_mtime_ns, expected) |
| 64 | self.assertEqual(Path(str(artifact) + '.minisig').stat().st_mtime_ns, expected) |
| 65 | self.assertEqual(artifact.parent.stat().st_mtime_ns, expected) |
| 66 | self.assertEqual(cache.root.stat().st_mtime_ns, expected) |
| 67 | |
| 68 | |
| 69 | class MirrorTests(unittest.TestCase): |
| 70 | def setUp(self): |
| 71 | self.temporary = tempfile.TemporaryDirectory() |
| 72 | self.addCleanup(self.temporary.cleanup) |
| 73 | self.cache = mirror.Mirror(self.temporary.name) |
| 74 | disk = patch.object(mirror.shutil, 'disk_usage', return_value=SimpleNamespace(free=64 * 1024**3)) |
| 75 | disk.start() |
| 76 | self.addCleanup(disk.stop) |
| 77 | mirror.stable_time.cache_clear() |
| 78 | dates = {version: {'date': date} for version, date in [('0.1.1', '2017-10-17'), ('0.2.0', '2018-03-15'), |
| 79 | ('0.3.0', '2018-09-28'), ('0.4.0', '2019-04-08'), ('0.5.0', '2019-09-30'), ('0.17.0', '2026-10-01')]} |
| 80 | metadata = patch.object(mirror, 'index', return_value=dates) |
| 81 | metadata.start() |
| 82 | self.addCleanup(metadata.stop) |
| 83 | |
| 84 | def dev_fixture(self): |
| 85 | fixture = Path(__file__).resolve().parents[1] / 'service/zig-mirror/fixture/zig-x86_64-windows-0.18.0-dev.4+5a23bf4b2.zip.minisig' |
| 86 | return fixture.name.removesuffix('.minisig'), fixture.read_bytes() |
| 87 | |
| 88 | def test_spec_filenames_and_existing_layout(self): |
| 89 | cases = [('zig-0.14.1.tar.xz', '0.14.1/'), ('zig-bootstrap-0.17.0.zip.minisig', '0.17.0/'), |
| 90 | ('zig-win64-0.1.1.zip', '0.1.1/'), ('zig-win64-0.2.0.zip', '0.2.0/'), |
| 91 | ('zig-x86_64-linux-0.14.1.tar.xz', '0.14.1/'), ('zig-linux-x86_64-0.13.0.tar.xz', '0.13.0/'), |
| 92 | ('zig-newarch-newos-0.18.0-dev.1+abc123.tar.xz', '0.18.0/dev/0.18.0-dev.1+abc123/')] |
| 93 | for name, prefix in cases: |
| 94 | self.assertEqual(str(mirror.relative(name)), prefix + name) |
| 95 | self.assertEqual(mirror.upstream(name), 'https://ziglang.org/' + ('builds/' if '-dev.' in name else 'download/' + mirror.filename(name) + '/') + urllib.parse.quote(name)) |
| 96 | for name in ['../zig-0.17.0.tar.xz', 'zig-00.17.0.tar.xz', 'zig-0.17.0.tar.xz/anything', 'foo.zip', 'zig-a-b-0.18.0-dev.1+ab?x.tar.xz', |
| 97 | 'zig-win64-0.3.0.zip', 'zig-win64-0.1.1.tar.xz', 'zig-win64-0.1.1.zip.minisig', 'zig-linux-0.1.1.zip']: |
| 98 | with self.assertRaises(ValueError): |
| 99 | mirror.filename(name) |
| 100 | |
| 101 | def test_concurrent_miss_downloads_once_and_atomic_signature_pair(self): |
| 102 | calls = [] |
| 103 | def fetch(url, target, limit): |
| 104 | calls.append(url) |
| 105 | time.sleep(.03) |
| 106 | target.write_bytes(b'signed data' if not url.endswith('.minisig') else b'signature') |
| 107 | with patch.object(mirror, 'fetch', side_effect=fetch), patch.object(mirror.subprocess, 'run') as verify: |
| 108 | verify.return_value.returncode = 0 |
| 109 | with concurrent.futures.ThreadPoolExecutor(max_workers=4) as workers: |
| 110 | paths = list(workers.map(self.cache.ensure, ['zig-0.17.0.tar.xz'] * 4)) |
| 111 | self.assertEqual(len(calls), 2) |
| 112 | self.assertTrue(all(path.read_bytes() == b'signed data' for path in paths)) |
| 113 | self.assertEqual(Path(str(paths[0]) + '.minisig').read_bytes(), b'signature') |
| 114 | self.assertFalse(list(self.cache.root.rglob('.download-*'))) |
| 115 | |
| 116 | def test_bad_hash_and_signature_never_publish(self): |
| 117 | def fetch(url, target, limit): |
| 118 | target.write_bytes(b'bad data') |
| 119 | with patch.object(mirror, 'fetch', side_effect=fetch): |
| 120 | with self.assertRaises(mirror.Unavailable): |
| 121 | self.cache.ensure('zig-0.17.0.tar.xz', checksum='0' * 64) |
| 122 | with patch.object(mirror.subprocess, 'run') as verify: |
| 123 | verify.return_value.returncode = 1 |
| 124 | with self.assertRaises(mirror.Unavailable): |
| 125 | self.cache.ensure('zig-0.17.0.tar.xz') |
| 126 | self.assertFalse(self.cache.path('zig-0.17.0.tar.xz').exists()) |
| 127 | self.assertFalse(list(self.cache.root.rglob('.download-*'))) |
| 128 | |
| 129 | def test_symlink_cannot_escape_cache(self): |
| 130 | (self.cache.root / '0.17.0').symlink_to(self.cache.root.parent) |
| 131 | with self.assertRaises(ValueError): |
| 132 | self.cache.ensure('zig-0.17.0.tar.xz') |
| 133 | |
| 134 | def test_reserve_rejects_new_download_but_retains_cached_reads(self): |
| 135 | with patch.object(mirror.shutil, 'disk_usage', return_value=SimpleNamespace(free=0)), patch.object(mirror, 'fetch') as fetch: |
| 136 | with self.assertRaises(mirror.Unavailable): |
| 137 | self.cache.ensure('zig-0.17.0.tar.xz') |
| 138 | fetch.assert_not_called() |
| 139 | target = self.cache.path('zig-0.17.0.tar.xz') |
| 140 | target.parent.mkdir() |
| 141 | target.write_bytes(b'cached') |
| 142 | self.assertEqual(self.cache.ensure(target.name), target) |
| 143 | |
| 144 | def test_backfill_dry_run_and_budget_do_not_download(self): |
| 145 | versions = ['0.17.0/', '../', 'not-a-version/'] |
| 146 | artifacts = ['zig-0.17.0.tar.xz', 'zig-0.17.0.tar.xz.minisig', 'private.txt'] |
| 147 | with patch.object(mirror, 'listing', side_effect=[versions, artifacts]), patch.object(mirror, 'index', return_value={}), patch.object(self.cache, 'ensure') as ensure, contextlib.redirect_stdout(io.StringIO()) as output: |
| 148 | mirror.backfill(self.cache, 0, False) |
| 149 | ensure.assert_not_called() |
| 150 | self.assertIn('http://mirrors.nektro.net/zig/0.17.0/zig-0.17.0.tar.xz', output.getvalue()) |
| 151 | self.assertNotIn('private.txt', output.getvalue()) |
| 152 | with patch.object(mirror, 'listing', side_effect=[versions, artifacts]), patch.object(mirror, 'index', return_value={}), patch.object(self.cache, 'ensure') as ensure: |
| 153 | with self.assertRaises(SystemExit): |
| 154 | mirror.backfill(self.cache, 0, True) |
| 155 | ensure.assert_not_called() |
| 156 | |
| 157 | def test_verify_rejects_corrupted_existing_archive(self): |
| 158 | target = self.cache.path('zig-0.17.0.tar.xz') |
| 159 | target.parent.mkdir() |
| 160 | target.write_bytes(b'corrupt') |
| 161 | entries = {'0.17.0': {'src': {'tarball': 'https://ziglang.org/download/0.17.0/zig-0.17.0.tar.xz', 'shasum': '0' * 64}}} |
| 162 | with patch.object(mirror, 'index', return_value=entries): |
| 163 | with self.assertRaises(mirror.Unavailable): |
| 164 | self.cache.verify() |
| 165 | |
| 166 | def test_verify_ignores_download_locks_and_pending_archives(self): |
| 167 | name = 'zig-0.5.0.tar.xz' |
| 168 | body = b'verified historical archive' |
| 169 | checksum = mirror.hashlib.sha256(body).hexdigest() |
| 170 | with patch.object(mirror, 'fetch', side_effect=lambda url, target, limit: target.write_bytes(body)): |
| 171 | artifact = self.cache.ensure(name, checksum=checksum) |
| 172 | self.assertEqual((self.cache.root / '.mirror-locks' / name).stat().st_size, 0) |
| 173 | pending = artifact.parent / '.download-pending' / name |
| 174 | pending.parent.mkdir() |
| 175 | pending.write_bytes(b'incomplete corrupt bytes') |
| 176 | entries = {'0.5.0': {'src': {'tarball': mirror.upstream(name), 'shasum': checksum}}} |
| 177 | with patch.object(mirror, 'index', return_value=entries), contextlib.redirect_stdout(io.StringIO()) as output: |
| 178 | self.cache.verify() |
| 179 | self.assertEqual(json.loads(output.getvalue()), {'verifiedArtifacts': 1}) |
| 180 | |
| 181 | def test_http_query_range_head_denials_and_timeout(self): |
| 182 | target = self.cache.path('zig-0.17.0.tar.xz') |
| 183 | target.parent.mkdir() |
| 184 | target.write_bytes(b'0123456789') |
| 185 | server = mirror.http.server.ThreadingHTTPServer(('127.0.0.1', 0), mirror.Handler) |
| 186 | server.mirror = self.cache |
| 187 | threading.Thread(target=server.serve_forever, daemon=True).start() |
| 188 | self.addCleanup(server.server_close) |
| 189 | self.addCleanup(server.shutdown) |
| 190 | base = 'http://127.0.0.1:' + str(server.server_port) |
| 191 | with urllib.request.urlopen(base + '/mirrors/zig/zig-0.17.0.tar.xz?source=spec-test') as response: |
| 192 | self.assertEqual(response.status, 200) |
| 193 | self.assertEqual(response.read(), b'0123456789') |
| 194 | for method, value, expected in [('GET', 'bytes=2-5', b'2345'), ('GET', 'bytes=-3', b'789'), ('HEAD', 'bytes=2-5', b'')]: |
| 195 | with urllib.request.urlopen(urllib.request.Request(base + '/zig-0.17.0.tar.xz', method=method, headers={'Range': value})) as response: |
| 196 | self.assertEqual(response.status, 206) |
| 197 | self.assertEqual(response.read(), expected) |
| 198 | for path, headers, status in [('/zig-0.17.0.tar.xz', {'Range': 'bytes=100-'}, 416), ('/../secret', {}, 404), ('/0.17.0/zig-0.17.0.tar.xz', {}, 404)]: |
| 199 | with self.assertRaises(urllib.error.HTTPError) as error: |
| 200 | urllib.request.urlopen(urllib.request.Request(base + path, headers=headers)) |
| 201 | self.assertEqual(error.exception.code, status) |
| 202 | with patch.object(self.cache, 'ensure', side_effect=mirror.Unavailable()): |
| 203 | with self.assertRaises(urllib.error.HTTPError) as error: |
| 204 | urllib.request.urlopen(base + '/zig-0.17.0.tar.xz') |
| 205 | self.assertEqual(error.exception.code, 504) |
| 206 | |
| 207 | def test_unsigned_stable_http_miss_uses_official_hash_and_fails_closed(self): |
| 208 | names = ['zig-win64-0.1.1.zip', 'zig-linux-x86_64-0.5.0.tar.xz', 'zig-linux-x86_64-0.4.0.tar.xz'] |
| 209 | body = b'official historical archive' |
| 210 | entries = {mirror.filename(name): {'date': '2017-10-17', 'platform': {'tarball': mirror.upstream(name), |
| 211 | 'shasum': mirror.hashlib.sha256(body).hexdigest() if i < 2 else '0' * 64}} |
| 212 | for i, name in enumerate(names)} |
| 213 | def fetch(url, target, limit): |
| 214 | self.assertNotIn('.minisig', url) |
| 215 | self.assertEqual(url, mirror.upstream(target.name)) |
| 216 | target.write_bytes(body) |
| 217 | server = mirror.http.server.ThreadingHTTPServer(('127.0.0.1', 0), mirror.Handler) |
| 218 | server.mirror = self.cache |
| 219 | threading.Thread(target=server.serve_forever, daemon=True).start() |
| 220 | self.addCleanup(server.server_close) |
| 221 | self.addCleanup(server.shutdown) |
| 222 | base = 'http://127.0.0.1:' + str(server.server_port) + '/mirrors/zig/' |
| 223 | with patch.object(mirror, 'index', return_value=entries) as index, patch.object(mirror, 'fetch', side_effect=fetch) as download, patch.object(mirror.subprocess, 'run') as signature: |
| 224 | for name in names[:2]: |
| 225 | with urllib.request.urlopen(base + name) as response: |
| 226 | self.assertEqual(response.read(), body) |
| 227 | for name in [names[2], 'zig-linux-x86_64-0.3.0.tar.xz']: |
| 228 | with self.assertRaises(urllib.error.HTTPError) as error: |
| 229 | urllib.request.urlopen(base + name) |
| 230 | self.assertEqual(error.exception.code, 504) |
| 231 | self.assertFalse(self.cache.path(name).exists()) |
| 232 | self.assertEqual(download.call_count, 3) |
| 233 | signature.assert_not_called() |
| 234 | index.side_effect = mirror.Unavailable('offline') |
| 235 | with urllib.request.urlopen(base + names[0]) as response: |
| 236 | self.assertEqual(response.read(), body) |
| 237 | self.assertFalse(list(self.cache.root.rglob('.download-*'))) |
| 238 | |
| 239 | @unittest.skipUnless(ctypes.util.find_library('sodium'), 'libsodium required') |
| 240 | def test_real_minisign_comment_and_tampering(self): |
| 241 | name, body = self.dev_fixture() |
| 242 | signature = self.cache.root / (name + '.minisig') |
| 243 | signature.write_bytes(body) |
| 244 | self.assertEqual(mirror.signed_time(signature), 1791194091 * 10**9) |
| 245 | for changed in [body.replace(b'1791194091', b'1791194092'), body.replace(b'RUSGO', b'RUSGP'), |
| 246 | body.replace(b'trusted comment: timestamp:', b'trusted comment: forged:')]: |
| 247 | signature.write_bytes(changed) |
| 248 | with self.assertRaises(mirror.Unavailable): |
| 249 | mirror.signed_time(signature) |
| 250 | signature.unlink() |
| 251 | os.mkfifo(signature) |
| 252 | with self.assertRaises(mirror.Unavailable): |
| 253 | mirror.signed_time(signature) |
| 254 | |
| 255 | @unittest.skipUnless(ctypes.util.find_library('sodium'), 'libsodium required') |
| 256 | def test_publisher_dates_pairs_and_folders_after_cleanup(self): |
| 257 | name, signature = self.dev_fixture() |
| 258 | def fetch(url, target, limit): |
| 259 | target.write_bytes(signature if url.endswith('.minisig') and '-dev.' in url else b'fixture') |
| 260 | with patch.object(mirror, 'fetch', side_effect=fetch), patch.object(mirror.subprocess, 'run') as verify: |
| 261 | verify.return_value.returncode = 0 |
| 262 | artifact = self.cache.ensure(name) |
| 263 | stamp = 1791194091 * 10**9 |
| 264 | self.assertEqual(artifact.stat().st_mtime_ns, stamp) |
| 265 | self.assertEqual(Path(str(artifact) + '.minisig').stat().st_mtime_ns, stamp) |
| 266 | for directory in [artifact.parent, artifact.parent.parent, artifact.parent.parent.parent, self.cache.root]: |
| 267 | self.assertEqual(directory.stat().st_mtime_ns, stamp) |
| 268 | (self.cache.root / '0.99.0').mkdir() |
| 269 | older = self.cache.ensure('zig-0.17.0.tar.xz') |
| 270 | self.assertEqual(older.stat().st_mtime_ns, 1790812800 * 10**9) |
| 271 | self.assertEqual(self.cache.root.stat().st_mtime_ns, stamp) |
| 272 | self.assertFalse(list(self.cache.root.rglob('.download-*'))) |
| 273 | self.assertIn('Signature for '+name, (artifact.parent/'preadme.md').read_text()) |
| 274 | self.assertIn('[All development builds](/mirrors/zig/0.18.0/dev/)', (artifact.parent/'preadme.md').read_text()) |
| 275 | self.assertIn('this release only has [development builds]', (artifact.parent.parent.parent/'preadme.md').read_text()) |
| 276 | |
| 277 | @unittest.skipUnless(ctypes.util.find_library('sodium'), 'libsodium required') |
| 278 | def test_reconcile_is_metadata_only_idempotent_and_journalled(self): |
| 279 | name, signature = self.dev_fixture() |
| 280 | artifact = self.cache.path(name);artifact.parent.mkdir(parents=True) |
| 281 | artifact.write_bytes(b'public compiler fixture') |
| 282 | Path(str(artifact)+'.minisig').write_bytes(signature) |
| 283 | stable = self.cache.path('zig-win64-0.1.1.zip');stable.parent.mkdir();stable.write_bytes(b'legacy') |
| 284 | root_readme = self.cache.root/'preadme.md';root_readme.write_text('owner root notes') |
| 285 | version_readme = artifact.parent/'preadme.md';version_readme.write_text('old public version notes') |
| 286 | hidden = self.cache.root/'.hist';hidden.mkdir();(hidden/'zig-0.17.0.tar.xz').write_bytes(b'private history') |
| 287 | original = {path: (path.stat().st_ino,path.stat().st_uid,path.stat().st_gid,path.stat().st_mode,path.stat().st_size) |
| 288 | for path in [artifact,Path(str(artifact)+'.minisig'),stable,root_readme,hidden/'zig-0.17.0.tar.xz']} |
| 289 | plan = self.cache.reconcile() |
| 290 | self.assertFalse(plan['errors']) |
| 291 | self.assertFalse((self.cache.root/'.mirror-locks').exists()) |
| 292 | with tempfile.TemporaryDirectory() as private: |
| 293 | journal=Path(private)/'undo.json' |
| 294 | self.cache.reconcile(True,journal) |
| 295 | self.assertEqual(journal.stat().st_mode&0o777,0o600) |
| 296 | records = [json.loads(line) for line in journal.read_text().splitlines()] |
| 297 | self.assertTrue(records[0]['readmes']) |
| 298 | self.assertEqual(set(records[1]['applied']), |
| 299 | {entry['path'] for entry in records[0]['entries']} | |
| 300 | {change['path'] for change in records[0]['readmes']}) |
| 301 | with self.assertRaises(FileExistsError): |
| 302 | self.cache.reconcile(True, journal) |
| 303 | with self.assertRaises(ValueError): |
| 304 | self.cache.reconcile(True, self.cache.root / 'public-undo.json') |
| 305 | for path,state in original.items(): |
| 306 | actual=path.stat();self.assertEqual((actual.st_ino,actual.st_uid,actual.st_gid,actual.st_mode,actual.st_size),state) |
| 307 | self.assertEqual(root_readme.read_text(),'owner root notes') |
| 308 | self.assertEqual(artifact.read_bytes(),b'public compiler fixture') |
| 309 | self.assertEqual(self.cache.reconcile()['changed'],0) |
| 310 | self.assertEqual(self.cache.reconcile()['readmes'],[]) |
| 311 | |
| 312 | def test_hidden_lock_creation_preserves_visible_root_date(self): |
| 313 | os.utime(self.cache.root, ns=(1234, 5678)) |
| 314 | with concurrent.futures.ThreadPoolExecutor(max_workers=4) as workers: |
| 315 | list(workers.map(lambda _: self.cache.lock_directory(), range(12))) |
| 316 | self.assertEqual(self.cache.root.stat().st_mtime_ns, 5678) |
| 317 | |
| 318 | @unittest.skipUnless(ctypes.util.find_library('sodium'), 'libsodium required') |
| 319 | def test_withdrawn_0150_uses_authenticated_signing_time(self): |
| 320 | name = 'zig-x86_64-linux-0.15.0.tar.xz' |
| 321 | fixture = Path(__file__).resolve().parents[1] / 'service/zig-mirror/fixture' / (name + '.minisig') |
| 322 | artifact = self.cache.path(name) |
| 323 | artifact.parent.mkdir() |
| 324 | artifact.write_bytes(b'already verified archive') |
| 325 | signature = Path(str(artifact) + '.minisig') |
| 326 | signature.write_bytes(fixture.read_bytes()) |
| 327 | with patch.object(mirror, 'index', side_effect=AssertionError('withdrawn release must not use another release date')): |
| 328 | self.assertEqual(mirror.artifact_time(artifact), mirror.signed_time(signature)) |
| 329 | signature.write_bytes(fixture.read_bytes().replace(b'timestamp:', b'tampered:')) |
| 330 | with self.assertRaises(mirror.Unavailable): |
| 331 | mirror.artifact_time(artifact) |
| 332 | |
| 333 | def test_missing_release_date_never_publishes(self): |
| 334 | with patch.object(mirror, 'fetch', side_effect=lambda url, target, limit: target.write_bytes(b'fixture')): |
| 335 | with self.assertRaises(mirror.Unavailable): |
| 336 | self.cache.ensure('zig-0.4.1.tar.xz', checksum=mirror.hashlib.sha256(b'fixture').hexdigest()) |
| 337 | self.assertFalse(self.cache.path('zig-0.4.1.tar.xz').exists()) |
| 338 | |
| 339 | def test_metadata_traversal_rejects_intermediate_symlinks(self): |
| 340 | with tempfile.TemporaryDirectory() as outside: |
| 341 | target=Path(outside)/'file';target.write_bytes(b'untouched') |
| 342 | (self.cache.root/'alias').symlink_to(outside) |
| 343 | with self.assertRaises(OSError): |
| 344 | mirror.set_mtime(self.cache.root/'alias/file',1) |
| 345 | |
| 346 | def test_dev_index_replaces_duplicate_list_with_heading(self): |
| 347 | artifact = self.cache.path('zig-x86_64-linux-0.18.0-dev.4+5a23bf4b2.tar.xz') |
| 348 | artifact.parent.mkdir(parents=True) |
| 349 | artifact.write_bytes(b'public compiler fixture') |
| 350 | directory = artifact.parent.parent |
| 351 | readme = directory / 'preadme.md' |
| 352 | readme.write_text('### Zig 0.18.0 development builds\n\n- [0.18.0-dev.4+5a23bf4b2](old-link/)\n') |
| 353 | previous = {path: path.stat().st_mtime_ns for path in [artifact, artifact.parent, directory, directory.parent, self.cache.root]} |
| 354 | change = self.cache.directory_readme(directory) |
| 355 | self.assertEqual(change['content'], '### Zig 0.18.0 development builds\n') |
| 356 | self.cache.apply_readme(change) |
| 357 | self.assertIsNone(self.cache.directory_readme(directory)) |
| 358 | self.assertEqual(readme.read_text(), change['content']) |
| 359 | for path, timestamp in previous.items(): |
| 360 | self.assertEqual(path.stat().st_mtime_ns, timestamp) |
| 361 | |
| 362 | def test_development_only_base_has_exact_sentence_without_release_docs(self): |
| 363 | directory = self.cache.root / '0.18.0' |
| 364 | (directory / 'dev').mkdir(parents=True) |
| 365 | with patch.object(mirror, 'index', side_effect=AssertionError('development navigation needs no release metadata')): |
| 366 | change = self.cache.directory_readme(directory) |
| 367 | self.assertEqual(change['content'], '### Zig 0.18.0\n\nthis release only has [development builds](/mirrors/zig/0.18.0/dev/) out.\n') |
| 368 | self.cache.apply_readme(change) |
| 369 | self.assertIsNone(self.cache.directory_readme(directory)) |
| 370 | |
| 371 | def test_release_links_follow_index_presence_and_precede_artifact_table(self): |
| 372 | artifact = self.cache.path('zig-x86_64-linux-0.19.0.tar.xz') |
| 373 | release = {'notes': 'https://ziglang.org/download/0.19.0/release-notes.html', |
| 374 | 'docs': 'https://ziglang.org/documentation/0.19.0/', |
| 375 | 'stdDocs': 'https://ziglang.org/documentation/0.19.0/std/'} |
| 376 | labels = {'notes': 'Release Notes', 'docs': 'Language Reference', 'stdDocs': 'Standard Library Documentation'} |
| 377 | for absent in [None, *release]: |
| 378 | with self.subTest(absent=absent), patch.object(mirror, 'index', return_value={'0.19.0': {key: url for key, url in release.items() if key != absent}}): |
| 379 | text = mirror.version_readme('0.19.0', [artifact], True).decode() |
| 380 | for key, url in release.items(): |
| 381 | if key == absent: |
| 382 | self.assertNotIn(f'[{labels[key]}]({url})', text) |
| 383 | else: |
| 384 | self.assertIn(f'- [{labels[key]}]({url})', text) |
| 385 | self.assertLess(text.index(url), text.index('<div ')) |
| 386 | self.assertNotIn('[Development builds]', text) |
| 387 | with patch.object(mirror, 'index', return_value={'0.19.0': {'notes': 'https://example.org/notes', 'docs': None, 'stdDocs': 'https://ziglang.org/bad)link'}}): |
| 388 | text = mirror.version_readme('0.19.0', [artifact]).decode() |
| 389 | for label in labels.values(): |
| 390 | self.assertNotIn(label, text) |
| 391 | self.assertIn(artifact.name, text) |
| 392 | |
| 393 | def test_refresh_updates_cached_release_links_without_changing_dates(self): |
| 394 | artifact = self.cache.path('zig-x86_64-linux-0.17.0.tar.xz') |
| 395 | artifact.parent.mkdir() |
| 396 | artifact.write_bytes(b'already verified archive') |
| 397 | signature = Path(str(artifact) + '.minisig') |
| 398 | signature.write_bytes(b'already verified signature') |
| 399 | (artifact.parent / 'dev').mkdir() |
| 400 | stamp = mirror.stable_time('0.17.0') |
| 401 | for path in [artifact, signature, artifact.parent, self.cache.root]: |
| 402 | mirror.set_mtime(path, stamp) |
| 403 | release = {'date': '2026-10-01', 'docs': 'https://ziglang.org/documentation/0.17.0/', |
| 404 | 'src': {'tarball': mirror.upstream(artifact.name), 'shasum': 'unused-cached-hash'}} |
| 405 | with patch.object(mirror, 'index', return_value={'0.17.0': release}): |
| 406 | self.cache.write_readmes(artifact.parent) |
| 407 | mirror.set_mtime(artifact.parent, stamp) |
| 408 | readme = artifact.parent / 'preadme.md' |
| 409 | custom = readme.read_text().replace('| Architecture |', '| Target |') + '\nCustom table notes.\n' |
| 410 | stale = custom.replace(mirror.SIGNATURE_NOTE, '<span style="font-style:italic;color:orange">Verify your build signatures.</span> <span style="opacity:0.7">The &#128272; links open the Minisign signatures.</span>') |
| 411 | readme.write_text(stale.replace('<div ', '[Development builds](/mirrors/zig/0.17.0/dev/)\n\n<div ', 1)) |
| 412 | with patch.object(mirror, 'index', return_value={'0.17.0': release}): |
| 413 | change = self.cache.directory_readme(artifact.parent) |
| 414 | self.assertEqual(mirror.base64.b64decode(change['previous']), readme.read_bytes()) |
| 415 | original = {path: path.stat().st_mtime_ns for path in [artifact, signature, artifact.parent, self.cache.root, readme]} |
| 416 | with patch.object(mirror, 'index', return_value={'0.17.0': release, 'master': {}}) as metadata, patch.object(mirror, 'fetch', side_effect=AssertionError('cached refresh must not redownload')): |
| 417 | self.cache.refresh() |
| 418 | self.assertNotIn('[Development builds]', readme.read_text()) |
| 419 | self.assertEqual(readme.read_text(), custom) |
| 420 | self.assertIn('[Language Reference](' + release['docs'] + ')', readme.read_text()) |
| 421 | self.assertIsNone(self.cache.directory_readme(artifact.parent)) |
| 422 | metadata.assert_any_call(refresh=True) |
| 423 | release['notes'] = 'https://ziglang.org/download/0.17.0/release-notes.html' |
| 424 | self.cache.refresh() |
| 425 | self.assertIn('[Release Notes](' + release['notes'] + ')', readme.read_text()) |
| 426 | self.assertIsNone(self.cache.directory_readme(artifact.parent)) |
| 427 | del release['docs'] |
| 428 | self.cache.refresh() |
| 429 | self.assertNotIn('Language Reference', readme.read_text()) |
| 430 | readme.write_text(readme.read_text() + '\nCustom table notes.\n') |
| 431 | mirror.set_mtime(readme, original[readme]) |
| 432 | self.assertIsNone(self.cache.directory_readme(artifact.parent)) |
| 433 | for path, timestamp in original.items(): |
| 434 | self.assertEqual(path.stat().st_mtime_ns, timestamp) |
| 435 | |
| 436 | def test_readme_covers_legacy_bootstrap_both_target_orders_and_dynamic_os(self): |
| 437 | names=['zig-0.1.1.tar.xz','zig-bootstrap-0.1.1.zip','zig-win64-0.1.1.zip', |
| 438 | 'zig-linux-aarch64-0.1.1.tar.xz','zig-x86_64-macos-0.1.1.tar.xz','zig-newarch-newos-0.1.1.zip'] |
| 439 | artifacts=[self.cache.path(name) for name in names] |
| 440 | for artifact in artifacts:artifact.parent.mkdir(parents=True,exist_ok=True);artifact.write_bytes(b'fixture') |
| 441 | text=mirror.version_readme('0.1.1',artifacts).decode() |
| 442 | for name in names:self.assertIn(name,text) |
| 443 | self.assertIn('Bootstrap',text);self.assertIn('newos',text);self.assertNotIn('&#128272;',text) |
| 444 | self.assertIn('role="region" aria-label="Zig downloads" tabindex="0"',text) |
| 445 | self.assertIn('>\n\n| Source ',text) |
| 446 | self.assertLess(text.index('</div>'),text.index('This historical release')) |
| 447 | self.assertNotIn('https://file.paperclover.net',text) |
| 448 | empty=mirror.version_readme('0.17.0',[],True).decode() |
| 449 | self.assertIn('[development builds]',empty);self.assertNotIn('| Linux',empty);self.assertNotIn('role="region"',empty) |
| 450 | |
| 451 | |
| 452 | if __name__ == '__main__': |
| 453 | unittest.main() |