Fix nRF52 contact persistence and ExtraFS recovery

This commit is contained in:
mikecarper committed 2026-09-02 11:41:38 -07:00
1 parent 66ad8c52df
commit cb27fa8539
23 files changed
+1591 -113

No files matched your search

@@ -0,0 +1,258 @@
#!/usr/bin/env python3
"""Integration guards for Companion contact writes across event-only sleep."""
from pathlib import Path
import re
import unittest
ROOT = Path(__file__).resolve().parents[1]
def function_body(source: str, signature: str) -> str:
start = source.index(signature)
opening_brace = source.index("{", start)
depth = 0
for position in range(opening_brace, len(source)):
if source[position] == "{":
depth += 1
elif source[position] == "}":
depth -= 1
if depth == 0:
return source[opening_brace + 1 : position]
raise AssertionError(f"unterminated function: {signature}")
class CompanionContactSleepContractTest(unittest.TestCase):
def test_initial_write_polls_but_failed_backoff_can_sleep(self):
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
body = function_body(mesh, "bool MyMesh::hasPendingWork() const")
nrf_start = body.index("#if defined(NRF52_PLATFORM)")
other_start = body.index("#else", nrf_start)
conditional_end = body.index("#endif", other_start)
nrf = body[nrf_start:other_start]
other = body[other_start:conditional_end]
self.assertRegex(
nrf,
r"contact_write_needs_polling\s*=\s*"
r"dirty_contacts_expiry\s*!=\s*0\s*"
r"&&\s*\(dirty_contacts_failures\s*==\s*0\s*"
r"\|\|\s*isContactWriteDue\s*\(\s*\)\)",
)
self.assertRegex(
other,
r"contact_write_needs_polling\s*=\s*isContactWriteDue\s*\(\s*\)",
)
self.assertRegex(body, r"\|\|\s*contact_write_needs_polling")
def test_contact_write_is_serviced_only_after_its_deadline(self):
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
body = function_body(mesh, "void MyMesh::loop()")
self.assertIn("if (isContactWriteDue())", body)
self.assertIn("_store->serviceContactWrites(this, save_filter)", body)
due = function_body(mesh, "bool MyMesh::isContactWriteDue() const")
self.assertEqual(due.count("_ms->getMillis()"), 1)
self.assertIn("(int32_t)(now - dirty_contacts_expiry) >= 0", due)
def test_repeated_mutations_do_not_postpone_first_write(self):
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
for signature in (
"void MyMesh::scheduleContactWrite(const ContactInfo& contact)",
"void MyMesh::scheduleContactWriteAfterRelease("
"const ContactInfo& contact)",
):
body = function_body(mesh, signature)
self.assertIn("mesh::scheduleLazyPersistenceMutation(", body)
self.assertIn("dirty_contacts_failures", body)
self.assertIn("futureMillis(LAZY_CONTACTS_WRITE_DELAY)", body)
self.assertNotRegex(
body,
r"dirty_contacts_expiry\s*=\s*"
r"futureMillis\s*\(\s*LAZY_CONTACTS_WRITE_DELAY\s*\)",
)
def test_every_direct_contact_rearm_preserves_zero_sentinel(self):
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
begin = function_body(mesh, "void MyMesh::begin(")
save = function_body(mesh, "void MyMesh::saveContacts()")
loop = function_body(mesh, "void MyMesh::loop()")
for body in (begin, save, loop):
self.assertIn("mesh::nonzeroLazyPersistenceDeadline(", body)
self.assertIn("futureMillis(CONTACT_PAGE_WRITE_GAP)", loop)
def test_failed_writes_back_off_until_a_success_resets_state(self):
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
header = (ROOT / "examples/companion_radio/MyMesh.h").read_text()
save = function_body(mesh, "void MyMesh::saveContacts()")
loop = function_body(mesh, "void MyMesh::loop()")
retry = function_body(mesh, "void MyMesh::scheduleContactWriteRetry()")
self.assertIn("uint8_t dirty_contacts_failures;", header)
self.assertIn("mesh::recordLazyPersistenceSaveFailure(", retry)
self.assertIn(
"mesh::LAZY_PERSISTENCE_MAX_RETRY_DELAY_MILLIS", retry
)
self.assertIn("LAZY_CONTACTS_WRITE_DELAY", retry)
self.assertIn("mesh::completeLazyPersistenceSave(", retry)
self.assertIn("retry in %lu ms", retry)
for body in (save, loop):
self.assertIn("scheduleContactWriteRetry();", body)
self.assertIn("mesh::resetLazyPersistenceAfterSuccess(", body)
def test_main_loop_gates_sleep_on_pending_work(self):
main = (ROOT / "examples/companion_radio/main.cpp").read_text()
sleep_start = main.index("bool can_sleep =")
sleep_end = main.index("if (can_sleep)", sleep_start)
sleep_gate = main[sleep_start:sleep_end]
self.assertIn("!the_mesh.hasPendingWork()", sleep_gate)
nrf52 = (ROOT / "src/helpers/NRF52Board.cpp").read_text()
body = function_body(nrf52, "void NRF52Board::sleep(uint32_t secs)")
self.assertIn("event-driven sleep instead of timed sleep", body)
self.assertIn("sd_app_evt_wait();", body)
def test_all_runtime_reboots_flush_contacts_first(self):
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
header = (ROOT / "examples/companion_radio/MyMesh.h").read_text()
self.assertIn("bool flushContactsBeforeReboot();", header)
flush = function_body(mesh, "bool MyMesh::flushContactsBeforeReboot()")
self.assertIn("_store->flushContactWrites(this, save_filter)", flush)
self.assertIn("scheduleContactWriteRetry();", flush)
self.assertIn("mesh::resetLazyPersistenceAfterSuccess(", flush)
framed = function_body(mesh, "void MyMesh::handleCmdFrame(")
reboot_at = framed.index("cmd_frame[0] == CMD_REBOOT")
framed_reboot = framed[reboot_at : framed.index(
"cmd_frame[0] == CMD_GET_BATT_AND_STORAGE", reboot_at
)]
self.assertIn("flushContactsBeforeReboot()", framed_reboot)
rescue = function_body(mesh, "void MyMesh::checkCLIRescueCmd()")
rescue_reboot = rescue[rescue.index('strcmp(cli_command, "reboot")') :]
self.assertIn("flushContactsBeforeReboot()", rescue_reboot)
callback = function_body(mesh, "void MyMesh::rebootNow()")
self.assertIn("flushContactsBeforeReboot()", callback)
loop = function_body(mesh, "void MyMesh::loop()")
scheduled = loop[: loop.index("#if COMPANION_FEATURE_TEMP_RADIO")]
self.assertIn("flushContactsBeforeReboot()", scheduled)
pending = function_body(mesh, "bool MyMesh::hasPendingWork() const")
self.assertIn("|| _scheduled_reboot_at != 0", pending)
def test_ota_apply_reboot_uses_companion_persistence_hook(self):
mesh_base_header = (ROOT / "src/Mesh.h").read_text()
mesh_base = (ROOT / "src/Mesh.cpp").read_text()
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
header = (ROOT / "examples/companion_radio/MyMesh.h").read_text()
self.assertIn(
"virtual bool prepareForOtaReboot() { return true; }",
mesh_base_header,
)
maintenance = function_body(
mesh_base, "void __attribute__((noinline)) Mesh::serviceLoopMaintenance()"
)
guard_at = maintenance.index("prepareForOtaReboot()")
apply_at = maintenance.index("ota::ota_reboot_to_apply()")
self.assertLess(guard_at, apply_at)
self.assertIn("oc.apply_at = futureMillis(1000)", maintenance)
self.assertIn("bool prepareForOtaReboot() override;", header)
guard = function_body(mesh, "bool MyMesh::prepareForOtaReboot()")
self.assertIn("dirty_contacts_failures != 0", guard)
self.assertIn("!isContactWriteDue()", guard)
self.assertIn("flushContactsBeforeReboot()", guard)
def test_deferred_ota_apply_prevents_event_only_sleep(self):
mesh_base_header = (ROOT / "src/Mesh.h").read_text()
mesh_base = (ROOT / "src/Mesh.cpp").read_text()
self.assertIn("bool hasPendingOtaApply() const;", mesh_base_header)
pending = function_body(mesh_base, "bool Mesh::hasPendingOtaApply() const")
self.assertIn("defined(ENABLE_OTA)", pending)
self.assertIn("!defined(OTA_SEEDER_ONLY)", pending)
self.assertIn("ota::ota_ctx().apply_pending", pending)
role_sources = (
ROOT / "examples/companion_radio/MyMesh.cpp",
ROOT / "examples/simple_sensor/SensorMesh.cpp",
ROOT / "examples/simple_room_server/MyMesh.cpp",
ROOT / "examples/simple_repeater/MyMesh.cpp",
)
for role_source in role_sources:
role = role_source.read_text()
signature = (
"bool SensorMesh::hasPendingWork() const"
if role_source.name == "SensorMesh.cpp"
else "bool MyMesh::hasPendingWork() const"
)
has_pending_work = function_body(role, signature)
self.assertIn("hasPendingOtaApply()", has_pending_work, role_source)
def test_every_companion_ui_shutdown_uses_persistence_guard(self):
abstract = (
ROOT / "examples/companion_radio/AbstractUITask.h"
).read_text()
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
header = (ROOT / "examples/companion_radio/MyMesh.h").read_text()
self.assertIn("public UIShutdownGuard", header)
self.assertIn("virtual bool prepareForUiShutdown() = 0;", abstract)
self.assertIn("_ui->setShutdownGuard(this)", mesh)
callback = function_body(mesh, "bool MyMesh::prepareForUiShutdown()")
self.assertIn("flushContactsBeforeReboot()", callback)
for ui_name in ("ui-orig", "ui-new", "ui-tiny"):
ui = (
ROOT / "examples/companion_radio" / ui_name / "UITask.cpp"
).read_text()
shutdown = function_body(ui, "void UITask::shutdown(bool restart)")
guard_at = shutdown.index("if (!prepareForShutdown()) return;")
reboot_at = shutdown.index("_board->reboot()")
poweroff_at = shutdown.index("_board->powerOff()")
self.assertLess(guard_at, reboot_at, ui_name)
self.assertLess(guard_at, poweroff_at, ui_name)
def test_identity_import_rearms_page_repairs(self):
mesh = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
framed = function_body(mesh, "void MyMesh::handleCmdFrame(")
import_at = framed.index("cmd_frame[0] == CMD_IMPORT_PRIVATE_KEY")
import_end = framed.index("cmd_frame[0] == CMD_SEND_RAW_DATA", import_at)
identity_import = framed[import_at:import_end]
flush_at = identity_import.index("flushContactsBeforeReboot()")
save_identity_at = identity_import.index("_store->saveMainIdentity(")
load_at = identity_import.index("_store->loadContacts(this)")
reset_at = identity_import.index(
"mesh::resetLazyPersistenceAfterSuccess(", save_identity_at
)
rearm_at = identity_import.index(
"mesh::scheduleLazyPersistenceMutation(", load_at
)
self.assertLess(flush_at, save_identity_at)
self.assertLess(reset_at, load_at)
self.assertLess(load_at, rearm_at)
self.assertIn("_store->hasPendingContactWrites()", identity_import)
def test_webconfig_reboot_request_is_one_shot_if_flush_fails(self):
webconfig = (
ROOT / "src/helpers/esp32/WebConfigServer.cpp"
).read_text()
tick = function_body(webconfig, "void WebConfigServer::tick(")
due_at = tick.index("WebConfigBatch::rebootDue(")
callback_at = tick.index("_cb->rebootNow()", due_at)
clear_at = tick.index("_reboot_at = 0", due_at)
self.assertLess(clear_at, callback_at)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,146 @@
#include <gtest/gtest.h>
#include <helpers/nrf52/InternalSecondaryFsRepair.h>
using mesh::storage::InternalSecondaryFsRepairResult;
using mesh::storage::InternalSecondaryFsBootResult;
using mesh::storage::isInternalExtraFsReservedByApplication;
using mesh::storage::isExpectedInternalExtraFsGeometry;
using mesh::storage::prepareInternalSecondaryFilesystem;
using mesh::storage::repairInternalSecondaryFilesystem;
TEST(InternalSecondaryFsBoot, RequiresExact100KiBGeometry) {
EXPECT_TRUE(isExpectedInternalExtraFsGeometry(0xD4000, 0x19000, 128));
EXPECT_FALSE(isExpectedInternalExtraFsGeometry(0xED000, 0x7000, 128));
EXPECT_FALSE(isExpectedInternalExtraFsGeometry(0xD4000, 0x7000, 128));
EXPECT_FALSE(isExpectedInternalExtraFsGeometry(0xD4000, 0x19000, 4096));
EXPECT_TRUE(isInternalExtraFsReservedByApplication(0xD4000));
EXPECT_TRUE(isInternalExtraFsReservedByApplication(0xC0000));
EXPECT_FALSE(isInternalExtraFsReservedByApplication(0xD4001));
EXPECT_FALSE(isInternalExtraFsReservedByApplication(0xED000));
}
TEST(InternalSecondaryFsBoot, HealthyFilesystemMountsWithoutOtherAccess) {
int blank_calls = 0;
int format_calls = 0;
int mount_calls = 0;
const auto result = prepareInternalSecondaryFilesystem(
[&]() { mount_calls++; return true; },
[&]() { blank_calls++; return false; },
[&]() { format_calls++; return true; });
EXPECT_EQ(result, InternalSecondaryFsBootResult::Mounted);
EXPECT_EQ(mount_calls, 1);
EXPECT_EQ(blank_calls, 0);
EXPECT_EQ(format_calls, 0);
}
TEST(InternalSecondaryFsBoot, FailedMountPreservesNonblankMedia) {
int format_calls = 0;
const auto result = prepareInternalSecondaryFilesystem(
[]() { return false; },
[]() { return false; },
[&]() { format_calls++; return true; });
EXPECT_EQ(result, InternalSecondaryFsBootResult::PreservedNonBlank);
EXPECT_EQ(format_calls, 0);
}
TEST(InternalSecondaryFsBoot, BlankMediaIsFormattedThenMounted) {
int mount_calls = 0;
int format_calls = 0;
const auto result = prepareInternalSecondaryFilesystem(
[&]() { return ++mount_calls == 2; },
[]() { return true; },
[&]() { format_calls++; return true; });
EXPECT_EQ(result, InternalSecondaryFsBootResult::InitializedBlank);
EXPECT_EQ(mount_calls, 2);
EXPECT_EQ(format_calls, 1);
}
TEST(InternalSecondaryFsBoot, BlankMediaFormatFailureStaysDisabled) {
int mount_calls = 0;
const auto result = prepareInternalSecondaryFilesystem(
[&]() { mount_calls++; return false; },
[]() { return true; },
[]() { return false; });
EXPECT_EQ(result, InternalSecondaryFsBootResult::InitializationFailed);
EXPECT_EQ(mount_calls, 1);
}
TEST(InternalSecondaryFsBoot, BlankMediaRemountFailureStaysDisabled) {
int mount_calls = 0;
const auto result = prepareInternalSecondaryFilesystem(
[&]() { mount_calls++; return false; },
[]() { return true; },
[]() { return true; });
EXPECT_EQ(result, InternalSecondaryFsBootResult::InitializationFailed);
EXPECT_EQ(mount_calls, 2);
}
TEST(InternalSecondaryFsRepair, RunsFormatMountAndValidationInOrder) {
int sequence = 0;
int formatted_at = 0;
int mounted_at = 0;
int validated_at = 0;
const auto result = repairInternalSecondaryFilesystem(
[&]() {
formatted_at = ++sequence;
return true;
},
[&]() {
mounted_at = ++sequence;
return true;
},
[&]() {
validated_at = ++sequence;
return true;
});
EXPECT_EQ(result, InternalSecondaryFsRepairResult::Repaired);
EXPECT_EQ(formatted_at, 1);
EXPECT_EQ(mounted_at, 2);
EXPECT_EQ(validated_at, 3);
}
TEST(InternalSecondaryFsRepair, FormatFailureStopsBeforeMount) {
int mount_calls = 0;
int validation_calls = 0;
const auto result = repairInternalSecondaryFilesystem(
[]() { return false; },
[&]() { mount_calls++; return true; },
[&]() { validation_calls++; return true; });
EXPECT_EQ(result, InternalSecondaryFsRepairResult::FormatFailed);
EXPECT_EQ(mount_calls, 0);
EXPECT_EQ(validation_calls, 0);
}
TEST(InternalSecondaryFsRepair, MountFailureStopsBeforeValidation) {
int validation_calls = 0;
const auto result = repairInternalSecondaryFilesystem(
[]() { return true; },
[]() { return false; },
[&]() { validation_calls++; return true; });
EXPECT_EQ(result, InternalSecondaryFsRepairResult::MountFailed);
EXPECT_EQ(validation_calls, 0);
}
TEST(InternalSecondaryFsRepair, RejectsFilesystemWhichRemainsInvalid) {
const auto result = repairInternalSecondaryFilesystem(
[]() { return true; },
[]() { return true; },
[]() { return false; });
EXPECT_EQ(result, InternalSecondaryFsRepairResult::ValidationFailed);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
+365
View File
@@ -0,0 +1,365 @@
#!/usr/bin/env python3
"""Contracts for the nRF52 Companion 100 KiB internal ExtraFS path."""
from pathlib import Path
import re
import unittest
ROOT = Path(__file__).resolve().parents[1]
PROFILE = ROOT / "variants" / "t1000-e" / "platformio.ini"
LINKER = ROOT / "boards" / "nrf52840_s140_v7_extrafs.ld"
MAIN = ROOT / "examples" / "companion_radio" / "main.cpp"
STORE = ROOT / "examples" / "companion_radio" / "DataStore.cpp"
STORE_HEADER = ROOT / "examples" / "companion_radio" / "DataStore.h"
MESH = ROOT / "examples" / "companion_radio" / "MyMesh.cpp"
def section(source: str, name: str) -> str:
start = source.index(f"[env:{name}]")
end = source.find("\n[", start + 1)
return source[start:] if end < 0 else source[start:end]
def function_body(source: str, signature: str) -> str:
start = source.index(signature)
opening = source.index("{", start)
depth = 0
for position in range(opening, len(source)):
if source[position] == "{":
depth += 1
elif source[position] == "}":
depth -= 1
if depth == 0:
return source[opening + 1 : position]
raise AssertionError(f"unterminated function: {signature}")
class Nrf52ExtraFsContractTest(unittest.TestCase):
def test_t1000_usb_and_ble_reserve_exact_100k_region(self):
profile = PROFILE.read_text(encoding="utf-8")
for environment in (
"t1000e_companion_radio_usb",
"t1000e_companion_radio_ble",
):
body = section(profile, environment)
self.assertIn(
"board_build.ldscript = boards/nrf52840_s140_v7_extrafs.ld",
body,
)
self.assertIn("board_upload.maximum_size = 708608", body)
linker = LINKER.read_text(encoding="utf-8")
self.assertRegex(
linker,
r"FLASH\s*\(rx\)\s*:\s*ORIGIN\s*=\s*0x27000,\s*"
r"LENGTH\s*=\s*0xD4000\s*-\s*0x27000",
)
main = MAIN.read_text(encoding="utf-8")
geometry = re.search(
r"CustomLFS\s+ExtraFS\s*\(\s*(0x[0-9A-Fa-f]+)\s*,\s*"
r"(0x[0-9A-Fa-f]+)\s*,\s*128\s*\)",
main,
)
self.assertIsNotNone(geometry)
start = int(geometry.group(1), 16)
size = int(geometry.group(2), 16)
self.assertEqual(start, 0xD4000)
self.assertEqual(size, 100 * 1024)
self.assertEqual(start + size, 0xED000)
def test_boot_mount_is_non_destructive_and_checks_runtime_geometry(self):
main = MAIN.read_text(encoding="utf-8")
setup = function_body(main, "void setup()")
self.assertIn("isExpectedInternalExtraFsGeometry(", setup)
self.assertIn("isInternalExtraFsReservedByApplication(", setup)
self.assertIn("uint32_t __flash_arduino_end[]", main)
self.assertIn("(uintptr_t)__flash_arduino_end", setup)
self.assertIn("prepareInternalSecondaryFilesystem(", setup)
self.assertIn(
"ExtraFS.Adafruit_LittleFS::begin()", setup
)
self.assertNotIn("ExtraFS.begin()", setup)
self.assertIn("PreservedNonBlank", setup)
self.assertIn("store.disableSecondaryFS();", setup)
self.assertLess(
setup.index("prepareInternalSecondaryFilesystem("),
setup.index("store.begin()"),
)
def test_corrupt_secondary_is_preserved_until_explicit_repair(self):
store = STORE.read_text(encoding="utf-8")
begin = function_body(store, "void DataStore::begin()")
invalid = re.search(
r"if\s*\(_fsExtra\s*!=\s*nullptr\s*&&\s*"
r"!validateLfsFilesystem\(_fsExtra\)\)\s*\{(.*?)\}",
begin,
re.DOTALL,
)
self.assertIsNotNone(invalid)
self.assertIn("_fsExtra = nullptr;", invalid.group(1))
self.assertNotIn("format", invalid.group(1))
self.assertNotIn("reinitializeInternalExtraFS", invalid.group(1))
header = STORE_HEADER.read_text(encoding="utf-8")
self.assertIn("FILESYSTEM* _configuredFsExtra;", header)
extra_constructor = re.search(
r"DataStore::DataStore\(FILESYSTEM& fs, FILESYSTEM& fsExtra,.*?\{",
store,
re.DOTALL,
)
self.assertIsNotNone(extra_constructor)
self.assertIn("_configuredFsExtra(&fsExtra)", extra_constructor.group(0))
def test_repair_is_internal_only_and_revalidates_before_activation(self):
store = STORE.read_text(encoding="utf-8")
repair = function_body(store, "bool DataStore::repairInternalExtraFS()")
self.assertRegex(
repair,
r"#if defined\(NRF52_PLATFORM\) && defined\(EXTRAFS\) "
r"&& !defined\(QSPIFLASH\)",
)
self.assertIn("reinitializeInternalExtraFS()", repair)
self.assertIn("return migrateToSecondaryFS();", repair)
active_retry = repair[
repair.index("if (_fsExtra != nullptr)") :
repair.index("if (!reinitializeInternalExtraFS())")
]
self.assertIn("return migrateToSecondaryFS();", active_retry)
self.assertNotIn("format()", active_retry)
reinitialize = function_body(
store, "bool DataStore::reinitializeInternalExtraFS()"
)
geometry_at = reinitialize.index("isExpectedInternalExtraFsGeometry(")
self.assertIn("isInternalExtraFsReservedByApplication(", reinitialize)
self.assertIn("uint32_t __flash_arduino_end[]", store)
self.assertIn("(uintptr_t)__flash_arduino_end", reinitialize)
unmount_at = reinitialize.index("extra->end()")
format_at = reinitialize.index("extra->format()")
mount_at = reinitialize.index(
"extra->Adafruit_LittleFS::begin()"
)
validate_at = reinitialize.index(
"validateLfsFilesystem(_configuredFsExtra)"
)
activate_at = reinitialize.index("_fsExtra = _configuredFsExtra;")
self.assertLess(geometry_at, unmount_at)
self.assertLess(unmount_at, format_at)
self.assertLess(format_at, mount_at)
self.assertLess(mount_at, validate_at)
self.assertLess(validate_at, activate_at)
def test_migration_failures_block_rebuild_and_can_be_retried(self):
store = STORE.read_text(encoding="utf-8")
header = STORE_HEADER.read_text(encoding="utf-8")
migration = function_body(store, "bool DataStore::migrateToSecondaryFS()")
self.assertIn("bool migrateToSecondaryFS();", header)
self.assertIn('SECONDARY_MIGRATION_JOURNAL = "/.extrafs.mig"', store)
pending_at = migration.index(
"writeJournal(SECONDARY_MIGRATION_PENDING)"
)
copy_at = migration.index("copySecondarySources()", pending_at)
committed_at = migration.index(
"writeJournal(SECONDARY_MIGRATION_COMMITTED)", copy_at
)
retire_at = migration.index("retireSecondarySources()", committed_at)
journal_remove_at = migration.index(
"_fsExtra->remove(SECONDARY_MIGRATION_JOURNAL)", retire_at
)
self.assertLess(pending_at, copy_at)
self.assertLess(copy_at, committed_at)
self.assertLess(committed_at, retire_at)
self.assertLess(retire_at, journal_remove_at)
copy_failure_end = migration.index(
"journal_state = MigrationJournalState::Committed", committed_at
)
self.assertIn(
"_fsExtra = nullptr;", migration[copy_at:copy_failure_end]
)
copy_helper = function_body(migration, "auto copy =")
self.assertNotIn("source_fs->remove(path)", copy_helper)
self.assertIn("return success;", migration)
self.assertIn("compared == expected_size", migration)
mesh = MESH.read_text(encoding="utf-8")
repair = function_body(mesh, "void MyMesh::repairInternalExtraFS(")
migrate_at = repair.index("_store->repairInternalExtraFS()")
return_at = repair.index("return;", migrate_at)
contacts_at = repair.index("_store->saveContacts(this, save_filter)")
self.assertLess(migrate_at, return_at)
self.assertLess(return_at, contacts_at)
self.assertNotIn("already_ready", repair)
def test_legacy_primary_files_move_before_contact_journal_can_fall_back(self):
store = STORE.read_text(encoding="utf-8")
migration = function_body(store, "bool DataStore::migrateToSecondaryFS()")
# Older ExtraFS test builds could leave identity and preferences only
# on the secondary filesystem. Recover those first: every journal
# failure below disables/falls back from that secondary filesystem.
self.assertIn(
'static const char* to_primary[] = {"/_main.id", "/new_prefs"};',
migration,
)
primary_move_at = migration.index("if (!migratePrimarySources())")
journal_flow_at = migration.index(
"MigrationJournalState journal_state = readJournal()"
)
self.assertLess(primary_move_at, journal_flow_at)
# A primary-present conflict is nonfatal: primary is canonical and the
# differing secondary copy is preserved for recovery. Only failure to
# create a missing primary copy may block the handoff.
copy_helper = function_body(migration, "auto copy =")
primary_helper = function_body(
migration, "auto migratePrimarySources ="
)
self.assertNotIn("source_fs->remove(path)", copy_helper)
self.assertLess(
copy_helper.rindex("filesEqual(source_fs, dest_fs, path)"),
copy_helper.rindex("return true;"),
)
conflict_at = primary_helper.index(
"if (!filesEqual(_fsExtra, _fs, path))"
)
missing_copy_at = primary_helper.index(
"else if (!copy(_fsExtra, _fs, path))"
)
remove_at = primary_helper.index("_fsExtra->remove(path)")
self.assertLess(conflict_at, missing_copy_at)
self.assertLess(missing_copy_at, remove_at)
conflict_branch = primary_helper[
conflict_at:missing_copy_at
]
self.assertIn("continue;", conflict_branch)
self.assertNotIn("success = false", conflict_branch)
missing_copy_branch = primary_helper[
missing_copy_at:remove_at
]
self.assertIn("success = false;", missing_copy_branch)
retire_branch = primary_helper[remove_at:]
self.assertNotIn("success = false;", retire_branch)
self.assertIn(
"preserving conflicting legacy secondary", primary_helper
)
def test_local_rescue_repair_rebuilds_live_contacts_and_channels(self):
mesh = MESH.read_text(encoding="utf-8")
repair = function_body(mesh, "void MyMesh::repairInternalExtraFS(")
self.assertIn("_store->repairInternalExtraFS()", repair)
self.assertIn("_store->saveContacts(this, save_filter)", repair)
self.assertIn("const bool channels_saved = saveChannels();", repair)
self.assertIn("_store->getStorageTotalKb() == 100", repair)
self.assertIn("getStorageTotalKb()", repair)
rescue = function_body(mesh, "void MyMesh::checkCLIRescueCmd()")
command_at = rescue.index('strcmp(cli_command, "repair extrafs")')
rebuild_at = rescue.index('strcmp(cli_command, "rebuild")')
repair_path = rescue[command_at:rebuild_at]
self.assertIn("repairInternalExtraFS(output);", repair_path)
terminal = function_body(mesh, "void MyMesh::handleTerminalCommand(")
self.assertIn('strcmp(command, "repair extrafs")', terminal)
self.assertIn("repairInternalExtraFS(terminalOutput());", terminal)
self.assertIn(
"repair extrafs (erases/rebuilds internal ExtraFS)", terminal
)
def test_rescue_rebuild_reports_success_only_after_every_save_succeeds(self):
mesh = MESH.read_text(encoding="utf-8")
rescue = function_body(mesh, "void MyMesh::checkCLIRescueCmd()")
rebuild_at = rescue.index('strcmp(cli_command, "rebuild")')
erase_at = rescue.index('strcmp(cli_command, "erase")', rebuild_at)
rebuild = rescue[rebuild_at:erase_at]
expected_saves = (
("identity_saved", "_store->saveMainIdentity(self_id)"),
("prefs_saved", "savePrefs()"),
("contacts_saved", "_store->saveContacts(this, save_filter)"),
("channels_saved", "saveChannels()"),
)
for result, call in expected_saves:
self.assertRegex(
rebuild,
rf"(?:const\s+)?bool\s+{result}\s*=\s*"
rf"{re.escape(call)}\s*;",
)
# A failed or still-pending contact rebuild must retain the lazy-write
# retry path instead of claiming that the destructive rebuild is done.
self.assertRegex(
rebuild,
r"(?:const\s+)?bool\s+contacts_complete\s*=\s*"
r"contacts_saved\s*&&\s*"
r"!_store->hasPendingContactWrites\(\)\s*;",
)
self.assertIsNotNone(
re.search(
r"if\s*\(\s*contacts_complete\s*\)\s*\{.*?\}\s*"
r"else\s*\{\s*scheduleContactWriteRetry\(\)\s*;",
rebuild,
re.DOTALL,
)
)
success_text = 'output.println(" > erase and rebuild done")'
success_at = rebuild.index(success_text)
success_prefix = rebuild[:success_at]
guard_at = success_prefix.rfind("if (")
brace_at = success_prefix.rfind("{")
self.assertGreaterEqual(guard_at, 0)
self.assertGreater(brace_at, guard_at)
success_guard = success_prefix[guard_at:brace_at]
for result in ("identity_saved", "prefs_saved", "channels_saved"):
self.assertIn(result, success_guard)
self.assertIn("contacts_complete", success_guard)
self.assertNotIn("||", success_guard)
# The positive message must follow all save attempts; the branch must
# also retain an explicit error response for a partial rebuild.
for _, call in expected_saves:
self.assertLess(rebuild.index(call), success_at)
self.assertIn("Error:", rebuild[success_at:])
def test_quarantined_extrafs_rescue_file_commands_are_null_safe(self):
mesh = MESH.read_text(encoding="utf-8")
rescue = function_body(mesh, "void MyMesh::checkCLIRescueCmd()")
unavailable = (
'ExtraFS is unavailable; run repair extrafs first'
)
cat_start = rescue.index('memcmp(cli_command, "cat", 3)')
rm_start = rescue.index('memcmp(cli_command, "rm ", 3)', cat_start)
reboot_start = rescue.index(
'strcmp(cli_command, "reboot")', rm_start
)
cat_path = rescue[cat_start:rm_start]
rm_path = rescue[rm_start:reboot_start]
for command_path in (cat_path, rm_path):
secondary_at = command_path.index(
"_store->getSecondaryFS()"
)
null_guard_at = command_path.index(
"if (selected_fs == nullptr)", secondary_at
)
access_at = min(
position
for operation in (
"_store->openRead(selected_fs, path)",
"_store->removeFile(selected_fs, path)",
)
if (position := command_path.find(operation)) >= 0
)
self.assertLess(null_guard_at, access_at)
self.assertIn(unavailable, command_path)
self.assertIn("uint8_t buffer[64];", cat_path)
self.assertNotRegex(cat_path, r"uint8_t\s+buffer\s*\[\s*file_size\s*\]")
if __name__ == "__main__":
unittest.main()
+66
View File
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
"""Static contract for the low-level ESP32 OTA commit reboot handoff."""
from pathlib import Path
import unittest
ROOT = Path(__file__).resolve().parents[1]
def function_body(source: str, signature: str) -> str:
start = source.index(signature)
opening_brace = source.index("{", start)
depth = 0
for position in range(opening_brace, len(source)):
if source[position] == "{":
depth += 1
elif source[position] == "}":
depth -= 1
if depth == 0:
return source[opening_brace + 1 : position]
raise AssertionError(f"unterminated function: {signature}")
class OtaDevRebootGuardContractTest(unittest.TestCase):
def test_slot_arm_never_reboots_inline(self):
header = (ROOT / "src/helpers/ota/OtaApply.h").read_text()
apply = (ROOT / "src/helpers/ota/OtaApply.cpp").read_text()
self.assertIn("bool ota_apply_arm();", header)
arm = function_body(apply, "bool ota_apply_arm()")
self.assertIn("esp_ota_set_boot_partition", arm)
self.assertNotIn("esp_restart", arm)
self.assertNotIn("ota_apply_commit", apply)
def test_dev_commit_arms_the_guarded_mesh_reboot(self):
cli = (ROOT / "src/helpers/ota/OtaCli.cpp").read_text()
context = (ROOT / "src/helpers/ota/OtaContext.h").read_text()
mesh = (ROOT / "src/Mesh.cpp").read_text()
dev = function_body(
cli,
"static bool handle_dev(const char* d, char* reply, OtaContext& c) {",
)
commit_at = dev.index('strncmp(sub, "commit", 6)')
commit = dev[commit_at : dev.index("} else {", commit_at)]
arm_at = commit.index("ota_apply_arm()")
pending_at = commit.index("c.apply_pending = true")
self.assertLess(arm_at, pending_at)
self.assertIn("c.bootloader_apply_pending = false", commit)
self.assertIn("c.apply_at = 0", commit)
self.assertIn("c.apply_hard = 0", commit)
self.assertNotIn("ota_reboot_to_apply", commit)
self.assertNotIn("esp_restart", commit)
self.assertIn("bool apply_pending = false;", context)
maintenance = function_body(
mesh, "void __attribute__((noinline)) Mesh::serviceLoopMaintenance()"
)
guard_at = maintenance.index("prepareForOtaReboot()")
reboot_at = maintenance.index("ota::ota_reboot_to_apply()")
self.assertLess(guard_at, reboot_at)
if __name__ == "__main__":
unittest.main()