46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
import hashlib
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location('activate', Path(__file__).with_name('activate.py'))
|
|
activate = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(activate)
|
|
|
|
class BundleIntegrityTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.tmp.cleanup)
|
|
self.root = Path(self.tmp.name)
|
|
(self.root / 'repo').mkdir()
|
|
data = {'platform': 'bullseye\narm64\n', 'repo/Packages': 'Package: test\n',
|
|
'package-versions.txt': 'test 1 arm64', 'requested-packages.txt': 'test', 'repo/test.deb': 'content'}
|
|
lines = []
|
|
for name, value in data.items():
|
|
(self.root / name).write_text(value)
|
|
lines.append(hashlib.sha256(value.encode()).hexdigest() + ' ' + name)
|
|
(self.root / 'SHA256SUMS').write_text('\n'.join(lines))
|
|
|
|
def test_valid_bundle(self):
|
|
self.assertEqual(self.root, activate.verify(self.root))
|
|
|
|
def test_corrupt_deb_is_rejected(self):
|
|
(self.root / 'repo/test.deb').write_text('changed')
|
|
with self.assertRaisesRegex(ValueError, 'Checksum mismatch'):
|
|
activate.verify(self.root)
|
|
|
|
def test_unlisted_deb_is_rejected(self):
|
|
(self.root / 'repo/extra.deb').write_text('extra')
|
|
with self.assertRaisesRegex(ValueError, 'Unlisted package'):
|
|
activate.verify(self.root)
|
|
|
|
def test_outside_manifest_path_is_rejected(self):
|
|
with (self.root / 'SHA256SUMS').open('a') as f:
|
|
f.write('\n' + '0' * 64 + ' ../outside')
|
|
with self.assertRaisesRegex(ValueError, 'Unsafe manifest'):
|
|
activate.verify(self.root)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|