feat(mqtt): persist observer preferences as JSON

This commit is contained in:
agessaman committed 2026-08-09 12:04:55 -07:00
1 parent 8f12b89d6d
commit da204b8fee
27 files changed
+2131 -254

No files matched your search

+2 -1
View File
@@ -33,7 +33,8 @@ does not reflect the GoogleTest count — run the built binary directly
| `test_mqtt_packet_filter` | `src/helpers/MQTTPacketFilter.h` | per-slot 0-15 allowlist parsing/formatting, numeric and named spellings; exact bounds; membership; candidate/eligible split and retry-completion policy; pre-queue union gate; default-mask detection |
| `test_mqtt_runtime_buffer_lifecycle` | `src/helpers/MQTTRuntimeBufferLifecycle.h` | idempotent allocation/release; partial-allocation degradation; retry of only missing buffers |
| `test_mqtt_prefs_codec` | `src/helpers/MQTTPrefsStorage.h`, `src/helpers/MQTTPrefsCodec.h` | binary pre-slot/3-slot/6-slot migration fixtures; v1 header integrity; downgrade preservation; shortest-payload write policy (default filters stay downgrade-readable) |
| `test_mqtt_prefs_atomic_store` | `src/helpers/MQTTPrefsAtomicStore.h` | transactional MQTT writes and legacy `/node_prefs` handoff; exact short-write detection; begin/finish/rename failure cleanup; original-file preservation |
| `test_mqtt_prefs_serializer` | `src/helpers/MQTTPrefsSerializer.h`, `src/helpers/ConfigSerializer.*` | semantic nested `/mqtt.json` round trips; numeric slot keys; required/future version handling; strict length/overflow/duplicate rejection; safe semantic repair; scratch-before-live loading |
| `test_mqtt_prefs_atomic_store` | `src/helpers/MQTTPrefsAtomicStore.h`, `src/helpers/MQTTPrefsRecovery.h` | production JSON begin/write/checksum-finish/schema-verify/commit orchestration; first-migration and rename-boundary recovery; legacy `/node_prefs` handoff; failure cleanup and original-file preservation |
| `test_mqtt_payload_builder` | `src/helpers/MQTTPayloadBuilder.cpp` | status/packet/raw JSON contracts; optional fields; escaping; RX metrics and path; score handling; exact buffer bounds; maximum representative payloads |
| `test_utils` | `src/Utils.cpp` | `Utils::toHex` (upstream) |
@@ -171,6 +171,23 @@ TEST(ConfigSerializer, LoadSerial_IgnoreUnknowns) {
EXPECT_TRUE(match);
}
TEST(ConfigSerializer, LoadSerial_KeyDigitsAfterFirstCharacter) {
MockInputStream s("{age:1,flags:2,name:\"ok\",slot1:7}");
TestStruct data;
data.age = data.flags = 0;
strcpy(data.name, "before");
EXPECT_TRUE(data.loadSerial(s));
EXPECT_EQ(1, data.age);
EXPECT_EQ(2, data.flags);
EXPECT_STREQ("ok", data.name);
}
TEST(ConfigSerializer, LoadSerial_RejectsLeadingDigitKey) {
MockInputStream s("{1slot:7}");
TestStruct data;
EXPECT_FALSE(data.loadSerial(s));
}
// ── main ───────────────────────────────────────────────────────
@@ -20,6 +20,7 @@ enum class FailurePoint {
PayloadWrite,
ImageWrite,
Finish,
Verify,
Commit,
};
@@ -44,7 +45,8 @@ public:
++write_calls;
if (!_open) return 0;
const bool should_fail = (write_calls == 1 && _failure == FailurePoint::HeaderWrite) ||
(write_calls == 2 && _failure == FailurePoint::PayloadWrite);
(write_calls == 2 && _failure == FailurePoint::PayloadWrite) ||
_failure == FailurePoint::ImageWrite;
const size_t written = should_fail && size > 0 ? size - 1 : size;
_staging.insert(_staging.end(), bytes, bytes + written);
return written;
@@ -68,6 +70,18 @@ public:
return true;
}
bool verify() {
++verify_calls;
return _failure != FailurePoint::Verify;
}
void discardFinishedTemp() {
++discard_calls;
_files.erase("/mqtt_prefs.tmp");
_finished = false;
_owns_temp = false;
}
void abort() {
++abort_calls;
_open = false;
@@ -88,6 +102,8 @@ public:
int finish_calls = 0;
int commit_calls = 0;
int abort_calls = 0;
int verify_calls = 0;
int discard_calls = 0;
private:
FailurePoint _failure;
@@ -113,6 +129,16 @@ AtomicStore::Result runWithObserverTail(InMemoryStore* store) {
return AtomicStore::write(*store, header, sizeof(header), payload, sizeof(payload));
}
AtomicStore::VerifiedImageResult runVerifiedJson(InMemoryStore* store) {
const uint8_t json[] = "{version:1,wifi:{ssid:\"mesh\"}}";
return AtomicStore::writeVerifiedImage(
*store,
[store, &json]() {
return store->write(json, sizeof(json) - 1) == sizeof(json) - 1;
},
[store]() { return store->verify(); });
}
class LegacyComPrefs {
public:
LegacyComPrefs() : bytes({'l', 'e', 'g', 'a', 'c', 'y', '-', 'c', 'o', 'm'}) {}
@@ -286,9 +312,14 @@ public:
}
return;
}
if (action == Recovery::Action::DiscardTemp) {
_files.erase("/mqtt_prefs.tmp");
return;
}
if (action == Recovery::Action::PromoteBackup) {
rename("/mqtt_prefs.bak", "/mqtt_prefs");
if (backup == Recovery::FileState::Usable && temp != Recovery::FileState::Missing) {
if (backup == Recovery::FileState::Usable && !Recovery::uncertain(temp) &&
temp != Recovery::FileState::Missing) {
_files.erase("/mqtt_prefs.tmp");
}
return;
@@ -298,12 +329,13 @@ public:
// 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");
if (!Recovery::uncertain(temp)) _files.erase("/mqtt_prefs.tmp");
if (!Recovery::uncertain(backup)) _files.erase("/mqtt_prefs.bak");
}
}
bool has(const char* path) const { return _files.count(path) != 0; }
void removePrimary() { _files.erase("/mqtt_prefs"); }
bool canStartSave() const { return !has("/mqtt_prefs.tmp") && !has("/mqtt_prefs.bak"); }
const std::vector<uint8_t>& primary() const { return _files.at("/mqtt_prefs"); }
static std::vector<uint8_t> oldImage() { return {'o', 'l', 'd'}; }
@@ -337,6 +369,50 @@ TEST(MQTTPrefsAtomicStore, CommitPublishesExactHeaderThenPayload) {
EXPECT_EQ(0, store.abort_calls);
}
TEST(MQTTPrefsAtomicStore, ProductionJsonPolicyCoversEveryVerificationBoundary) {
const std::vector<uint8_t> old_source = {
'o', 'l', 'd', '-', 'p', 'r', 'e', 'f', 's'};
const struct {
FailurePoint point;
AtomicStore::VerifiedImageResult expected;
int writes;
int finishes;
int verifies;
int commits;
int aborts;
int discards;
bool keeps_temp;
} cases[] = {
{FailurePoint::None, AtomicStore::VerifiedImageResult::Committed,
1, 1, 1, 1, 0, 0, false},
{FailurePoint::Begin, AtomicStore::VerifiedImageResult::BeginFailed,
0, 0, 0, 0, 1, 0, false},
{FailurePoint::ImageWrite, AtomicStore::VerifiedImageResult::WriteFailed,
1, 0, 0, 0, 1, 0, false},
{FailurePoint::Finish, AtomicStore::VerifiedImageResult::FinishFailed,
1, 1, 0, 0, 1, 0, false},
{FailurePoint::Verify, AtomicStore::VerifiedImageResult::VerifyFailed,
1, 1, 1, 0, 0, 1, false},
{FailurePoint::Commit, AtomicStore::VerifiedImageResult::CommitFailed,
1, 1, 1, 1, 1, 0, true},
};
for (const auto& test_case : cases) {
InMemoryStore store(test_case.point);
EXPECT_EQ(test_case.expected, runVerifiedJson(&store));
EXPECT_EQ(test_case.writes, store.write_calls);
EXPECT_EQ(test_case.finishes, store.finish_calls);
EXPECT_EQ(test_case.verifies, store.verify_calls);
EXPECT_EQ(test_case.commits, store.commit_calls);
EXPECT_EQ(test_case.aborts, store.abort_calls);
EXPECT_EQ(test_case.discards, store.discard_calls);
EXPECT_EQ(test_case.keeps_temp, store.tempExists());
if (test_case.point != FailurePoint::None) {
EXPECT_EQ(old_source, store.source());
}
}
}
TEST(MQTTPrefsAtomicStore, AnyFailureAbortsAndPreservesExistingSource) {
const std::vector<uint8_t> source = {'o', 'l', 'd', '-', 'p', 'r', 'e', 'f', 's'};
const struct {
@@ -546,6 +622,23 @@ TEST(MQTTPrefsAtomicStore, PowerCutDuringTempWriteKeepsPrimaryAndAllowsNextSave)
EXPECT_TRUE(store.canStartSave());
}
TEST(MQTTPrefsAtomicStore, TornFirstMigrationTempIsDiscardedSoLegacyCanRetry) {
SpiffsMqttTransaction store;
store.removePrimary();
store.cutDuringTempWrite();
EXPECT_EQ(Recovery::Action::DiscardTemp,
Recovery::select(Recovery::FileState::Missing,
Recovery::FileState::Preserve,
Recovery::FileState::Missing));
store.recover(Recovery::FileState::Missing,
Recovery::FileState::Preserve,
Recovery::FileState::Missing);
EXPECT_FALSE(store.has("/mqtt_prefs"));
EXPECT_FALSE(store.has("/mqtt_prefs.tmp"));
EXPECT_TRUE(store.canStartSave());
}
TEST(MQTTPrefsAtomicStore, RecoveredUsablePrimaryClearsOpaqueTransactionArtifacts) {
{
SpiffsMqttTransaction store;
@@ -605,8 +698,14 @@ TEST(MQTTPrefsAtomicStore, RecoveryNeverOverwritesOpaqueNewerLayout) {
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.
// A syntactically valid future temp may already have passed verification and
// reached the rename phase. It wins over the stale supported backup and is
// promoted into the authoritative name, where older firmware will hold it.
EXPECT_EQ(Recovery::Action::PromoteTemp,
Recovery::select(Recovery::FileState::Missing, Recovery::FileState::FutureUsable,
Recovery::FileState::Usable));
// A corrupt or incomplete temp is different: the supported backup remains
// the last known committed image.
EXPECT_EQ(Recovery::Action::PromoteBackup,
Recovery::select(Recovery::FileState::Missing, Recovery::FileState::Preserve,
Recovery::FileState::Usable));
@@ -617,6 +716,34 @@ TEST(MQTTPrefsAtomicStore, RecoveryNeverOverwritesOpaqueNewerLayout) {
Recovery::FileState::Preserve));
}
TEST(MQTTPrefsAtomicStore, AmbiguousFutureOrOomTempIsNeverDeleted) {
for (const Recovery::FileState uncertain : {
Recovery::FileState::FutureClaimed,
Recovery::FileState::Indeterminate}) {
SpiffsMqttTransaction store;
store.cutAt(SpiffsMqttTransaction::Boundary::AfterBackupRename);
store.recover(Recovery::FileState::Missing, uncertain,
Recovery::FileState::Usable);
// The supported backup can run this boot, but the candidate that this
// firmware could not classify remains available to newer firmware or a
// later boot with more heap. Its presence also blocks a new transaction.
EXPECT_EQ(SpiffsMqttTransaction::oldImage(), store.primary());
EXPECT_TRUE(store.has("/mqtt_prefs.tmp"));
EXPECT_FALSE(store.canStartSave());
}
}
TEST(MQTTPrefsAtomicStore, UsablePrimaryDoesNotCleanIndeterminateArtifact) {
SpiffsMqttTransaction store;
store.cutDuringTempWrite();
store.recover(Recovery::FileState::Usable,
Recovery::FileState::Indeterminate);
EXPECT_EQ(SpiffsMqttTransaction::oldImage(), store.primary());
EXPECT_TRUE(store.has("/mqtt_prefs.tmp"));
EXPECT_FALSE(store.canStartSave());
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
@@ -28,6 +28,7 @@ MQTTPrefs defaults() {
prefs.alert_wifi_minutes = 30;
prefs.alert_mqtt_minutes = 240;
prefs.alert_min_interval_min = 60;
prefs.mqtt_neighbors_interval = MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS;
return prefs;
}
@@ -59,6 +60,21 @@ Codec::DecodePlan classify(const std::vector<uint8_t>& bytes) {
return Codec::classify(bytes.data(), prefix_size, bytes.size());
}
void writeV1Payload(std::vector<uint8_t>* bytes, const MQTTPrefs& prefs, size_t payload_len) {
LegacyV1MQTTPrefs frozen;
Codec::freezeV1(prefs, &frozen);
ASSERT_GE(bytes->size(), sizeof(MQTTPrefsHeader) + payload_len);
memcpy(bytes->data() + sizeof(MQTTPrefsHeader), &frozen, payload_len);
}
MQTTPrefs decodeV1(const std::vector<uint8_t>& bytes, const Codec::DecodePlan& plan) {
LegacyV1MQTTPrefs frozen = {};
memcpy(&frozen, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
MQTTPrefs loaded = defaults();
Codec::migrateV1(frozen, plan.payload_len, &loaded);
return loaded;
}
void fillHighEntropy(std::vector<uint8_t>* bytes) {
uint32_t state = 0x89abcdef;
for (size_t i = 0; i < bytes->size(); ++i) {
@@ -244,8 +260,7 @@ TEST(MQTTPrefsCodec, CurrentVersionedPayloadRoundTripsExactly) {
ASSERT_EQ(Codec::Source::Current, plan.source);
ASSERT_FALSE(plan.preserve_file);
ASSERT_TRUE(plan.observer_fields_present);
MQTTPrefs loaded = defaults();
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
MQTTPrefs loaded = decodeV1(bytes, plan);
EXPECT_EQ(0, memcmp(&source, &loaded, sizeof(source)));
}
@@ -277,8 +292,7 @@ TEST(MQTTPrefsCodec, DefaultFiltersKeepTheDowngradeReadablePayloadLength) {
ASSERT_EQ(Codec::kV1PreFilterPayloadSize, plan.payload_len);
ASSERT_FALSE(plan.preserve_file);
MQTTPrefs loaded = defaults();
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
MQTTPrefs loaded = decodeV1(bytes, plan);
EXPECT_EQ(0, memcmp(&source, &loaded, sizeof(source)));
}
@@ -299,8 +313,7 @@ TEST(MQTTPrefsCodec, AnyNonDefaultFilterOptsIntoTheLongerPayload) {
const Codec::DecodePlan plan = classify(bytes);
ASSERT_EQ(Codec::kV1BaselinePayloadSize, plan.payload_len);
MQTTPrefs loaded = defaults();
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
MQTTPrefs loaded = decodeV1(bytes, plan);
EXPECT_EQ(mask, loaded.mqtt_slot_packet_filter[slot]) << slot;
EXPECT_EQ(0, memcmp(&source, &loaded, sizeof(source)));
}
@@ -338,7 +351,7 @@ TEST(MQTTPrefsCodec, PreFilterV1PayloadDefaultsEverySlotToAllTypes) {
std::vector<uint8_t> bytes(sizeof(MQTTPrefsHeader) + Codec::kV1PreFilterPayloadSize, 0);
writeHeader(&bytes, MQTT_PREFS_VERSION,
static_cast<uint16_t>(Codec::kV1PreFilterPayloadSize));
memcpy(bytes.data() + sizeof(MQTTPrefsHeader), &source, Codec::kV1PreFilterPayloadSize);
writeV1Payload(&bytes, source, Codec::kV1PreFilterPayloadSize);
const Codec::DecodePlan plan = classify(bytes);
ASSERT_EQ(Codec::Source::Current, plan.source);
@@ -346,8 +359,7 @@ TEST(MQTTPrefsCodec, PreFilterV1PayloadDefaultsEverySlotToAllTypes) {
ASSERT_TRUE(plan.observer_fields_present);
ASSERT_FALSE(plan.preserve_file);
MQTTPrefs loaded = defaults();
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
MQTTPrefs loaded = decodeV1(bytes, plan);
EXPECT_STREQ("pre-filter-node", loaded.mqtt_origin);
EXPECT_EQ(1u, loaded.mqtt_neighbors_enabled);
EXPECT_EQ(MQTT_NEIGHBORS_MAX_INTERVAL_MS, loaded.mqtt_neighbors_interval);
@@ -368,7 +380,7 @@ TEST(MQTTPrefsCodec, CompatibleShortV1PayloadPreservesDefaultsBeyondObserverBoun
std::vector<uint8_t> bytes(sizeof(MQTTPrefsHeader) + Codec::kV1PreObserverPayloadSize, 0);
writeHeader(&bytes, MQTT_PREFS_VERSION,
static_cast<uint16_t>(Codec::kV1PreObserverPayloadSize));
memcpy(bytes.data() + sizeof(MQTTPrefsHeader), &source, Codec::kV1PreObserverPayloadSize);
writeV1Payload(&bytes, source, Codec::kV1PreObserverPayloadSize);
const Codec::DecodePlan plan = classify(bytes);
ASSERT_EQ(Codec::Source::Current, plan.source);
@@ -376,8 +388,7 @@ TEST(MQTTPrefsCodec, CompatibleShortV1PayloadPreservesDefaultsBeyondObserverBoun
ASSERT_FALSE(plan.preserve_file);
ASSERT_FALSE(plan.observer_fields_present);
MQTTPrefs loaded = defaults();
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
MQTTPrefs loaded = decodeV1(bytes, plan);
EXPECT_STREQ("short-v1-node", loaded.mqtt_origin);
EXPECT_STREQ("ntp.short.example", loaded.mqtt_ntp_server);
EXPECT_EQ(0, loaded.snmp_enabled);
@@ -400,7 +411,7 @@ TEST(MQTTPrefsCodec, PreNeighborsV1PayloadLoadsObserverFieldsAndDefaultsNeighbor
std::vector<uint8_t> bytes(sizeof(MQTTPrefsHeader) + Codec::kV1PreNeighborsPayloadSize, 0);
writeHeader(&bytes, MQTT_PREFS_VERSION,
static_cast<uint16_t>(Codec::kV1PreNeighborsPayloadSize));
memcpy(bytes.data() + sizeof(MQTTPrefsHeader), &source, Codec::kV1PreNeighborsPayloadSize);
writeV1Payload(&bytes, source, Codec::kV1PreNeighborsPayloadSize);
const Codec::DecodePlan plan = classify(bytes);
ASSERT_EQ(Codec::Source::Current, plan.source);
@@ -408,10 +419,7 @@ TEST(MQTTPrefsCodec, PreNeighborsV1PayloadLoadsObserverFieldsAndDefaultsNeighbor
ASSERT_FALSE(plan.preserve_file);
ASSERT_TRUE(plan.observer_fields_present);
MQTTPrefs loaded = defaults();
loaded.mqtt_neighbors_enabled = 1; // pretend stale
loaded.mqtt_neighbors_interval = MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS; // caller's defaulted tail
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
MQTTPrefs loaded = decodeV1(bytes, plan);
EXPECT_STREQ("pre-neighbors-node", loaded.mqtt_origin);
EXPECT_STREQ("PNW", loaded.alert_region);
@@ -475,6 +483,34 @@ TEST(MQTTPrefsCodec, CorruptOrShortVersionedInputsArePreserved) {
EXPECT_TRUE(plan.preserve_file);
}
TEST(MQTTPrefsCodec, HeaderValidV1StillRequiresBoundedPayloadStrings) {
MQTTPrefs source = defaults();
LegacyV1MQTTPrefs frozen;
Codec::freezeV1(source, &frozen);
ASSERT_TRUE(Codec::isPlausibleV1(frozen, Codec::kV1BaselinePayloadSize));
memset(frozen.mqtt_slot_topic[5], 'x', sizeof(frozen.mqtt_slot_topic[5]));
EXPECT_FALSE(Codec::isPlausibleV1(frozen, Codec::kV1BaselinePayloadSize));
}
TEST(MQTTPrefsCodec, HeaderValidV1AllowsNumericValuesForSerializerRepair) {
MQTTPrefs source = defaults();
LegacyV1MQTTPrefs frozen;
Codec::freezeV1(source, &frozen);
// A valid v1 header fixes the layout. Numeric bytes cannot trigger an
// out-of-bounds read and are normalized after migration, so plausibility
// must not reject deployed files merely because these values need repair.
frozen.timezone_offset = 99;
frozen.mqtt_status_enabled = 0xff;
frozen.mqtt_rx_enabled = 0xff;
frozen.snmp_enabled = 0xff;
frozen.alert_enabled = 0xff;
frozen.mqtt_neighbors_enabled = 0xa5;
EXPECT_TRUE(Codec::isPlausibleV1(frozen, Codec::kV1BaselinePayloadSize));
EXPECT_TRUE(Codec::isPlausibleV1(frozen, Codec::kV1PreNeighborsPayloadSize));
}
TEST(MQTTPrefsCodec, LegacyPlausibilityRejectsHighEntropyBytesAtEveryWhitelistedSize) {
// A headerless raw struct has no checksum, so this only reduces false
// migrations; it cannot prove that a plausible-looking file is authentic.
@@ -531,7 +567,9 @@ TEST(MQTTPrefsCodec, LongerSameVersionPayloadLoadsTheBaselineAndIgnoresTheTail)
const size_t payload_len = Codec::kV1BaselinePayloadSize + kTail;
std::vector<uint8_t> bytes(sizeof(MQTTPrefsHeader) + payload_len, 0xA5);
writeHeader(&bytes, MQTT_PREFS_VERSION, static_cast<uint16_t>(payload_len));
memcpy(bytes.data() + sizeof(MQTTPrefsHeader), &source, sizeof(source));
LegacyV1MQTTPrefs frozen;
Codec::freezeV1(source, &frozen);
memcpy(bytes.data() + sizeof(MQTTPrefsHeader), &frozen, sizeof(frozen));
const Codec::DecodePlan plan = classify(bytes);
ASSERT_EQ(Codec::Source::Current, plan.source);
@@ -542,9 +580,11 @@ TEST(MQTTPrefsCodec, LongerSameVersionPayloadLoadsTheBaselineAndIgnoresTheTail)
// Reading plan.payload_len bytes recovers this build's whole struct exactly,
// and cannot run past it into the unknown tail.
LegacyV1MQTTPrefs loaded_frozen = {};
memcpy(&loaded_frozen, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
ASSERT_TRUE(Codec::isPlausibleV1(loaded_frozen, plan.payload_len));
MQTTPrefs loaded = defaults();
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
EXPECT_EQ(0, memcmp(&source, &loaded, sizeof(source)));
Codec::migrateV1(loaded_frozen, plan.payload_len, &loaded);
EXPECT_STREQ("future-node", loaded.mqtt_origin);
EXPECT_STREQ("field-ssid", loaded.wifi_ssid);
EXPECT_STREQ("field-secret", loaded.wifi_password);
@@ -0,0 +1,358 @@
#include <gtest/gtest.h>
#include <string>
#define WITH_MQTT_BRIDGE 1
#define PROGMEM
#include "helpers/MQTTPrefsSerializer.h"
class InputStream : public Stream {
public:
explicit InputStream(const std::string& text) : _text(text) {}
int available() override { return static_cast<int>(_text.size() - _pos); }
int read() override { return _pos < _text.size() ? _text[_pos++] : -1; }
int peek() override { return _pos < _text.size() ? _text[_pos] : -1; }
private:
std::string _text;
size_t _pos = 0;
};
class OutputStream : public Stream {
public:
size_t write(uint8_t byte) override { _text.push_back(static_cast<char>(byte)); return 1; }
size_t print(int value, int = DEC) override { return appendNumber(value); }
size_t print(unsigned int value, int = DEC) override { return appendNumber(value); }
size_t print(long value, int = DEC) override { return appendNumber(value); }
size_t print(unsigned long value, int = DEC) override { return appendNumber(value); }
size_t print(long long value, int = DEC) override { return appendNumber(value); }
size_t print(unsigned long long value, int = DEC) override { return appendNumber(value); }
int available() override { return 0; }
int read() override { return -1; }
int peek() override { return -1; }
const std::string& text() const { return _text; }
private:
template <typename T> size_t appendNumber(T value) {
const std::string number = std::to_string(value);
_text += number;
return number.size();
}
std::string _text;
};
class StickyFailingStream : public Stream {
public:
explicit StickyFailingStream(size_t limit) : _limit(limit) {}
size_t write(uint8_t) override {
if (_failed || _written >= _limit) {
_failed = true;
return 0;
}
++_written;
return 1;
}
size_t print(int value, int = DEC) override {
const std::string number = std::to_string(value);
return Print::print(number.c_str());
}
int available() override { return 0; }
int read() override { return -1; }
int peek() override { return -1; }
bool failed() const { return _failed; }
private:
size_t _limit;
size_t _written = 0;
bool _failed = false;
};
static MQTTPrefs defaults() {
MQTTPrefs prefs = {};
prefs.mqtt_status_enabled = 1;
prefs.mqtt_packets_enabled = 1;
prefs.mqtt_tx_enabled = 2;
prefs.mqtt_rx_enabled = 1;
prefs.mqtt_status_interval = 300000;
prefs.wifi_power_save = 1;
prefs.timezone_offset = -7;
prefs.radio_watchdog_minutes = 5;
prefs.alert_wifi_minutes = 30;
prefs.alert_mqtt_minutes = 240;
prefs.alert_min_interval_min = 60;
prefs.mqtt_neighbors_interval = MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS;
strcpy(prefs.snmp_community, "public");
for (int i = 0; i < MQTT_PREFS_SLOT_COUNT; ++i) {
strcpy(prefs.mqtt_slot_preset[i], "none");
prefs.mqtt_slot_packet_filter[i] = 0xffff;
}
return prefs;
}
TEST(MQTTPrefsSerializer, RoundTripsEveryGroupAndNumericSlotKeys) {
MQTTPrefs source = defaults();
strcpy(source.wifi_ssid, "mesh-net");
strcpy(source.wifi_password, "p\\\"ass\nword");
strcpy(source.timezone_string, "MST7MDT,M3.2.0");
strcpy(source.mqtt_ntp_server, "time.example");
strcpy(source.mqtt_origin, "observer-one");
strcpy(source.mqtt_iata, "SEA");
source.mqtt_neighbors_enabled = 1;
source.mqtt_neighbors_interval = MQTT_NEIGHBORS_MAX_INTERVAL_MS;
strcpy(source.mqtt_owner_public_key,
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
strcpy(source.mqtt_email, "owner@example.com");
strcpy(source.mqtt_slot_preset[5], "custom");
strcpy(source.mqtt_slot_host[5], "broker.example");
source.mqtt_slot_port[5] = 65535;
strcpy(source.mqtt_slot_username[5], "user-six");
strcpy(source.mqtt_slot_password[5], "secret-six");
strcpy(source.mqtt_slot_token[5], "token-six");
strcpy(source.mqtt_slot_topic[5], "mesh/{iata}/{type}");
strcpy(source.mqtt_slot_audience[5], "audience-six");
source.mqtt_slot_packet_filter[5] = 0x8001;
source.snmp_enabled = 1;
source.radio_watchdog_minutes = 120;
source.alert_enabled = 1;
strcpy(source.alert_psk_hex, "0123456789abcdef0123456789abcdef");
strcpy(source.alert_hashtag, "#ops");
strcpy(source.alert_region, "PNW");
OutputStream output;
MQTTPrefsSerializer writer(&source);
ASSERT_TRUE(writer.saveSerial(output));
EXPECT_NE(std::string::npos, output.text().find("slot6:{")) << output.text();
EXPECT_NE(std::string::npos, output.text().find("packet_filter:32769")) << output.text();
MQTTPrefs loaded = defaults();
InputStream input(output.text());
MQTTPrefsSerializer reader(&loaded);
ASSERT_TRUE(reader.loadSerial(input)) << output.text();
bool repaired = true;
ASSERT_TRUE(reader.apply(&repaired));
EXPECT_FALSE(repaired);
EXPECT_STREQ(source.wifi_password, loaded.wifi_password);
EXPECT_STREQ(source.mqtt_owner_public_key, loaded.mqtt_owner_public_key);
EXPECT_STREQ(source.mqtt_slot_host[5], loaded.mqtt_slot_host[5]);
EXPECT_EQ(65535, loaded.mqtt_slot_port[5]);
EXPECT_EQ(0x8001, loaded.mqtt_slot_packet_filter[5]);
EXPECT_EQ(MQTT_NEIGHBORS_MAX_INTERVAL_MS, loaded.mqtt_neighbors_interval);
EXPECT_STREQ("PNW", loaded.alert_region);
}
TEST(MQTTPrefsSerializer, MissingOptionalKeysKeepDefaults) {
MQTTPrefs prefs = defaults();
InputStream input("{version:1,mqtt:{origin:\"changed\"}}");
MQTTPrefsSerializer serializer(&prefs);
ASSERT_TRUE(serializer.loadSerial(input));
bool repaired = true;
ASSERT_TRUE(serializer.apply(&repaired));
EXPECT_FALSE(repaired);
EXPECT_STREQ("changed", prefs.mqtt_origin);
EXPECT_EQ(1, prefs.mqtt_packets_enabled);
EXPECT_EQ(300000u, prefs.mqtt_status_interval);
EXPECT_EQ(0xffff, prefs.mqtt_slot_packet_filter[5]);
}
TEST(MQTTPrefsSerializer, RequiresSupportedVersion) {
MQTTPrefs missing = defaults();
InputStream no_version("{wifi:{ssid:\"x\"}}");
MQTTPrefsSerializer missing_serializer(&missing);
ASSERT_TRUE(missing_serializer.loadSerial(no_version));
bool repaired = false;
EXPECT_FALSE(missing_serializer.apply(&repaired));
MQTTPrefs future = defaults();
InputStream future_input("{version:2,wifi:{ssid:\"x\"}}");
MQTTPrefsSerializer future_serializer(&future);
ASSERT_TRUE(future_serializer.loadSerial(future_input));
EXPECT_TRUE(future_serializer.hasFutureVersion());
EXPECT_FALSE(future_serializer.apply(&repaired));
}
TEST(MQTTPrefsSerializer, FutureVersionProbeIgnoresV1FieldTypeChanges) {
InputStream probe_input("{version:2,wifi:{power_save:\"max\"}}");
MQTTPrefsVersionProbe probe;
ASSERT_TRUE(probe.loadSerial(probe_input));
EXPECT_TRUE(probe.hasFutureVersion());
// The same image is not valid under v1, demonstrating why recovery must
// probe the version before invoking the current schema.
MQTTPrefs prefs = defaults();
InputStream v1_input("{version:2,wifi:{power_save:\"max\"}}");
MQTTPrefsSerializer v1(&prefs);
EXPECT_FALSE(v1.loadSerial(v1_input));
for (const char* future_text : {
"{version:2,this_key_is_too_long:1}",
"{version:2,x:[1]}",
"{version:2,wifi:{ssid:\"torn\"}"}) {
InputStream future_input(future_text);
MQTTPrefsVersionProbe future_probe;
EXPECT_FALSE(future_probe.loadSerial(future_input)) << future_text;
EXPECT_TRUE(future_probe.hasFutureVersion()) << future_text;
}
}
TEST(MQTTPrefsSerializer, RejectsDuplicateKnownKey) {
MQTTPrefs prefs = defaults();
InputStream input("{version:1,mqtt:{origin:\"one\",origin:\"two\"}}");
MQTTPrefsSerializer serializer(&prefs);
EXPECT_FALSE(serializer.loadSerial(input));
}
TEST(MQTTPrefsSerializer, RejectsOverlongStringAndIntegerOverflow) {
MQTTPrefs prefs = defaults();
InputStream long_string(
"{version:1,wifi:{ssid:\"12345678901234567890123456789012\"}}");
MQTTPrefsSerializer string_serializer(&prefs);
EXPECT_FALSE(string_serializer.loadSerial(long_string));
prefs = defaults();
InputStream overflow("{version:1,mqtt:{slot1:{port:999999999999}}}");
MQTTPrefsSerializer number_serializer(&prefs);
EXPECT_FALSE(number_serializer.loadSerial(overflow));
prefs = defaults();
InputStream quoted_number("{version:\"1\"}");
MQTTPrefsSerializer quoted_number_serializer(&prefs);
EXPECT_FALSE(quoted_number_serializer.loadSerial(quoted_number));
prefs = defaults();
InputStream bare_string("{version:1,wifi:{ssid:meshnet}}");
MQTTPrefsSerializer bare_string_serializer(&prefs);
EXPECT_FALSE(bare_string_serializer.loadSerial(bare_string));
}
TEST(MQTTPrefsSerializer, RejectsScalarObjectShapeMismatches) {
MQTTPrefs prefs = defaults();
InputStream object_version("{version:{x:1}}");
MQTTPrefsSerializer object_version_serializer(&prefs);
EXPECT_FALSE(object_version_serializer.loadSerial(object_version));
prefs = defaults();
InputStream object_port("{version:1,mqtt:{slot1:{port:{x:1883}}}}");
MQTTPrefsSerializer object_port_serializer(&prefs);
EXPECT_FALSE(object_port_serializer.loadSerial(object_port));
prefs = defaults();
InputStream scalar_mqtt("{version:1,mqtt:1}");
MQTTPrefsSerializer scalar_mqtt_serializer(&prefs);
EXPECT_FALSE(scalar_mqtt_serializer.loadSerial(scalar_mqtt));
prefs = defaults();
InputStream scalar_slot("{version:1,mqtt:{slot1:1}}");
MQTTPrefsSerializer scalar_slot_serializer(&prefs);
EXPECT_FALSE(scalar_slot_serializer.loadSerial(scalar_slot));
}
TEST(MQTTPrefsSerializer, RepairsSemanticRanges) {
MQTTPrefs prefs = defaults();
InputStream input(
"{version:1,wifi:{power_save:9},time:{utc_offset:99},"
"mqtt:{tx_enabled:7,status:{enabled:3,interval_ms:10},"
"neighbors:{enabled:2,interval_ms:100},slot1:{port:-1,packet_filter:-2}},"
"radio:{watchdog_min:121},alert:{rate_limit_min:1}}");
MQTTPrefsSerializer serializer(&prefs);
ASSERT_TRUE(serializer.loadSerial(input));
bool repaired = false;
ASSERT_TRUE(serializer.apply(&repaired));
EXPECT_TRUE(repaired);
EXPECT_EQ(1, prefs.wifi_power_save);
EXPECT_EQ(-7, prefs.timezone_offset);
EXPECT_EQ(2, prefs.mqtt_tx_enabled);
EXPECT_EQ(300000u, prefs.mqtt_status_interval);
EXPECT_EQ(MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS, prefs.mqtt_neighbors_interval);
EXPECT_EQ(0, prefs.mqtt_slot_port[0]);
EXPECT_EQ(0xffff, prefs.mqtt_slot_packet_filter[0]);
EXPECT_EQ(5, prefs.radio_watchdog_minutes);
EXPECT_EQ(60, prefs.alert_min_interval_min);
}
TEST(MQTTPrefsSerializer, RepairsTextValuesToSafeDefaults) {
MQTTPrefs prefs = defaults();
InputStream input(
"{version:1,time:{ntp_server:\"bad/host\"},mqtt:{iata:\"sea\","
"owner:{public_key:\"not-a-key\"},slot1:{preset:\"not-a-preset\"},"
"slot2:{preset:\"analyzer-us\"},slot3:{preset:\"analyzer-us\"}},"
"alert:{psk_hex:\"not-hex\",hashtag:\"#stale\"}}");
MQTTPrefsSerializer serializer(&prefs);
ASSERT_TRUE(serializer.loadSerial(input));
bool repaired = false;
ASSERT_TRUE(serializer.apply(&repaired));
EXPECT_TRUE(repaired);
EXPECT_STREQ("SEA", prefs.mqtt_iata);
EXPECT_STREQ("", prefs.mqtt_ntp_server);
EXPECT_STREQ("", prefs.mqtt_owner_public_key);
EXPECT_STREQ("none", prefs.mqtt_slot_preset[0]);
EXPECT_STREQ("analyzer-us", prefs.mqtt_slot_preset[1]);
// Historical firmware allowed duplicate aliases. Preserve them on load;
// current setters prevent creating new duplicates without silently changing
// a deployed configuration during migration.
EXPECT_STREQ("analyzer-us", prefs.mqtt_slot_preset[2]);
EXPECT_STREQ("", prefs.alert_psk_hex);
EXPECT_STREQ("", prefs.alert_hashtag);
}
TEST(MQTTPrefsSerializer, LateParseOrVersionFailureCannotMutateLivePrefs) {
MQTTPrefs live = defaults();
strcpy(live.wifi_ssid, "live-network");
MQTTPrefs scratch = defaults();
InputStream truncated("{wifi:{ssid:\"uncommitted\"},version:1");
MQTTPrefsSerializer truncated_serializer(&scratch);
EXPECT_FALSE(truncated_serializer.loadSerial(truncated));
EXPECT_STREQ("live-network", live.wifi_ssid);
scratch = defaults();
InputStream future("{wifi:{ssid:\"future-network\"},version:2}");
MQTTPrefsSerializer future_serializer(&scratch);
ASSERT_TRUE(future_serializer.loadSerial(future));
EXPECT_TRUE(future_serializer.hasFutureVersion());
bool repaired = false;
EXPECT_FALSE(future_serializer.apply(&repaired));
EXPECT_STREQ("live-network", live.wifi_ssid);
}
TEST(MQTTPrefsSerializer, StickyShortWriteFailsTheCompleteSave) {
MQTTPrefs prefs = defaults();
MQTTPrefsSerializer serializer(&prefs);
StickyFailingStream output(20);
EXPECT_FALSE(serializer.saveSerial(output));
EXPECT_TRUE(output.failed());
}
TEST(MQTTPrefsSerializer, SaveNormalizationIsIdempotentAgainstKnownDefaults) {
MQTTPrefs prefs = defaults();
prefs.timezone_offset = 99;
strcpy(prefs.mqtt_ntp_server, "bad/host");
strcpy(prefs.mqtt_iata, "not-iata");
strcpy(prefs.mqtt_slot_preset[0], "not-a-preset");
MQTTPrefs repair_defaults = defaults();
repair_defaults.timezone_offset = -7;
strcpy(repair_defaults.mqtt_iata, "sea");
strcpy(repair_defaults.mqtt_slot_preset[0], "analyzer-us");
MQTTPrefsSerializer writer(&prefs, &repair_defaults);
bool repaired = false;
ASSERT_TRUE(writer.normalize(&repaired));
EXPECT_TRUE(repaired);
EXPECT_EQ(-7, prefs.timezone_offset);
EXPECT_STREQ("", prefs.mqtt_ntp_server);
EXPECT_STREQ("SEA", prefs.mqtt_iata);
EXPECT_STREQ("none", prefs.mqtt_slot_preset[0]);
OutputStream output;
ASSERT_TRUE(writer.saveSerial(output));
MQTTPrefs loaded = defaults();
InputStream input(output.text());
MQTTPrefsSerializer reader(&loaded);
ASSERT_TRUE(reader.loadSerial(input));
repaired = true;
ASSERT_TRUE(reader.apply(&repaired));
EXPECT_FALSE(repaired) << output.text();
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
@@ -129,6 +129,18 @@ TEST(ValueFits, RejectsNullOrZeroBuffer) {
EXPECT_FALSE(mqttValueFits("abc", 0));
}
TEST(AssignedPresetSlot, FindsDuplicatesOutsideTheTargetSlot) {
const char presets[4][24] = {
"analyzer-us", "none", "analyzer-eu", "analyzer-us"
};
EXPECT_EQ(3, mqttAssignedPresetSlot(presets, "analyzer-us", 0));
EXPECT_EQ(0, mqttAssignedPresetSlot(presets, "analyzer-us", 3));
EXPECT_EQ(-1, mqttAssignedPresetSlot(presets, "analyzer-eu", 2));
EXPECT_EQ(-1, mqttAssignedPresetSlot(presets, "missing", 0));
EXPECT_EQ(-1, mqttAssignedPresetSlot(presets, nullptr, 0));
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
@@ -230,6 +230,7 @@ TEST(WebConfigBatch, CliFailureRepliesAreRecognisedInEveryShapeCommonCLIEmits) {
"Err - bad params", // MyMesh setperm
"ERR: bad pubkey", // neighbor.remove
"Error: IATA code must be exactly 3 letters",// observer setters
"Error: setting not persisted; change rolled back", // observer storage failure
"(ERR: clock cannot go backwards)", // clock sync, parenthesised
"Unknown command", // top-level fallthrough
"unknown config: mqtt.nope", // set fallthrough