Skip to content

Commit 9a0b408

Browse files
committed
[red] keep C3 runtime policy checks bound to current baseline
Signed-off-by: Viwat Vchirawongkwin <viwat.v@chula.ac.th>
1 parent 4369354 commit 9a0b408

1 file changed

Lines changed: 76 additions & 10 deletions

File tree

‎tests/firmware_tests/host/test_target_runtime_contract.py‎

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import tomllib
3232
import types
3333
import unittest
34+
from unittest import mock
3435

3536

3637
HOST_DIR = Path(__file__).resolve().parent
@@ -50,6 +51,43 @@
5051
"esp32-c3": "PYBLE_ESP32_C3",
5152
}
5253
EXACT_BOARD_TARGET = "waveshare-esp32-s3-lcd-147b"
54+
C3_RESOURCE_THRESHOLD_KEYS = {
55+
"application_headroom_min_bytes",
56+
"application_image_max_bytes",
57+
"gc_free_min_bytes",
58+
"idf_internal_free_min_bytes",
59+
"idf_internal_largest_block_min_bytes",
60+
"idf_internal_minimum_free_min_bytes",
61+
}
62+
C3_FIXED_PERFORMANCE_THRESHOLDS = {
63+
"get_verified_goodput_min_bytes_per_second": 6600,
64+
"put_committed_goodput_min_bytes_per_second": 6600,
65+
"reset_to_service_advertisement_max_ms": 3000,
66+
}
67+
68+
69+
def _load_release_module():
70+
spec = importlib.util.spec_from_file_location(
71+
"pyble_release_target_runtime_contract",
72+
FIRMWARE_DIR / "scripts" / "release_bundle.py",
73+
)
74+
if spec is None or spec.loader is None:
75+
raise AssertionError("cannot construct release_bundle.py import spec")
76+
module = importlib.util.module_from_spec(spec)
77+
spec.loader.exec_module(module)
78+
return module
79+
80+
81+
RELEASE = _load_release_module()
82+
83+
84+
def _current_c3_profile():
85+
policy = json.loads(
86+
(FIRMWARE_DIR / "qualification" / "oi1-gates.json").read_bytes()
87+
)
88+
return next(
89+
item for item in policy["profiles"] if item["profile_id"] == "esp32-c3-4mb"
90+
)
5391

5492
EXPECTED_FROZEN_PATHS = Counter(
5593
{
@@ -1234,16 +1272,7 @@ def test_c3_disables_phy_calibration_storage_without_relaxing_oi1(self):
12341272
"C3 must run full PHY calibration instead of growing PHY NVS state",
12351273
)
12361274

1237-
policy = json.loads(
1238-
(FIRMWARE_DIR / "qualification" / "oi1-gates.json").read_text(
1239-
encoding="utf-8"
1240-
)
1241-
)
1242-
profile = next(
1243-
item
1244-
for item in policy["profiles"]
1245-
if item["profile_id"] == "esp32-c3-4mb"
1246-
)
1275+
profile = _current_c3_profile()
12471276
thresholds = profile["thresholds"]
12481277
self.assertEqual(
12491278
thresholds,
@@ -1262,6 +1291,43 @@ def test_c3_disables_phy_calibration_storage_without_relaxing_oi1(self):
12621291
"carry the fixed ADR-0037 product SLOs",
12631292
)
12641293

1294+
def test_c3_phy_storage_cannot_be_reenabled(self):
1295+
with mock.patch(
1296+
__name__ + "._sdkconfig_values",
1297+
return_value={"CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE": "y"},
1298+
), self.assertRaisesRegex(AssertionError, "C3 must run full PHY calibration"):
1299+
self.test_c3_disables_phy_calibration_storage_without_relaxing_oi1()
1300+
1301+
def test_c3_policy_loader_failure_is_not_ignored(self):
1302+
with mock.patch.object(
1303+
RELEASE, "_load_qualification_policy",
1304+
side_effect=RELEASE.ReleaseError("injected policy validation failure"),
1305+
), self.assertRaisesRegex(RELEASE.ReleaseError, "injected policy validation failure"):
1306+
_current_c3_profile()
1307+
1308+
def test_c3_resource_threshold_mutations_are_rejected(self):
1309+
policy_path = FIRMWARE_DIR / "qualification" / "oi1-gates.json"
1310+
original_read = Path.read_bytes
1311+
for key in sorted(C3_RESOURCE_THRESHOLD_KEYS):
1312+
with self.subTest(threshold=key):
1313+
changed = json.loads(policy_path.read_bytes())
1314+
profile = next(
1315+
item for item in changed["profiles"]
1316+
if item["profile_id"] == "esp32-c3-4mb"
1317+
)
1318+
profile["thresholds"][key] += 1
1319+
payload = (json.dumps(changed, indent=2, sort_keys=True) + "\n").encode()
1320+
1321+
def read(path):
1322+
return payload if path == policy_path else original_read(path)
1323+
1324+
with mock.patch.object(
1325+
Path, "read_bytes", autospec=True, side_effect=read
1326+
), self.assertRaisesRegex(
1327+
RELEASE.ReleaseError, "thresholds were not derived from baseline esp32-c3-4mb"
1328+
):
1329+
_current_c3_profile()
1330+
12651331

12661332
class GeneratedRuntimeContractTests(unittest.TestCase):
12671333
@classmethod

0 commit comments

Comments
 (0)