"""Host tests for the Issue #182 CD_RESET contract claim adjudication rules (pure functions). `tools.contracts.adjudicate_claim` is deterministic and side-effect free, so synthetic claim dicts are fed directly. No emulator, no ROM, no reference BIOS execution is involved. VERIFIED is outside CLAIM_STATUSES and must never be reachable: an unexecuted callee can not be promoted by these rules. Issue #208 strengthens OBSERVED: a source reference (source code URL/revision) only proves that source text exists; it is not execution evidence. OBSERVED promotion now additionally requires non-empty, format-valid evidence entries (FORMAT_ONLY; not authenticated) (execution conditions, core revision, input type, verification result, sha256 evidence hash). `verification.executed_callee` is part of the evidence and is not sufficient by itself. """ import hashlib import sys import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / 'tools')) from contracts import ( # noqa: E402 CALLEE_BEHAVIOR_FIELDS, CLAIM_STATUSES, EVIDENCE_TRUST_LEVEL, FIELDS, HARDWARE_OBSERVATION_FIELDS, SOURCE_KIND_MEANINGS, adjudicate_claim, load_contracts, resolve_evidence, ) CALLER = {'huc:include/hucc/pcengine.inc': 'CALLER_SOURCE'} HLE = {'hugo:bios.c': 'HLE_SOURCE'} REAL = {'gear:src/cdrom.cpp': 'EMULATOR_SOURCE'} UNKNOWN = {'status': 'UNKNOWN', 'value': 'UNKNOWN', 'sources': [], 'confidence': 'UNKNOWN'} HYPOTHESIS = { 'status': 'HYPOTHESIS', 'value': 'HuCC caller-side expectation only', 'sources': ['huc:include/hucc/pcengine.inc'], 'confidence': 'SOURCE_SUPPORTED', } # SYNTHETIC evidence: well-formed in shape only. It is NOT a real measurement, does not # support any particular contract field, and its hash resolves to nothing. EVIDENCE_ENTRY = { 'execution_conditions': 'SYNTHETIC test fixture; no real execution took place', 'core_revision': 'b49ae82a012eade566d72e9d61c9d95a5caa4da3', 'input_type': 'SYNTHETIC placeholder input', 'verification_result': 'SYNTHETIC placeholder result', 'evidence_hash': 'd1a2f529224a70666ede15c4cf774da484bf1b96495a53e08d474f09f8ff5a50', } OBSERVED = { 'status': 'OBSERVED', 'value': 'SYNTHETIC observed value (not a measurement)', 'sources': ['gear:src/cdrom.cpp'], 'confidence': 'SOURCE_SUPPORTED', 'evidence': [EVIDENCE_ENTRY], } # Issue #225: an OBSERVED claim must declare observation_kind covering its field. OBSERVED_CALLEE = dict(OBSERVED, observation_kind='CALLEE_EXECUTION') OBSERVED_HW = dict(OBSERVED, observation_kind='HW_BUS_READBACK') BLOCKED = {'status': 'BLOCKED', 'value': 'UNKNOWN', 'sources': [], 'confidence': 'UNKNOWN'} # Real-core provenance fixture (Issue #359 fix): no SYNTHETIC marker, and its hash is in REAL_RESOLVED, # which stands for resolve_evidence() having accepted the artifact. test_real_core_artifact_resolves_and_yields_observed # exercises that path end to end; the other positive tests pass REAL_RESOLVED directly. REAL_ARTIFACT_BYTES = b'real-core cd-read probe artifact (test fixture bytes)\n' REAL_ENTRY = { 'execution_conditions': 'REAL-CORE cd-read probe run on the pinned Geargrafx core', 'core_revision': 'b49ae82a012eade566d72e9d61c9d95a5caa4da3', 'input_type': 'cd-read probe sector', 'verification_result': 'probe observation matches the stored artifact', 'evidence_hash': hashlib.sha256(REAL_ARTIFACT_BYTES).hexdigest(), } REAL_RESOLVED = frozenset({REAL_ENTRY['evidence_hash']}) REAL_OBSERVED = dict(OBSERVED, value='real-core observed value', evidence=[REAL_ENTRY]) REAL_OBSERVED_CALLEE = dict(REAL_OBSERVED, observation_kind='CALLEE_EXECUTION') REAL_OBSERVED_HW = dict(REAL_OBSERVED, observation_kind='HW_BUS_READBACK') class AdjudicateClaimTests(unittest.TestCase): def test_categories_are_only_the_four_and_verified_is_absent(self): self.assertEqual(CLAIM_STATUSES, {'UNKNOWN', 'HYPOTHESIS', 'OBSERVED', 'BLOCKED'}) self.assertNotIn('VERIFIED', CLAIM_STATUSES) def test_unknown_claim_is_accepted(self): adjudicate_claim(UNKNOWN, CALLER, field='flags', context='slot 1 flags') def test_hypothesis_claim_is_accepted(self): adjudicate_claim(HYPOTHESIS, CALLER, field='arguments', context='slot 1 arguments') def test_real_core_hash_match_is_refused_without_producer_authentication(self): # Issue #359 / #124: non-synthetic evidence whose hash matches is still refused. Nothing # authenticates the executing principal, so a hash match or a REALCORE string is not a measurement. with self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(REAL_OBSERVED_CALLEE, REAL, field='stack', context='slot 1 stack', executed_callee=True, resolved_evidence_hashes=REAL_RESOLVED) def test_synthetic_evidence_with_executed_callee_is_never_observed(self): # Issue #359 MAJOR: SYNTHETIC evidence + executed_callee=True must NOT yield OBSERVED, with or # without a resolved set (the synthetic text is refused even if its hash were listed). synthetic = dict(OBSERVED_CALLEE) with self.assertRaisesRegex(ValueError, 'SYNTHETIC'): adjudicate_claim(synthetic, REAL, field='stack', context='slot 1 stack', executed_callee=True) with self.assertRaisesRegex(ValueError, 'SYNTHETIC'): adjudicate_claim(synthetic, REAL, field='stack', context='slot 1 stack', executed_callee=True, resolved_evidence_hashes=frozenset({EVIDENCE_ENTRY['evidence_hash']})) hw = dict(OBSERVED_HW, value='$1804 readback') with self.assertRaisesRegex(ValueError, 'SYNTHETIC'): adjudicate_claim(hw, REAL, field='hw_bus_reset', context='hw_bus_reset', executed_callee=False) def test_unresolved_real_looking_evidence_is_refused_for_observed(self): # Non-synthetic text alone is not provenance: the hash must be resolved by resolve_evidence(). with self.assertRaisesRegex(ValueError, 'requires real-core evidence'): adjudicate_claim(REAL_OBSERVED_CALLEE, REAL, field='stack', context='slot 1 stack', executed_callee=True) def test_real_core_artifact_resolves_but_observed_is_still_refused(self): # End to end: artifact bytes on disk -> resolve_evidence (pinned revision + sha256) resolves, yet # OBSERVED stays refused: hash-and-pin consistency is not producer authentication (#124). with tempfile.TemporaryDirectory() as tmp: (Path(tmp) / REAL_ENTRY['evidence_hash']).write_bytes(REAL_ARTIFACT_BYTES) resolved = frozenset({REAL_ENTRY['evidence_hash']}) if resolve_evidence( REAL_ENTRY, artifact_dir=tmp, pinned_revision=REAL_ENTRY['core_revision']) else frozenset() self.assertEqual(resolved, REAL_RESOLVED) with self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(REAL_OBSERVED_CALLEE, REAL, field='stack', context='slot 1 stack', executed_callee=True, resolved_evidence_hashes=resolved) def test_blocked_claim_is_accepted(self): adjudicate_claim(BLOCKED, CALLER, field='stack', context='slot 1 stack') def test_verified_status_is_rejected_illegal_promotion(self): bad = dict(HYPOTHESIS, status='VERIFIED') with self.assertRaisesRegex(ValueError, 'VERIFIED is not represented'): adjudicate_claim(bad, CALLER, field='returns', context='slot 1 returns') def test_unknown_status_string_is_rejected(self): bad = dict(UNKNOWN, status='PROMISED') with self.assertRaisesRegex(ValueError, 'unsupported claim status'): adjudicate_claim(bad, CALLER, field='flags', context='slot 1 flags') def test_missing_status_is_rejected(self): bad = {k: v for k, v in UNKNOWN.items() if k != 'status'} with self.assertRaisesRegex(ValueError, 'unsupported claim status'): adjudicate_claim(bad, CALLER, field='flags', context='slot 1 flags') def test_hypothesis_requires_sources(self): bad = dict(HYPOTHESIS, sources=[]) with self.assertRaisesRegex(ValueError, 'invalid HYPOTHESIS claim'): adjudicate_claim(bad, CALLER, field='arguments', context='slot 1 arguments') def test_hypothesis_rejects_unknown_value_and_wrong_confidence(self): for bad in (dict(HYPOTHESIS, value='UNKNOWN'), dict(HYPOTHESIS, confidence='UNKNOWN')): with self.assertRaisesRegex(ValueError, 'invalid HYPOTHESIS claim'): adjudicate_claim(bad, CALLER, field='arguments', context='slot 1 arguments') def test_hypothesis_caller_source_accepted(self): # CALLER_SOURCE + HYPOTHESIS = caller 側の期待のみ。callee の実行は保証されない。 adjudicate_claim(HYPOTHESIS, CALLER, field='arguments', context='slot 1 arguments') def test_hypothesis_hle_source_accepted(self): # HLE_SOURCE + HYPOTHESIS = HLE エミュレータ挙動のみ。実機ファームウェアの証拠ではない。 hle_hypothesis = { 'status': 'HYPOTHESIS', 'value': 'Hu-Go HLE writes $222D as disc-present marker', 'sources': ['hugo:bios.c'], 'confidence': 'SOURCE_SUPPORTED', } adjudicate_claim(hle_hypothesis, HLE, field='ram', context='slot 1 ram') def test_hypothesis_emulator_source_accepted(self): # EMULATOR_SOURCE + HYPOTHESIS = 実コアのソース参照のみ。実行証跡ではなく callee ABI/実行は未確認。 # callee execution なしでは OBSERVED に昇格しない(callee ABI は HYPOTHESIS のまま)。 emulator_hypothesis = { 'status': 'HYPOTHESIS', 'value': 'Geargrafx core source suggests $1804 readback', 'sources': ['gear:src/cdrom.cpp'], 'confidence': 'SOURCE_SUPPORTED', } adjudicate_claim(emulator_hypothesis, REAL, field='stack', context='slot 1 stack') def test_emulator_source_meaning_does_not_claim_execution(self): self.assertIn('実行証跡ではなく', SOURCE_KIND_MEANINGS['EMULATOR_SOURCE']) self.assertNotIn('実測', SOURCE_KIND_MEANINGS['EMULATOR_SOURCE']) def test_source_kind_meanings_document_all_three_kinds(self): self.assertEqual(set(SOURCE_KIND_MEANINGS), {'CALLER_SOURCE', 'HLE_SOURCE', 'EMULATOR_SOURCE'}) for meaning in SOURCE_KIND_MEANINGS.values(): self.assertIsInstance(meaning, str) self.assertTrue(meaning) def test_observed_rejected_for_caller_source_only(self): # CALLER_SOURCE だけでは OBSERVED になれない(callee 実行を保証しない)。 bad = dict(OBSERVED, sources=['huc:include/hucc/pcengine.inc']) with self.assertRaisesRegex(ValueError, 'EMULATOR_SOURCE source reference'): adjudicate_claim(bad, CALLER, field='stack', context='slot 1 stack') def test_observed_rejected_for_hle_source_only(self): # HLE_SOURCE だけでは OBSERVED になれない(実機ファームウェアの証拠ではない)。 bad = dict(OBSERVED, sources=['hugo:bios.c']) with self.assertRaisesRegex(ValueError, 'EMULATOR_SOURCE source reference'): adjudicate_claim(bad, HLE, field='stack', context='slot 1 stack') def test_emulator_source_hypothesis_not_promotable_to_observed_without_executed_callee(self): # EMULATOR_SOURCE + HYPOTHESIS は callee ABI を HYPOTHESIS のまま保つ。 # callee execution なしでは OBSERVED へ昇格しない。 with self.assertRaisesRegex(ValueError, 'executed callee'): adjudicate_claim(OBSERVED, REAL, field='returns', context='slot 1 returns') def test_observed_requires_emulator_source(self): # HLE/caller text alone can never be OBSERVED: it is not an emulator-source reference. bad = dict(OBSERVED, sources=['huc:include/hucc/pcengine.inc']) with self.assertRaisesRegex(ValueError, 'EMULATOR_SOURCE source reference'): adjudicate_claim(bad, CALLER, field='stack', context='slot 1 stack', executed_callee=True) def test_observed_requires_non_empty_evidence(self): # Issue #208 negative case: an OBSERVED claim without evidence entries is rejected # even with an EMULATOR_SOURCE and an executed callee. bad = dict(OBSERVED, evidence=[]) with self.assertRaisesRegex(ValueError, 'non-empty evidence entries'): adjudicate_claim(bad, REAL, field='stack', context='slot 1 stack', executed_callee=True) def test_observed_without_evidence_key_is_rejected(self): bad = {k: v for k, v in OBSERVED.items() if k != 'evidence'} with self.assertRaisesRegex(ValueError, 'non-empty evidence entries'): adjudicate_claim(bad, REAL, field='stack', context='slot 1 stack', executed_callee=True) def test_observed_with_invalid_evidence_hash_is_rejected(self): bad = dict(OBSERVED, evidence=[dict(EVIDENCE_ENTRY, evidence_hash='not-a-sha256')]) with self.assertRaisesRegex(ValueError, 'sha256'): adjudicate_claim(bad, REAL, field='stack', context='slot 1 stack', executed_callee=True) def test_observed_with_incomplete_evidence_entry_is_rejected(self): incomplete = dict(EVIDENCE_ENTRY) del incomplete['verification_result'] bad = dict(OBSERVED, evidence=[incomplete]) with self.assertRaisesRegex(ValueError, 'evidence entry must contain exactly'): adjudicate_claim(bad, REAL, field='stack', context='slot 1 stack', executed_callee=True) def test_executed_callee_alone_without_evidence_is_insufficient(self): # Issue #208: executed_callee self-attestation is part of the evidence, not sufficient. bad = {k: v for k, v in OBSERVED.items() if k != 'evidence'} with self.assertRaisesRegex(ValueError, 'non-empty evidence entries'): adjudicate_claim(bad, REAL, field='stack', context='slot 1 stack', executed_callee=True) def test_observed_callee_field_rejected_without_executed_callee(self): # Callee ABI behavior (returns) may not be OBSERVED without an executed callee. with self.assertRaisesRegex(ValueError, 'executed callee'): adjudicate_claim(OBSERVED, REAL, field='returns', context='slot 1 returns') def test_observed_callee_field_refused_even_when_callee_executed(self): with self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(REAL_OBSERVED_CALLEE, REAL, field='returns', context='slot 1 returns', executed_callee=True, resolved_evidence_hashes=REAL_RESOLVED) def test_observed_hardware_observation_accepted_without_executed_callee(self): # hw_bus_reset is a low-level $1804 register/bus fact (hardware observation), # not callee ABI behavior: OBSERVED with an EMULATOR_SOURCE reference plus real-core evidence must be # accepted even though no callee was executed. claim = dict(REAL_OBSERVED_HW, value='$1804 bit 1: assert -> readback $02, release -> $00, bus FREE') with self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(claim, REAL, field='hw_bus_reset', context='slot 1 hw_bus_reset', executed_callee=False, resolved_evidence_hashes=REAL_RESOLVED) def test_observed_hardware_observation_rejects_caller_only_source(self): # A hardware observation still needs an EMULATOR_SOURCE reference; caller/HLE # text alone can never be OBSERVED. bad = dict(OBSERVED, sources=['huc:include/hucc/pcengine.inc']) with self.assertRaisesRegex(ValueError, 'EMULATOR_SOURCE source reference'): adjudicate_claim(bad, CALLER, field='hw_bus_reset', context='slot 1 hw_bus_reset', executed_callee=False) def test_hardware_observation_does_not_promote_callee_abi(self): # hw_bus_reset being OBSERVED must not relax callee-behavior claims: returns # observed on hardware evidence alone (no executed callee) is still rejected. claim = dict(OBSERVED, value='returns $00 in A') with self.assertRaisesRegex(ValueError, 'executed callee'): adjudicate_claim(claim, REAL, field='returns', context='slot 1 returns', executed_callee=False) def test_blocked_requires_unknown_value_confidence_and_no_registers(self): for bad in (dict(BLOCKED, value='some blocker text'), dict(BLOCKED, confidence='SOURCE_SUPPORTED'), dict(BLOCKED, registers=['A'])): with self.assertRaisesRegex(ValueError, 'invalid BLOCKED claim'): adjudicate_claim(bad, CALLER, field='stack', context='slot 1 stack') def test_unknown_rejects_sources_and_non_unknown_value(self): for bad in (dict(UNKNOWN, sources=['huc:include/hucc/pcengine.inc']), dict(UNKNOWN, value='UNKNOWN-ish')): with self.assertRaisesRegex(ValueError, 'invalid UNKNOWN claim'): adjudicate_claim(bad, CALLER, field='flags', context='slot 1 flags') def test_sources_must_be_known_source_keys(self): bad = dict(HYPOTHESIS, sources=['not:a:source']) with self.assertRaisesRegex(ValueError, 'known sources'): adjudicate_claim(bad, CALLER, field='arguments', context='slot 1 arguments') def test_duplicate_sources_rejected(self): bad = dict(HYPOTHESIS, sources=['huc:include/hucc/pcengine.inc'] * 2) with self.assertRaisesRegex(ValueError, 'duplicate'): adjudicate_claim(bad, CALLER, field='arguments', context='slot 1 arguments') # ---- Issue #208 (MAJOR-2): evidence trust boundary. EVIDENCE_ENTRY is SYNTHETIC test data, # never a real measurement; acceptance only means the *shape* is valid (FORMAT_ONLY). def test_evidence_trust_level_is_format_only(self): self.assertEqual(EVIDENCE_TRUST_LEVEL, 'FORMAT_ONLY') def test_real_core_evidence_is_refused_for_callee_field_without_authentication(self): with self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(REAL_OBSERVED_CALLEE, REAL, field='stack', context='slot 1 stack', executed_callee=True, resolved_evidence_hashes=REAL_RESOLVED) def test_evidence_entry_is_refused_for_every_field_without_authentication(self): # The same evidence entry used to pass for unrelated fields (only shape and hash were checked). # Now every field is refused: authentication is missing, so the field correspondence is moot. for field in ('stack', 'returns', 'irq', 'flags'): with self.subTest(field=field), self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(REAL_OBSERVED_CALLEE, REAL, field=field, context=field, executed_callee=True, resolved_evidence_hashes=REAL_RESOLVED) def test_observed_claim_must_not_contain_registers_key(self): for regs in ([], None, ['A'], ['A', 'X']): for field in ('preserved', 'stack'): with self.assertRaisesRegex(ValueError, 'must not contain registers', msg=repr((regs, field))): adjudicate_claim(dict(OBSERVED, registers=regs), REAL, field=field, context='x', executed_callee=True) def test_observed_without_registers_key_and_other_statuses_unchanged(self): self.assertNotIn('registers', OBSERVED) with self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(REAL_OBSERVED_CALLEE, REAL, field='preserved', context='x', executed_callee=True, resolved_evidence_hashes=REAL_RESOLVED) # UNKNOWN / HYPOTHESIS / BLOCKED regression: registers rules unchanged. adjudicate_claim(UNKNOWN, CALLER, field='flags', context='x') adjudicate_claim(HYPOTHESIS, CALLER, field='preserved', context='x') adjudicate_claim(dict(HYPOTHESIS, registers=['A', 'X']), CALLER, field='preserved', context='x') adjudicate_claim(BLOCKED, CALLER, field='flags', context='x') for bad, message in ((dict(UNKNOWN, registers=['A']), 'invalid UNKNOWN claim'), (dict(BLOCKED, registers=['A']), 'invalid BLOCKED claim')): with self.assertRaisesRegex(ValueError, message): adjudicate_claim(bad, CALLER, field='flags', context='x') def test_malformed_input_types_raise_valueerror(self): good = dict(OBSERVED) for bad_claim in (None, [], 'OBSERVED', 123, ('status',)): with self.assertRaisesRegex(ValueError, 'claim must be a dict', msg=repr(bad_claim)): adjudicate_claim(bad_claim, REAL, field='stack', context='x', executed_callee=True) for bad_kinds in (None, [], 'EMULATOR_SOURCE', 123): with self.assertRaisesRegex(ValueError, 'source_kinds must be a dict', msg=repr(bad_kinds)): adjudicate_claim(good, bad_kinds, field='stack', context='x', executed_callee=True) for bad_kinds in ({1: 'EMULATOR_SOURCE'}, {None: 'EMULATOR_SOURCE'}): with self.assertRaisesRegex(ValueError, 'source keys must be strings'): adjudicate_claim(good, bad_kinds, field='stack', context='x', executed_callee=True) for bad_kinds in ({'gear:src/cdrom.cpp': 'NOPE'}, {'gear:src/cdrom.cpp': None}, {'gear:src/cdrom.cpp': ['EMULATOR_SOURCE']}): with self.assertRaisesRegex(ValueError, 'invalid source kind'): adjudicate_claim(good, bad_kinds, field='stack', context='x', executed_callee=True) for bad_field in (None, 1, ['stack'], b'stack'): with self.assertRaisesRegex(ValueError, 'claim field must be a string'): adjudicate_claim(good, REAL, field=bad_field, context='x', executed_callee=True) for bad_exec in (1, 0, 'true', None): with self.assertRaisesRegex(ValueError, 'executed_callee must be a bool', msg=repr(bad_exec)): adjudicate_claim(good, REAL, field='stack', context='x', executed_callee=bad_exec) for bad_regs in ({1: 2}, 0, 'A', ['Q'], ['A', 'A'], [1]): with self.assertRaisesRegex(ValueError, 'registers must be a list', msg=repr(bad_regs)): adjudicate_claim(dict(good, registers=bad_regs), REAL, field='stack', context='x', executed_callee=True) with self.assertRaisesRegex(ValueError, 'claim keys must be strings'): adjudicate_claim({1: 'x'}, REAL, field='stack', context='x', executed_callee=True) def test_invalid_evidence_hashes_are_rejected(self): for h, message in (('0' * 64, 'evidence_hash must be a 64-hex'), ('a' * 64, 'evidence_hash must be a 64-hex'), ('D1A2' + 'f' * 60, 'evidence_hash must be a 64-hex'), ('f' * 63, 'evidence_hash must be a 64-hex'), ('g' * 64, 'evidence_hash must be a 64-hex'), (12345, 'values must be non-empty strings'), (None, 'values must be non-empty strings')): bad = dict(OBSERVED, evidence=[dict(EVIDENCE_ENTRY, evidence_hash=h)]) with self.subTest(h=repr(h)), self.assertRaisesRegex(ValueError, message): adjudicate_claim(bad, REAL, field='stack', context='slot 1 stack', executed_callee=True) def test_evidence_entry_with_unknown_or_non_string_field_is_rejected(self): for entry, message in ((dict(EVIDENCE_ENTRY, extra='x'), 'evidence entry must contain exactly'), (dict(EVIDENCE_ENTRY, core_revision=1), 'values must be non-empty strings'), (dict(EVIDENCE_ENTRY, input_type=''), 'values must be non-empty strings')): bad = dict(OBSERVED, evidence=[entry]) with self.subTest(message=message), self.assertRaisesRegex(ValueError, message): adjudicate_claim(bad, REAL, field='stack', context='slot 1 stack', executed_callee=True) def test_non_observed_claim_carrying_evidence_is_rejected(self): # Evidence must not ride along on HYPOTHESIS: no implicit promotion path. bad = dict(HYPOTHESIS, evidence=[EVIDENCE_ENTRY]) with self.assertRaisesRegex(ValueError, 'only an OBSERVED claim may carry evidence'): adjudicate_claim(bad, CALLER, field='arguments', context='slot 1 arguments') def test_unknown_claim_key_cannot_bypass_checks(self): # e.g. a self-declared executed_callee inside the claim is not a recognised key. bad = dict(OBSERVED, executed_callee=True) with self.assertRaisesRegex(ValueError, 'unknown claim keys'): adjudicate_claim(bad, REAL, field='returns', context='slot 1 returns') def test_invalid_source_kind_is_rejected(self): with self.assertRaisesRegex(ValueError, 'invalid source kind'): adjudicate_claim(OBSERVED, {'gear:src/cdrom.cpp': 'EMULATOR_TRACE'}, field='stack', context='slot 1 stack', executed_callee=True) # ---- Issue #207 (MAJOR-1) x #208: callee ABI vs hardware observation. def test_hardware_observed_with_evidence_and_no_callee_is_refused_without_authentication(self): with self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(REAL_OBSERVED_HW, REAL, field='hw_bus_reset', context='hw_bus_reset', executed_callee=False, resolved_evidence_hashes=REAL_RESOLVED) def test_hardware_observed_without_evidence_is_rejected(self): for bad in (dict(OBSERVED, evidence=[]), {k: v for k, v in OBSERVED.items() if k != 'evidence'}): with self.assertRaisesRegex(ValueError, 'non-empty evidence entries'): adjudicate_claim(bad, REAL, field='hw_bus_reset', context='hw_bus_reset', executed_callee=False) def test_callee_observed_with_executed_false_is_rejected_even_with_evidence(self): with self.assertRaisesRegex(ValueError, 'executed callee'): adjudicate_claim(OBSERVED, REAL, field='returns', context='slot 1 returns', executed_callee=False) def test_callee_observed_executed_true_without_evidence_is_rejected(self): bad = dict(OBSERVED, evidence=[]) with self.assertRaisesRegex(ValueError, 'non-empty evidence entries'): adjudicate_claim(bad, REAL, field='returns', context='slot 1 returns', executed_callee=True) def test_unknown_field_name_cannot_bypass_executed_callee(self): for name in ('hw_bus_reset2', 'returns ', 'Returns', 'callee_returns', ''): with self.assertRaisesRegex(ValueError, 'unknown claim field', msg=repr(name)): adjudicate_claim(OBSERVED, REAL, field=name, context='x', executed_callee=False) def test_hardware_and_callee_field_sets_are_disjoint(self): self.assertFalse(CALLEE_BEHAVIOR_FIELDS & HARDWARE_OBSERVATION_FIELDS) self.assertEqual(CALLEE_BEHAVIOR_FIELDS, FIELDS) def test_caller_or_hle_evidence_cannot_promote_callee_observed(self): hle = {'hugo:bios.c': 'HLE_SOURCE'} for kinds, src in ((CALLER, 'huc:include/hucc/pcengine.inc'), (hle, 'hugo:bios.c')): bad = dict(OBSERVED, sources=[src]) with self.assertRaisesRegex(ValueError, 'EMULATOR_SOURCE source reference'): adjudicate_claim(bad, kinds, field='returns', context='slot 1 returns', executed_callee=True) def test_verified_is_unreachable(self): bad = dict(OBSERVED, status='VERIFIED') for field in ('returns', 'hw_bus_reset'): with self.assertRaisesRegex(ValueError, 'VERIFIED'): adjudicate_claim(bad, REAL, field=field, context='x', executed_callee=True) def test_malformed_types_raise_valueerror_not_typeerror(self): cases = [(dict(OBSERVED, status=['OBSERVED']), 'stack', True, 'unsupported claim status'), (OBSERVED, ['x'], True, 'claim field must be a string'), (dict(HYPOTHESIS, evidence=0), 'arguments', False, 'claim evidence must be a list'), (dict(UNKNOWN, evidence=None), 'flags', False, 'claim evidence must be a list'), (OBSERVED, 'stack', 'no', 'executed_callee must be a bool'), (dict(OBSERVED, registers=['Q']), 'stack', True, 'registers must be a list'), (dict(OBSERVED, evidence=[dict(EVIDENCE_ENTRY, core_revision='x')]), 'stack', True, 'core_revision must be a 40-hex'), (dict(OBSERVED, evidence=[dict(EVIDENCE_ENTRY, execution_conditions=' ')]), 'stack', True, 'values must be non-empty strings')] for claim, field, executed, message in cases: with self.subTest(message=message), self.assertRaisesRegex(ValueError, message): adjudicate_claim(claim, {**REAL, **CALLER}, field=field, context='x', executed_callee=executed) class DeclarationFailClosedTests(unittest.TestCase): """Issue #359 round 3: declaration-level fail-closed text rule. OBSERVED refuses any non-ASCII text, and any field carrying a SYNTHETIC fragment (a 4+ letter run, or a whole field that is a fragment of the marker). Look-alikes outside the confusables table (Cyrillic Һ/И/palochka, Cherokee, dotless i) and cross-field splits in any order must be refused. Every case here is a REAL-CORE entry, so the REALCORE and executed_callee rules are not the reason. """ # Each refusal is asserted against the message the intended check raises (ASCII rule or SYNTHETIC rule), # so a removed or weakened check fails its own test. The REALCORE marker is present in every case here. def assertRefusedClaim(self, claim, message): with self.assertRaisesRegex(ValueError, message): adjudicate_claim(claim, REAL, field='stack', context='slot 1 stack', executed_callee=True, resolved_evidence_hashes=REAL_RESOLVED) def assertRefusedText(self, text, message): # Later evidence fields carry no 'i' or 'c', so fe6cb52's ordered marker check cannot complete the # marker from other fields: the look-alike itself is what must be refused. entry = dict(REAL_ENTRY, execution_conditions=text, input_type='probe', verification_result='stored') self.assertRefusedClaim(dict(REAL_OBSERVED_CALLEE, evidence=[entry]), message) def test_cyrillic_shha_marker_outside_table_is_refused(self): self.assertRefusedText('REALCORE SYNTҺETIC fixture', 'pure ASCII') # U+04BA (Һ) def test_cyrillic_i_marker_outside_table_is_refused(self): self.assertRefusedText('REALCORE SYNTHETИC fixture', 'SYNTHETIC content') # U+0418 (И) def test_palochka_marker_outside_table_is_refused(self): self.assertRefusedText('REALCORE SYNTHETӀC fixture', 'SYNTHETIC content') # U+04C0 (palochka) def test_cherokee_marker_outside_table_is_refused(self): for text, message in (('REALCORE SᎩNTHETIC fixture', 'SYNTHETIC content'), ('REALCORE SYNᎻᎬTIC fixture', 'pure ASCII')): with self.subTest(text=text): self.assertRefusedText(text, message) def test_dotless_i_marker_is_refused(self): for text in ('REALCORE SYNTHETıC fixture', 'REALCORE SYNTHETǀC fixture'): with self.subTest(text=text): self.assertRefusedText(text, 'SYNTHETIC content') def test_non_ascii_letter_outside_the_marker_is_refused(self): self.assertRefusedText('REALCORE probe sector é', 'pure ASCII') def test_reversed_split_thetic_earlier_syn_later_is_refused(self): claim = dict(REAL_OBSERVED_CALLEE, evidence=[dict(REAL_ENTRY, execution_conditions='REALCORE THETIC'), dict(REAL_ENTRY, execution_conditions='REALCORE SYN')]) self.assertRefusedClaim(claim, 'SYNTHETIC content') def test_field_containing_only_syn_with_valid_realcore_is_refused(self): self.assertRefusedClaim(dict(REAL_OBSERVED_CALLEE, evidence=[dict(REAL_ENTRY, input_type='SYN')]), 'SYNTHETIC content') def test_fullwidth_marker_is_still_refused(self): self.assertRefusedText('REALCORE SYNTHETIC fixture', 'SYNTHETIC content') class LedgerAdjudicationTests(unittest.TestCase): def test_ledger_statuses_stay_in_the_four_categories(self): data = load_contracts() for api in data['apis']: for field in FIELDS: claim = api['contract'][field] with self.subTest(slot=api['slot'], field=field): self.assertIn(claim['status'], CLAIM_STATUSES) self.assertNotEqual(claim['status'], 'VERIFIED') adjudicate_claim(claim, {key: src['kind'] for key, src in data['sources'].items()}, field=field, context=f"slot {api['slot']} {field}", executed_callee=bool(api['verification'].get('executed_callee'))) def test_slot1_returns_is_unknown_and_no_callee_executed(self): data = load_contracts() slot1 = data['apis'][1] self.assertEqual((slot1['candidate_name'], slot1['candidate_entry']), ('CD_RESET', '0xE003')) self.assertEqual(slot1['contract']['returns']['status'], 'UNKNOWN') self.assertEqual(slot1['verification'], {'status': 'NOT_RUN', 'executed_callee': False, 'evidence': []}) # Slot 1 claims declare evidence slots but no execution has been performed. for field in FIELDS: self.assertEqual(slot1['contract'][field]['evidence'], []) # Nothing in the whole ledger is VERIFIED: no executed reference-ROM callee exists. for api in data['apis']: for field in FIELDS: self.assertNotEqual(api['contract'][field]['status'], 'VERIFIED') class ProvenanceBypassTests(unittest.TestCase): """Issue #359 MEDIUM fix: OBSERVED refuses unverified evidence. Each case must be refused (ValueError).""" def assertRefused(self, claim, message, *, field='stack', executed_callee=True, resolved=REAL_RESOLVED): with self.assertRaisesRegex(ValueError, message): adjudicate_claim(claim, REAL, field=field, context=f'slot 1 {field}', executed_callee=executed_callee, resolved_evidence_hashes=resolved) def test_synthetic_text_with_marker_removed_is_refused(self): entry = dict(REAL_ENTRY, execution_conditions='test fixture; no real execution took place') self.assertRefused(dict(REAL_OBSERVED_CALLEE, evidence=[entry]), 'lacks the REALCORE provenance marker') def test_spaced_synthetic_marker_is_refused(self): entry = dict(REAL_ENTRY, execution_conditions='REAL-CORE S Y N T H E T I C fixture') self.assertRefused(dict(REAL_OBSERVED_CALLEE, evidence=[entry]), 'SYNTHETIC content') def test_synthetic_marker_in_claim_value_with_valid_hash_is_refused(self): self.assertRefused(dict(REAL_OBSERVED_CALLEE, value='SYNTHETIC observed value'), 'SYNTHETIC content') def test_arbitrary_hash_not_in_resolved_set_is_refused(self): entry = dict(REAL_ENTRY, evidence_hash='ab' * 32) self.assertRefused(dict(REAL_OBSERVED_CALLEE, evidence=[entry]), 'resolved by resolve_evidence') def test_forged_executed_callee_with_hardware_kind_is_refused(self): self.assertRefused(REAL_OBSERVED_HW, 'requires observation kind CALLEE_EXECUTION', field='hw_bus_reset', executed_callee=True) def test_unresolved_real_core_evidence_is_refused(self): self.assertRefused(REAL_OBSERVED_CALLEE, 'resolved evidence set is empty', resolved=frozenset()) def test_entry_without_real_core_marker_is_refused(self): entry = dict(REAL_ENTRY, execution_conditions='cd-read probe run on the pinned core') self.assertRefused(dict(REAL_OBSERVED_CALLEE, evidence=[entry]), 'lacks the REALCORE provenance marker') # Issue #359 round 2: marker normalization must fold homoglyphs, combining marks and field splits. # Each case is a REAL-CORE entry (REALCORE marker present) whose SYNTHETIC marker is disguised. def assertRefusedSynthetic(self, claim): with self.assertRaisesRegex(ValueError, 'SYNTHETIC'): adjudicate_claim(claim, REAL, field='stack', context='slot 1 stack', executed_callee=True, resolved_evidence_hashes=REAL_RESOLVED) def test_cyrillic_homoglyph_synthetic_marker_is_refused(self): # Cyrillic U+0423 (for Y), U+041D (H), U+0415 (E), U+0421 (C). for text in ('REALCORE SУNTHETIC fixture', 'REALCORE SYNTНЕTIС fixture'): with self.subTest(text=text): entry = dict(REAL_ENTRY, execution_conditions=text) self.assertRefusedSynthetic(dict(REAL_OBSERVED_CALLEE, evidence=[entry])) def test_greek_homoglyph_synthetic_marker_is_refused(self): # Greek U+03A5 (Y), U+039D (N), U+03A4 (T), U+0397 (H), U+0395 (E), U+0399 (I). for text in ('REALCORE SΥNΤHΕTΙC fixture', 'REALCORE SΥΝTHETIC fixture'): with self.subTest(text=text): entry = dict(REAL_ENTRY, execution_conditions=text) self.assertRefusedSynthetic(dict(REAL_OBSERVED_CALLEE, evidence=[entry])) def test_combining_mark_synthetic_marker_is_refused(self): # Combining acute U+0301 inserted inside the marker. for text in ('REALCORE SÝNTHETIC fixture', 'REALCORE SYNTHETÍC fixture'): with self.subTest(text=text): entry = dict(REAL_ENTRY, execution_conditions=text) self.assertRefusedSynthetic(dict(REAL_OBSERVED_CALLEE, evidence=[entry])) def test_marker_split_across_two_fields_is_refused(self): # 'SYN' in execution_conditions and 'THETIC' in input_type: each field alone is clean. entry = dict(REAL_ENTRY, execution_conditions='REALCORE SYN', input_type='THETIC') self.assertRefusedSynthetic(dict(REAL_OBSERVED_CALLEE, evidence=[entry])) # Issue #359 / #124: a record dressed up as a real measurement must not reach OBSERVED. Every case below # carries the REALCORE text, a matching hash, executed_callee=True and the declared kind. None is # authenticated, so each is refused with the authentication message. def assertRefusedUnauthenticated(self, claim, *, field='stack', executed_callee=True, resolved=REAL_RESOLVED): with self.assertRaisesRegex(ValueError, 'not authenticated'): adjudicate_claim(claim, REAL, field=field, context=f'slot 1 {field}', executed_callee=executed_callee, resolved_evidence_hashes=resolved) def test_fully_resolved_real_core_entry_is_refused_without_authentication(self): self.assertRefusedUnauthenticated(REAL_OBSERVED_CALLEE) def test_caller_built_resolved_set_is_not_authentication(self): # Any hash the caller places in the set is a declaration, not a resolve_evidence() result. entry = dict(REAL_ENTRY, evidence_hash='ef' * 32) self.assertRefusedUnauthenticated(dict(REAL_OBSERVED_CALLEE, evidence=[entry]), resolved=frozenset({entry['evidence_hash']})) def test_unknown_verification_result_is_never_promoted_to_measured(self): # A validation result of UNKNOWN (Issue #371 vocabulary) must not become a measurement. entry = dict(REAL_ENTRY, verification_result='UNKNOWN') self.assertRefusedUnauthenticated(dict(REAL_OBSERVED_CALLEE, evidence=[entry])) def test_declared_callee_kind_on_hardware_claim_with_real_text_is_refused(self): # Kind relabelled to CALLEE_EXECUTION for a hw readback: the correspondence check refuses it. with self.assertRaisesRegex(ValueError, 'does not support field'): adjudicate_claim(dict(REAL_OBSERVED_HW, observation_kind='CALLEE_EXECUTION'), REAL, field='hw_bus_reset', context='slot 1 hw_bus_reset', executed_callee=True, resolved_evidence_hashes=REAL_RESOLVED) if __name__ == '__main__': unittest.main()