diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index c583283de..76ad788d1 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -20,6 +20,7 @@ #include "bridges/MQTTBridge.h" #include "MQTTDefaults.h" #include "MQTTPrefsCodec.h" +#include "MQTTPrefsRecovery.h" #endif // Believe it or not, this std C function is busted on some platforms! @@ -473,20 +474,85 @@ static void setMQTTPrefsDefaults(MQTTPrefs* prefs) { applyMQTTDefaults(prefs); } -static File openMqttPrefsRead(FILESYSTEM* fs) { +static File openMqttPrefsRead(FILESYSTEM* fs, const char* path = "/mqtt_prefs") { #if defined(RP2040_PLATFORM) - return fs->open("/mqtt_prefs", "r"); + return fs->open(path, "r"); #else - return fs->open("/mqtt_prefs"); + return fs->open(path); #endif } -// Filesystem adapter for MQTTPrefsAtomicStore. It writes the new image only to -// /mqtt_prefs.tmp, verifies size, then publishes by renaming over /mqtt_prefs. -// ESP32 targets use SPIFFS, whose rename refuses an existing destination -// (SPIFFS_ERR_CONFLICTING_NAME) — so commit() must remove /mqtt_prefs first. -// That is a brief non-atomic window; the verified tmp remains until rename -// succeeds, and abort() cleans it up on failure. +static MQTTPrefsRecovery::FileState mqttPrefsFileState(FILESYSTEM* fs, const char* path) { + if (!fs->exists(path)) return MQTTPrefsRecovery::FileState::Missing; + File file = openMqttPrefsRead(fs, path); + if (!file) return MQTTPrefsRecovery::FileState::Preserve; + const size_t file_size = file.size(); + uint8_t prefix[sizeof(MQTTPrefsHeader)] = {}; + const size_t prefix_size = file_size < sizeof(prefix) ? file_size : sizeof(prefix); + const size_t prefix_read = file.read(prefix, prefix_size); + file.close(); + return MQTTPrefsCodec::classify(prefix, prefix_read, file_size).preserve_file + ? MQTTPrefsRecovery::FileState::Preserve + : MQTTPrefsRecovery::FileState::Usable; +} + +// Restore the only usable image before the normal loader inspects /mqtt_prefs. +// SPIFFS cannot rename over an existing destination, so publishing moves the +// old primary to .bak before moving the verified temp into the empty name. +// The decision helper deliberately treats unsupported/corrupt files as opaque: +// no recovery path overwrites one with an older layout. +static bool recoverMqttPrefsFiles(FILESYSTEM* fs) { + const MQTTPrefsRecovery::FileState primary = mqttPrefsFileState(fs, "/mqtt_prefs"); + const MQTTPrefsRecovery::FileState temp = mqttPrefsFileState(fs, "/mqtt_prefs.tmp"); + const MQTTPrefsRecovery::FileState backup = mqttPrefsFileState(fs, "/mqtt_prefs.bak"); + const MQTTPrefsRecovery::Action action = MQTTPrefsRecovery::select(primary, temp, backup); + + if (action == MQTTPrefsRecovery::Action::KeepPrimary) { + // A current/known legacy primary has already published. Every transaction + // artifact is therefore unpublished or stale, including a partial temp + // left by a reset during write(), and can be discarded. Preserve artifacts + // only when the primary itself is opaque (the branch above still keeps it). + if (primary == MQTTPrefsRecovery::FileState::Usable) { + if (temp != MQTTPrefsRecovery::FileState::Missing) fs->remove("/mqtt_prefs.tmp"); + if (backup != MQTTPrefsRecovery::FileState::Missing) fs->remove("/mqtt_prefs.bak"); + } + return false; + } + if (action == MQTTPrefsRecovery::Action::PromoteTemp) { + if (fs->rename("/mqtt_prefs.tmp", "/mqtt_prefs")) { + // A usable temp is now the committed primary. Its backup is necessarily + // a stale transaction artifact, even if this firmware cannot decode it. + if (temp == MQTTPrefsRecovery::FileState::Usable && + backup != MQTTPrefsRecovery::FileState::Missing) { + fs->remove("/mqtt_prefs.bak"); + } + MESH_DEBUG_PRINTLN("MQTT: recovered /mqtt_prefs from transaction temp"); + return false; + } + MESH_DEBUG_PRINTLN("MQTT: could not recover /mqtt_prefs temp; files preserved"); + return true; + } + if (action == MQTTPrefsRecovery::Action::PromoteBackup) { + if (fs->rename("/mqtt_prefs.bak", "/mqtt_prefs")) { + // Symmetric case: a usable backup is now primary, so any interrupted + // temp is no longer authoritative and must not block a later save. + if (backup == MQTTPrefsRecovery::FileState::Usable && + temp != MQTTPrefsRecovery::FileState::Missing) { + fs->remove("/mqtt_prefs.tmp"); + } + MESH_DEBUG_PRINTLN("MQTT: recovered /mqtt_prefs from transaction backup"); + return false; + } + MESH_DEBUG_PRINTLN("MQTT: could not recover /mqtt_prefs backup; files preserved"); + return true; + } + return false; +} + +// Filesystem adapter for MQTTPrefsAtomicStore. It writes the new image to +// /mqtt_prefs.tmp and verifies its size. Publishing is a recoverable SPIFFS +// transaction: primary -> .bak, then tmp -> primary, then best-effort backup +// cleanup. A power loss at every boundary leaves at least one recoverable file. class MQTTPrefsFileStore { public: explicit MQTTPrefsFileStore(FILESYSTEM* fs) : _fs(fs) {} @@ -494,17 +560,13 @@ public: bool begin() { _finished = false; _open = false; + _owns_temp = false; _bytes_written = 0; - // Clear only a stale, unpublished transaction. Guarded on exists(): on the - // normal path commit() renames the tmp away, so there is nothing to remove - // and an unconditional remove() makes the ESP32 VFS layer log - // "[E] vfs_api.cpp remove(): /mqtt_prefs.tmp does not exists" on EVERY save. - // Functionally harmless, but it looks like a fault to operators reading the - // serial log during a config change. - if (_fs->exists("/mqtt_prefs.tmp")) { - _fs->remove("/mqtt_prefs.tmp"); - if (_fs->exists("/mqtt_prefs.tmp")) return false; // could not clear it - } + // Recovery owns stale artifacts. Do not delete them here: a failed commit + // may have moved the old primary to .bak and left a verified temp that the + // next boot must choose between. Refusing the save is safer than erasing an + // image this firmware cannot decode. + if (_fs->exists("/mqtt_prefs.tmp") || _fs->exists("/mqtt_prefs.bak")) return false; #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) _file = _fs->open("/mqtt_prefs.tmp", FILE_O_WRITE); #elif defined(RP2040_PLATFORM) @@ -513,6 +575,7 @@ public: _file = _fs->open("/mqtt_prefs.tmp", "w", true); #endif _open = _file; + _owns_temp = _open; return _open; } @@ -542,19 +605,32 @@ public: bool commit() { if (!_finished) return false; - // SPIFFS: rename(tmp, dest) fails when dest exists. LittleFS/POSIX replace - // in place; remove-then-rename is required on ESP32 and correct elsewhere. - if (_fs->exists("/mqtt_prefs") && !_fs->remove("/mqtt_prefs")) { + // SPIFFS refuses rename(tmp, existing_dest). Move the existing image to a + // recoverable backup first, then publish temp into the now-empty primary. + // Never remove either image after a failed boundary; boot recovery selects + // the completed temp or restores the backup. + if (_fs->exists("/mqtt_prefs.bak")) return false; + if (_fs->exists("/mqtt_prefs") && !_fs->rename("/mqtt_prefs", "/mqtt_prefs.bak")) { return false; } - return _fs->rename("/mqtt_prefs.tmp", "/mqtt_prefs"); + if (!_fs->rename("/mqtt_prefs.tmp", "/mqtt_prefs")) return false; + // Cleanup failure is non-fatal: the new primary is published and recovery + // will remove a known-good stale backup on a later boot. + if (_fs->exists("/mqtt_prefs.bak")) _fs->remove("/mqtt_prefs.bak"); + return true; } void abort() { if (_open) _file.close(); _open = false; + // Once finish() has verified the temp, commit may already have moved the + // primary to .bak. Keep the temp on a commit failure so recovery can + // publish it (or fall back to .bak) after reset. + if (_owns_temp && !_finished && _fs->exists("/mqtt_prefs.tmp")) { + _fs->remove("/mqtt_prefs.tmp"); + } _finished = false; - if (_fs->exists("/mqtt_prefs.tmp")) _fs->remove("/mqtt_prefs.tmp"); + _owns_temp = false; } private: @@ -562,6 +638,7 @@ private: File _file; bool _open = false; bool _finished = false; + bool _owns_temp = false; size_t _bytes_written = 0; }; @@ -684,7 +761,10 @@ static const char* mqttPrefsSaveResultName(MQTTPrefsAtomicStore::Result result) void CommonCLI::loadMQTTPrefs( FILESYSTEM* fs, MQTTPrefsAtomicStore::LegacyUpgradeGate* legacy_upgrade) { setMQTTPrefsDefaults(&_mqtt_prefs); - _mqtt_prefs_hold = false; + // Complete or preserve an interrupted SPIFFS transaction before decoding. + // A failed recovery leaves the artifacts untouched and blocks this boot from + // replacing them with defaults through a later CLI save. + _mqtt_prefs_hold = recoverMqttPrefsFiles(fs); bool has_observer_fields = false; bool mqtt_rewrite_pending = false; bool migrated_legacy_mqtt = false; diff --git a/src/helpers/MQTTPrefsRecovery.h b/src/helpers/MQTTPrefsRecovery.h new file mode 100644 index 000000000..7534678eb --- /dev/null +++ b/src/helpers/MQTTPrefsRecovery.h @@ -0,0 +1,49 @@ +#pragma once + +#include + +// Pure recovery policy for the three MQTT preference transaction files. The +// writer first moves the old primary to .bak, then moves the verified .tmp to +// the primary name. On a reset, the loader uses this policy before decoding +// /mqtt_prefs. "Preserve" is deliberately distinct from "Usable": it covers +// an unsupported newer layout, corruption, or an unreadable file and must +// never be replaced by an older image. +namespace MQTTPrefsRecovery { + +enum class FileState : uint8_t { + Missing, + Usable, + Preserve, +}; + +enum class Action : uint8_t { + None, + KeepPrimary, + PromoteTemp, + PromoteBackup, +}; + +inline Action select(FileState primary, FileState temp, FileState backup) { + // A primary of any kind owns the name. In particular, do not roll a newer + // or corrupt primary back to an older backup just because it cannot be read + // by this firmware. + if (primary != FileState::Missing) return Action::KeepPrimary; + + // A completed temp is the new image and wins over the old backup. + if (temp == FileState::Usable) return Action::PromoteTemp; + + // If temp is opaque but a known-good backup exists, boot from the backup. + // The caller may discard the opaque temp once that usable backup has become + // primary. Otherwise, rename the opaque temp into the empty primary name so + // the normal loader can hold it. + if (temp == FileState::Preserve) { + return backup == FileState::Usable ? Action::PromoteBackup : Action::PromoteTemp; + } + + // No temp survived. The backup is the only recoverable image, even when it + // is a newer layout that this firmware must preserve rather than decode. + if (backup != FileState::Missing) return Action::PromoteBackup; + return Action::None; +} + +} // namespace MQTTPrefsRecovery diff --git a/test/test_mqtt_prefs_atomic_store/test_mqtt_prefs_atomic_store.cpp b/test/test_mqtt_prefs_atomic_store/test_mqtt_prefs_atomic_store.cpp index 32561dd6c..67880a824 100644 --- a/test/test_mqtt_prefs_atomic_store/test_mqtt_prefs_atomic_store.cpp +++ b/test/test_mqtt_prefs_atomic_store/test_mqtt_prefs_atomic_store.cpp @@ -6,8 +6,10 @@ #include #include "helpers/MQTTPrefsAtomicStore.h" +#include "helpers/MQTTPrefsRecovery.h" namespace AtomicStore = MQTTPrefsAtomicStore; +namespace Recovery = MQTTPrefsRecovery; namespace { @@ -23,14 +25,18 @@ enum class FailurePoint { class InMemoryStore { public: - explicit InMemoryStore(FailurePoint failure) : _failure(failure) { + explicit InMemoryStore(FailurePoint failure, bool preexisting_recovery_temp = false) + : _failure(failure), _preexisting_recovery_temp(preexisting_recovery_temp) { _files["/mqtt_prefs"] = {'o', 'l', 'd', '-', 'p', 'r', 'e', 'f', 's'}; + if (_preexisting_recovery_temp) _files["/mqtt_prefs.tmp"] = {'r', 'e', 'c', 'o', 'v', 'e', 'r'}; } bool begin() { ++begin_calls; + if (_preexisting_recovery_temp) return false; _files.erase("/mqtt_prefs.tmp"); _open = _failure != FailurePoint::Begin; + _owns_temp = _open; return _open; } @@ -49,6 +55,7 @@ public: _open = false; if (_failure == FailurePoint::Finish) return false; _files["/mqtt_prefs.tmp"] = _staging; + _finished = true; return true; } @@ -57,6 +64,7 @@ public: if (_failure == FailurePoint::Commit) return false; _files["/mqtt_prefs"] = _files["/mqtt_prefs.tmp"]; _files.erase("/mqtt_prefs.tmp"); + _finished = false; return true; } @@ -64,7 +72,12 @@ public: ++abort_calls; _open = false; _staging.clear(); - _files.erase("/mqtt_prefs.tmp"); + // Mirrors MQTTPrefsFileStore: after finish(), a failed commit may already + // have moved the old primary to .bak, so the verified temp is recovery + // data rather than disposable staging. + if (_owns_temp && !_finished) _files.erase("/mqtt_prefs.tmp"); + _finished = false; + _owns_temp = false; } const std::vector& source() const { return _files.at("/mqtt_prefs"); } @@ -78,7 +91,10 @@ public: private: FailurePoint _failure; + bool _preexisting_recovery_temp = false; bool _open = false; + bool _finished = false; + bool _owns_temp = false; std::vector _staging; std::map> _files; }; @@ -212,67 +228,95 @@ AtomicStore::ImageResult runCommonPrefsImage(InMemoryCommonPrefsStore* store) { }); } -// Models ESP32 SPIFFS rename semantics: rename fails when the destination -// already exists (SPIFFS_ERR_CONFLICTING_NAME). Production MQTTPrefsFileStore -// must remove /mqtt_prefs before renaming the verified tmp into place. -class SpiffsLikeMqttPrefsStore { +// Models the exact SPIFFS transaction used by MQTTPrefsFileStore. Files only +// move by rename: SPIFFS rejects a destination that already exists, so the old +// primary must remain available as .bak until the new temp owns the primary. +class SpiffsMqttTransaction { public: - SpiffsLikeMqttPrefsStore() { - _files["/mqtt_prefs"] = {'o', 'l', 'd', '-', 'p', 'r', 'e', 'f', 's'}; + enum class Boundary { + BeforeBackupRename, + AfterBackupRename, + AfterPrimaryRename, + AfterBackupCleanup, + }; + + SpiffsMqttTransaction() { + _files["/mqtt_prefs"] = oldImage(); } - bool begin() { - _files.erase("/mqtt_prefs.tmp"); - _staging.clear(); - _open = true; - return true; + void writeVerifiedTemp() { _files["/mqtt_prefs.tmp"] = newImage(); } + void cutDuringTempWrite() { _files["/mqtt_prefs.tmp"] = {'n'}; } + + void cutAt(Boundary boundary) { + writeVerifiedTemp(); + if (boundary == Boundary::BeforeBackupRename) return; + rename("/mqtt_prefs", "/mqtt_prefs.bak"); + if (boundary == Boundary::AfterBackupRename) return; + rename("/mqtt_prefs.tmp", "/mqtt_prefs"); + if (boundary == Boundary::AfterPrimaryRename) return; + _files.erase("/mqtt_prefs.bak"); } - size_t write(const uint8_t* bytes, size_t size) { - if (!_open) return 0; - _staging.insert(_staging.end(), bytes, bytes + size); - return size; + // Inject ordinary operation failures (as distinct from a power cut). A + // failed temp rename leaves both the verified temp and old backup intact. + bool publish(bool fail_backup_rename, bool fail_temp_rename, bool fail_cleanup) { + writeVerifiedTemp(); + if (fail_backup_rename || !rename("/mqtt_prefs", "/mqtt_prefs.bak")) return false; + if (fail_temp_rename || !rename("/mqtt_prefs.tmp", "/mqtt_prefs")) return false; + if (!fail_cleanup) _files.erase("/mqtt_prefs.bak"); + return true; // backup cleanup is intentionally non-fatal after publish } - bool finish() { - _open = false; - _files["/mqtt_prefs.tmp"] = _staging; - _finished = true; - return true; + void recover(Recovery::FileState primary = Recovery::FileState::Usable, + Recovery::FileState temp = Recovery::FileState::Usable, + Recovery::FileState backup = Recovery::FileState::Usable) { + const bool had_primary = _files.count("/mqtt_prefs") != 0; + const auto stateFor = [&](const char* path, Recovery::FileState readable) { + return _files.count(path) == 0 ? Recovery::FileState::Missing : readable; + }; + const Recovery::Action action = Recovery::select( + stateFor("/mqtt_prefs", primary), stateFor("/mqtt_prefs.tmp", temp), + stateFor("/mqtt_prefs.bak", backup)); + if (action == Recovery::Action::PromoteTemp) { + rename("/mqtt_prefs.tmp", "/mqtt_prefs"); + // Match production: once a usable temp becomes primary, every backup is + // stale and is cleared so a second save can start this boot. + if (temp == Recovery::FileState::Usable && backup != Recovery::FileState::Missing) { + _files.erase("/mqtt_prefs.bak"); + } + return; + } + if (action == Recovery::Action::PromoteBackup) { + rename("/mqtt_prefs.bak", "/mqtt_prefs"); + if (backup == Recovery::FileState::Usable && temp != Recovery::FileState::Missing) { + _files.erase("/mqtt_prefs.tmp"); + } + return; + } + + // A usable primary is authoritative, so production cleans every stale or + // incomplete transaction artifact. It only preserves artifacts when the + // primary itself is opaque. + if (had_primary && primary == Recovery::FileState::Usable) { + _files.erase("/mqtt_prefs.tmp"); + _files.erase("/mqtt_prefs.bak"); + } } - // Naive POSIX-style replace — wrong for SPIFFS when dest exists. - bool commitReplaceInPlace() { - if (!_finished || _files.count("/mqtt_prefs.tmp") == 0) return false; - if (_files.count("/mqtt_prefs") != 0) return false; // CONFLICTING_NAME - _files["/mqtt_prefs"] = _files["/mqtt_prefs.tmp"]; - _files.erase("/mqtt_prefs.tmp"); - return true; - } - - // Matches CommonCLI MQTTPrefsFileStore::commit() on ESP32. - bool commitRemoveThenRename() { - if (!_finished || _files.count("/mqtt_prefs.tmp") == 0) return false; - _files.erase("/mqtt_prefs"); - _files["/mqtt_prefs"] = _files["/mqtt_prefs.tmp"]; - _files.erase("/mqtt_prefs.tmp"); - return true; - } - - void abort() { - _open = false; - _finished = false; - _staging.clear(); - _files.erase("/mqtt_prefs.tmp"); - } - - const std::vector& source() const { return _files.at("/mqtt_prefs"); } - bool tempExists() const { return _files.count("/mqtt_prefs.tmp") != 0; } + bool has(const char* path) const { return _files.count(path) != 0; } + bool canStartSave() const { return !has("/mqtt_prefs.tmp") && !has("/mqtt_prefs.bak"); } + const std::vector& primary() const { return _files.at("/mqtt_prefs"); } + static std::vector oldImage() { return {'o', 'l', 'd'}; } + static std::vector newImage() { return {'n', 'e', 'w'}; } private: - bool _open = false; - bool _finished = false; - std::vector _staging; + bool rename(const char* from, const char* to) { + if (_files.count(from) == 0 || _files.count(to) != 0) return false; + _files[to] = _files[from]; + _files.erase(from); + return true; + } + std::map> _files; }; @@ -313,7 +357,7 @@ TEST(MQTTPrefsAtomicStore, AnyFailureAbortsAndPreservesExistingSource) { InMemoryStore store(test_case.point); EXPECT_EQ(test_case.expected, run(&store)); EXPECT_EQ(source, store.source()); - EXPECT_FALSE(store.tempExists()); + EXPECT_EQ(test_case.point == FailurePoint::Commit, store.tempExists()); EXPECT_EQ(1, store.begin_calls); EXPECT_EQ(test_case.writes, store.write_calls); EXPECT_EQ(test_case.finishes, store.finish_calls); @@ -322,6 +366,14 @@ TEST(MQTTPrefsAtomicStore, AnyFailureAbortsAndPreservesExistingSource) { } } +TEST(MQTTPrefsAtomicStore, BeginFailureDoesNotErasePreexistingRecoveryTemp) { + InMemoryStore store(FailurePoint::Begin, true); + + EXPECT_EQ(AtomicStore::Result::BeginFailed, run(&store)); + EXPECT_TRUE(store.tempExists()); + EXPECT_EQ(1, store.abort_calls); +} + TEST(MQTTPrefsAtomicStore, LegacyCrossFileUpgradeCommitsTailBeforeCompactingComPrefs) { InMemoryStore mqtt_store(FailurePoint::None); LegacyComPrefs com_prefs; @@ -457,37 +509,112 @@ TEST(MQTTPrefsAtomicStore, NodePrefsMigrationFailurePreservesSourceAndNeverPrefe } } -TEST(MQTTPrefsAtomicStore, SpiffsRenameRequiresRemoveBeforePublish) { - const uint8_t header[] = {0xf5, 'M', 'Q', 'P', 1, 0, 0x09, 0x00}; - const uint8_t payload[] = {'n', 'e', 'w', '-', 'p', 'r', 'e', 'f', 's'}; - const std::vector published = {0xf5, 'M', 'Q', 'P', 1, 0, 0x09, 0x00, - 'n', 'e', 'w', '-', 'p', 'r', 'e', 'f', 's'}; - const std::vector previous = {'o', 'l', 'd', '-', 'p', 'r', 'e', 'f', 's'}; +TEST(MQTTPrefsAtomicStore, SpiffsPowerCutsAtEveryPublishBoundaryLeaveRecoverableImage) { + const struct { + SpiffsMqttTransaction::Boundary boundary; + std::vector expected_after_reboot; + } cases[] = { + // Temp has not become the committed image yet, so the old primary wins. + {SpiffsMqttTransaction::Boundary::BeforeBackupRename, SpiffsMqttTransaction::oldImage()}, + // Old primary is .bak and verified new temp wins the recovery race. + {SpiffsMqttTransaction::Boundary::AfterBackupRename, SpiffsMqttTransaction::newImage()}, + {SpiffsMqttTransaction::Boundary::AfterPrimaryRename, SpiffsMqttTransaction::newImage()}, + {SpiffsMqttTransaction::Boundary::AfterBackupCleanup, SpiffsMqttTransaction::newImage()}, + }; - { - SpiffsLikeMqttPrefsStore store; - ASSERT_TRUE(store.begin()); - ASSERT_EQ(sizeof(header), store.write(header, sizeof(header))); - ASSERT_EQ(sizeof(payload), store.write(payload, sizeof(payload))); - ASSERT_TRUE(store.finish()); - // Dest exists → SPIFFS-style rename must fail (the pre-fix production bug). - EXPECT_FALSE(store.commitReplaceInPlace()); - EXPECT_EQ(previous, store.source()); - EXPECT_TRUE(store.tempExists()); - store.abort(); - EXPECT_FALSE(store.tempExists()); + for (const auto& test_case : cases) { + SpiffsMqttTransaction store; + store.cutAt(test_case.boundary); + store.recover(); + ASSERT_TRUE(store.has("/mqtt_prefs")); + EXPECT_EQ(test_case.expected_after_reboot, store.primary()); + EXPECT_FALSE(store.has("/mqtt_prefs.tmp")); + EXPECT_FALSE(store.has("/mqtt_prefs.bak")); } +} +TEST(MQTTPrefsAtomicStore, PowerCutDuringTempWriteKeepsPrimaryAndAllowsNextSave) { + SpiffsMqttTransaction store; + store.cutDuringTempWrite(); + + // The partial temp is opaque to the codec, but the existing primary is the + // only committed image. Recovery discards the incomplete transaction rather + // than blocking every later config save behind /mqtt_prefs.tmp. + store.recover(Recovery::FileState::Usable, Recovery::FileState::Preserve); + EXPECT_EQ(SpiffsMqttTransaction::oldImage(), store.primary()); + EXPECT_FALSE(store.has("/mqtt_prefs.tmp")); + EXPECT_TRUE(store.canStartSave()); +} + +TEST(MQTTPrefsAtomicStore, RecoveredUsablePrimaryClearsOpaqueTransactionArtifacts) { { - SpiffsLikeMqttPrefsStore store; - ASSERT_TRUE(store.begin()); - ASSERT_EQ(sizeof(header), store.write(header, sizeof(header))); - ASSERT_EQ(sizeof(payload), store.write(payload, sizeof(payload))); - ASSERT_TRUE(store.finish()); - EXPECT_TRUE(store.commitRemoveThenRename()); - EXPECT_EQ(published, store.source()); - EXPECT_FALSE(store.tempExists()); + SpiffsMqttTransaction store; + store.cutAt(SpiffsMqttTransaction::Boundary::AfterBackupRename); + // A current-format temp wins; the old backup need not be decodable to be + // stale once that usable temp owns the primary name. + store.recover(Recovery::FileState::Usable, Recovery::FileState::Usable, + Recovery::FileState::Preserve); + EXPECT_EQ(SpiffsMqttTransaction::newImage(), store.primary()); + EXPECT_TRUE(store.canStartSave()); } + { + SpiffsMqttTransaction store; + store.cutAt(SpiffsMqttTransaction::Boundary::AfterBackupRename); + // Conversely, when the usable backup becomes primary, an opaque temp was + // never published and must not leave saves permanently blocked. + store.recover(Recovery::FileState::Usable, Recovery::FileState::Preserve, + Recovery::FileState::Usable); + EXPECT_EQ(SpiffsMqttTransaction::oldImage(), store.primary()); + EXPECT_TRUE(store.canStartSave()); + } +} + +TEST(MQTTPrefsAtomicStore, SpiffsRenameAndCleanupFailuresRemainRecoverable) { + { + SpiffsMqttTransaction store; + EXPECT_FALSE(store.publish(true, false, false)); + store.recover(); + EXPECT_EQ(SpiffsMqttTransaction::oldImage(), store.primary()); + EXPECT_FALSE(store.has("/mqtt_prefs.tmp")); + EXPECT_FALSE(store.has("/mqtt_prefs.bak")); + } + { + SpiffsMqttTransaction store; + EXPECT_FALSE(store.publish(false, true, false)); + EXPECT_FALSE(store.has("/mqtt_prefs")); + EXPECT_TRUE(store.has("/mqtt_prefs.tmp")); + EXPECT_TRUE(store.has("/mqtt_prefs.bak")); + store.recover(); + EXPECT_EQ(SpiffsMqttTransaction::newImage(), store.primary()); + EXPECT_FALSE(store.has("/mqtt_prefs.tmp")); + EXPECT_FALSE(store.has("/mqtt_prefs.bak")); + } + { + SpiffsMqttTransaction store; + EXPECT_TRUE(store.publish(false, false, true)); + EXPECT_EQ(SpiffsMqttTransaction::newImage(), store.primary()); + EXPECT_TRUE(store.has("/mqtt_prefs.bak")); + store.recover(); + EXPECT_FALSE(store.has("/mqtt_prefs.bak")); + } +} + +TEST(MQTTPrefsAtomicStore, RecoveryNeverOverwritesOpaqueNewerLayout) { + // An unreadable primary owns its name, even if an older usable backup and a + // verified temp exist. This is the downgrade-preservation invariant. + EXPECT_EQ(Recovery::Action::KeepPrimary, + Recovery::select(Recovery::FileState::Preserve, Recovery::FileState::Usable, + Recovery::FileState::Usable)); + // If there is no primary, a usable backup wins over an opaque temp. Once + // promoted, production treats the backup as authoritative and clears temp. + EXPECT_EQ(Recovery::Action::PromoteBackup, + Recovery::select(Recovery::FileState::Missing, Recovery::FileState::Preserve, + Recovery::FileState::Usable)); + // With no other image, an opaque backup is renamed into the empty primary + // name so CommonCLI will hold it rather than silently replace it with defaults. + EXPECT_EQ(Recovery::Action::PromoteBackup, + Recovery::select(Recovery::FileState::Missing, Recovery::FileState::Missing, + Recovery::FileState::Preserve)); } int main(int argc, char** argv) {