SelfRising/tests/test_materialize.py

61 lines
2.6 KiB
Python

import gzip
import hashlib
import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
spec = importlib.util.spec_from_file_location('materialize', Path(__file__).resolve().parents[1] / 'scripts/materialize_reproduction.py')
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
class MaterializeTests(unittest.TestCase):
def fixture(self, root):
source = root / 'source'
(source / 'v3').mkdir(parents=True)
entries = []
for name, compressed in [('a.txt', False), ('nested/trace.json', True)]:
data = (name + '\n').encode()
stored = name + ('.gz' if compressed else '')
path = source / 'v3' / stored
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(gzip.compress(data) if compressed else data)
entries.append(dict(path=name, stored=stored, gzip=compressed, size=len(data), sha256=hashlib.sha256(data).hexdigest()))
(source / 'PACKING.json').write_text(json.dumps({'files': entries}))
return source, entries
def test_restore_exact_bytes_and_refuse_existing_destination(self):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
source, _ = self.fixture(root)
output = root / 'output'
self.assertEqual(module.materialize(source, output), 2)
self.assertEqual((output / 'nested/trace.json').read_bytes(), b'nested/trace.json\n')
with self.assertRaises(FileExistsError):
module.materialize(source, output)
def test_corruption_does_not_leave_partial_output(self):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
source, _ = self.fixture(root)
(source / 'v3/a.txt').write_text('corrupted')
with self.assertRaises(ValueError):
module.materialize(source, root / 'output')
self.assertFalse((root / 'output').exists())
def test_traversal_and_duplicate_names_rejected(self):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
source, entries = self.fixture(root)
for invalid in [entries + [entries[0]], [dict(entries[0], path='../escape', stored='../escape')]]:
(source / 'PACKING.json').write_text(json.dumps({'files': invalid}))
with self.assertRaises(ValueError):
module.materialize(source, root / 'output')
self.assertFalse((root / 'escape').exists())
if __name__ == '__main__':
unittest.main()