Datasets:
File size: 2,156 Bytes
0e239c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import hashlib
import json
from pathlib import Path
import tempfile
import unittest
import sys
sys.path.insert(0,str(Path(__file__).resolve().parent/'reusable_support'))
from batch05_runtime_resources import validate_runtime_descriptor,verify_runtime_files
class ResourceBindingTests(unittest.TestCase):
def descriptor(self,root):
data=b'{}\n';(root/'bundle-manifest.json').write_bytes(data)
h=hashlib.sha256(data).hexdigest()
return dict(kind='frozen-evaluator-dependencies',image_ref='example/repo@sha256:'+'1'*64,container_path='/crossswe-evaluator',manifest_sha256=h,files=[dict(path='bundle-manifest.json',kind='file',bytes=len(data),sha256=h)])
def test_rejects_tampered_or_extra_dependency_bytes(self):
with tempfile.TemporaryDirectory() as d:
root=Path(d);r=self.descriptor(root);verify_runtime_files(root,r)
(root/'bundle-manifest.json').write_bytes(b'bad')
with self.assertRaises(AssertionError):verify_runtime_files(root,r)
r=self.descriptor(root);(root/'extra').write_text('x')
with self.assertRaises(AssertionError):verify_runtime_files(root,r)
def test_rejects_path_escape_and_mutable_reference(self):
with tempfile.TemporaryDirectory() as d:
r=self.descriptor(Path(d));r['files'][0]['path']='../outside'
with self.assertRaises(AssertionError):validate_runtime_descriptor(r)
r=self.descriptor(Path(d));r['image_ref']='example/repo:latest'
with self.assertRaises(AssertionError):validate_runtime_descriptor(r)
def test_checks_symlink_metadata_without_reading_target(self):
with tempfile.TemporaryDirectory() as d:
root=Path(d);r=self.descriptor(root);target='/not-a-host-dependency'
(root/'link').symlink_to(target);r['files'].append(dict(path='link',kind='symlink',target=target,bytes=len(target),sha256=hashlib.sha256(target.encode()).hexdigest()))
verify_runtime_files(root,r)
(root/'link').unlink();(root/'link').symlink_to('/other')
with self.assertRaises(AssertionError):verify_runtime_files(root,r)
|