diff --git a/benchmarks/valid/positive_set_closure.py b/benchmarks/valid/positive_set_closure.py index daa992d4..e2e72bfd 100644 --- a/benchmarks/valid/positive_set_closure.py +++ b/benchmarks/valid/positive_set_closure.py @@ -128,6 +128,13 @@ def _closed_counts(pairs: object) -> tuple[tuple[str, int], ...]: return pairs +def _closed_total(value: object) -> int: + """A non-negative exact int: booleans and floats are not closure counts.""" + if type(value) is not int or value < 0: + raise _mismatch() + return value + + def _gates(item: PositiveSetClosureV1) -> tuple[bool, bool, bool]: total_ok = item.total_accepted_positives >= COMBINED_POSITIVE_FLOOR ecosystems_ok = all(count > 0 for _, count in item.positives_by_ecosystem) @@ -146,9 +153,7 @@ def _validate_closure(item: PositiveSetClosureV1) -> None: raise _mismatch() _closed_vocabulary(item.accepted_families, _FAMILY) _closed_counts(item.positives_by_ecosystem) - if item.total_accepted_positives < 0 or item.total_accepted_positives != sum( - count for _, count in item.positives_by_ecosystem - ): + if _closed_total(item.total_accepted_positives) != sum(count for _, count in item.positives_by_ecosystem): raise _mismatch() if bool(item.accepted_batch_digests) != (item.total_accepted_positives > 0): raise _mismatch() diff --git a/tests/test_valid_benchmark_positive_set_closure.py b/tests/test_valid_benchmark_positive_set_closure.py index 3135ff33..9e41eb71 100644 --- a/tests/test_valid_benchmark_positive_set_closure.py +++ b/tests/test_valid_benchmark_positive_set_closure.py @@ -54,6 +54,7 @@ from benchmarks.valid.replacement_artifact_contract import ( # noqa: E402 ArtifactKind, validate_artifact, ) +from openultrasast.core.canonical import canonical_bytes, content_digest # noqa: E402 @pytest.fixture(autouse=True) @@ -215,6 +216,51 @@ def test_closure_assembler_fails_closed_on_impossible_combinations(): assert error.value.reason is PositiveSetClosureStateReason.STATE_MISMATCH +_CLOSED_BASE = { + "batch_digests": ("a" * 64,), + "accepted_batch_digests": ("a" * 64,), + "total_accepted_positives": 45, + "positives_by_ecosystem": {"javascript": 21, "python": 24}, + "accepted_families": ("injection", "path_traversal"), + "status": PositiveSetClosureStatus.CLOSED, + "reason": PositiveSetClosureReason.ALL_GAPS_CLOSED, +} +_SINGLE_POSITIVE = { + "total_accepted_positives": 1, + "positives_by_ecosystem": {"javascript": 1, "python": 0}, + "status": PositiveSetClosureStatus.INSUFFICIENT, + "reason": PositiveSetClosureReason.INSUFFICIENT_TOTAL_POSITIVES, +} + + +def test_closure_refuses_float_and_bool_totals_closed(): + assert type(positive_set_closure(**_CLOSED_BASE).total_accepted_positives) is int + assert type(positive_set_closure(**{**_CLOSED_BASE, **_SINGLE_POSITIVE}).total_accepted_positives) is int + + for forged in ( + {**_CLOSED_BASE, "total_accepted_positives": 45.0}, + {**_CLOSED_BASE, **_SINGLE_POSITIVE, "total_accepted_positives": True}, + ): + with pytest.raises(PositiveSetClosureStateError) as error: + positive_set_closure(**forged) + assert error.value.reason is PositiveSetClosureStateReason.STATE_MISMATCH + + +def test_closure_decode_refuses_a_self_consistent_float_total(): + encoded = positive_set_closure_bytes(positive_set_closure(**_CLOSED_BASE)) + forged = {key: tuple(value) if isinstance(value, list) else value for key, value in json.loads(encoded).items()} + assert forged["total_accepted_positives"] == 45 + forged["total_accepted_positives"] = 45.0 + forged["closure_digest"] = content_digest({key: value for key, value in forged.items() if key != "closure_digest"}) + content = canonical_bytes(forged) + assert b'"total_accepted_positives":45.0' in content + assert decode_positive_set_closure(encoded).total_accepted_positives == 45 + + with pytest.raises(PositiveSetClosureStateError) as error: + decode_positive_set_closure(content) + assert error.value.reason is PositiveSetClosureStateReason.STATE_MISMATCH + + def test_closure_decodes_hostile_bytes_and_persists_durably(): batches = _py_only_batches() closure = project_positive_set_closure(batches)