File size: 3,705 Bytes
ddf04fa | 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | """Offline reference-only cache regressions; does not change benchmark scoring.
Run: python tests/check_cyber_reference.py
Requires Python 3.12+ and git. Uses only the standard library and bundled fixtures.
"""
import argparse
from dataclasses import replace
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
import zipfile
ROOT = Path(__file__).resolve().parents[1]
TASK = ROOT / 'data/cyber/tasks/swe_rl_cybersecurity_task_02'
parser = argparse.ArgumentParser()
parser.add_argument('--patch', type=Path, default=TASK / 'gold.diff')
args = parser.parse_args()
class ReferenceCacheTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temp = tempfile.TemporaryDirectory()
cls.addClassCleanup(cls.temp.cleanup)
root = Path(cls.temp.name)
for archive in ['repository', 'hidden_tests']:
with zipfile.ZipFile(TASK / f'{archive}.zip') as z:
z.extractall(root)
subprocess.run(['git', 'apply', str(args.patch.resolve())],
cwd=root / 'repository', check=True)
sys.path[:0] = [str(root / 'repository'), str(root / 'hidden_tests')]
import test_critical_extensions_and_security_edge_cases
cls.t = test_critical_extensions_and_security_edge_cases
def check_policy_change(self, flag):
t = self.t
store = t.TrustStore()
rp, rs = t.generate_key_pair('cache-review-root')
cp, cs = t.generate_key_pair('cache-review-ca')
lp, _ = t.generate_key_pair('cache-review-leaf')
policies = lambda oid: t.CertificatePoliciesExtension([t.CertificatePolicy(oid)])
root = t._make_ca('r', 'CN=Root', 'CN=Root', rp, rs,
policies=policies(t.POLICY_CORP), is_root=True)
store.add_trust_anchor(root)
mapping = flag == 'initial_policy_mapping_inhibit'
ca = t._make_ca('c', 'CN=CA', 'CN=Root', cp, rs, rp,
policies=policies(t.POLICY_CORP if mapping else t.ANY_POLICY),
mappings=t.PolicyMappingsExtension([
t.PolicyMapping(t.POLICY_CORP, t.POLICY_PARTNER_A)
], critical=True) if mapping else None)
store.add_intermediate(ca)
leaf = t._make_leaf('l', 'CN=leaf', 'CN=CA', lp, cs, cp,
policies=policies(t.POLICY_PARTNER_A if mapping else t.POLICY_CORP))
config = t.ValidationConfig(current_time='2026-06-01T00:00:00Z',
enforce_policy_tree=True, initial_explicit_policy=True,
initial_policy_set={t.POLICY_CORP}, check_revocation=False)
strict = replace(config, **{flag: True})
engine = t.PathValidationEngine(store)
self.assertTrue(engine.validate(leaf, config).is_valid)
self.assertFalse(t.PathValidationEngine(store).validate(leaf, strict).is_valid)
self.assertFalse(engine.validate(leaf, strict).is_valid,
'A stricter policy must not reuse a permissive cached decision')
engine = t.PathValidationEngine(store)
self.assertFalse(engine.validate(leaf, strict).is_valid)
self.assertTrue(engine.validate(leaf, config).is_valid,
'A permissive policy must not reuse a stricter cached decision')
def test_mapping_inhibition_is_in_cache_identity(self):
self.check_policy_change('initial_policy_mapping_inhibit')
def test_any_policy_inhibition_is_in_cache_identity(self):
self.check_policy_change('initial_any_policy_inhibit')
if __name__ == '__main__':
unittest.main(argv=[sys.argv[0]], verbosity=2)
|