CommonLibraries/tests/manifest_test.py

38 lines
1.4 KiB
Python
Raw Normal View History

import importlib.util
import json
import shutil
import sys
import tempfile
from pathlib import Path
spec = importlib.util.spec_from_file_location('artifacts', Path(__file__).parents[1] / 'scripts/artifacts.py')
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
source = Path(sys.argv[1])
with tempfile.TemporaryDirectory() as temp:
target = Path(temp) / 'isolated'
shutil.copytree(source, target)
module.verify(target)
path = target / 'runtime-manifest.json'
original = path.read_text()
for case in ('missing', 'tamper', 'abi', 'revision'):
victim = target / 'include/yolo_runtime.h'
saved = victim.read_bytes()
try:
if case == 'missing': victim.unlink()
elif case == 'tamper': victim.write_bytes(saved + b'changed')
elif case == 'abi':
manifest = json.loads(original)
manifest['abi'] = 999
path.write_text(json.dumps(manifest))
try:
module.verify(target, '0' * 64 if case == 'revision' else None)
except (ValueError, FileNotFoundError):
pass
else:
raise AssertionError('Invalid Runtime accepted: ' + case)
finally:
victim.write_bytes(saved)
path.write_text(original)
print('RUNTIME_MANIFEST_NEGATIVE_CHECKS_OK')