import hashlib import importlib.util import json from pathlib import Path from unittest.mock import patch import tempfile import unittest from windows_updates import SDK_VERSION, check_tool, resolved_deskup, numeric_version, pack_release, update_version, validate_previous, verify_manifest, verify_sdk_version class UpdatePublicationTests(unittest.TestCase): def test_packaging_module_imports_in_clean_checkout_without_flutter_config(self): spec = importlib.util.spec_from_file_location('isolated_windows_updates', Path(__file__).with_name('windows_updates.py')) module = importlib.util.module_from_spec(spec) with patch.object(Path, 'is_file', return_value=False): spec.loader.exec_module(module) self.assertEqual(module.SDK_VERSION, SDK_VERSION) def test_packaging_tool_check_does_not_resolve_flutter_dependencies(self): from subprocess import CompletedProcess with patch('windows_updates.resolved_deskup', side_effect=AssertionError('No Flutter config')), \ patch('windows_updates.run', return_value=CompletedProcess([], 0, f'vpk {SDK_VERSION}', '')): check_tool(['vpk']) def test_build_sdk_check_rejects_mismatched_and_missing_versions(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) cmake = root / 'windows/velopack.cmake' cmake.parent.mkdir() with patch('windows_updates.resolved_deskup', return_value=root): for contents in ('set(DESKUP_VELOPACK_VERSION "9.9.9")', ''): cmake.write_text(contents) with self.assertRaisesRegex(ValueError, 'Resolved deskup must use'): verify_sdk_version() cmake.write_text(f'set(DESKUP_VELOPACK_VERSION "{SDK_VERSION}")') self.assertEqual(verify_sdk_version(), SDK_VERSION) def test_deskup_is_located_from_resolved_package_config(self): with tempfile.TemporaryDirectory(prefix='deskup package ') as directory: root = Path(directory) config = root / '.dart_tool/package_config.json' config.parent.mkdir() package = root / 'cache/deskup-0.1.0' for uri in ['../cache/deskup-0.1.0/', package.as_uri() + '/']: config.write_text(json.dumps({'packages': [ {'name': 'deskup', 'rootUri': uri}, ]}), encoding='utf-8') with patch('windows_updates.ROOT', root): self.assertEqual(resolved_deskup().resolve(), package.resolve()) def test_missing_package_config_requests_pub_get(self): with tempfile.TemporaryDirectory() as directory: with patch('windows_updates.ROOT', Path(directory)): with self.assertRaisesRegex(ValueError, 'flutter_local.dart pub get'): resolved_deskup() def test_display_version_conversion_is_shared_and_ordered(self): self.assertEqual(update_version('1.2.3', '42'), '1.2.3-build.42') self.assertEqual(update_version('1.2.3', '0042'), '1.2.3-build.42') for version, build in [('1.2', 1), ('1.2.3', -1), ('1.2.3', 'beta')]: with self.assertRaises(ValueError): update_version(version, build) def test_invalid_pack_version_never_starts_vpk(self): with patch('windows_updates.run') as run: with self.assertRaises(ValueError): pack_release(['vpk'], app=Path('app'), output=Path('release'), package_id='Cutdex', version='1.0.0.42', title='Cutdex') run.assert_not_called() def test_build_number_participates_in_version_order(self): self.assertLess(numeric_version('1.0.0-build.1'), numeric_version('1.0.0-build.2')) self.assertLess(numeric_version('1.0.0-build.9'), numeric_version('1.0.0-build.10')) self.assertLess(numeric_version('1.0.0-build.999'), numeric_version('1.0.1-build.1')) self.assertLess(numeric_version('1.0.0-build.999'), numeric_version('1.0.0')) for version in ['1.0', '1.0.0+2', '1.0.0.2', '1.0.0-build.02', '1.0.0-beta', '../1.0.0']: with self.assertRaises(ValueError): numeric_version(version) def test_reused_build_and_mixed_identity_are_rejected(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / 'releases.win-x64.json').write_text(json.dumps({ 'Assets': [{'PackageId': 'Cutdex', 'Version': '1.0.0-build.2'}]})) for candidate in ['1.0.0-build.1', '1.0.0-build.2']: with self.assertRaises(ValueError): validate_previous(root, 'Cutdex', candidate) with self.assertRaises(ValueError): validate_previous(root, 'Cutdex.Dev', '1.0.0-build.3') validate_previous(root, 'Cutdex', '1.0.0-build.3') def test_post_audit_tampering_and_added_files_are_rejected(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / 'cutdex.exe').write_bytes(b'app') (root / 'processing').mkdir() (root / 'processing/checksums.json').write_text('{}') manifest = {name: hashlib.sha256((root / name).read_bytes()).hexdigest() for name in ['cutdex.exe', 'processing/checksums.json']} (root / 'checksums.json').write_text(json.dumps(manifest)) verify_manifest(root) (root / 'cutdex.exe').write_bytes(b'changed') with self.assertRaises(ValueError): verify_manifest(root) (root / 'cutdex.exe').write_bytes(b'app') (root / 'injected.dll').write_bytes(b'new') with self.assertRaises(ValueError): verify_manifest(root) def test_escaping_manifest_path_is_rejected(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) (root / 'checksums.json').write_text(json.dumps({'../outside.exe': '0' * 64})) with self.assertRaises(ValueError): verify_manifest(root) def test_relative_staging_directory_is_normalized(self): with tempfile.TemporaryDirectory(dir=Path.cwd()) as directory: root = Path(directory).relative_to(Path.cwd()) (root / 'cutdex.exe').write_bytes(b'app') (root / 'checksums.json').write_text(json.dumps({ 'cutdex.exe': hashlib.sha256(b'app').hexdigest(), })) verify_manifest(root) def test_symlink_inside_staging_directory_is_rejected(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) original = root / 'cutdex.exe' original.write_bytes(b'app') try: (root / 'linked.exe').symlink_to(original) except OSError as error: if getattr(error, 'winerror', None) != 1314: raise self.skipTest(f'Symlinks unavailable: {error}') digest = hashlib.sha256(b'app').hexdigest() (root / 'checksums.json').write_text(json.dumps({ 'cutdex.exe': digest, 'linked.exe': digest, })) with self.assertRaisesRegex(ValueError, 'Invalid manifest path'): verify_manifest(root) if __name__ == '__main__': unittest.main()