Fix storage recovery and deferred runtime state

Recover usable ESP32 preferences and channels before permitting replacement; keep identity and ACL persistence transactional and complete.

Preserve MQTT commit boundaries, restore OTA identity and policy after deferred allocation, retain radio gain retries, and reject invalid flood limits. Add production-path fault regressions and CI coverage.
This commit is contained in:
mikecarper committed 2026-09-15 16:46:17 -07:00
1 parent 86defc840c
commit e2a1828987
32 files changed
+1546 -189

No files matched your search

+9
View File
@@ -13,6 +13,7 @@
#define MESH_DEBUG_PRINTLN(...) ((void)0)
class FakeFilesystem;
inline FakeFilesystem* metadata_filesystem = nullptr;
struct FakeFileHandle {
FakeFilesystem* fs;
std::string path;
@@ -38,6 +39,7 @@ public:
class FakeFilesystem {
public:
FakeFilesystem() { metadata_filesystem = this; }
std::map<std::string, std::vector<uint8_t>> files;
std::set<std::string> unreadable;
std::set<std::string> directories_on_read;
@@ -54,6 +56,10 @@ public:
size_t bytes_written = 0;
size_t directory_closes = 0;
size_t missing_read_opens = 0;
bool metadata_error = false;
std::string fail_read_path;
size_t fail_read_open = 0;
size_t fail_read_after = 0;
bool exists(const char* path) const { return files.count(path) != 0; }
File open(const char* path, const char* mode = "r", bool = false) {
@@ -94,6 +100,9 @@ inline size_t File::size() const {
}
inline int File::read(uint8_t* output, size_t length) {
if (!*this || handle->directory) return 0;
if (handle->fs->fail_read_path == handle->path
&& handle->fs->read_open_count[handle->path] == handle->fs->fail_read_open
&& handle->position >= handle->fs->fail_read_after) return 0;
auto found = handle->fs->files.find(handle->path);
if (found == handle->fs->files.end() || handle->position >= found->second.size()) return 0;
length = std::min(length, found->second.size() - handle->position);
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include <cerrno>
#include <cstring>
struct stat {};
inline int stat(const char* path, struct stat*) {
if (!metadata_filesystem || metadata_filesystem->metadata_error) {
errno = EIO;
return -1;
}
if (strncmp(path, "/spiffs", 7) != 0) { errno = EINVAL; return -1; }
if (!metadata_filesystem->files.count(path + 7)) { errno = ENOENT; return -1; }
return 0;
}
+42 -1
View File
@@ -475,9 +475,50 @@ static void allocation_failure_preserves_saved_clients() {
CHECK(recovered.getNumClients() == 1 && recovered.getClient(KEY, PUB_KEY_SIZE));
}
static void incomplete_acl_load_is_never_authoritative() {
for (unsigned fault = 0; fault < 5; ++fault) {
FakeFilesystem fs;
ClientACL original;
original.load(&fs, SELF);
CHECK(original.putClient(mesh::Identity(KEY), PERM_ACL_ADMIN));
CHECK(original.putClient(mesh::Identity(SECOND_KEY), PERM_ACL_REGION_MGR));
CHECK(original.save(&fs));
const auto image = fs.files["/s_contacts"];
fs.read_open_count.clear();
if (fault < 2) {
fs.fail_read_path = "/s_contacts";
fs.fail_read_open = 2; // integrity pass succeeds, actual load fails
fs.fail_read_after = fault == 0 ? 0 : CONTACT_RECORD_SIZE;
} else if (fault == 2) {
fs.unreadable.insert("/s_contacts");
} else if (fault == 3) {
fs.metadata_error = true;
} else {
fs.files["/s_contacts"].resize(CONTACT_RECORD_SIZE + 5);
}
const auto retained = fs.files["/s_contacts"];
ClientACL partial;
partial.load(&fs, SELF);
CHECK(partial.getNumClients() == 0);
CHECK(!partial.putClient(mesh::Identity(ARCHIVED_KEY), PERM_ACL_ADMIN));
CHECK(!partial.applyPermissions(SELF, KEY, PUB_KEY_SIZE, PERM_ACL_ADMIN));
CHECK(!partial.save(&fs));
CHECK(!partial.authorizeLoginTimestamp(KEY, 900, 0, PERM_ACL_ADMIN));
CHECK(fs.files["/s_contacts"] == retained);
fs.fail_read_path.clear(); fs.unreadable.clear(); fs.metadata_error = false;
fs.files["/s_contacts"] = image;
partial.load(&fs, SELF); // explicit retry can read the original authority
CHECK(partial.getNumClients() == 2);
CHECK(partial.getClient(KEY, PUB_KEY_SIZE)->isAdmin());
CHECK(partial.getClient(SECOND_KEY, PUB_KEY_SIZE)->isRegionMgr());
CHECK(partial.save(&fs));
}
}
int main() {
const struct { const char* name; void (*run)(); } tests[] = {
{"allocation failure preserves clients", allocation_failure_preserves_saved_clients},
{"incomplete ACL is not authoritative", incomplete_acl_load_is_never_authoritative},
{"missing read differs from empty file", missing_read_is_not_empty_file},
{"first admin and monotonic retries", first_admin_and_retries},
{"reboot preserves ceiling", reboot_preserves_ceiling},
@@ -507,5 +548,5 @@ int main() {
test.run();
std::printf("PASS: %s\n", test.name);
}
std::puts("25 ClientACL SPIFFS checks passed");
std::puts("26 ClientACL SPIFFS checks passed");
}