Datasets:
Download scripts/test_materialize_context_v2.py from CrossSWE/CrossSWE-Bench: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/datasets/CrossSWE/CrossSWE-Bench/resolve/main/scripts/test_materialize_context_v2.py
- Command line
-
hf download hf://datasets/CrossSWE/CrossSWE-Bench/scripts/test_materialize_context_v2.py
-
curl -L -o test_materialize_context_v2.py https://huggingface.co/datasets/CrossSWE/CrossSWE-Bench/resolve/main/scripts/test_materialize_context_v2.py
3.87 kB
| import hashlib | |
| import io | |
| import json | |
| import os | |
| from pathlib import Path | |
| import tarfile | |
| import tempfile | |
| import unittest | |
| from materialize_agent_context import materialize | |
| def fixture(root,links=(),parts=False): | |
| archive=root/'context.tar.gz';entries=[] | |
| with tarfile.open(archive,'w:gz') as tar: | |
| body=b'original upstream instructions\n';name='testbed/repo/AGENTS.md' | |
| info=tarfile.TarInfo(name);info.size=len(body);info.mode=0o644;tar.addfile(info,io.BytesIO(body)) | |
| entries.append({'path':name,'bytes':len(body),'sha256':hashlib.sha256(body).hexdigest(),'mode':0o644}) | |
| for name,target in links: | |
| info=tarfile.TarInfo(name);info.type=tarfile.SYMTYPE;info.linkname=target;info.mode=0o777;tar.addfile(info) | |
| data=target.encode();entries.append({'path':name,'kind':'symlink','target':target,'bytes':len(data),'sha256':hashlib.sha256(data).hexdigest(),'mode':0o777}) | |
| raw=archive.read_bytes();reference={'path':archive.name,'bytes':len(raw),'sha256':hashlib.sha256(raw).hexdigest()} | |
| if parts: | |
| offset=0;reference['parts']=[] | |
| for index,block in enumerate([raw[:len(raw)//2],raw[len(raw)//2:]]): | |
| path=root/f'context.tar.gz.part-{index:04d}';path.write_bytes(block) | |
| reference['parts'].append({'path':path.name,'offset':offset,'bytes':len(block),'sha256':hashlib.sha256(block).hexdigest()});offset+=len(block) | |
| manifest=root/'context-manifest.json';manifest.write_text(json.dumps({'archive':reference,'files':entries})) | |
| return manifest | |
| class ContextV2Tests(unittest.TestCase): | |
| def test_internal_upstream_link_remains_a_link(self): | |
| with tempfile.TemporaryDirectory() as temporary: | |
| root=Path(temporary);manifest=fixture(root,[('testbed/repo/CLAUDE.md','AGENTS.md')]) | |
| try:materialize(manifest,root/'out') | |
| except ValueError as error:self.fail('Valid original source symlink rejected: '+str(error)) | |
| path=root/'out/testbed/repo/CLAUDE.md' | |
| self.assertTrue(path.is_symlink());self.assertEqual(os.readlink(path),'AGENTS.md') | |
| def test_escape_link_is_rejected(self): | |
| with tempfile.TemporaryDirectory() as temporary: | |
| root=Path(temporary);manifest=fixture(root,[('escape','../outside')]) | |
| with self.assertRaises(ValueError):materialize(manifest,root/'out') | |
| def test_indirect_escape_via_another_link_is_rejected(self): | |
| with tempfile.TemporaryDirectory() as temporary: | |
| root=Path(temporary);manifest=fixture(root,[('d1/a','..'),('d2/b','../d1/a/../outside')]) | |
| with self.assertRaises(ValueError):materialize(manifest,root/'out') | |
| self.assertFalse((root/'out').exists()) | |
| def test_archive_cannot_write_through_a_link_parent(self): | |
| with tempfile.TemporaryDirectory() as temporary: | |
| root=Path(temporary);manifest=fixture(root,[('testbed','inside')]) | |
| with self.assertRaises(ValueError):materialize(manifest,root/'out') | |
| def test_parts_are_sufficient_without_full_archive(self): | |
| with tempfile.TemporaryDirectory() as temporary: | |
| root=Path(temporary);manifest=fixture(root,parts=True);(root/'context.tar.gz').unlink() | |
| try:materialize(manifest,root/'out') | |
| except (OSError,ValueError) as error:self.fail('Closed archive parts were not restored: '+str(error)) | |
| self.assertTrue((root/'out/testbed/repo/AGENTS.md').is_file()) | |
| def test_part_tampering_cannot_be_ignored_when_full_archive_exists(self): | |
| with tempfile.TemporaryDirectory() as temporary: | |
| root=Path(temporary);manifest=fixture(root,parts=True) | |
| with (root/'context.tar.gz.part-0000').open('ab') as stream:stream.write(b'changed') | |
| with self.assertRaises(ValueError):materialize(manifest,root/'out') | |
| if __name__=='__main__':unittest.main() | |