Merge observer OTA channel switching into keymindCascade
This commit is contained in:
commit
63f4642252
32 files changed
+3090
-1708
No files matched your search
@@ -86,11 +86,12 @@ int main() {
|
||||
auto original=cli.fs.files["/com_prefs"];
|
||||
// Appending preferences must not move the established preamble bytes.
|
||||
const size_t preamble_offset=866;
|
||||
assert(original.size()==873);
|
||||
assert(original.size()==874);
|
||||
assert(original[preamble_offset]==48 && original[preamble_offset+1]==0);
|
||||
assert(original[preamble_offset+2]==1);
|
||||
assert(original[preamble_offset+3]==0x50 && original[preamble_offset+4]==0x01);
|
||||
assert(original[871]==0 && original[872]==0); // debug and trace remain appended
|
||||
assert(original[873]==0); // channel selection does not move the previous fields
|
||||
Capture capture; assert(writeCommonPrefsImage(capture,&cli.prefs));
|
||||
assert(capture.bytes==original); // Both serializers retain the same layout.
|
||||
for (int fault : {0,1,2,3,4}) {
|
||||
|
||||
@@ -158,7 +158,7 @@ int main() {
|
||||
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
|
||||
assert(cli._common_save_succeeded);
|
||||
const auto baseline = cli.fs.files["/com_prefs"];
|
||||
assert(baseline.size() == 873);
|
||||
assert(baseline.size() == 874);
|
||||
const size_t debug_offset = 871, trace_offset = 872;
|
||||
const size_t logging_offset = 863, preamble_offset = 866;
|
||||
const size_t espnow_offset = 868, gps_offset = 869;
|
||||
@@ -166,6 +166,7 @@ int main() {
|
||||
assert(baseline[preamble_offset] == 48 && baseline[espnow_offset] == 1);
|
||||
assert(baseline[gps_offset] == 0 && baseline[gps_offset + 1] == 0);
|
||||
assert(baseline[trace_offset] == 0);
|
||||
assert(baseline[873] == 0); // OTA channel follows the established tail.
|
||||
Capture capture;
|
||||
assert(writeCommonPrefsImage(capture, &cli.prefs));
|
||||
assert(capture.bytes == baseline); // Checked and ordinary writers agree.
|
||||
|
||||
@@ -468,6 +468,35 @@ TEST(DynamicConfigSerializer, LoadCustom_Basic) {
|
||||
EXPECT_TRUE(g2);
|
||||
EXPECT_STREQ("Scott", tmp);
|
||||
}
|
||||
TEST(NodePrefs, OtaChannelDefaultsToNative) {
|
||||
NodePrefs prefs;
|
||||
EXPECT_EQ(0, prefs.ota_channel); // 0 == native
|
||||
}
|
||||
|
||||
TEST(NodePrefs, OtaChannelRoundTrip) {
|
||||
NodePrefs saved;
|
||||
saved.ota_channel = 2; // dev
|
||||
|
||||
MockPrintStream output;
|
||||
ASSERT_TRUE(saved.saveSerial(output));
|
||||
std::string serialised(reinterpret_cast<const char*>(output.getBytes()), output.getLength());
|
||||
EXPECT_NE(std::string::npos, serialised.find("ota_ch:2"));
|
||||
|
||||
MockInputStream input(serialised.c_str());
|
||||
NodePrefs loaded;
|
||||
loaded.ota_channel = 1; // start different
|
||||
ASSERT_TRUE(loaded.loadSerial(input)) << serialised;
|
||||
EXPECT_EQ(2, loaded.ota_channel);
|
||||
}
|
||||
|
||||
TEST(NodePrefs, OtaChannelMissingKeyKeepsDefault) {
|
||||
// A /prefs.json written before this field existed has no ota_ch key.
|
||||
MockInputStream input("{name:\"n\"}");
|
||||
NodePrefs loaded; // ota_channel default 0 (native)
|
||||
ASSERT_TRUE(loaded.loadSerial(input));
|
||||
EXPECT_EQ(0, loaded.ota_channel);
|
||||
}
|
||||
|
||||
|
||||
// ── main ───────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check observer channel selection and fork identity without invoking PIO."""
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SPEC = importlib.util.spec_from_file_location("verify_ota_channel", ROOT / "scripts/verify_ota_channel.py")
|
||||
VERIFY = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(VERIFY)
|
||||
PROD = "https://observer.gessaman.com/v"
|
||||
BETA = "https://observer.gessaman.com/beta/v"
|
||||
CHANNEL_ENV = (
|
||||
"OTA_MANIFEST_BASE_URL", "OTA_MANIFEST_BASE_STABLE_URL", "OTA_MANIFEST_BASE_DEV_URL",
|
||||
"FIRMWARE_BUILD_NUMBER", "OTA_CHANNEL_TAG", "FILENAME_CHANNEL_TAG",
|
||||
)
|
||||
|
||||
|
||||
class ObserverBuildChannelTests(unittest.TestCase):
|
||||
def shell(self, body, settings=None):
|
||||
environment = {key: value for key, value in os.environ.items() if key not in CHANNEL_ENV}
|
||||
environment.update(settings or {})
|
||||
bash = shutil.which("bash")
|
||||
git_bash = Path("C:/Program Files/Git/bin/bash.exe")
|
||||
if os.name == "nt" and git_bash.is_file():
|
||||
bash = str(git_bash)
|
||||
result = subprocess.run(
|
||||
[bash, "-c", 'set -euo pipefail; cd "$1"; source build.sh; '
|
||||
'pio() { echo FORBIDDEN_PLATFORMIO >&2; return 99; }; ' + body,
|
||||
"observer-channel-test", ROOT.as_posix()],
|
||||
cwd=ROOT, env=environment, text=True, capture_output=True, timeout=15,
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
self.assertNotIn("FORBIDDEN_PLATFORMIO", result.stdout + result.stderr)
|
||||
return result.stdout
|
||||
|
||||
def flags(self, target, settings=None):
|
||||
output = self.shell(
|
||||
"PLATFORMIO_BUILD_FLAGS=-DKEEP_CALLER_FLAG=1; "
|
||||
f"apply_observer_ota_channel_flags {shlex.quote(target)}; "
|
||||
'printf "%s" "$PLATFORMIO_BUILD_FLAGS"', settings,
|
||||
)
|
||||
return dict(flag[2:].split("=", 1) for flag in shlex.split(output))
|
||||
|
||||
def test_production_beta_and_custom_native_bases(self):
|
||||
for native in (PROD, BETA, "https://observer.gessaman.com/v-plus"):
|
||||
with self.subTest(native=native):
|
||||
flags = self.flags("Heltec_v3_repeater_observer_mqtt",
|
||||
{"OTA_MANIFEST_BASE_URL": native})
|
||||
self.assertEqual(flags["KEEP_CALLER_FLAG"], "1")
|
||||
self.assertEqual(flags["OTA_MANIFEST_BASE"], f'"{native}"')
|
||||
self.assertEqual(flags["OTA_MANIFEST_BASE_STABLE"], f'"{PROD}"')
|
||||
self.assertEqual(flags["OTA_MANIFEST_BASE_DEV"], f'"{BETA}"')
|
||||
|
||||
def test_default_and_named_channel_overrides(self):
|
||||
self.assertEqual(self.flags("Heltec_v3_room_server_observer_mqtt")["OTA_MANIFEST_BASE"], f'"{PROD}"')
|
||||
flags = self.flags("Heltec_v3_room_server_observer_mqtt", {
|
||||
"OTA_MANIFEST_BASE_URL": "https://fork.test/preview/v",
|
||||
"OTA_MANIFEST_BASE_STABLE_URL": "https://fork.test/v",
|
||||
"OTA_MANIFEST_BASE_DEV_URL": "https://fork.test/dev/v",
|
||||
})
|
||||
self.assertEqual(flags["OTA_MANIFEST_BASE_STABLE"], '"https://fork.test/v"')
|
||||
self.assertEqual(flags["OTA_MANIFEST_BASE_DEV"], '"https://fork.test/dev/v"')
|
||||
|
||||
def test_non_observer_keeps_destination_and_unset_flags(self):
|
||||
self.assertEqual(self.flags("Heltec_v3_repeater", {"OTA_MANIFEST_BASE_URL": BETA}),
|
||||
{"KEEP_CALLER_FLAG": "1"})
|
||||
self.assertEqual(self.shell(
|
||||
"unset PLATFORMIO_BUILD_FLAGS; apply_observer_ota_channel_flags Heltec_v3_repeater; "
|
||||
'printf "%s" "${PLATFORMIO_BUILD_FLAGS-unset}"'), "unset")
|
||||
original = "-DOTA_MANIFEST_BASE='\"https://fork.test/v\"'"
|
||||
self.assertEqual(self.shell(
|
||||
f"PLATFORMIO_BUILD_FLAGS={shlex.quote(original)}; "
|
||||
'apply_observer_ota_channel_flags Heltec_v3_companion_radio_full; '
|
||||
'printf "%s" "$PLATFORMIO_BUILD_FLAGS"'), original)
|
||||
|
||||
def test_observer_version_tags_keep_beta_identity(self):
|
||||
settings = {"FIRMWARE_BUILD_NUMBER": "9", "OTA_CHANNEL_TAG": "beta-dev",
|
||||
"FILENAME_CHANNEL_TAG": "-dev"}
|
||||
self.assertEqual(self.shell(
|
||||
"get_firmware_version_string Heltec_v3_repeater_observer_mqtt v1.17.1 abc1234", settings),
|
||||
"v1.17.1.9-dev-abc1234")
|
||||
self.assertEqual(self.shell(
|
||||
"get_firmware_version_string Heltec_v3_repeater_observer_mqtt v1.17.1 abc1234 embedded", settings),
|
||||
"v1.17.1.9-observer-beta-dev-abc1234")
|
||||
self.assertEqual(self.shell(
|
||||
"get_firmware_version_string Heltec_v3_repeater v1.17.1 abc1234", settings),
|
||||
"v1.17.1-abc1234")
|
||||
self.assertEqual(self.shell(
|
||||
"get_firmware_version_string Heltec_v3_repeater_observer_mqtt v1.17.1 abc1234 embedded"),
|
||||
"v1.17.1-observer-abc1234")
|
||||
|
||||
def test_variant_native_bases_do_not_override_workflow_channel(self):
|
||||
for path in ROOT.glob("variants/*/platformio.ini"):
|
||||
self.assertNotIn("-D OTA_MANIFEST_BASE=", path.read_text(), str(path))
|
||||
|
||||
@staticmethod
|
||||
def blob(native=PROD, compat="2+keymind1"):
|
||||
return (f"ota-base-native:{native}\0ota-base-stable:{PROD}\0"
|
||||
f"ota-base-dev:{BETA}\0ota-compat:{compat}\0").encode()
|
||||
|
||||
def test_verifier_requires_fork_capability_and_exact_channel(self):
|
||||
self.assertEqual(VERIFY.check(self.blob(), "prod", PROD, PROD, BETA, ("keymind1",)), [])
|
||||
self.assertTrue(VERIFY.check(self.blob(compat="2+eth"), "prod", PROD, PROD, BETA, ("keymind1",)))
|
||||
self.assertTrue(VERIFY.check(self.blob(BETA), "prod", PROD, PROD, BETA, ("keymind1",)))
|
||||
self.assertEqual(VERIFY.check(self.blob(BETA), "beta", BETA, PROD, BETA, ("keymind1",)), [])
|
||||
|
||||
def test_verifier_checks_every_published_binary(self):
|
||||
with tempfile.TemporaryDirectory(prefix="mesh-observer-channel-") as temporary:
|
||||
directory = Path(temporary)
|
||||
(directory / "one.bin").write_bytes(self.blob())
|
||||
(directory / "two-merged.bin").write_bytes(self.blob(BETA))
|
||||
result = subprocess.run([
|
||||
os.sys.executable, str(ROOT / "scripts/verify_ota_channel.py"), "--expect", "prod",
|
||||
"--native-url", PROD, "--stable-url", PROD, "--dev-url", BETA,
|
||||
"--required-cap", "keymind1", "--bin-dir", str(directory),
|
||||
], text=True, capture_output=True, timeout=10)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn("two-merged.bin", result.stderr)
|
||||
self.assertIn("1 of 2 builds", result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,247 @@
|
||||
"""Exercise the production observer OTA stream, channel commit, and deferred call."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_replay_reset_integration import extract_braced
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def compile_run(code):
|
||||
compiler = shutil.which("g++") or shutil.which("clang++")
|
||||
if compiler is None:
|
||||
raise unittest.SkipTest("a host C++17 compiler is required")
|
||||
with tempfile.TemporaryDirectory(prefix="observer-ota-channel-") as directory:
|
||||
cpp = Path(directory) / "test.cpp"
|
||||
binary = Path(directory) / ("test.exe" if os.name == "nt" else "test")
|
||||
cpp.write_text(code, encoding="utf-8")
|
||||
result = subprocess.run(
|
||||
[compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror", "-I", str(ROOT / "src"),
|
||||
str(cpp), "-o", str(binary)], text=True, capture_output=True, timeout=60)
|
||||
if result.returncode:
|
||||
raise AssertionError(result.stderr)
|
||||
result = subprocess.run([str(binary)], text=True, capture_output=True, timeout=10)
|
||||
if result.returncode:
|
||||
raise AssertionError(f"exit {result.returncode}: {result.stdout}\n{result.stderr}")
|
||||
|
||||
|
||||
class ObserverOtaChannelTest(unittest.TestCase):
|
||||
def test_stream_refuses_unsafe_images_before_boot_commit(self):
|
||||
board = (ROOT / "src/helpers/ESP32Board.cpp").read_text(encoding="utf-8")
|
||||
stream = extract_braced(board, "static bool ota_streamFirmware(")
|
||||
code = r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <helpers/OtaChannel.h>
|
||||
static uint32_t clock_ms=0, yields=0;
|
||||
uint32_t millis() { return clock_ms; }
|
||||
void delay(uint32_t ms) { clock_ms+=ms; ++yields; }
|
||||
namespace mesh {
|
||||
struct Logging { void printf(const char*, ...) {} };
|
||||
Logging& usbLoggingPort() { static Logging value; return value; }
|
||||
}
|
||||
static constexpr int U_FLASH=0;
|
||||
struct UpdateMock {
|
||||
bool begin_ok=true, write_ok=true, end_ok=true;
|
||||
bool aborted=false, boot_committed=false;
|
||||
int end_calls=0;
|
||||
std::vector<uint8_t> written;
|
||||
bool begin(size_t, int) { return begin_ok; }
|
||||
size_t write(const uint8_t* data, size_t size) {
|
||||
if (!write_ok) return 0;
|
||||
written.insert(written.end(), data, data+size); return size;
|
||||
}
|
||||
bool end() { ++end_calls; boot_committed=end_ok; return end_ok; }
|
||||
void abort() { aborted=true; }
|
||||
const char* errorString() { return "injected failure"; }
|
||||
} Update;
|
||||
struct Client {
|
||||
std::string image;
|
||||
size_t pos=0, chunk=1;
|
||||
bool stalled=false, disconnect_on_read=false;
|
||||
int available() { return stalled ? 1 : int(image.size()-pos); }
|
||||
bool connected() { return stalled || pos<image.size(); }
|
||||
int read(uint8_t* out, size_t wanted) {
|
||||
if (disconnect_on_read) { stalled=false; pos=image.size(); return -1; }
|
||||
if (stalled) { clock_ms+=5000; return 0; }
|
||||
if (wanted>chunk) wanted=chunk;
|
||||
if (wanted>image.size()-pos) wanted=image.size()-pos;
|
||||
memcpy(out, image.data()+pos, wanted); pos+=wanted; return int(wanted);
|
||||
}
|
||||
};
|
||||
@STREAM@
|
||||
int main() {
|
||||
char reply[160]={};
|
||||
auto run = [&](std::string image, size_t chunk, bool expect_ok) {
|
||||
Update=UpdateMock(); clock_ms=0; yields=0;
|
||||
Client client; client.image=std::move(image); client.chunk=chunk;
|
||||
const bool ok=ota_streamFirmware(client, client.image.size(), reply);
|
||||
assert(ok==expect_ok && Update.boot_committed==expect_ok);
|
||||
assert(Update.end_calls==(expect_ok ? 1 : 0));
|
||||
assert(expect_ok ? !Update.aborted : Update.aborted);
|
||||
return ok;
|
||||
};
|
||||
const std::string tag(ota_compat_tag, sizeof(ota_compat_tag));
|
||||
const std::string literal("ota-compat:\0", 12);
|
||||
const std::string compatible=literal+std::string(2111, 'x')+tag+"tail";
|
||||
for (size_t chunk : {size_t(1), size_t(7), size_t(63), size_t(64), size_t(2048)})
|
||||
run(compatible, chunk, true);
|
||||
run(std::string("ota-compat:2\0", 13), 2048, false); // upstream JSON generation
|
||||
run("untagged-image", 7, false);
|
||||
run(literal+"only-search-literal", 1, false);
|
||||
run(tag.substr(0, tag.size()-1), 1, false); // no complete NUL-terminated tag
|
||||
run(tag+std::string("ota-compat:2\0", 13), 7, false);
|
||||
run(std::string("ota-compat:2\0", 13)+tag, 2048, false);
|
||||
run(tag+tag, 3, true); // duplicate identical constants are harmless
|
||||
const char future[]="ota-compat:1+keymind1+future";
|
||||
run(tag+std::string(future, sizeof(future)), 5, false);
|
||||
run(tag+"ota-compat:2+future", 1, false); // pending tag after a good one
|
||||
std::string oversized=tag+"ota-compat:1+keymind1+"+std::string(128, 'a');
|
||||
oversized.push_back(0);
|
||||
run(oversized, 1, false);
|
||||
run(std::string("ota-compat:2147483648\0", 22)+tag, 2048, true);
|
||||
|
||||
Update=UpdateMock(); Client incomplete; incomplete.image=tag;
|
||||
assert(!ota_streamFirmware(incomplete, tag.size()+1, reply));
|
||||
assert(Update.aborted && !Update.boot_committed && Update.end_calls==0);
|
||||
Update=UpdateMock(); Client stalled; stalled.stalled=true; clock_ms=0; yields=0;
|
||||
assert(!ota_streamFirmware(stalled, tag.size(), reply));
|
||||
assert(Update.aborted && Update.end_calls==0 && yields>0);
|
||||
assert(strstr(reply, "timeout"));
|
||||
Update=UpdateMock(); stalled.stalled=true; stalled.disconnect_on_read=true;
|
||||
assert(!ota_streamFirmware(stalled, tag.size(), reply));
|
||||
assert(Update.aborted && Update.end_calls==0 && strstr(reply, "incomplete"));
|
||||
Update=UpdateMock(); Update.write_ok=false; Client bad_write; bad_write.image=tag;
|
||||
assert(!ota_streamFirmware(bad_write, tag.size(), reply));
|
||||
assert(Update.aborted && Update.end_calls==0);
|
||||
Update=UpdateMock(); Update.end_ok=false; Client bad_end; bad_end.image=tag;
|
||||
assert(!ota_streamFirmware(bad_end, tag.size(), reply));
|
||||
assert(!Update.boot_committed && Update.end_calls==1);
|
||||
}
|
||||
'''.replace("@STREAM@", stream)
|
||||
compile_run(code)
|
||||
|
||||
def test_channel_setting_rolls_back_and_deferred_update_keeps_snapshot(self):
|
||||
cli = (ROOT / "src/helpers/CommonCLI_Observer.cpp").read_text(encoding="utf-8")
|
||||
branch = extract_braced(cli, 'if (memcmp(command, "ota branch", 10) == 0')
|
||||
roles = []
|
||||
for role in ("simple_repeater", "simple_room_server"):
|
||||
header = (ROOT / "examples" / role / "MyMesh.h").read_text(encoding="utf-8")
|
||||
method = extract_braced(header, "bool beginDeferredOtaUpdate() override")
|
||||
roles.append("struct " + role + " : Deferred { " + method + " };\n")
|
||||
code = r'''
|
||||
#define WITH_MQTT_BRIDGE 1
|
||||
#define OTA_MANIFEST_BASE "https://native.example/v"
|
||||
#define OTA_MANIFEST_BASE_STABLE "https://prod.example/v"
|
||||
#define OTA_MANIFEST_BASE_DEV "https://beta.example/v"
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <helpers/OtaChannel.h>
|
||||
static uint32_t now_ms=10;
|
||||
uint32_t millis() { return now_ms; }
|
||||
struct Prefs { uint8_t ota_channel=0; };
|
||||
struct Common {
|
||||
Prefs prefs; Prefs* _prefs=&prefs;
|
||||
bool save_ok=true; int saves=0; uint8_t persisted=0;
|
||||
bool saveCommonPrefs() {
|
||||
++saves; if (!save_ok) return false;
|
||||
persisted=_prefs->ota_channel; return true;
|
||||
}
|
||||
bool run(const char* command, char* reply) { @BRANCH@ return false; }
|
||||
};
|
||||
struct Deferred {
|
||||
Prefs _prefs;
|
||||
unsigned long _ota_update_at=0;
|
||||
uint8_t _ota_update_channel=0;
|
||||
virtual bool beginDeferredOtaUpdate()=0;
|
||||
void otaAlert(const char*) {}
|
||||
};
|
||||
@ROLES@
|
||||
template<typename Role> void check_deferred() {
|
||||
Role role; role._prefs.ota_channel=OTA_CH_DEV;
|
||||
assert(role.beginDeferredOtaUpdate());
|
||||
role._prefs.ota_channel=OTA_CH_STABLE;
|
||||
assert(role._ota_update_channel==OTA_CH_DEV);
|
||||
assert(role._ota_update_at==2510);
|
||||
assert(!strcmp(ota_resolve_base(role._ota_update_channel), "https://beta.example/v"));
|
||||
}
|
||||
int main() {
|
||||
Common cli; char reply[160]={};
|
||||
assert(cli.run("ota branch", reply) && cli.saves==0);
|
||||
assert(strstr(reply, "build: custom"));
|
||||
assert(cli.run("ota branch beta", reply));
|
||||
assert(cli.prefs.ota_channel==OTA_CH_DEV && cli.persisted==OTA_CH_DEV && cli.saves==1);
|
||||
cli.save_ok=false;
|
||||
assert(cli.run("ota branch prod", reply));
|
||||
assert(cli.prefs.ota_channel==OTA_CH_DEV && cli.persisted==OTA_CH_DEV);
|
||||
assert(strstr(reply, "ERR: OTA channel save failed"));
|
||||
const int saves=cli.saves;
|
||||
assert(cli.run("ota branch Beta", reply) && cli.saves==saves);
|
||||
assert(!cli.run("ota branches", reply));
|
||||
cli.save_ok=true;
|
||||
for (const char* choice : {"ota branch prod", "ota branch stable", "ota branch default", "ota branch dev"})
|
||||
assert(cli.run(choice, reply) && !strncmp(reply, "channel set", 11));
|
||||
check_deferred<simple_repeater>(); check_deferred<simple_room_server>();
|
||||
}
|
||||
'''.replace("@BRANCH@", branch).replace("@ROLES@", "\n".join(roles))
|
||||
compile_run(code)
|
||||
|
||||
def test_worker_releases_network_pin_after_task_failure_and_completion(self):
|
||||
board = (ROOT / "src/helpers/ESP32Board.cpp").read_text(encoding="utf-8")
|
||||
args = extract_braced(board, "struct OtaTaskArgs {") + ";"
|
||||
entry = extract_braced(board, "static void ota_task_entry(")
|
||||
worker = extract_braced(board, "bool ESP32Board::otaFromManifest(")
|
||||
code = r'''
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <cstdint>
|
||||
using TaskHandle_t=void*; using BaseType_t=int;
|
||||
static constexpr BaseType_t pdPASS=1;
|
||||
static bool spawn_ok=false;
|
||||
void delay(uint32_t) {}
|
||||
void vTaskDelete(void*) {}
|
||||
BaseType_t xTaskCreatePinnedToCore(void (*entry)(void*), const char*, int, void* arg, int,
|
||||
TaskHandle_t*, int) {
|
||||
if (!spawn_ok) return 0;
|
||||
entry(arg); return pdPASS;
|
||||
}
|
||||
struct NetworkLink {
|
||||
int locks=0, releases=0;
|
||||
void lockSwitching() { ++locks; }
|
||||
void unlockSwitching() { ++releases; }
|
||||
} network;
|
||||
NetworkLink& activeNetworkLink() { return network; }
|
||||
struct ESP32Board {
|
||||
bool otaFromManifest(const char*, const char*, bool, char[]);
|
||||
bool otaFromManifestImpl(const char* base, const char* version, bool dry, char[]) {
|
||||
assert(network.locks==network.releases+1);
|
||||
assert(!strcmp(base, "base") && !strcmp(version, "version") && dry);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@ARGS@
|
||||
@ENTRY@
|
||||
@WORKER@
|
||||
int main() {
|
||||
ESP32Board board; char reply[160]={};
|
||||
assert(!board.otaFromManifest("base", "version", true, reply));
|
||||
assert(network.locks==1 && network.releases==1 && strstr(reply, "spawn failed"));
|
||||
spawn_ok=true;
|
||||
assert(!board.otaFromManifest("base", "version", true, reply));
|
||||
assert(network.locks==2 && network.releases==2);
|
||||
}
|
||||
'''.replace("@ARGS@", args).replace("@ENTRY@", entry).replace("@WORKER@", worker)
|
||||
compile_run(code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,157 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
#include "helpers/OtaChannel.h"
|
||||
|
||||
// The three base URLs are provided as -D macros by the test env (see platformio.ini).
|
||||
TEST(OtaChannel, ResolvesNativeToBaseMacro) {
|
||||
EXPECT_STREQ(ota_resolve_base(OTA_CH_NATIVE), "https://stable.example/mqtt/v");
|
||||
}
|
||||
TEST(OtaChannel, ResolvesStable) {
|
||||
EXPECT_STREQ(ota_resolve_base(OTA_CH_STABLE), "https://stable.example/mqtt/v");
|
||||
}
|
||||
TEST(OtaChannel, ResolvesDev) {
|
||||
EXPECT_STREQ(ota_resolve_base(OTA_CH_DEV), "https://dev.example/mqtt/dev/v");
|
||||
}
|
||||
TEST(OtaChannel, BasesAreStoredBehindCiTags) {
|
||||
EXPECT_STREQ(ota_tagged_native, "ota-base-native:https://stable.example/mqtt/v");
|
||||
EXPECT_STREQ(ota_tagged_stable, "ota-base-stable:https://stable.example/mqtt/v");
|
||||
EXPECT_STREQ(ota_tagged_dev, "ota-base-dev:https://dev.example/mqtt/dev/v");
|
||||
}
|
||||
TEST(OtaChannel, ParseKnownKeywords) {
|
||||
uint8_t ch = 99;
|
||||
EXPECT_TRUE(ota_parse_channel("prod", &ch)); EXPECT_EQ(ch, OTA_CH_STABLE);
|
||||
EXPECT_TRUE(ota_parse_channel("stable", &ch)); EXPECT_EQ(ch, OTA_CH_STABLE);
|
||||
EXPECT_TRUE(ota_parse_channel("beta", &ch)); EXPECT_EQ(ch, OTA_CH_DEV);
|
||||
EXPECT_TRUE(ota_parse_channel("default", &ch)); EXPECT_EQ(ch, OTA_CH_NATIVE);
|
||||
EXPECT_TRUE(ota_parse_channel("dev", &ch)); EXPECT_EQ(ch, OTA_CH_DEV);
|
||||
}
|
||||
TEST(OtaChannel, ParseRejectsUnknownAndLeavesOutputUntouched) {
|
||||
uint8_t ch = 7;
|
||||
EXPECT_FALSE(ota_parse_channel("production", &ch));
|
||||
EXPECT_FALSE(ota_parse_channel("Beta", &ch));
|
||||
EXPECT_FALSE(ota_parse_channel("", &ch));
|
||||
EXPECT_EQ(ch, 7);
|
||||
}
|
||||
TEST(OtaChannel, NameLabels) {
|
||||
EXPECT_STREQ(ota_channel_name(OTA_CH_NATIVE), "default");
|
||||
EXPECT_STREQ(ota_channel_name(OTA_CH_STABLE), "prod");
|
||||
EXPECT_STREQ(ota_channel_name(OTA_CH_DEV), "beta");
|
||||
}
|
||||
TEST(OtaChannel, NativeChannelNameMatchesBase) {
|
||||
EXPECT_STREQ(ota_native_channel_name(), "prod");
|
||||
}
|
||||
|
||||
TEST(OtaCompat, OwnTagParses) {
|
||||
OtaCompat own;
|
||||
ASSERT_TRUE(ota_compat_parse(ota_compat_tag + sizeof(OTA_COMPAT_TAG) - 1, &own));
|
||||
EXPECT_EQ(own.gen, OTA_STATE_GEN);
|
||||
EXPECT_EQ(own.caps, OTA_CAP_KEYMIND_PREFS);
|
||||
}
|
||||
TEST(OtaCompat, ParsesCapsAndRejectsJunk) {
|
||||
OtaCompat c;
|
||||
ASSERT_TRUE(ota_compat_parse("12+eth+future", &c));
|
||||
EXPECT_EQ(c.gen, 12);
|
||||
EXPECT_EQ(c.caps, OTA_CAP_ETH);
|
||||
EXPECT_FALSE(ota_compat_parse("", &c));
|
||||
EXPECT_FALSE(ota_compat_parse("x1", &c));
|
||||
EXPECT_FALSE(ota_compat_parse("2eth", &c));
|
||||
EXPECT_FALSE(ota_compat_parse("2+", &c));
|
||||
EXPECT_FALSE(ota_compat_parse("2147483648+keymind1", &c));
|
||||
ASSERT_TRUE(ota_compat_parse("1+keymind1", &c));
|
||||
EXPECT_EQ(c.caps, OTA_CAP_KEYMIND_PREFS);
|
||||
}
|
||||
TEST(OtaCompat, FindsCompleteTagOnly) {
|
||||
const char img[] = "\xe9junk\0ota-compat:2+eth\0tail";
|
||||
const char* v = ota_compat_find((const uint8_t*)img, sizeof(img));
|
||||
ASSERT_NE(v, nullptr);
|
||||
EXPECT_STREQ(v, "2+eth");
|
||||
const char cut[] = "junk ota-compat:2+e"; // value runs past the chunk end
|
||||
EXPECT_EQ(ota_compat_find((const uint8_t*)cut, sizeof(cut) - 1), nullptr);
|
||||
const char literal_first[] = "ota-compat:\0code\0ota-compat:1\0"; // search literal precedes the tag
|
||||
v = ota_compat_find((const uint8_t*)literal_first, sizeof(literal_first));
|
||||
ASSERT_NE(v, nullptr);
|
||||
EXPECT_STREQ(v, "1");
|
||||
const char none[] = "ota-compat";
|
||||
EXPECT_EQ(ota_compat_find((const uint8_t*)none, sizeof(none)), nullptr);
|
||||
}
|
||||
TEST(OtaCompat, TargetMustKeepStateAndTransports) {
|
||||
EXPECT_TRUE(ota_compat_ok({2, 0}, {2, 0}));
|
||||
EXPECT_TRUE(ota_compat_ok({1, 0}, {2, OTA_CAP_ETH}));
|
||||
EXPECT_FALSE(ota_compat_ok({2, 0}, {1, 0})); // cannot read /mqtt.json
|
||||
EXPECT_FALSE(ota_compat_ok({2, OTA_CAP_ETH}, {2, 0})); // drops Ethernet
|
||||
EXPECT_TRUE(ota_compat_ok({2, OTA_CAP_ETH}, {3, OTA_CAP_ETH}));
|
||||
EXPECT_FALSE(ota_compat_ok({1, OTA_CAP_KEYMIND_PREFS}, {2, 0})); // upstream JSON generation
|
||||
EXPECT_TRUE(ota_compat_ok({1, OTA_CAP_KEYMIND_PREFS}, {1, OTA_CAP_KEYMIND_PREFS}));
|
||||
}
|
||||
|
||||
TEST(OtaCompat, ScannerHandlesEveryTagSplitAndSkipsBareLiteral) {
|
||||
const char img[] = "ota-compat:\0junk\0ota-compat:1+keymind1\0tail";
|
||||
for (size_t chunk = 1; chunk <= sizeof(img); ++chunk) {
|
||||
OtaCompatScanner scanner;
|
||||
for (size_t offset = 0; offset < sizeof(img); offset += chunk) {
|
||||
const size_t remaining = sizeof(img) - offset;
|
||||
scanner.consume((const uint8_t*)img + offset, chunk < remaining ? chunk : remaining);
|
||||
}
|
||||
OtaCompat value;
|
||||
ASSERT_TRUE(scanner.finish(&value)) << chunk;
|
||||
EXPECT_EQ(value.gen, 1);
|
||||
EXPECT_EQ(value.caps, OTA_CAP_KEYMIND_PREFS);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(OtaCompat, ScannerRejectsTruncatedAndSkipsMalformedCandidates) {
|
||||
const char truncated[] = "ota-compat:1+keymind1";
|
||||
OtaCompatScanner scanner;
|
||||
scanner.consume((const uint8_t*)truncated, sizeof(truncated) - 1);
|
||||
OtaCompat value;
|
||||
EXPECT_FALSE(scanner.finish(&value));
|
||||
|
||||
const char image[] = "ota-compat:2147483648\0ota-compat:1+keymind1\0";
|
||||
scanner = OtaCompatScanner();
|
||||
scanner.consume((const uint8_t*)image, sizeof(image));
|
||||
ASSERT_TRUE(scanner.finish(&value));
|
||||
EXPECT_EQ(value.caps, OTA_CAP_KEYMIND_PREFS);
|
||||
}
|
||||
|
||||
TEST(OtaCompat, ScannerRefusesConflictingTagsInAnyOrder) {
|
||||
const char compatible[] = "ota-compat:1+keymind1";
|
||||
const char upstream[] = "ota-compat:2";
|
||||
OtaCompat value;
|
||||
for (bool compatible_first : {true, false}) {
|
||||
OtaCompatScanner scanner;
|
||||
scanner.consume((const uint8_t*)(compatible_first ? compatible : upstream),
|
||||
compatible_first ? sizeof(compatible) : sizeof(upstream));
|
||||
scanner.consume((const uint8_t*)(compatible_first ? upstream : compatible),
|
||||
compatible_first ? sizeof(upstream) : sizeof(compatible));
|
||||
EXPECT_FALSE(scanner.finish(&value));
|
||||
}
|
||||
OtaCompatScanner repeated;
|
||||
repeated.consume((const uint8_t*)compatible, sizeof(compatible));
|
||||
repeated.consume((const uint8_t*)compatible, sizeof(compatible));
|
||||
EXPECT_TRUE(repeated.finish(&value));
|
||||
const char different_unknown[] = "ota-compat:1+keymind1+future";
|
||||
repeated.consume((const uint8_t*)different_unknown, sizeof(different_unknown));
|
||||
EXPECT_FALSE(repeated.finish(&value));
|
||||
}
|
||||
|
||||
TEST(OtaCompat, ScannerRefusesOversizedOrIncompleteTagAfterValidTag) {
|
||||
const char compatible[] = "ota-compat:1+keymind1";
|
||||
OtaCompatScanner scanner;
|
||||
scanner.consume((const uint8_t*)compatible, sizeof(compatible));
|
||||
const char incomplete[] = "ota-compat:2+future";
|
||||
scanner.consume((const uint8_t*)incomplete, sizeof(incomplete) - 1);
|
||||
OtaCompat value;
|
||||
EXPECT_FALSE(scanner.finish(&value));
|
||||
|
||||
scanner = OtaCompatScanner();
|
||||
scanner.consume((const uint8_t*)compatible, sizeof(compatible));
|
||||
std::string oversized = "ota-compat:1+keymind1+" + std::string(128, 'a');
|
||||
oversized.push_back(0);
|
||||
for (char byte : oversized) scanner.consume((const uint8_t*)&byte, 1);
|
||||
EXPECT_FALSE(scanner.finish(&value));
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
"""Exercise OTA channel persistence through the production common prefs store."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_common_usb_debug import HARNESS
|
||||
from test_replay_reset_integration import extract_braced
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
MAIN = r'''
|
||||
struct Capture {
|
||||
std::vector<uint8_t> bytes;
|
||||
std::map<const void*, size_t> offsets;
|
||||
size_t write(const uint8_t* data, size_t size) {
|
||||
offsets[data] = bytes.size();
|
||||
bytes.insert(bytes.end(), data, data + size);
|
||||
return size;
|
||||
}
|
||||
size_t offset(const void* field) const { return offsets.at(field); }
|
||||
};
|
||||
|
||||
void seed(CommonCLI& cli) {
|
||||
auto& p = cli.prefs;
|
||||
strcpy(p.node_name, "OTA channel node");
|
||||
strcpy(p.password, "channel-pass");
|
||||
strcpy(p.owner_info, "owner information");
|
||||
p.node_lat = 40.125; p.node_lon = -105.25;
|
||||
p.freq = 920.25f; p.bw = 125; p.sf = 9; p.cr = 6;
|
||||
p.tx_power_dbm = 14;
|
||||
p.primary_radio_preamble = 96;
|
||||
p.gps_enabled = 1; p.gps_interval = 300;
|
||||
p.gps_sync_interval_hours = 336;
|
||||
p.espnow_bridge_enabled = 0;
|
||||
p.usb_logging_enabled = 0; p.usb_debug_enabled = 1;
|
||||
p.trace_when_repeat_off = 1;
|
||||
setDefaultDirectRetryPrefs(&p);
|
||||
}
|
||||
|
||||
void assertExistingPrefs(const NodePrefs& actual, const NodePrefs& expected) {
|
||||
assert(strcmp(actual.node_name, expected.node_name) == 0);
|
||||
assert(strcmp(actual.password, expected.password) == 0);
|
||||
assert(strcmp(actual.owner_info, expected.owner_info) == 0);
|
||||
assert(actual.node_lat == expected.node_lat && actual.node_lon == expected.node_lon);
|
||||
assert(actual.freq == expected.freq && actual.bw == expected.bw);
|
||||
assert(actual.sf == expected.sf && actual.cr == expected.cr);
|
||||
assert(actual.tx_power_dbm == expected.tx_power_dbm);
|
||||
assert(actual.primary_radio_preamble == expected.primary_radio_preamble);
|
||||
assert(actual.gps_enabled == expected.gps_enabled && actual.gps_interval == expected.gps_interval);
|
||||
assert(actual.gps_sync_interval_hours == expected.gps_sync_interval_hours);
|
||||
assert(actual.espnow_bridge_enabled == expected.espnow_bridge_enabled);
|
||||
assert(actual.usb_logging_enabled == expected.usb_logging_enabled);
|
||||
assert(actual.usb_debug_enabled == expected.usb_debug_enabled);
|
||||
assert(actual.trace_when_repeat_off == expected.trace_when_repeat_off);
|
||||
}
|
||||
|
||||
void testRoundTripsAndLayout() {
|
||||
CommonCLI cli;
|
||||
seed(cli);
|
||||
assert(cli.prefs.ota_channel == 0);
|
||||
Capture original;
|
||||
assert(writeCommonPrefsImage(original, &cli.prefs));
|
||||
const size_t channel_offset = original.offset(&cli.prefs.ota_channel);
|
||||
const size_t trace_offset = original.offset(&cli.prefs.trace_when_repeat_off);
|
||||
assert(channel_offset == trace_offset + sizeof(cli.prefs.trace_when_repeat_off));
|
||||
assert(channel_offset + sizeof(cli.prefs.ota_channel) == original.bytes.size());
|
||||
// The established common core remains at its public file offsets.
|
||||
assert(original.offset(cli.prefs.node_name) == 4);
|
||||
assert(original.offset(&cli.prefs.freq) == 72);
|
||||
assert(original.offset(&cli.prefs.sf) == 112);
|
||||
assert(original.offset(&cli.prefs.bw) == 116);
|
||||
|
||||
for (uint8_t channel : {0, 1, 2}) {
|
||||
cli.prefs.ota_channel = channel;
|
||||
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
|
||||
assert(cli._common_save_succeeded);
|
||||
const auto image = cli.fs.files.at("/com_prefs");
|
||||
Capture checked;
|
||||
assert(writeCommonPrefsImage(checked, &cli.prefs));
|
||||
assert(checked.bytes == image); // Checked and ordinary writers agree.
|
||||
assert(image.size() == original.bytes.size() && image[channel_offset] == channel);
|
||||
for (size_t i = 0; i < image.size(); ++i) {
|
||||
assert(i == channel_offset || image[i] == original.bytes[i]);
|
||||
}
|
||||
CommonCLI reader;
|
||||
reader.prefs.ota_channel = 2; // A reused preference object must be replaced.
|
||||
reader.fs.files["/com_prefs"] = image;
|
||||
reader.loadPrefsInt(&reader.fs, "/com_prefs");
|
||||
assert(reader.prefs.ota_channel == channel);
|
||||
assertExistingPrefs(reader.prefs, cli.prefs);
|
||||
}
|
||||
}
|
||||
|
||||
void testLegacyTruncatedAndCorruptImages() {
|
||||
CommonCLI writer;
|
||||
seed(writer);
|
||||
writer.prefs.ota_channel = 2;
|
||||
Capture captured;
|
||||
assert(writeCommonPrefsImage(captured, &writer.prefs));
|
||||
const size_t channel_offset = captured.offset(&writer.prefs.ota_channel);
|
||||
const size_t gps_offset = captured.offset(&writer.prefs.gps_sync_interval_hours);
|
||||
const size_t debug_offset = captured.offset(&writer.prefs.usb_debug_enabled);
|
||||
const size_t trace_offset = captured.offset(&writer.prefs.trace_when_repeat_off);
|
||||
const auto current = captured.bytes;
|
||||
CommonCLI reader;
|
||||
|
||||
// Derive the previous image from the append boundary, without hardcoding its size.
|
||||
reader.fs.files["/com_prefs"] = current;
|
||||
reader.loadPrefsInt(&reader.fs, "/com_prefs");
|
||||
assert(reader.prefs.ota_channel == 2);
|
||||
reader.fs.files["/com_prefs"] = {current.begin(), current.begin() + channel_offset};
|
||||
reader.loadPrefsInt(&reader.fs, "/com_prefs");
|
||||
assert(reader.prefs.ota_channel == 0);
|
||||
assertExistingPrefs(reader.prefs, writer.prefs);
|
||||
|
||||
for (size_t length = 0; length <= channel_offset; ++length) {
|
||||
reader.prefs.ota_channel = 2;
|
||||
reader.prefs.usb_debug_enabled = 1;
|
||||
reader.prefs.trace_when_repeat_off = 1;
|
||||
reader.prefs.gps_sync_interval_hours = 336;
|
||||
reader.fs.files["/com_prefs"] = {current.begin(), current.begin() + length};
|
||||
reader.loadPrefsInt(&reader.fs, "/com_prefs");
|
||||
assert(reader.prefs.ota_channel == 0);
|
||||
assert(reader.prefs.gps_sync_interval_hours == (length >= gps_offset + 2 ? 336 : 0));
|
||||
assert(reader.prefs.usb_debug_enabled == (length > debug_offset ? 1 : 0));
|
||||
assert(reader.prefs.trace_when_repeat_off == (length > trace_offset ? 1 : 0));
|
||||
}
|
||||
|
||||
for (unsigned invalid = 3; invalid <= 255; ++invalid) {
|
||||
auto corrupt = current;
|
||||
corrupt[channel_offset] = static_cast<uint8_t>(invalid);
|
||||
reader.prefs.ota_channel = 2;
|
||||
reader.fs.files["/com_prefs"] = corrupt;
|
||||
reader.loadPrefsInt(&reader.fs, "/com_prefs");
|
||||
assert(reader.prefs.ota_channel == 0);
|
||||
assertExistingPrefs(reader.prefs, writer.prefs);
|
||||
}
|
||||
|
||||
// A file reporting a full size can still return a short/error read at the new byte.
|
||||
reader.fs.files["/com_prefs"] = current;
|
||||
reader.fs.fail_read_after = static_cast<int>(channel_offset);
|
||||
reader.prefs.ota_channel = 2;
|
||||
reader.loadPrefsInt(&reader.fs, "/com_prefs");
|
||||
assert(reader.prefs.ota_channel == 0);
|
||||
assertExistingPrefs(reader.prefs, writer.prefs);
|
||||
reader.fs.fail_read_after = -1;
|
||||
reader.prefs.ota_channel = 2;
|
||||
reader.loadPrefsInt(&reader.fs, "/missing");
|
||||
assert(reader.prefs.ota_channel == 0);
|
||||
reader.fs.fail_read_open = true;
|
||||
reader.prefs.ota_channel = 2;
|
||||
reader.loadPrefsInt(&reader.fs, "/com_prefs");
|
||||
assert(reader.prefs.ota_channel == 0);
|
||||
}
|
||||
|
||||
void testFailedTransactionsRetainPreviousChannel() {
|
||||
for (uint8_t previous_channel : {1, 2}) {
|
||||
for (int fault : {0, 1, 2, 3, 4, 5}) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Observer finish() checks reopening and size; standard writers also read CRCs.
|
||||
if (fault == 3) continue;
|
||||
#endif
|
||||
CommonCLI cli;
|
||||
seed(cli);
|
||||
cli.prefs.ota_channel = previous_channel;
|
||||
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
|
||||
assert(cli._common_save_succeeded);
|
||||
const auto previous = cli.fs.files.at("/com_prefs");
|
||||
const NodePrefs expected = cli.prefs;
|
||||
Capture capture;
|
||||
assert(writeCommonPrefsImage(capture, &cli.prefs));
|
||||
const size_t channel_offset = capture.offset(&cli.prefs.ota_channel);
|
||||
if (fault == 0) cli.fs.fail_write = true;
|
||||
if (fault == 1) cli.fs.fail_write_after = static_cast<int>(channel_offset);
|
||||
if (fault == 2) cli.fs.fail_read_open = true;
|
||||
if (fault == 3) cli.fs.fail_read_after = static_cast<int>(channel_offset);
|
||||
if (fault == 4) cli.fs.fail_rename = 1;
|
||||
if (fault == 5) cli.fs.fail_rename_from = {"/com_prefs.tmp"};
|
||||
cli.prefs.ota_channel = previous_channel == 1 ? 2 : 1;
|
||||
assert(!cli.trySavePrefs());
|
||||
assert(cli.fs.files.at("/com_prefs") == previous);
|
||||
cli.fs.fail_write = false; cli.fs.fail_write_after = -1;
|
||||
cli.fs.fail_read_open = false; cli.fs.fail_read_after = -1;
|
||||
cli.fs.fail_rename = 0; cli.fs.fail_rename_from.clear();
|
||||
assert(cli.recoverCommonPrefsFiles(&cli.fs));
|
||||
assert(cli.fs.files.at("/com_prefs") == previous);
|
||||
cli.loadPrefsInt(&cli.fs, "/com_prefs");
|
||||
assert(cli.prefs.ota_channel == previous_channel);
|
||||
assertExistingPrefs(cli.prefs, expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
testRoundTripsAndLayout();
|
||||
testLegacyTruncatedAndCorruptImages();
|
||||
testFailedTransactionsRetainPreviousChannel();
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def persistence_harness():
|
||||
source = (ROOT / "src/helpers/CommonCLI.cpp").read_text(encoding="utf-8")
|
||||
header = (ROOT / "src/helpers/CommonCLI.h").read_text(encoding="utf-8")
|
||||
fields = header.split("class NodePrefs :", 1)[1].split("private:", 1)[0]
|
||||
fields = "\n".join(re.findall(
|
||||
r"^\s*(?:float|double|char|u?int(?:8|16|32)_t)\s+[^;]+;", fields, re.M))
|
||||
constants = "\n".join(re.findall(
|
||||
r"^\s*#define\s+\w+\s+(?:0x[\dA-Fa-f]+|\d+)\s*$", header, re.M))
|
||||
constants += "\n" + "\n".join(re.findall(
|
||||
r"^static const size_t LEGACY_\w+\s*=[^;]+;", source, re.M))
|
||||
constants += "\n" + re.search(
|
||||
r"^static constexpr uint8_t DEFAULT_ADVERT_LOC_POLICY\s*=[^;]+;",
|
||||
header, re.M).group()
|
||||
helpers = "\n".join(extract_braced(source, signature) for signature in (
|
||||
"static bool bwMatches(", "static bool isValidLoRaBandwidth(",
|
||||
"static float defaultLoRaBandwidth(", "static void markDirectRetryPrefsValid(",
|
||||
"static void applyFloodRetryPreset(", "static void applyDirectRetryPreset(",
|
||||
"static void setDefaultDirectRetryPrefs(", "static bool directRetryPrefsValid("))
|
||||
methods = "\n".join(extract_braced(source, signature) for signature in (
|
||||
"static const char* commonPrefsSaveResultName(",
|
||||
"bool CommonCLI::recoverCommonPrefsFiles(",
|
||||
"bool CommonCLI::saveCommonPrefsImageAtomically(",
|
||||
"void CommonCLI::savePrefs(FILESYSTEM*",
|
||||
"void CommonCLI::savePrefs(PrefsSaveRouting::Scope scope)",
|
||||
"bool CommonCLI::trySavePrefs(", "void CommonCLI::loadPrefsInt("))
|
||||
code = HARNESS[:HARNESS.index("struct Capture {")] + MAIN
|
||||
replacements = {
|
||||
"FIELDS": fields, "CONSTANTS": constants, "HELPERS": helpers,
|
||||
"LEGACY": extract_braced(header, "struct LegacyObserverTail") + ";",
|
||||
"SERIALIZER": "template<typename Writer>\n" + extract_braced(
|
||||
source, "static bool writeCommonPrefsImage("),
|
||||
"STORE": extract_braced(source, "class CommonPrefsFileStore"),
|
||||
"METHODS": methods,
|
||||
"DYNAMIC": "", "SET_DEBUG": "", "SET_MASTER": "",
|
||||
"GET_DEBUG": "", "GET_MASTER": "",
|
||||
}
|
||||
for key, value in replacements.items():
|
||||
code = code.replace("@" + key + "@", value)
|
||||
return code
|
||||
|
||||
|
||||
class OtaChannelPersistenceTests(unittest.TestCase):
|
||||
def test_actual_serializers_loads_and_failed_commits_for_every_platform(self):
|
||||
code = persistence_harness()
|
||||
sanitizer_flags = [] if os.name == "nt" else [
|
||||
"-fsanitize=address,undefined", "-fno-sanitize-recover=all",
|
||||
"-fno-pie", "-no-pie"]
|
||||
variants = [("ENABLE_OTA", platform) for platform in (
|
||||
"NRF52_PLATFORM", "STM32_PLATFORM", "ESP32_PLATFORM", "RP2040_PLATFORM")]
|
||||
variants.append(("ENABLE_OTA", "ESP32_PLATFORM", "WITH_MQTT_BRIDGE"))
|
||||
variants.extend((("NRF52_PLATFORM",), ("ESP32_PLATFORM", "WITH_MQTT_BRIDGE")))
|
||||
with tempfile.TemporaryDirectory(prefix="common-ota-channel-") as directory:
|
||||
work = Path(directory)
|
||||
transaction = (ROOT / "src/helpers/ContactFileTransaction.h").read_text()
|
||||
(work / "ContactFileTransaction.h").write_text(transaction.replace(
|
||||
'#include "IdentityStore.h"', '#include <helpers/IdentityStore.h>'))
|
||||
(work / "test.cpp").write_text(code, encoding="utf-8")
|
||||
for macros in variants:
|
||||
with self.subTest(macros=macros):
|
||||
exe = work / ("test.exe" if os.name == "nt" else "test")
|
||||
built = subprocess.run([
|
||||
os.environ.get("CXX", "g++"), "-std=c++17",
|
||||
*["-D" + macro + "=1" for macro in macros], *sanitizer_flags,
|
||||
"-I" + str(work), "-I" + str(ROOT / "test/fixtures/radio_profiles/mocks"),
|
||||
"-I" + str(ROOT / "src"), "-I" + str(ROOT / "src/helpers"),
|
||||
str(work / "test.cpp"), "-o", str(exe)],
|
||||
capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(built.returncode, 0, built.stderr)
|
||||
tested = subprocess.run([str(exe)], capture_output=True, text=True, timeout=10)
|
||||
self.assertEqual(tested.returncode, 0, tested.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -21,7 +21,7 @@ int main() {
|
||||
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
|
||||
assert(cli._common_save_succeeded);
|
||||
const auto baseline = cli.fs.files["/com_prefs"];
|
||||
assert(baseline.size() == 873);
|
||||
assert(baseline.size() == 874);
|
||||
const size_t trace_offset = 872, debug_offset = 871, gps_offset = 869;
|
||||
assert(baseline[trace_offset] == 0 && baseline[debug_offset] == 1);
|
||||
assert(baseline[866] == 48 && baseline[867] == 0 && baseline[868] == 1);
|
||||
@@ -69,7 +69,7 @@ int main() {
|
||||
assert(cli.prefs.trace_when_repeat_off == 0);
|
||||
|
||||
// Both enable and disable must preserve the saved and requested state when
|
||||
// staging, the new final-byte write, readback or publication fails.
|
||||
// staging, the trace-byte write, readback or publication fails.
|
||||
for (uint8_t desired : {1, 0}) {
|
||||
cli.set(desired ? "repeat.trace off" : "repeat.trace on", reply);
|
||||
assert(cli.prefs.trace_when_repeat_off == uint8_t(!desired));
|
||||
@@ -128,7 +128,7 @@ int main() {
|
||||
// reset prior RAM intent to OFF. GPS was published before local debug/trace;
|
||||
// a complete GPS-only image must not reinterpret either GPS byte as intent.
|
||||
auto image = enabled;
|
||||
for (size_t size = 0; size < image.size(); ++size) {
|
||||
for (size_t size = 0; size <= trace_offset; ++size) {
|
||||
CommonCLI reader;
|
||||
reader.prefs.trace_when_repeat_off = 1;
|
||||
reader.prefs.usb_debug_enabled = 1;
|
||||
@@ -139,6 +139,12 @@ int main() {
|
||||
else assert(reader.prefs.usb_debug_enabled == 0);
|
||||
if (size >= gps_offset + 2) assert(reader.prefs.gps_sync_interval_hours == 336);
|
||||
}
|
||||
// An old image containing trace but lacking the later channel byte keeps trace ON.
|
||||
CommonCLI old_reader;
|
||||
old_reader.prefs.ota_channel = 2;
|
||||
old_reader.fs.files["/com_prefs"] = {image.begin(), image.begin() + trace_offset + 1};
|
||||
old_reader.loadPrefsInt(&old_reader.fs, "/com_prefs");
|
||||
assert(old_reader.prefs.trace_when_repeat_off == 1 && old_reader.prefs.ota_channel == 0);
|
||||
// Explicit old GPS-only files with either byte equal to one must not arm
|
||||
// the local boolean preferences, regardless of their previous RAM values.
|
||||
for (uint16_t hours : {1, 256, 257, 336}) {
|
||||
|
||||
Reference in new issue
Block a user