#!/usr/bin/env python3 """Validate actual shipped schemas, catalog coverage, manifests and fixture provenance.""" from pathlib import Path import hashlib,json,sys,xml.etree.ElementTree as ET import jsonschema ROOT=Path(__file__).resolve().parents[1] def load(path):return json.loads((ROOT/path).read_text()) def check(value,schema):jsonschema.Draft7Validator(load('schemas/'+schema+'.schema.json'),format_checker=jsonschema.FormatChecker()).validate(value) for p in (ROOT/'schemas').glob('*.schema.json'):jsonschema.Draft7Validator.check_schema(json.loads(p.read_text())) check(load('data/sources.json'),'source') check(load('tests/fixtures/demo.inventory.json'),'inventory') for p in (ROOT/'data/checklists').glob('*.json'):check(json.loads(p.read_text()),'checklist') source_ids={s['id'] for s in load('data/sources.json')['sources']} assert len(source_ids)==len(load('data/sources.json')['sources']) original=next(s for s in load('data/sources.json')['sources'] if s['id']=='FSTEC_LINUX_2022') assert original['artifact_sha256'] is None and original['verification_state']=='pending_original' cards=load('data/hardening/fstec-linux-2022.catalog.json')['items'] expected={f'2.{section}.{number}' for section,count in enumerate([2,2,11,8,11,6],1) for number in range(1,count+1)} assert {c['paragraph'] for c in cards}==expected and len(cards)==40 for c in cards: assert set(c['source_refs'])<=source_ids assert c['mapping_state']=='UNKNOWN' and c['accepted_value'] is None assert c['manual_check_ru'] and c['reversibility_ru'] assert next(c for c in cards if c['paragraph']=='2.6.1')['recovery_class']=='REBOOT_REQUIRED' assert len({x['id'] for x in load('data/checklists/all.ru.json')['items']})==17 for task in load('resources/tasks/manifest.json')['tasks']: p=ROOT/'resources/tasks'/task['path'];assert task['sha256']==hashlib.sha256(p.read_bytes()).hexdigest() assert not task['mutating'] and task['parameters']=={} assert set(task['source_refs'])<=source_ids text=(ROOT/'resources/tasks/windows/ad-inventory.ps1').read_text() assert '-Properties $allowed' in text and '-ResultSetSize 50001' in text assert 'Start-Job' not in text and 'Register-ScheduledTask' not in text qrc=ET.parse(ROOT/'resources/resources.qrc') paths={e.attrib['alias']:(ROOT/'resources'/e.text).resolve() for e in qrc.iter('file')} for a in load('resources/asset-manifest.json')['assets']: assert a['path'] in paths and paths[a['path']].is_file() assert hashlib.sha256(paths[a['path']].read_bytes()).hexdigest()==a['sha256'],a['path'] for p in paths.values():assert p.is_file() for p in (ROOT/'tests/fixtures').glob('*.json'):assert json.loads(p.read_text())['synthetic'] is True # Validate schema rejection, not just self-consistency of accepted examples. demo=load('tests/fixtures/demo.inventory.json') for mutated in [{**demo,'schema_version':2},{**demo,'unexpected':1},{**demo,'synthetic':'true'}]: try:check(mutated,'inventory') except jsonschema.ValidationError:pass else:raise AssertionError('Invalid inventory accepted') print(f'PASS: schemas, {len(paths)} resources, {len(source_ids)} source references, 40 cards, 17 checklist instructions, task hashes and synthetic fixtures')