|
10 | 10 | import copy |
11 | 11 | import errno |
12 | 12 | import hashlib |
| 13 | +import importlib.util |
13 | 14 | import inspect |
14 | 15 | import io |
15 | 16 | import json |
|
33 | 34 | import oi1_profile_bench as profile_bench # noqa: E402 |
34 | 35 |
|
35 | 36 |
|
| 37 | +def load_release_module(): |
| 38 | + spec = importlib.util.spec_from_file_location( |
| 39 | + "pyble_release_profile_bench_contract", |
| 40 | + REPO_ROOT / "firmware" / "scripts" / "release_bundle.py", |
| 41 | + ) |
| 42 | + if spec is None or spec.loader is None: |
| 43 | + raise AssertionError("cannot construct release_bundle.py import spec") |
| 44 | + module = importlib.util.module_from_spec(spec) |
| 45 | + spec.loader.exec_module(module) |
| 46 | + return module |
| 47 | + |
| 48 | + |
| 49 | +RELEASE = load_release_module() |
| 50 | +CURRENT_POLICY_PATH = REPO_ROOT / "firmware" / "qualification" / "oi1-gates.json" |
| 51 | + |
| 52 | + |
| 53 | +def load_current_policy(): |
| 54 | + payload = CURRENT_POLICY_PATH.read_bytes() |
| 55 | + return json.loads(payload.decode("utf-8")), hashlib.sha256(payload).hexdigest() |
| 56 | + |
| 57 | + |
36 | 58 | PROFILE_ORDER = ( |
37 | 59 | "esp32-4mb", |
38 | 60 | "esp32-s3-n16r8", |
@@ -391,6 +413,88 @@ def _valid_observation(profile_id): |
391 | 413 | } |
392 | 414 |
|
393 | 415 |
|
| 416 | +class CurrentPolicyBindingTest(unittest.TestCase): |
| 417 | + def setUp(self): |
| 418 | + self.policy = json.loads(CURRENT_POLICY_PATH.read_bytes()) |
| 419 | + self.baseline_path = REPO_ROOT / self.policy["baseline_evidence"]["path"] |
| 420 | + self.baseline = json.loads(self.baseline_path.read_bytes()) |
| 421 | + |
| 422 | + @staticmethod |
| 423 | + def canonical(value): |
| 424 | + return (json.dumps(value, indent=2, sort_keys=True) + "\n").encode("utf-8") |
| 425 | + |
| 426 | + @contextlib.contextmanager |
| 427 | + def changed_inputs(self, policy, baseline=None): |
| 428 | + # Override only selected reads; real checkout ancestry and every other |
| 429 | + # source/evidence check remain active. No repository file is written. |
| 430 | + replacements = {CURRENT_POLICY_PATH: self.canonical(policy)} |
| 431 | + if baseline is not None: |
| 432 | + replacements[self.baseline_path] = self.canonical(baseline) |
| 433 | + original_read = Path.read_bytes |
| 434 | + |
| 435 | + def read(path): |
| 436 | + return replacements[path] if path in replacements else original_read(path) |
| 437 | + |
| 438 | + with mock.patch.object(Path, "read_bytes", autospec=True, side_effect=read): |
| 439 | + yield |
| 440 | + |
| 441 | + def test_active_policy_uses_actual_checkout_era_and_full_validator(self): |
| 442 | + with mock.patch.object( |
| 443 | + RELEASE, |
| 444 | + "_qualification_derivation_for_checkout", |
| 445 | + wraps=RELEASE._qualification_derivation_for_checkout, |
| 446 | + ) as selector: |
| 447 | + policy, digest = load_current_policy() |
| 448 | + self.assertEqual(policy, self.policy) |
| 449 | + self.assertEqual(digest, hashlib.sha256(CURRENT_POLICY_PATH.read_bytes()).hexdigest()) |
| 450 | + selector.assert_called_once_with( |
| 451 | + REPO_ROOT, RELEASE._read_lock(REPO_ROOT)["pyble"]["agent_version"] |
| 452 | + ) |
| 453 | + |
| 454 | + def test_every_profile_threshold_is_bound_to_actual_baseline(self): |
| 455 | + for index, profile in enumerate(self.policy["profiles"]): |
| 456 | + for key in profile["thresholds"]: |
| 457 | + with self.subTest(profile=profile["profile_id"], threshold=key): |
| 458 | + changed = copy.deepcopy(self.policy) |
| 459 | + changed["profiles"][index]["thresholds"][key] += 1 |
| 460 | + with self.changed_inputs(changed), self.assertRaisesRegex( |
| 461 | + RELEASE.ReleaseError, "thresholds were not derived from baseline" |
| 462 | + ): |
| 463 | + load_current_policy() |
| 464 | + |
| 465 | + def test_changed_baseline_digest_is_rejected(self): |
| 466 | + changed = copy.deepcopy(self.policy) |
| 467 | + changed["baseline_evidence"]["sha256"] = "0" * 64 |
| 468 | + with self.changed_inputs(changed), self.assertRaisesRegex( |
| 469 | + RELEASE.ReleaseError, "baseline evidence digest changed" |
| 470 | + ): |
| 471 | + load_current_policy() |
| 472 | + |
| 473 | + def test_rehashed_baseline_source_filename_mismatch_is_rejected(self): |
| 474 | + baseline = copy.deepcopy(self.baseline) |
| 475 | + baseline["source_commit"] = "0" * 40 |
| 476 | + changed = copy.deepcopy(self.policy) |
| 477 | + changed["baseline_evidence"]["sha256"] = hashlib.sha256( |
| 478 | + self.canonical(baseline) |
| 479 | + ).hexdigest() |
| 480 | + with self.changed_inputs(changed, baseline), self.assertRaisesRegex( |
| 481 | + RELEASE.ReleaseError, "source commit disagrees with its filename" |
| 482 | + ): |
| 483 | + load_current_policy() |
| 484 | + |
| 485 | + def test_rehashed_incomplete_baseline_observations_are_rejected(self): |
| 486 | + baseline = copy.deepcopy(self.baseline) |
| 487 | + baseline["profiles"][0]["oi1_observation"][ |
| 488 | + "reset_to_service_advertisement_ms" |
| 489 | + ].pop() |
| 490 | + changed = copy.deepcopy(self.policy) |
| 491 | + changed["baseline_evidence"]["sha256"] = hashlib.sha256( |
| 492 | + self.canonical(baseline) |
| 493 | + ).hexdigest() |
| 494 | + with self.changed_inputs(changed, baseline), self.assertRaises(RELEASE.ReleaseError): |
| 495 | + load_current_policy() |
| 496 | + |
| 497 | + |
394 | 498 | class FrozenConstantsTest(unittest.TestCase): |
395 | 499 | def test_exact_v060_candidate_profiles_and_common_workload_are_frozen(self): |
396 | 500 | self.assertEqual(tuple(bench.PROFILE_ORDER), CURRENT_PROFILE_ORDER) |
@@ -427,7 +531,8 @@ def test_committed_v060_policy_uses_fixed_slos_and_retains_a8_baseline(self): |
427 | 531 | REPO_ROOT / "firmware" / "qualification" / "oi1-gates.json" |
428 | 532 | ) |
429 | 533 | payload = policy_path.read_bytes() |
430 | | - policy = json.loads(payload.decode("utf-8")) |
| 534 | + policy, policy_sha256 = load_current_policy() |
| 535 | + self.assertEqual(policy_sha256, hashlib.sha256(payload).hexdigest()) |
431 | 536 |
|
432 | 537 | self.assertEqual(policy["schema_version"], 3) |
433 | 538 | self.assertEqual( |
|
0 commit comments