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:
1 parent
86defc840c
commit
e2a1828987
32 files changed
+1546
-189
No files matched your search
Vendored
+20
@@ -49,6 +49,7 @@ struct Prefs {
|
||||
uint32_t rx_ps_rx_us=1000, rx_ps_sleep_us=1000;
|
||||
bool cad_enabled=false, rx_boosted_gain=false;
|
||||
uint8_t extra_sf[4]={};
|
||||
uint8_t flood_max=16, flood_max_advert=16, flood_max_unscoped=16;
|
||||
};
|
||||
struct Callbacks {
|
||||
unsigned saves=0, tx_calls=0, gain_calls=0;
|
||||
@@ -175,5 +176,24 @@ int main() {
|
||||
assert(cli.callbacks.saves==saves);
|
||||
cli.call(sender,"set unknown.setting 1",reply);
|
||||
assert(!strcmp(reply,"unknown config") && cli.callbacks.saves==saves);
|
||||
for (const char* key : {"flood.max", "flood.max.advert", "flood.max.unscoped"}) {
|
||||
for (const char* invalid : {"", "invalid", "65", "256", "-1", "1tail",
|
||||
"1.5", "4294967296", "999999999999999999999"}) {
|
||||
saves=cli.callbacks.saves;
|
||||
snprintf(text,sizeof(text),"set %s %s",key,invalid); cli.call(sender,text,reply);
|
||||
assert(!strncmp(reply,"Error",5) && cli.callbacks.saves==saves);
|
||||
assert(cli.prefs.flood_max==16 && cli.prefs.flood_max_advert==16
|
||||
&& cli.prefs.flood_max_unscoped==16);
|
||||
}
|
||||
}
|
||||
for (const char* key : {"flood.max", "flood.max.advert", "flood.max.unscoped"}) {
|
||||
for (unsigned value : {0u, 1u, 64u}) {
|
||||
saves=cli.callbacks.saves;
|
||||
snprintf(text,sizeof(text),"set %s %u",key,value); cli.call(sender,text,reply);
|
||||
assert(!strcmp(reply,"OK") && cli.callbacks.saves==saves+1);
|
||||
snprintf(text,sizeof(text),"get %s",key); cli.call(sender,text,reply);
|
||||
unsigned actual=1000; assert(sscanf(reply,"> %u",&actual)==1 && actual==value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,9 @@ static std::string query(const ClientACL& acl, const char* command, bool local =
|
||||
}
|
||||
|
||||
static void empty_and_single_entry() {
|
||||
FakeFilesystem fs; mesh::LocalIdentity self;
|
||||
ClientACL acl;
|
||||
acl.load(&fs, self);
|
||||
CHECK(query(acl, "get acl") == "ACL: empty");
|
||||
CHECK(query(acl, "get acl 1") == "ACL: empty");
|
||||
CHECK(query(acl, "get acl 2") == "Err - ACL page range: 1-1");
|
||||
@@ -79,7 +81,9 @@ static void empty_and_single_entry() {
|
||||
}
|
||||
|
||||
static void skips_inactive_and_preserves_full_keys() {
|
||||
FakeFilesystem fs; mesh::LocalIdentity self;
|
||||
ClientACL acl;
|
||||
acl.load(&fs, self);
|
||||
std::vector<std::string> active;
|
||||
for (unsigned i = 0; i < 9; ++i) {
|
||||
auto* client = add(acl, i, i % 2 == 0 ? uint8_t(i + 1) : 0);
|
||||
@@ -92,7 +96,9 @@ static void skips_inactive_and_preserves_full_keys() {
|
||||
}
|
||||
|
||||
static void full_table_pages() {
|
||||
FakeFilesystem fs; mesh::LocalIdentity self;
|
||||
ClientACL acl;
|
||||
acl.load(&fs, self);
|
||||
for (unsigned i = 0; i < MAX_CLIENTS; ++i) add(acl, i, PERM_ACL_ADMIN);
|
||||
const unsigned pages = (MAX_CLIENTS + 1) / 2;
|
||||
for (unsigned page = 1; page <= pages; ++page) {
|
||||
@@ -108,7 +114,9 @@ static void full_table_pages() {
|
||||
}
|
||||
|
||||
static void rejects_bad_pages() {
|
||||
FakeFilesystem fs; mesh::LocalIdentity self;
|
||||
ClientACL acl;
|
||||
acl.load(&fs, self);
|
||||
add(acl, 0, 3);
|
||||
for (const char* command : {
|
||||
"get acl 0", "get acl -1", "get acl +1", "get acl 1x", "get acl 1 2",
|
||||
@@ -124,7 +132,9 @@ static void rejects_bad_pages() {
|
||||
}
|
||||
|
||||
static void matching_and_local_fallback() {
|
||||
FakeFilesystem fs; mesh::LocalIdentity self;
|
||||
ClientACL acl;
|
||||
acl.load(&fs, self);
|
||||
char reply[157] = "unchanged";
|
||||
for (const char* command : {"", "get", "get ac", "get aclx", "get acl.other", "set acl"}) {
|
||||
CHECK(!mesh::cli::handleACLGet(acl, command, reply, sizeof(reply), false));
|
||||
@@ -136,7 +146,9 @@ static void matching_and_local_fallback() {
|
||||
}
|
||||
|
||||
static void bounds_never_truncate_keys() {
|
||||
FakeFilesystem fs; mesh::LocalIdentity self;
|
||||
ClientACL acl;
|
||||
acl.load(&fs, self);
|
||||
add(acl, 0, 3);
|
||||
add(acl, 1, 3);
|
||||
const auto expected = query(acl, "get acl");
|
||||
@@ -174,7 +186,9 @@ static void listing_does_not_mutate_acl() {
|
||||
}
|
||||
|
||||
static void actual_role_dispatch_handles_radio_and_local() {
|
||||
FakeFilesystem fs; mesh::LocalIdentity self;
|
||||
ClientACL acl;
|
||||
acl.load(&fs, self);
|
||||
auto* admin = add(acl, 0, PERM_ACL_ADMIN);
|
||||
add(acl, 1, PERM_ACL_READ_WRITE);
|
||||
const auto expected = query(acl, "get acl");
|
||||
@@ -208,7 +222,9 @@ static void actual_role_dispatch_handles_radio_and_local() {
|
||||
}
|
||||
|
||||
static void repeater_delegation_stays_denied() {
|
||||
FakeFilesystem fs; mesh::LocalIdentity self;
|
||||
ClientACL acl;
|
||||
acl.load(&fs, self);
|
||||
auto* sender = add(acl, 0, 3);
|
||||
for (unsigned permissions = 0; permissions <= 255; ++permissions) {
|
||||
sender->permissions = uint8_t(permissions);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
@@ -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");
|
||||
}
|
||||
+10
-4
@@ -26,6 +26,7 @@ public:
|
||||
size_t read(uint8_t* bytes, size_t length);
|
||||
size_t write(const uint8_t* bytes, size_t length);
|
||||
size_t size() const;
|
||||
int available() const { return static_cast<int>(size() - position); }
|
||||
void flush() {}
|
||||
void close() { fs = nullptr; }
|
||||
};
|
||||
@@ -37,12 +38,13 @@ public:
|
||||
size_t max_write = std::numeric_limits<size_t>::max();
|
||||
size_t max_read = std::numeric_limits<size_t>::max();
|
||||
std::string fail_open;
|
||||
unsigned fail_open_remaining = std::numeric_limits<unsigned>::max();
|
||||
int stat_error = 0;
|
||||
unsigned fail_rename = 0, renames = 0, writes = 0;
|
||||
std::vector<Files> snapshots;
|
||||
bool exists(const char* path) const {
|
||||
#if defined(ESP32_PLATFORM)
|
||||
if (fail_open == path) return false; // ESP32 VFS exists opens the file.
|
||||
if (fail_open == path && fail_open_remaining != 0) return false; // ESP32 VFS exists opens the file.
|
||||
#endif
|
||||
return files.count(path) != 0;
|
||||
}
|
||||
@@ -52,7 +54,10 @@ public:
|
||||
void _unlockFS() {}
|
||||
FakeFilesystem* _getFS() { return this; }
|
||||
File open(const char* path, const char* mode = "r", bool = false) {
|
||||
if (fail_open == path) return File();
|
||||
if (fail_open == path && fail_open_remaining != 0) {
|
||||
--fail_open_remaining;
|
||||
return File();
|
||||
}
|
||||
if (*mode != 'r') {
|
||||
files[path].clear();
|
||||
return File(this, path, true);
|
||||
@@ -61,9 +66,9 @@ public:
|
||||
}
|
||||
bool rename(const char* from, const char* to) {
|
||||
++renames;
|
||||
if (renames == fail_rename || !exists(from)) return false;
|
||||
if (renames == fail_rename || files.count(from) == 0) return false;
|
||||
#if defined(ESP32_PLATFORM)
|
||||
if (exists(to)) return false;
|
||||
if (files.count(to) != 0) return false;
|
||||
#endif
|
||||
files[to] = files.at(from);
|
||||
files.erase(from);
|
||||
@@ -153,6 +158,7 @@ class DataStore {
|
||||
bool _channel_load_incomplete = false;
|
||||
bool _uncached_contact_load_incomplete = false;
|
||||
struct IdentityAdapter {
|
||||
bool recover(const char*) { return true; }
|
||||
bool load(const char*, mesh::LocalIdentity&) {
|
||||
File file = filesystem.open(identity_path);
|
||||
uint8_t data[96];
|
||||
|
||||
Reference in new issue
Block a user