Download tests/test_expansion.py from Helinity/python-security-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 4.42 kB
-
https://huggingface.co/datasets/Helinity/python-security-benchmark/resolve/main/tests/test_expansion.py
- Command line
-
hf download hf://datasets/Helinity/python-security-benchmark/tests/test_expansion.py
-
curl -L -o test_expansion.py https://huggingface.co/datasets/Helinity/python-security-benchmark/resolve/main/tests/test_expansion.py
4.42 kB
| """Fail-closed checks for the versioned application-fixture release.""" | |
| from copy import deepcopy | |
| import json | |
| import pytest | |
| from research.expansion.release import catalog, eligible, build, DESTINATION, official_data | |
| def test_catalog_has_distinct_stable_ids_and_explicit_contracts(): | |
| rows = catalog() | |
| assert len(rows) == 70 | |
| assert len({r['sample_id'] for r in rows}) == 70 | |
| assert sum(r['hard_negative'] for r in rows) == 10 | |
| for row in rows: | |
| assert row['assumptions'] and row['threat_model'] and row['cwe_rationale'] | |
| assert row['source_type'] == 'synthetic_educational' | |
| assert len(row['code_files']) == 2 | |
| def test_eligibility_requires_both_observations_and_matching_code(): | |
| row = catalog()[0] | |
| evidence = {'code_sha256': row['code_sha256'], 'method': 'isolated_linux_runtime', | |
| 'normal_passed': True, 'adversarial_passed': True} | |
| assert eligible(row, evidence) | |
| for key in ('normal_passed', 'adversarial_passed'): | |
| changed = deepcopy(evidence) | |
| changed[key] = False | |
| assert not eligible(row, changed) | |
| assert not eligible(row, {**evidence, 'method': 'mock'}) | |
| assert not eligible(row, {**evidence, 'code_sha256': 'stale'}) | |
| assert not eligible(row, {}) | |
| def test_generated_release_matches_runtime_and_preserved_baselines(): | |
| artifacts = build() | |
| for name, content in artifacts.items(): | |
| assert (DESTINATION / name).read_bytes() == content, name | |
| def test_official_loader_refuses_missing_artifacts(tmp_path, monkeypatch): | |
| import research.expansion.release as release | |
| monkeypatch.setattr(release, 'DESTINATION', tmp_path) | |
| with pytest.raises(ValueError, match='Stale'): | |
| official_data('test') | |
| def test_all_runtime_contracts_and_isolation(): | |
| evidence = json.loads((DESTINATION / 'runtime.json').read_text()) | |
| assert len(evidence['results']) == 70 | |
| assert all(r['normal_passed'] and r['adversarial_passed'] for r in evidence['results'].values()) | |
| assert evidence['isolation']['interfaces'] == ['lo'] | |
| assert evidence['isolation']['uid'] == 65534 | |
| assert evidence['isolation']['root_read_only'] is True | |
| assert '--network=none' in evidence['restrictions'] | |
| assert not any('mount' in option or 'volume' in option for option in evidence['restrictions']) | |
| def test_split_integrity_and_all_exclusions(): | |
| notes = json.loads((DESTINATION / 'eligibility.json').read_text()) | |
| accepted = {r['sample_id'] for r in notes if r['eligible']} | |
| rows = [json.loads(line) for split in ('train', 'validation', 'test') | |
| for line in (DESTINATION / f'official/{split}.jsonl').read_text().splitlines()] | |
| assert len(rows) == len(accepted) == 66 | |
| assert {r['sample_id'] for r in rows} == accepted | |
| assert all(r['full_behavior_contract'] for r in rows) | |
| pairs = {} | |
| groups = {} | |
| for row in rows: | |
| if row['pair_id']: | |
| pairs.setdefault(row['pair_id'], []).append(row) | |
| groups.setdefault(row['leakage_group_id'], set()).add(row['split']) | |
| assert all({r['label'] for r in pair} == {'secure', 'insecure'} for pair in pairs.values()) | |
| assert all(len(splits) == 1 for splits in groups.values()) | |
| report = json.loads((DESTINATION / 'leakage.json').read_text()) | |
| assert report['cross_split_near_matches'] == 0 | |
| assert report['exact_ast'] == report['exact_text'] == [] | |
| def test_new_evaluator_scope_enforces_eligibility(monkeypatch, tmp_path): | |
| import research.evaluate as evaluator | |
| import research.expansion.release as release | |
| import sys | |
| monkeypatch.setattr(release, 'DESTINATION', tmp_path) | |
| monkeypatch.setattr(sys, 'argv', ['evaluate', '--scope', 'official_3c1', '--baseline']) | |
| with pytest.raises(SystemExit) as error: | |
| evaluator.main() | |
| assert error.value.code == 1 | |
| def test_official_loader_rejects_tampered_eligibility(tmp_path, monkeypatch): | |
| import shutil | |
| import research.expansion.release as release | |
| copied = tmp_path / 'release' | |
| shutil.copytree(DESTINATION, copied) | |
| path = copied / 'eligibility.json' | |
| notes = json.loads(path.read_text()) | |
| excluded = next(row for row in notes if not row['eligible']) | |
| excluded['eligible'] = True | |
| path.write_text(json.dumps(notes)) | |
| monkeypatch.setattr(release, 'DESTINATION', copied) | |
| with pytest.raises(ValueError, match='Stale Phase 3C.1 artifact: eligibility.json'): | |
| official_data('test') | |