"""Release channel isolation and full update validation regression tests.""" import base64 from pathlib import Path import plistlib import subprocess import tempfile import unittest from unittest.mock import patch from macos_updates import configure, validate_url, publish, SPARKLE class UpdateConfigurationTests(unittest.TestCase): def test_production_rejects_local_feed(self): with self.assertRaises(ValueError): validate_url('http://localhost:8765/appcast.xml') with self.assertRaises(ValueError): validate_url('http://example.com/appcast.xml', local=True) with self.assertRaises(ValueError): validate_url('https://user:password@example.com/appcast.xml') def test_local_configuration_is_scoped_to_development(self): with tempfile.TemporaryDirectory() as directory: app = Path(directory) / 'cutdex.app' (app / 'Contents').mkdir(parents=True) path = app / 'Contents/Info.plist' key = base64.b64encode(bytes(range(32))).decode() original = {'CFBundleIdentifier': 'app.cutdex.desktop'} path.write_bytes(plistlib.dumps(original)) with self.assertRaises(ValueError): configure(app, 'http://localhost:8765/appcast.xml', key, local=True) self.assertEqual(plistlib.loads(path.read_bytes()), original) original['CFBundleIdentifier'] += '.dev' path.write_bytes(plistlib.dumps(original)) configure(app, 'http://localhost:8765/appcast.xml', key, local=True) info = plistlib.loads(path.read_bytes()) self.assertFalse(info['SUAllowsAutomaticUpdates']) self.assertFalse(info['SUEnableAutomaticChecks']) self.assertNotIn('NSAllowsArbitraryLoads', info['NSAppTransportSecurity']) self.assertEqual(set(info['NSAppTransportSecurity']['NSExceptionDomains']), {'localhost'}) def test_invalid_public_key_does_not_change_app(self): with tempfile.TemporaryDirectory() as directory: app = Path(directory) (app / 'Contents').mkdir() path = app / 'Contents/Info.plist' original = plistlib.dumps({'CFBundleIdentifier': 'app.cutdex.desktop'}) path.write_bytes(original) with self.assertRaises(ValueError): configure(app, 'https://updates.example.com/appcast.xml', 'YQ==') self.assertEqual(path.read_bytes(), original) class PublicationVersionTests(unittest.TestCase): def test_publication_writes_signed_archive_and_preserves_feed_on_tool_failure(self): for failure in (False, True): with self.subTest(failure=failure), tempfile.TemporaryDirectory() as directory: output = Path(directory) feed = output / 'appcast.xml' original = (f'' '1' '').encode() feed.write_bytes(original) generated = (f'' '2' '' '').encode() def generate(command, *, cwd, check): self.assertEqual(cwd, Path.cwd()) self.assertTrue(check) if command[0] == 'ditto': Path(command[-1]).write_bytes(b'archive') elif failure: raise subprocess.CalledProcessError(19, command) else: Path(command[command.index('-o') + 1]).write_bytes(generated) with patch('macos_updates.validate_app', return_value={'CFBundleVersion': '2'}), \ patch('macos_updates.run', side_effect=generate): if failure: with self.assertRaises(subprocess.CalledProcessError) as raised: publish(Path('app'), output, 'https://example.com/', Path('tools')) self.assertEqual(raised.exception.returncode, 19) self.assertEqual(feed.read_bytes(), original) self.assertEqual(list(output.glob('*.zip')), []) else: archive = publish(Path('app'), output, 'https://example.com/', Path('tools')) self.assertEqual(archive.read_bytes(), b'archive') self.assertEqual(feed.read_bytes(), generated) self.assertFalse((output / 'appcast.xml.pending').exists()) def test_older_or_equivalent_build_cannot_replace_current_feed(self): for current, candidate in [('900002', '900001'), ('2', '2.0'), ('2.10', '2.9')]: with self.subTest(current=current, candidate=candidate), tempfile.TemporaryDirectory() as directory: output = Path(directory) feed = output / 'appcast.xml' original = (f'' f'{current}' '').encode() feed.write_bytes(original) def generate(command, **kwargs): if command[0] == 'ditto': Path(command[-1]).write_bytes(b'archive') else: Path(command[command.index('-o') + 1]).write_text( f'' f'{candidate}' '' '') with patch('macos_updates.validate_app', return_value={'CFBundleVersion': candidate}), \ patch('macos_updates.run', side_effect=generate) as run: with self.assertRaisesRegex(ValueError, 'newer'): publish(Path('app'), output, 'https://example.com/', Path('tools')) run.assert_not_called() self.assertEqual(feed.read_bytes(), original) self.assertEqual(list(output.glob('*.zip')), []) if __name__ == '__main__': unittest.main()