Harden USB logging and persist watchdog events in management reports

This commit is contained in:
mikecarper committed 2026-10-03 15:29:19 -07:00
1 parent bd55369aa9
commit 3add4695cc
98 files changed
+7207 -372

No files matched your search

@@ -8,6 +8,7 @@ using esp_event_handler_t = void (*)(void*, esp_event_base_t, int32_t, void*);
constexpr int ARDUINO_USB_CDC_ANY_EVENT = -1;
constexpr int ARDUINO_USB_CDC_DISCONNECTED_EVENT = 1;
constexpr int ARDUINO_USB_CDC_LINE_STATE_EVENT = 2;
constexpr int ARDUINO_USB_CDC_TX_EVENT = 3;
struct arduino_usb_cdc_event_data_t {
struct { bool dtr; bool rts; } line_state{};
};
@@ -35,6 +36,7 @@ class MockSerial : public Stream {
assert(false && "mode0 must never call USBCDC::flush");
}
void setDebugOutput(bool enabled) { debug_enabled = enabled; }
void setTxTimeoutMs(unsigned timeout) { tx_timeout_ms = timeout; }
void onEvent(int event, esp_event_handler_t handler) {
assert(event == ARDUINO_USB_CDC_ANY_EVENT);
callback = handler;
@@ -44,6 +46,7 @@ class MockSerial : public Stream {
int rx_count = 0;
int registrations = 0;
bool debug_enabled = true;
unsigned tx_timeout_ms = 99;
esp_event_handler_t callback = nullptr;
};
@@ -7,3 +7,6 @@ uint32_t tud_cdc_n_write_available(uint8_t instance);
uint32_t tud_cdc_n_write(uint8_t instance, const void* data, uint32_t size);
uint32_t tud_cdc_n_write_flush(uint8_t instance);
bool tud_cdc_n_write_clear(uint8_t instance);
bool tud_mounted();
bool tud_connect();
bool tud_disconnect();
@@ -3,10 +3,15 @@
#include <iostream>
#include <string>
#include "helpers/UsbLogging.h"
#include "helpers/UsbLoggingClientActivity.h"
#include "MeshCore.h"
#include "helpers/SerialPacketLog.h"
MockSerial Serial;
static bool connected = true;
static bool mounted = true;
static unsigned disconnect_calls = 0;
static unsigned connect_calls = 0;
static bool auto_drain = false;
static std::string fifo;
static std::string host;
@@ -15,6 +20,10 @@ static unsigned flush_calls = 0;
static unsigned clear_calls = 0;
static void (*during_write)() = nullptr;
bool tud_mounted() { return mounted; }
bool tud_disconnect() { ++disconnect_calls; mounted = false; connected = false; return true; }
bool tud_connect() { ++connect_calls; return true; }
bool tud_cdc_n_connected(uint8_t instance) {
assert(!mock_isr);
assert(instance == 0);
@@ -82,6 +91,50 @@ static void fresh_session() {
auto_drain = false;
}
static void check_logging_watchdog_transport_observations_and_recovery() {
fresh_session();
mesh::setUsbLoggingEnabled(true);
const auto initial = mesh::observeUsbLoggingTransport();
assert(initial.supported && initial.host_connected && initial.reader_connected);
assert(!initial.pending);
assert(!mesh::isUsbLoggingClientActive()); // An open CDC is not an app poll.
mesh::noteUsbLoggingStatsCommand("stats-core");
assert(mesh::isUsbLoggingClientActive());
fresh_session(); // Even a close/reopen between service calls revokes it.
assert(!mesh::isUsbLoggingClientActive());
mesh::noteUsbLoggingStatsCommand("stats-radio");
mesh::setUsbLoggingEnabled(false);
assert(!mesh::isUsbLoggingClientActive());
mesh::setUsbLoggingEnabled(true);
mesh::probeUsbLoggingTransport();
const auto queued = mesh::observeUsbLoggingTransport();
assert(queued.pending && queued.tx_progress == initial.tx_progress);
assert(mesh::recoverUsbLoggingTransport(1) == mesh::UsbLoggingRecoveryResult::Attempted);
// Purge and probe merely change queues; neither is host acknowledgement.
assert(mesh::observeUsbLoggingTransport().tx_progress == initial.tx_progress);
host += fifo; fifo.clear();
Serial.callback(nullptr, nullptr, ARDUINO_USB_CDC_TX_EVENT, nullptr);
const auto completed = mesh::observeUsbLoggingTransport();
assert(completed.tx_progress == initial.tx_progress + 1);
assert(!completed.pending);
const auto detached_at = millis();
assert(mesh::recoverUsbLoggingTransport(2) == mesh::UsbLoggingRecoveryResult::Attempted);
assert(disconnect_calls == 1 && connect_calls == 0);
assert(millis() == detached_at); // No blocking wait or descriptor rebuild.
g_mock_millis += 49;
mesh::serviceUsbLoggingPort();
assert(connect_calls == 0);
++g_mock_millis;
mesh::serviceUsbLoggingPort();
assert(connect_calls == 1 && !mesh::observeUsbLoggingTransport().host_connected);
mock_isr = true;
assert(mesh::recoverUsbLoggingTransport(1) == mesh::UsbLoggingRecoveryResult::Deferred);
mesh::probeUsbLoggingTransport();
mock_isr = false;
mounted = connected = true;
fresh_session();
}
static void check_native_short_writes_and_mota() {
const unsigned before = write_calls;
assert(put(mesh::usbCompanionPort(), std::string(200, 'B')) == 64);
@@ -150,6 +203,7 @@ static void check_disconnect_cleanup_keeps_new_host_input() {
}
static void check_debug_formatter_and_reentrancy() {
mesh::setUsbDebugEnabled(true);
auto_drain = true;
const std::string long_text(1000, 'D');
assert(mesh::nrf52DebugPrintf("%s\n", long_text.c_str()) == 255);
@@ -165,6 +219,39 @@ static void check_debug_formatter_and_reentrancy() {
drain_all();
assert(host == "outer\n");
fresh_session();
mesh::setUsbDebugEnabled(false);
}
static void check_independent_debug_gate() {
assert(!mesh::isUsbDebugEnabled());
assert(mesh::isUsbLoggingEnabled());
const auto before = write_calls;
MESH_DEBUG_PRINTLN("default-off debug");
assert(mesh::nrf52DebugPrintf("also quiet\n") == 0);
assert(write_calls == before && !mesh::hasPendingUsbTerminalOutput());
mesh::SerialLogLine<> raw;
raw.printf("12:34:56 - 3/10/2026 U RAW: 0102");
assert(raw.flush(mesh::usbLoggingPort()));
drain_all();
assert(host == "12:34:56 - 3/10/2026 U RAW: 0102\r\n");
fresh_session();
mesh::setUsbDebugEnabled(true);
assert(mesh::isUsbDebugEnabled() && mesh::isUsbDebugLoggingEnabled());
MESH_DEBUG_PRINTLN("explicit debug");
drain_all();
assert(host == "DEBUG: explicit debug\n");
fresh_session();
mesh::setUsbLoggingEnabled(false);
assert(mesh::isUsbDebugEnabled() && !mesh::isUsbDebugLoggingEnabled());
const auto gated = write_calls;
MESH_DEBUG_PRINTLN("master is off");
assert(write_calls == gated && !mesh::hasPendingUsbTerminalOutput());
mesh::setUsbLoggingEnabled(true);
assert(mesh::isUsbDebugLoggingEnabled());
mesh::setUsbDebugEnabled(false);
assert(mesh::isUsbLoggingEnabled() && !mesh::isUsbDebugLoggingEnabled());
assert(!Serial.debug_enabled); // native raw framework output stays forbidden
}
static void check_cached_logging_gate_and_isr() {
@@ -215,6 +302,8 @@ int main() {
assert(!Serial.debug_enabled);
mesh::serviceUsbLoggingPort();
assert(&mesh::usbConsolePort() == &mesh::usbTerminalPort());
check_logging_watchdog_transport_observations_and_recovery();
check_independent_debug_gate();
check_native_short_writes_and_mota();
check_ordered_text_and_functional_reserve();
check_stalled_host_and_visible_overflow();
@@ -227,6 +316,9 @@ int main() {
assert(&mesh::usbConsolePort() == &Serial);
assert(mesh::canAcceptUsbConsoleCommand());
assert(mesh::usbTerminalDroppedBytes() == 0);
assert(!mesh::observeUsbLoggingTransport().supported);
assert(mesh::recoverUsbLoggingTransport(1) == mesh::UsbLoggingRecoveryResult::Unsupported);
mesh::probeUsbLoggingTransport();
#endif
std::cout << "ESP32 TinyUSB transport checks passed\n";
}
+84 -13
View File
@@ -19,7 +19,7 @@ int main() {
assert(equal(tag, hex("85632d07c6e8f37f950acd320a2ecc93").data(), 16));
assert(p == hex("40c02b9690c4dc04daef7f6afe5c"));
assert(open(k.data(), ad.data(), ad.size(), p.data(), p.size(), tag)); assert(p == plain);
for (size_t n : {size_t(0), size_t(1), size_t(15), size_t(16), size_t(17), size_t(78), size_t(177)}) {
for (size_t n : {size_t(0), size_t(1), size_t(15), size_t(16), size_t(17), size_t(78), size_t(177), MAX_PAYLOAD}) {
std::vector<uint8_t> b(n, 0x39), original = b;
assert(seal(k.data(), ad.data(), ad.size(), b.data(), n, tag));
assert(open(k.data(), ad.data(), ad.size(), b.data(), n, tag)); assert(b == original);
@@ -27,7 +27,7 @@ int main() {
assert(!open(k.data(), ad.data(), ad.size(), b.data(), n, tag));
for (uint8_t c : b) assert(c == 0);
}
assert(!seal(k.data(), ad.data(), ad.size(), p.data(), 178, tag));
assert(!seal(k.data(), ad.data(), ad.size(), p.data(), MAX_PAYLOAD + 1, tag));
History history;
history.sample(0, 3900, -4); history.sample(59, 3100, 98);
assert(history.week().voltage == 3100 && history.week().high == temperature(98));
@@ -58,18 +58,89 @@ int main() {
assert(acl.count == 1 && acl.entries[0][12] == 3);
for (uint8_t i = 1; i < MAX_KEYS; ++i) { token[0] = i; assert(acl.add(token, ADMIN)); }
token[0] = 240; assert(!acl.add(token, ADMIN)); assert(acl.pages() == MAX_PAGES);
// Emit cross-language packets with all header fields and maximum ACL pages.
for (uint8_t page = 0; page < acl.pages(); ++page) {
uint8_t raw[MAX_PAYLOAD] = {}, enc[32]; memcpy(raw, "MGR1", 4); memcpy(raw + 4, radio, 16);
write32(raw + 20, 42); write32(raw + 28, 0x01110105); write16(raw + 76, FIRMWARE);
raw[71] = 5; raw[78] = page; raw[79] = acl.pages(); raw[80] = acl.count;
raw[81] = page * PER_PAGE; raw[82] = PER_PAGE;
memcpy(raw + HEADER, acl.entries[page * PER_PAGE], PER_PAGE * ENTRY);
mesh::UsbLoggingStatus usb;
usb.supported = usb.logging_enabled = usb.watchdog_enabled = true;
usb.host_connected = usb.reader_connected = usb.logger_active = true;
usb.stalled = usb.recovering = usb.recovery_deferred = usb.persistence_ready = true;
usb.watchdog_auto = true; usb.auto_connected_seconds = 1209600;
usb.stage = 2; usb.backoff_step = 8; usb.retry_seconds = 604800;
usb.inactive_seconds = UINT32_MAX;
usb.last_event.reasons = 15;
usb.last_event.action = mesh::UsbLoggingWatchdogEvent::REBOOT_REQUESTED;
usb.last_event.epoch = 1700000001; usb.last_event.uptime_seconds = 777;
usb.last_event.sequence = 42; usb.last_event.persisted = true;
uint8_t usb_block[USB_STATUS_SIZE] = {};
encodeUsbStatus(usb_block, usb);
assert(read16(usb_block) == 2047 && usb_block[2] == (2 | (8 << 2)));
assert(read32(usb_block + 3) == 604800 && read32(usb_block + 7) == UINT32_MAX);
assert(read32(usb_block + 11) == 1209600);
usb.logger_active = false; encodeUsbStatus(usb_block, usb);
assert(read16(usb_block) == 1023); // host/reader connection does not imply a logger
usb.logger_active = true;
// Emit all wire versions for independent Python and browser decoders.
for (const char* magic : {"MGR1", "MGR2", "MGR3"}) {
const auto* version = reinterpret_cast<const uint8_t*>(magic);
const bool current = currentPage(version), has_usb = usbPage(version);
const size_t header = headerSize(version), per_page = entriesPerPage(version);
for (uint8_t page = 0; page < acl.pages(per_page); ++page) {
uint8_t raw[179] = {}, enc[32]; memcpy(raw, magic, 4);
memcpy(raw + 4, radio, 16);
write32(raw + 20, 42); write32(raw + 28, 0x01110105); write16(raw + 76, FIRMWARE);
raw[71] = 5; raw[78] = page; raw[79] = acl.pages(per_page); raw[80] = acl.count;
raw[81] = page * per_page;
raw[82] = acl.count - raw[81] < per_page ? acl.count - raw[81] : per_page;
if (has_usb) encodeUsbStatus(raw + HEADER, usb);
if (current) mesh::encodeUsbWatchdogEvent(raw + WATCHDOG_EVENT_OFFSET, usb.last_event);
const size_t private_len = raw[82] * ENTRY, size = header + private_len + TAG;
memcpy(raw + header, acl.entries[raw[81]], private_len);
deriveKey(root, "MeshCore-MGR1-SIV", radio, enc);
assert(seal(enc, raw, header, raw + header, private_len, raw + header + private_len));
assert(validPage(raw, size)); print(raw, size);
assert(size <= MAX_PAYLOAD && floodSize(size) <= 179);
assert(validPage(raw, floodSize(size), true));
if (floodSize(size) > size) {
raw[floodSize(size) - 1] ^= 1;
assert(!validPage(raw, floodSize(size), true)); raw[floodSize(size) - 1] ^= 1;
}
if (has_usb) {
raw[85] |= 0x40; assert(!validPage(raw, size)); raw[85] &= ~0x40;
raw[84] |= 0x08; assert(!validPage(raw, size)); raw[84] &= ~0x08;
}
if (current) {
const uint8_t original = raw[98];
for (uint8_t invalid : {uint8_t(original & 0xf0), uint8_t(original & 0x8f), uint8_t(0xdf)}) {
raw[98] = invalid; assert(!validPage(raw, size));
}
raw[98] = original;
write32(raw + 107, 0); assert(!validPage(raw, size)); write32(raw + 107, 42);
}
raw[81]++; assert(!validPage(raw, size)); raw[81]--;
assert(!validPage(raw, size - 1)); raw[80] = 37; assert(!validPage(raw, size));
}
}
// A current report with no recorded event is authenticated, not invented.
{
uint8_t raw[179] = {}, enc[32]; memcpy(raw, "MGR3", 4); memcpy(raw + 4, radio, 16);
write32(raw + 20, 43); raw[79] = 1;
deriveKey(root, "MeshCore-MGR1-SIV", radio, enc);
assert(seal(enc, raw, HEADER, raw + HEADER, PER_PAGE * ENTRY, raw + MAX_PAYLOAD - TAG));
assert(validPage(raw, sizeof(raw))); print(raw, sizeof(raw));
raw[81]++; assert(!validPage(raw, sizeof(raw))); raw[81]--;
assert(!validPage(raw, sizeof(raw) - 1)); raw[80] = 37; assert(!validPage(raw, sizeof(raw)));
assert(seal(enc, raw, CURRENT_HEADER, raw + CURRENT_HEADER, 0, raw + CURRENT_HEADER));
assert(validPage(raw, CURRENT_HEADER + TAG)); print(raw, CURRENT_HEADER + TAG);
}
// Every ACL count retains complete coverage and the unchanged flood ceiling.
for (unsigned total = 0; total <= MAX_KEYS; ++total) {
for (unsigned page = 0; page < (total ? (total + 3) / 4 : 1); ++page) {
uint8_t raw[179] = {}; memcpy(raw, "MGR3", 4);
raw[78] = page; raw[79] = total ? (total + 3) / 4 : 1; raw[80] = total;
raw[81] = page * 4; raw[82] = total - raw[81] < 4 ? total - raw[81] : 4;
const size_t size = pageSize(raw);
assert(validPage(raw, size) && floodSize(size) <= 179);
assert(validPage(raw, floodSize(size), true));
}
}
for (size_t n = 0; n < CURRENT_HEADER + TAG; ++n) {
std::vector<uint8_t> truncated(n);
for (size_t i = 0; i < n && i < 4; ++i) truncated[i] = "MGR3"[i];
assert(!validPage(truncated.data(), n));
}
for (size_t n = 0; n < HEADER + TAG; ++n) assert(!validPage(plain.data(), n));
}
+42 -3
View File
@@ -6,6 +6,11 @@
#include <string>
using namespace mesh;
using namespace mesh::management;
namespace mesh {
UsbLoggingStatus test_usb_status;
unsigned usb_status_calls = 0;
UsbLoggingStatus usbLoggingStatus() { ++usb_status_calls; return test_usb_status; }
}
struct Fixture {
MemoryFS fs;
Mesh mesh;
@@ -16,7 +21,7 @@ struct Fixture {
CommonCLI cli;
CommonCLICallbacks callbacks;
std::unique_ptr<ManagementReporter> reporter;
Fixture() { test_millis = 0; reboot(); }
Fixture() { test_millis = 0; test_usb_status = UsbLoggingStatus(); usb_status_calls = 0; reboot(); }
void reboot() { reporter.reset(); test_millis = 0; reporter.reset(new ManagementReporter(mesh, board, sensors, acl, prefs, callbacks, cli, &fs)); }
std::string cmd(const char* text, bool ok = true) {
char b[200], reply[160] = {}; strcpy(b, text);
@@ -100,23 +105,57 @@ int main() {
{
Fixture f;
for (unsigned i = 0; i < 36; ++i) { ClientInfo c; c.id.pub_key[0] = i; f.acl.clients.push_back(c); }
f.configure(false); f.advance(21 * DAY + 600); assert(f.mesh.packets.size() == 6);
for (unsigned i = 0; i < 6; ++i) {
test_usb_status.supported = true; test_usb_status.watchdog_enabled = true;
test_usb_status.host_connected = test_usb_status.reader_connected = true;
test_usb_status.stalled = true; test_usb_status.retry_seconds = 7200;
test_usb_status.inactive_seconds = 3600; test_usb_status.backoff_step = 1;
test_usb_status.last_event.reasons = UsbLoggingWatchdogEvent::TX_STALLED;
test_usb_status.last_event.action = UsbLoggingWatchdogEvent::REENUMERATE;
test_usb_status.last_event.sequence = 17; test_usb_status.last_event.uptime_seconds = 300;
test_usb_status.last_event.persisted = true;
f.configure(false); f.advance(21 * DAY + 600); assert(f.mesh.packets.size() == 9);
assert(usb_status_calls == 1);
for (unsigned i = 0; i < 9; ++i) {
const auto& p = f.mesh.packets[i]; assert(validPage(p.payload, p.payload_len));
assert(p.payload[78] == i && p.payload[80] == 36 && p.flood);
assert(!memcmp(p.payload, "MGR3", 4) && read16(p.payload + HEADER) == 61);
assert(!(read16(p.payload + HEADER) & 1024)); // an open reader is not an active logger
assert(read32(p.payload + HEADER + 3) == 7200);
assert(read32(p.payload + HEADER + 7) == 3600);
UsbLoggingWatchdogEvent event;
assert(decodeUsbWatchdogEvent(p.payload + WATCHDOG_EVENT_OFFSET, event));
assert(event.reasons == UsbLoggingWatchdogEvent::TX_STALLED && event.action == UsbLoggingWatchdogEvent::REENUMERATE);
assert(event.sequence == 17 && event.uptime_seconds == 300 && event.persisted && event.epoch == 0);
assert(floodSize(p.payload_len) <= 179);
}
}
{
Fixture f;
for (unsigned i = 0; i < 7; ++i) { ClientInfo c; c.id.pub_key[0] = i; f.acl.clients.push_back(c); }
f.configure(false); f.advance(21 * DAY); assert(f.mesh.packets.size() == 1);
assert(!memcmp(f.mesh.packets[0].payload, "MGR3", 4));
assert(read16(f.mesh.packets[0].payload + HEADER) == 0); // unsupported is explicit
test_usb_status.supported = true; test_usb_status.retry_seconds = 999;
test_usb_status.watchdog_auto = true; test_usb_status.auto_connected_seconds = 100;
test_usb_status.host_connected = test_usb_status.reader_connected = test_usb_status.logger_active = true;
test_usb_status.last_event.reasons = UsbLoggingWatchdogEvent::CLIENT_INACTIVE;
test_usb_status.last_event.action = UsbLoggingWatchdogEvent::SOFT_RECOVERY;
test_usb_status.last_event.sequence = 1; test_usb_status.last_event.epoch = 1700000001;
f.board.voltage = 2900; f.board.temperature = 60; f.advance(60);
assert(f.mesh.packets.size() == 2);
assert(read16(f.mesh.packets[1].payload + HEADER) == 0); // frozen report snapshot
assert(read32(f.mesh.packets[1].payload + HEADER + 11) == 0);
assert(read32(f.mesh.packets[1].payload + WATCHDOG_EVENT_OFFSET + 9) == 0);
assert(usb_status_calls == 1);
// The low reading was not in the first snapshot and must survive into the next one.
f.board.voltage = 3740; f.board.temperature = 24; f.advance(21 * DAY + 3600);
assert(f.mesh.packets.size() == 4);
assert(read16(f.mesh.packets.back().payload + 66) == 2900);
assert(f.mesh.packets.back().payload[69] == temperature(60));
assert(read16(f.mesh.packets.back().payload + HEADER) == 1561);
assert(read32(f.mesh.packets.back().payload + HEADER + 11) == 100);
assert(read32(f.mesh.packets.back().payload + WATCHDOG_EVENT_OFFSET + 9) == 1);
assert(usb_status_calls == 2);
}
{
Fixture f; f.configure(false); f.mesh.temp = true; f.advance(22 * DAY); assert(f.mesh.packets.empty());
@@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
#include "class/cdc/cdc.h"
#define CFG_TUD_CDC 1
#define CFG_TUD_CDC_TX_BUFSIZE 64
bool tud_mounted();
bool tud_connect();
bool tud_disconnect();
bool tud_cdc_n_connected(uint8_t);
uint32_t tud_cdc_n_available(uint8_t);
void tud_cdc_n_read_flush(uint8_t);
uint32_t tud_cdc_n_write_available(uint8_t);
uint32_t tud_cdc_n_write(uint8_t, const void*, uint32_t);
uint32_t tud_cdc_n_write_flush(uint8_t);
uint32_t tud_cdc_n_write_clear(uint8_t);
void tud_cdc_get_line_coding(cdc_line_coding_t*);
void TinyUSB_Port_EnterDFU();
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "../../../mocks/Arduino.h"
#pragma GCC diagnostic pop
#include <cassert>
uint32_t tud_cdc_n_write_available(uint8_t instance);
class MockSerial : public Stream {
public:
int available() override { return 0; }
int read() override { return -1; }
int peek() override { return -1; }
int availableForWrite() override { return tud_cdc_n_write_available(0); }
size_t write(const uint8_t*, size_t) override {
assert(false && "native nRF52 console must not use retrying Serial.write");
return 0;
}
void flush() override { assert(false && "console must not forward Serial.flush"); }
};
extern MockSerial Serial;
+3
View File
@@ -0,0 +1,3 @@
#pragma once
#include <stdint.h>
struct cdc_line_coding_t { uint32_t bit_rate; };
+131
View File
@@ -0,0 +1,131 @@
#include <Arduino.h>
#include <Adafruit_TinyUSB.h>
#include <helpers/UsbLogging.h>
#include <helpers/UsbLoggingClientActivity.h>
#include <cassert>
#include <cstdio>
#include <functional>
#include <string>
MockSerial Serial;
static bool mounted = true, dtr = true, dfu = false;
static uint32_t baud = 115200;
static std::string fifo, host;
static std::function<void()> during_write;
extern "C" void tud_cdc_line_state_cb(uint8_t, bool, bool);
extern "C" void tud_cdc_line_coding_cb(uint8_t, const cdc_line_coding_t*);
extern "C" void tud_cdc_tx_complete_cb(uint8_t);
extern "C" void tud_umount_cb();
bool tud_mounted() { return mounted; }
bool tud_connect() { mounted = true; return true; }
bool tud_disconnect() { mounted = false; dtr = false; return true; }
bool tud_cdc_n_connected(uint8_t n) { assert(n == 0); return mounted && dtr; }
uint32_t tud_cdc_n_available(uint8_t n) { assert(n == 0); return 0; }
void tud_cdc_n_read_flush(uint8_t n) { assert(n == 0); }
uint32_t tud_cdc_n_write_available(uint8_t n) { assert(n == 0); return 64 - fifo.size(); }
uint32_t tud_cdc_n_write(uint8_t n, const void* data, uint32_t size) {
assert(n == 0 && size <= 64 - fifo.size());
if (during_write) {
auto callback = std::move(during_write);
during_write = nullptr;
callback();
}
fifo.append(static_cast<const char*>(data), size);
return size;
}
uint32_t tud_cdc_n_write_flush(uint8_t n) { assert(n == 0); return 0; }
uint32_t tud_cdc_n_write_clear(uint8_t n) { assert(n == 0); fifo.clear(); return 0; }
void tud_cdc_get_line_coding(cdc_line_coding_t* coding) { coding->bit_rate = baud; }
void TinyUSB_Port_EnterDFU() { dfu = true; }
static void service() {
mesh::serviceUsbLoggingPort();
mesh::serviceUsbTerminalPort();
(void)mesh::takeUsbTerminalSessionReset();
g_mock_millis += 8;
assert(mesh::tryCompleteUsbTerminalSessionReset());
}
static void drain() {
for (unsigned i = 0; i < 200; ++i) {
if (!fifo.empty()) {
host += fifo;
fifo.clear();
tud_cdc_tx_complete_cb(0);
}
service(); // Exercise the actual every-loop completion path too.
if (fifo.empty() && !mesh::hasPendingUsbTerminalOutput()) return;
}
assert(false && "finite queue did not drain");
}
int main() {
static_assert(MESH_USB_CONSOLE_COOPERATIVE, "ordinary native nRF52 must cooperate");
mesh::setUsbLoggingEnabled(true);
tud_cdc_line_state_cb(0, true, false);
service();
auto& console = mesh::usbConsolePort();
const std::string reply = "-> " + std::string(200, 'R') + "\r\n";
assert(console.write(reinterpret_cast<const uint8_t*>(reply.data()), reply.size()) == reply.size());
assert(mesh::hasPendingUsbTerminalOutput());
for (unsigned i = 0; i < 5; ++i) service();
assert(fifo == reply.substr(0, 64)); // Clean epochs must not clear the FIFO.
assert(mesh::usbLoggingPort().write(reinterpret_cast<const uint8_t*>("LOG\r\n"), 5) == 5);
mesh::probeUsbLoggingTransport();
drain();
assert(host == reply + "LOG\r\n[USB watchdog] heartbeat\r\n");
host.clear();
const std::string raw = "RAW: " + std::string(512, 'A') + "\r\n";
assert(mesh::usbLoggingPort().write(reinterpret_cast<const uint8_t*>(raw.data()), raw.size()) == raw.size());
drain();
assert(host == raw); // Shared-queue capacity must not drop >256-byte RAW.
host.clear();
uint8_t stale_printf[256];
std::fill(stale_printf, stale_printf + 255, 'Z');
stale_printf[255] = 0;
assert(mesh::usbLoggingPort().write(stale_printf, 512) == 0); // No OOB read.
during_write = [] {
dtr = false;
tud_cdc_line_state_cb(0, false, false);
dtr = true;
tud_cdc_line_state_cb(0, true, false);
(void)mesh::takeUsbTerminalSessionReset();
g_mock_millis += 8;
// The old producer still owns the buffer and native writer gate.
assert(!mesh::tryCompleteUsbTerminalSessionReset());
};
assert(console.write(reinterpret_cast<const uint8_t*>(reply.data()), reply.size()) == reply.size());
service();
drain();
assert(host.empty()); // Failed busy purge must be retried before reopening.
// A DTR-high host that stops reading never forces a retry/flush wait.
fifo.assign(64, 'X');
const std::string record(100, 'D');
for (unsigned i = 0; i < 100; ++i)
mesh::usbLoggingPort().write(reinterpret_cast<const uint8_t*>(record.data()), record.size());
assert(console.availableForWrite() >= 3072 && mesh::canAcceptUsbConsoleCommand());
const std::string huge(4096, 'F');
assert(console.write(reinterpret_cast<const uint8_t*>(huge.data()), huge.size()) == 0);
for (unsigned i = 0; i < 10000; ++i) mesh::serviceUsbTerminalPort();
assert(fifo.size() == 64); // No progress invented by queue admission.
assert(mesh::observeUsbLoggingTransport().pending);
mesh::noteUsbLoggingStatsCommand("stats-core");
assert(mesh::isUsbLoggingClientActive());
dtr = false;
tud_cdc_line_state_cb(0, false, false);
dtr = true;
tud_cdc_line_state_cb(0, true, false);
service();
assert(!mesh::isUsbLoggingClientActive() && !mesh::hasPendingUsbTerminalOutput());
drain();
assert(host.empty()); // No old response/log leaks across rapid reopen.
mesh::noteUsbLoggingStatsCommand("stats-radio");
mounted = false;
tud_umount_cb();
assert(!mesh::isUsbLoggingClientActive());
mounted = true; dtr = true;
tud_cdc_line_state_cb(0, true, false);
service();
baud = 1200; dtr = false;
tud_cdc_line_state_cb(0, false, false);
assert(dfu); // Existing bootloader entry is preserved.
puts("native nRF52 console transport passed");
}
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
inline uint32_t mock_watchdog_millis = 0;
inline uint32_t millis() { return mock_watchdog_millis; }
class Stream { public: virtual ~Stream() = default; };
+98
View File
@@ -0,0 +1,98 @@
#pragma once
#include <algorithm>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include <functional>
#include <map>
#include <string>
#include <vector>
namespace fs { class FS; }
class File {
public:
File() = default;
File(fs::FS* fs, std::string path, bool writable)
: _fs(fs), _path(std::move(path)), _writable(writable) {}
explicit operator bool() const { return _fs != nullptr; }
size_t size() const;
size_t read(uint8_t* buffer, size_t count);
size_t write(const uint8_t* buffer, size_t count);
void flush() {}
void close() { _fs = nullptr; }
private:
fs::FS* _fs = nullptr;
std::string _path;
size_t _offset = 0;
bool _writable = false;
};
namespace fs {
class FS {
public:
std::map<std::string, std::vector<uint8_t>> files;
std::string fail_stat, fail_open, short_read;
std::string fail_rename_from, fail_rename_to;
bool fail_write = false, fail_remove = false;
bool corrupt_final_image = false, fail_final_readback = false;
bool final_renamed = false;
unsigned writes = 0, write_opens = 0, renames = 0, removes = 0;
std::function<void()> after_final_rename;
bool exists(const char* path) const { return files.count(path) != 0; }
bool mkdir(const char*) { return true; }
File open(const char* path, const char* mode, bool = false) {
const bool writable = mode[0] == 'w';
if (fail_open == path || (!writable && fail_final_readback
&& final_renamed && std::string(path) == "/usb_wdg"))
return {};
if (writable) {
++write_opens;
files[path].clear();
} else if (!exists(path)) return {};
return File(this, path, writable);
}
bool remove(const char* path) {
++removes;
if (fail_remove) return false;
return files.erase(path) != 0;
}
bool rename(const char* from, const char* to) {
++renames;
if ((fail_rename_from == from && fail_rename_to == to)
|| !exists(from) || exists(to)) return false;
files[to] = std::move(files[from]);
files.erase(from);
if (std::string(from) == "/usb_wdg.tmp" && std::string(to) == "/usb_wdg") {
final_renamed = true;
if (corrupt_final_image) files[to][0] ^= 1;
if (after_final_rename) after_final_rename();
}
return true;
}
};
inline FS* stat_filesystem = nullptr;
} // namespace fs
inline size_t File::size() const {
return _fs ? _fs->files.at(_path).size() : 0;
}
inline size_t File::read(uint8_t* buffer, size_t count) {
if (!_fs || _writable) return 0;
const auto& bytes = _fs->files.at(_path);
size_t available = std::min(count, bytes.size() - _offset);
if (_fs->short_read == _path && available) --available;
std::memcpy(buffer, bytes.data() + _offset, available);
_offset += available;
return available;
}
inline size_t File::write(const uint8_t* buffer, size_t count) {
if (!_fs || !_writable) return 0;
++_fs->writes;
if (_fs->fail_write) return 0;
auto& bytes = _fs->files.at(_path);
bytes.insert(bytes.end(), buffer, buffer + count);
return count;
}
+2
View File
@@ -0,0 +1,2 @@
#pragma once
namespace mesh { struct LocalIdentity {}; }
@@ -0,0 +1,23 @@
#pragma once
#include <stddef.h>
namespace mesh { namespace ota {
struct OtaManager {
enum State { IDLE, FAILED, FETCHING, VERIFYING, COMPLETE };
State state = IDLE;
size_t serve_jobs = 0, manifest_jobs = 0;
State fetchState() const { return state; }
size_t pendingServeJobs() const { return serve_jobs; }
size_t pendingManifestJobs() const { return manifest_jobs; }
};
struct OtaContext {
OtaManager manager;
bool apply_pending = false, bootloader_apply_pending = false;
bool folder_active = false, folder_dest = false, capture_waiting = false;
bool serving = false, serve_expected = false;
bool folderCaptureWaiting() const { return capture_waiting; }
};
inline OtaContext* test_ota_context = nullptr;
inline const OtaContext* ota_context_if_active() { return test_ota_context; }
} } // namespace mesh::ota
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include <FS.h>
struct stat { size_t st_size = 0; };
inline int stat(const char* absolute, struct stat* info) {
const std::string prefix = "/spiffs";
const std::string path(absolute);
if (!fs::stat_filesystem || path.compare(0, prefix.size(), prefix)) {
errno = EIO;
return -1;
}
const auto relative = path.substr(prefix.size());
if (fs::stat_filesystem->fail_stat == relative) { errno = EIO; return -1; }
const auto found = fs::stat_filesystem->files.find(relative);
if (found == fs::stat_filesystem->files.end()) { errno = ENOENT; return -1; }
info->st_size = found->second.size();
return 0;
}
File diff suppressed because it is too large. Load diff
+6 -2
View File
@@ -26,7 +26,11 @@ HARNESS = r'''
struct NodePrefs { @FIELDS@ };
void markDirectRetryPrefsValid(NodePrefs*) {}
bool isValidLoRaBandwidth(float bw) { return bw==62.5f || bw==125; }
namespace mesh { struct RadioProfiles { static constexpr unsigned MaxPreamble=65535; }; }
namespace mesh {
struct RadioProfiles { static constexpr unsigned MaxPreamble=65535; };
void setUsbLoggingEnabled(bool) {}
void setUsbDebugEnabled(bool) {}
}
struct Profiles {
bool accepted=true;
uint16_t saved=48;
@@ -78,7 +82,7 @@ int main() {
CommonCLI cli;
assert(cli.saveCommonPrefs());
auto original=cli.fs.files["/com_prefs"];
const size_t preamble_offset=original.size()-3;
const size_t preamble_offset=original.size()-4;
assert(preamble_offset>=864);
assert(original[preamble_offset]==48 && original[preamble_offset+1]==0);
assert(original[preamble_offset+2]==1);
+358
View File
@@ -0,0 +1,358 @@
"""Exercise actual CommonCLI USB debug commands, images, and import commits."""
from pathlib import Path
import os
import re
import subprocess
import tempfile
import unittest
from test_replay_reset_integration import extract_braced
ROOT = Path(__file__).resolve().parents[1]
HARNESS = r'''
#include <cassert>
#include <math.h>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <helpers/IdentityStore.h>
class TestFS : public MemoryFS {
public:
using MemoryFS::open;
File open(const char* path, uint8_t mode) {
return MemoryFS::open(path, mode == FILE_O_WRITE ? "w" : "r");
}
};
#undef FILESYSTEM
#define FILESYSTEM TestFS
#include "ContactFileTransaction.h"
#define ATOMIC_FILE_WRITER_IMPLEMENTATION
#include <helpers/AtomicFileWriter.h>
#include <helpers/PrefsSaveRouting.h>
#include <helpers/CommonPrefsRecovery.h>
#include <helpers/MQTTPrefsAtomicStore.h>
#include <helpers/CLICommandUtils.h>
#include <helpers/RepeaterRadioTiming.h>
#include <helpers/bridges/ESPNowBridgeFormat.h>
#include <helpers/radiolib/LR2021SideDetectorConfig.h>
#define MIN_LORA_TX_POWER -9
#define MAX_LORA_TX_POWER 22
#include <helpers/radiolib/RadioPowerLimits.h>
#define MESH_DEBUG_PRINTLN(...) ((void)0)
#define MESH_USB_LOGGING_AVAILABLE 1
#define DEFAULT_POWERSAVING_ENABLED 1
#define DEFAULT_CAD_ENABLED 0
#define BRIDGE_MAX_BAUD 115200
#define RX_POWERSAVING_DEFAULT_RX_US 65625UL
#define RX_POWERSAVING_DEFAULT_SLEEP_US 60000UL
#define FLOOD_RETRY_PATH_GATE_DISABLED 0xFF
#define MIN_LOCAL_ADVERT_INTERVAL 60
#define constrain(value, low, high) ((value) < (low) ? (low) : ((value) > (high) ? (high) : (value)))
@CONSTANTS@
namespace mesh {
struct RadioProfiles { static constexpr unsigned MaxPreamble = 65535; };
static bool usb_logging = true, usb_debug = false;
static bool prohibit_stale_debug = false;
static unsigned usb_updates = 0, debug_updates = 0;
void setUsbLoggingEnabled(bool enabled) {
if (enabled && prohibit_stale_debug) assert(!usb_debug);
usb_logging = enabled; ++usb_updates;
}
void setUsbDebugEnabled(bool enabled) { usb_debug = enabled; ++debug_updates; }
bool isUsbLoggingEnabled() { return usb_logging; }
bool isUsbDebugEnabled() { return usb_debug; }
bool isUsbDebugLoggingEnabled() { return usb_logging && usb_debug; }
}
struct NodePrefs {
@FIELDS@
bool dirty = false;
void clearDirty() { dirty = false; }
void markUnsaved() { dirty = true; }
};
@HELPERS@
void ensureRxPowerSavingDefaults(uint32_t*, uint32_t*) {}
void recalcRxPowerSavingFromLevel(uint8_t, uint8_t, float, uint8_t,
uint32_t*, uint32_t*) {}
@LEGACY@
@SERIALIZER@
@STORE@;
struct CommonCLI;
struct Callbacks {
CommonCLI* cli;
void savePrefs(PrefsSaveRouting::Scope scope);
void updateAdvertTimer() {}
};
struct CommonCLI {
FILESYSTEM fs;
NodePrefs prefs;
NodePrefs* _prefs = &prefs;
LegacyObserverTail _legacy_tail{};
Callbacks callbacks{this};
Callbacks* _callbacks = &callbacks;
bool _com_prefs_needs_upgrade = false;
bool _common_save_result_known = false, _common_save_succeeded = false;
bool _observer_save_result_known = false, _observer_save_succeeded = false;
uint32_t _prefs_save_failures = 0;
CommonCLI() {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
fs.rename_replaces = true;
#endif
prefs.freq = 910; prefs.bw = 62.5f; prefs.sf = 7; prefs.cr = 5;
prefs.primary_radio_preamble = 48;
}
void savePrefs(FILESYSTEM*, PrefsSaveRouting::Scope);
void savePrefs(PrefsSaveRouting::Scope scope = PrefsSaveRouting::Scope::Common);
bool trySavePrefs(PrefsSaveRouting::Scope scope = PrefsSaveRouting::Scope::Common);
void loadPrefsInt(FILESYSTEM*, const char*);
bool recoverCommonPrefsFiles(FILESYSTEM*);
bool saveCommonPrefsImageAtomically(FILESYSTEM*);
bool saveMQTTPrefs(FILESYSTEM*) { return true; }
void set(const char* config, char* reply) {
@SET_DEBUG@
@SET_MASTER@
strcpy(reply, "unknown");
}
void get(const char* config, char* reply) {
@GET_DEBUG@
@GET_MASTER@
strcpy(reply, "unknown");
}
};
void Callbacks::savePrefs(PrefsSaveRouting::Scope scope) {
cli->savePrefs(&cli->fs, scope);
}
@METHODS@
// Execute the production dynamic schema, replacing only its generic codec.
// Registered uint8_t references emulate imported configuration values.
class ConfigSerializer {
protected:
virtual void structure() = 0;
template<typename T> void def(const char*, T&) {}
void def(const char* key, uint8_t& value) { bytes[key] = &value; }
void def(const char*, char*, size_t) {}
public:
std::map<std::string, uint8_t*> bytes;
bool import(const char* key, uint8_t value) {
structure();
if (!bytes.count(key)) return false;
*bytes[key] = value;
return true;
}
};
@DYNAMIC@;
struct Capture {
std::vector<uint8_t> bytes;
size_t write(const uint8_t* data, size_t n) {
bytes.insert(bytes.end(), data, data + n);
return n;
}
};
int main() {
CommonCLI cli;
char reply[160] = {};
assert(cli.prefs.usb_debug_enabled == 0);
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
assert(cli._common_save_succeeded);
const auto baseline = cli.fs.files["/com_prefs"];
assert(baseline.size() == 870);
const size_t debug_offset = baseline.size() - 1;
const size_t logging_offset = baseline.size() - 7;
const size_t preamble_offset = baseline.size() - 4;
const size_t espnow_offset = baseline.size() - 2;
assert(baseline[debug_offset] == 0 && baseline[logging_offset] == 1);
assert(baseline[preamble_offset] == 48 && baseline[espnow_offset] == 1);
Capture capture;
assert(writeCommonPrefsImage(capture, &cli.prefs));
assert(capture.bytes == baseline); // Checked and ordinary writers agree.
cli.set("usb.debug on", reply);
assert(strcmp(reply, "OK - USB debug on (saved)") == 0);
assert(cli.prefs.usb_debug_enabled == 1 && mesh::isUsbDebugLoggingEnabled());
auto enabled = cli.fs.files["/com_prefs"];
assert(enabled.size() == baseline.size() && enabled[debug_offset] == 1);
assert(std::equal(baseline.begin(), baseline.begin() + debug_offset, enabled.begin()));
cli.set("usb.logging off", reply);
assert(!mesh::isUsbLoggingEnabled() && mesh::isUsbDebugEnabled());
assert(!mesh::isUsbDebugLoggingEnabled());
cli.get("usb.debug", reply);
assert(strcmp(reply, "> on") == 0); // Saved intent is visible with master OFF.
cli.get("usb.logging", reply);
assert(strcmp(reply, "> off") == 0);
// Malformed tokens and suffixes never mutate either gate or the image.
const auto unchanged = cli.fs.files["/com_prefs"];
for (const char* command : {"usb.debug", "usb.debug true", "usb.debug 1",
"usb.debug ON", "usb.debug off junk", "usb.debug off ",
"usb.debug on reboot", "usb.debug off reboot"}) {
const unsigned updates = mesh::debug_updates;
cli.set(command, reply);
assert(strcmp(reply, "Error: usage set usb.debug on|off") == 0);
assert(cli.prefs.usb_debug_enabled == 1 && mesh::isUsbDebugEnabled());
assert(mesh::debug_updates == updates);
assert(cli.fs.files["/com_prefs"] == unchanged);
}
cli.set("usb.debugger off", reply);
assert(strcmp(reply, "unknown") == 0);
cli.set("usb.debug\t \toff", reply);
assert(strcmp(reply, "OK - USB debug off (saved)") == 0);
assert(cli.prefs.usb_debug_enabled == 0 && !mesh::isUsbDebugEnabled());
const auto debug_off = cli.fs.files["/com_prefs"];
// Failed first/write/verification/publication stages cannot enable debug.
for (int fault : {0, 1, 2, 3}) {
if (fault == 0) cli.fs.fail_write = true;
if (fault == 1) cli.fs.fail_write_after = debug_offset;
if (fault == 2) cli.fs.fail_read_open = true;
if (fault == 3) cli.fs.fail_rename = 1;
const unsigned updates = mesh::debug_updates;
cli.set("usb.debug on", reply);
assert(strcmp(reply, "Error: USB debug not saved; unchanged") == 0);
assert(cli.prefs.usb_debug_enabled == 0 && !mesh::isUsbDebugEnabled());
assert(mesh::debug_updates == updates && !mesh::isUsbLoggingEnabled());
assert(cli.fs.files["/com_prefs"] == debug_off);
cli.fs.fail_write = false; cli.fs.fail_write_after = -1;
cli.fs.fail_read_open = false; cli.fs.fail_rename = 0;
}
// Imported schemas apply gates only after a successful common commit.
BridgePrefs dynamic(&cli.prefs);
assert(dynamic.import("usb_dbg", 1));
assert(!mesh::isUsbDebugEnabled());
const unsigned observer_updates = mesh::debug_updates;
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Observer);
assert(!mesh::isUsbDebugEnabled() && mesh::debug_updates == observer_updates);
assert(cli.fs.files["/com_prefs"] == debug_off);
cli.fs.fail_write = true;
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
assert(!cli._common_save_succeeded && !mesh::isUsbDebugEnabled());
cli.fs.fail_write = false;
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
assert(cli._common_save_succeeded && mesh::isUsbDebugEnabled());
assert(!mesh::isUsbDebugLoggingEnabled());
assert(dynamic.import("usb_log", 1));
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
assert(mesh::isUsbDebugLoggingEnabled());
// A debug-OFF import must take effect before the master is enabled again.
mesh::setUsbLoggingEnabled(false);
assert(dynamic.import("usb_dbg", 0));
mesh::prohibit_stale_debug = true;
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
mesh::prohibit_stale_debug = false;
assert(mesh::isUsbLoggingEnabled() && !mesh::isUsbDebugEnabled());
for (uint8_t bad : {2, 255}) {
assert(dynamic.import("usb_dbg", bad));
cli.savePrefs(&cli.fs, PrefsSaveRouting::Scope::Common);
assert(cli._common_save_succeeded && !mesh::isUsbDebugEnabled());
assert(cli.prefs.usb_debug_enabled == 0 && cli.fs.files["/com_prefs"][debug_offset] == 0);
}
// Every older/torn length inherits debug OFF, even after loading debug ON.
auto image = baseline;
image[logging_offset] = 0; image[debug_offset] = 1;
for (size_t size = 0; size < image.size(); ++size) {
CommonCLI reader;
reader.prefs.usb_debug_enabled = 1;
reader.fs.files["/com_prefs"] = {image.begin(), image.begin() + size};
reader.loadPrefsInt(&reader.fs, "/com_prefs");
assert(reader.prefs.usb_debug_enabled == 0);
if (size > logging_offset) assert(reader.prefs.usb_logging_enabled == 0);
}
for (uint8_t saved : {0, 1, 2, 255}) {
image[debug_offset] = saved;
cli.fs.files["/com_prefs"] = image;
cli.prefs.usb_debug_enabled = 1;
cli.loadPrefsInt(&cli.fs, "/com_prefs");
assert(cli.prefs.usb_debug_enabled == (saved == 1 ? 1 : 0));
assert(cli.prefs.usb_logging_enabled == 0);
assert(cli.prefs.primary_radio_preamble == 48 && cli.prefs.espnow_bridge_enabled == 1);
}
cli.prefs.usb_debug_enabled = 1;
cli.loadPrefsInt(&cli.fs, "/missing");
assert(cli.prefs.usb_debug_enabled == 0);
}
'''
class CommonUsbDebugTest(unittest.TestCase):
def test_actual_common_commands_and_persistence(self):
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
for key, value in {
'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': extract_braced(header, 'class BridgePrefs :'),
'SET_DEBUG': extract_braced(source, 'if (strncmp(config, "usb.debug", 9)'),
'SET_MASTER': extract_braced(source, 'if (strncmp(config, "usb.logging", 11)'),
'GET_DEBUG': extract_braced(source, 'if (strcmp(config, "usb.debug") == 0)'),
'GET_MASTER': extract_braced(source, 'if (strcmp(config, "usb.logging") == 0)'),
}.items():
code = code.replace('@' + key + '@', value)
sanitizer_flags = [] if os.name == 'nt' else [
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
'-fno-pie', '-no-pie']
with tempfile.TemporaryDirectory(prefix='common-usb-debug-') 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')
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')))
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)
def test_outer_load_applies_both_gates(self):
source = (ROOT / 'src/helpers/CommonCLI.cpp').read_text(encoding='utf-8')
load = extract_braced(source, 'void CommonCLI::loadPrefs(FILESYSTEM*')
self.assertIn('_prefs->usb_debug_enabled = 0;', load)
self.assertIn('mesh::setUsbLoggingEnabled(_prefs->usb_logging_enabled != 0);', load)
self.assertIn('mesh::setUsbDebugEnabled(_prefs->usb_debug_enabled != 0);', load)
self.assertLess(load.index('mesh::setUsbDebugEnabled('),
load.index('mesh::setUsbLoggingEnabled('))
if __name__ == '__main__':
unittest.main()
+9 -7
View File
@@ -32,13 +32,13 @@ int main(){
assert(mesh.savePrefs());
const auto on_disk=mesh.store.fs.files["/new_prefs"];
#if defined(TBEAM_1W)
assert(on_disk.size()==239);
assert(on_disk.size()==240);
#elif defined(RP2040_PLATFORM) && defined(ENABLE_WIFI_INTERFACE)
assert(on_disk.size()==329);
assert(on_disk.size()==330);
#else
assert(on_disk.size()==232);
assert(on_disk.size()==233);
#endif
assert(on_disk.back()==1);
assert(on_disk[on_disk.size()-2]==1&&on_disk.back()==0);
// Both choices survive a fresh instance reading the actual on-device image.
for(bool enabled : {false,true,false}){
assert(mesh.setBluetoothEnabledPreference(enabled));
@@ -49,7 +49,8 @@ int main(){
assert(prefs.ble_pin==876543&&prefs.freq==910.525f);
assert(prefs.bluetooth_stealth_mode==2&&prefs.one_key_dm_enabled==1);
assert(prefs.wifi_enabled==0&&prefs.usb_logging_enabled==1);
assert(reboot.fs.files["/new_prefs"].back()==enabled);
const auto& saved=reboot.fs.files["/new_prefs"];
assert(saved[saved.size()-2]==enabled&&saved.back()==0);
}
const auto off_disk=mesh.store.fs.files["/new_prefs"];
// The public setter must roll live state back when the atomic save fails.
@@ -69,7 +70,7 @@ int main(){
assert(mesh.isBluetoothEnabledPreference());
mesh.store.fs.fail_write=false;
// Legacy images cannot accidentally inherit an off value from the caller.
for(unsigned size : {84u,156u,215u,226u,unsigned(on_disk.size()-1)}){
for(unsigned size : {84u,156u,215u,226u,unsigned(on_disk.size()-2)}){
DataStore legacy;legacy.fs.files["/new_prefs"]=on_disk;
legacy.fs.files["/new_prefs"].resize(size);
CompanionNodePrefs prefs;prefs.bluetooth_enabled=0;
@@ -80,7 +81,8 @@ int main(){
// Invalid new encoding rejects the transaction rather than partially loading.
for(uint8_t corrupt : {2,255}){
DataStore damaged;damaged.fs.files["/new_prefs"]=off_disk;
damaged.fs.files["/new_prefs"].back()=corrupt;
auto& damaged_disk=damaged.fs.files["/new_prefs"];
damaged_disk[damaged_disk.size()-2]=corrupt;
CompanionNodePrefs prefs;prefs.bluetooth_enabled=1;
strcpy(prefs.node_name,"unchanged");double lat=1,lon=2;
assert(!damaged.loadPrefsInt("/new_prefs",prefs,lat,lon));
+203
View File
@@ -0,0 +1,203 @@
"""Execute the public-key CLI with production formatting and real identity data."""
from itertools import product
from pathlib import Path
import os
import subprocess
import tempfile
import unittest
from test_radio_receive_contract import method
ROOT = Path(__file__).resolve().parents[1]
HARNESS = r'''
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <Identity.h>
namespace mesh {
@HEX_CHARS@
@TO_HEX@
@IDENTITY_METHODS@
}
class IdentityInput : public Stream {
public:
std::vector<uint8_t> bytes;
size_t offset=0;
int available() override{return int(bytes.size()-offset);}
int read() override{return offset<bytes.size()?bytes[offset++]:-1;}
};
struct MyMesh {
mesh::LocalIdentity self_id;
unsigned fallback_calls=0;
bool handleLocalControlCommand(const char*,char*,size_t);
};
@COMMAND@
std::array<uint8_t,PRV_KEY_SIZE+PUB_KEY_SIZE> snapshot(const MyMesh& node){
std::array<uint8_t,PRV_KEY_SIZE+PUB_KEY_SIZE> bytes;
assert(node.self_id.writeTo(bytes.data(),bytes.size())==bytes.size());
return bytes;
}
int main(){
static_assert(PUB_KEY_SIZE==32&&PRV_KEY_SIZE==64,"Exercise the entire identity");
const uint8_t public_key[PUB_KEY_SIZE]={
0x00,0xff,0x01,0x80,0x0a,0xaf,0x10,0x7f,
0x55,0xaa,0xfe,0xef,0x03,0x20,0x9c,0xcd,
0x04,0x30,0xb2,0xd3,0x05,0x40,0x56,0xe4,
0x06,0x50,0x78,0xf5,0x07,0x60,0x9a,0xc6
};
const std::string expected=
"> 00FF01800AAF107F55AAFEEF03209CCD0430B2D3054056E4065078F507609AC6";
assert(expected.size()==2+PUB_KEY_SIZE*2);
IdentityInput input;
input.bytes.insert(input.bytes.end(),public_key,public_key+sizeof(public_key));
for(unsigned i=0;i<PRV_KEY_SIZE;++i)input.bytes.push_back(uint8_t(0xe7^i));
MyMesh node;assert(node.self_id.readFrom(input));
const auto original=snapshot(node);
assert(!memcmp(node.self_id.pub_key,public_key,sizeof(public_key)));
for(unsigned i=0;i<PRV_KEY_SIZE;++i)assert(original[i]==uint8_t(0xe7^i));
// Every capacity around the 66-byte reply boundary is exercised with exact
// prefix expectations and both leading/trailing canaries under sanitizers.
std::vector<size_t> capacities;
for(size_t size=1;size<=80;++size)capacities.push_back(size);
for(size_t size : {size_t(128),size_t(160),size_t(176),size_t(256)})capacities.push_back(size);
for(size_t size : capacities){
std::vector<uint8_t> guarded(size+16,0xa7);
char* reply=reinterpret_cast<char*>(guarded.data()+8);
const unsigned fallback=node.fallback_calls;
assert(node.handleLocalControlCommand("get public.key",reply,size));
const size_t written=size-1<expected.size()?size-1:expected.size();
assert(strlen(reply)==written&&!memcmp(reply,expected.data(),written));
assert(reply[written]==0&&node.fallback_calls==fallback);
for(size_t i=0;i<8;++i)assert(guarded[i]==0xa7);
for(size_t i=8+size;i<guarded.size();++i)assert(guarded[i]==0xa7);
for(size_t i=written+1;i<size;++i)assert(uint8_t(reply[i])==0xa7);
assert(snapshot(node)==original);
}
// Invalid caller pointers and zero capacity never read commands or change
// output, identity, or any later command-handler state.
char guarded[160];memset(guarded,'X',sizeof(guarded));
const unsigned before_invalid=node.fallback_calls;
assert(!node.handleLocalControlCommand(nullptr,guarded,sizeof(guarded)));
assert(!node.handleLocalControlCommand("get public.key",nullptr,sizeof(guarded)));
assert(!node.handleLocalControlCommand("get public.key",guarded,0));
assert(!node.handleLocalControlCommand(nullptr,nullptr,0));
for(char byte : guarded)assert(byte=='X');
assert(node.fallback_calls==before_invalid&&snapshot(node)==original);
// A const input is accepted without modification, as is the established
// leading-space normalization. Other whitespace is not an implicit alias.
char command[]=" get public.key";
const std::string command_before=command;
assert(node.handleLocalControlCommand(command,guarded,sizeof(guarded)));
assert(guarded==expected&&command_before==command&&snapshot(node)==original);
auto reject=[&](const std::string& command){
memset(guarded,'X',sizeof(guarded));
const unsigned before=node.fallback_calls;
assert(!node.handleLocalControlCommand(command.c_str(),guarded,sizeof(guarded)));
assert(node.fallback_calls==before+1);
for(char byte : guarded)assert(byte=='X');
assert(snapshot(node)==original);
};
for(const char* command : {"", " ", "get public", "get public.keyx",
"get public.key extra", "get public.key ", "get public.key\t",
"get public.key\r", "get public.key\n", "get\tpublic.key",
"\tget public.key", "GET public.key", "get Public.key", "get public.KEY",
"get pub.key", "get publickey", "get public_key", "get private.key",
"get prv.key", "set public.key", "set public.key 00", "set prv.key 00",
"erase public.key", "public.key"})reject(command);
const std::string exact="get public.key";
for(size_t length=0;length<exact.size();++length)reject(exact.substr(0,length));
for(size_t offset=0;offset<exact.size();++offset){
std::string mutated=exact;mutated[offset]='#';reject(mutated);
}
// Read the live identity, not a hardcoded value or a cached response. The
// public query must never expose the different, nonzero private-key bytes.
for(unsigned i=0;i<PUB_KEY_SIZE;++i)node.self_id.pub_key[i]=uint8_t(0xff-i);
const auto changed=snapshot(node);
assert(!memcmp(changed.data(),original.data(),PRV_KEY_SIZE));
assert(node.handleLocalControlCommand("get public.key",guarded,sizeof(guarded)));
assert(!strcmp(guarded,
"> FFFEFDFCFBFAF9F8F7F6F5F4F3F2F1F0EFEEEDECEBEAE9E8E7E6E5E4E3E2E1E0"));
assert(snapshot(node)==changed);
}
'''
class CompanionIdentityCliTests(unittest.TestCase):
def test_production_query_bounds_exact_matching_identity_and_feature_gates(self):
source = (ROOT / 'examples/companion_radio/MyMesh.cpp').read_text()
local = method(source, 'bool MyMesh::handleLocalControlCommand(')
# Keep the production method signature, null/capacity guard, leading
# normalization and identity branch intact. The unrelated later
# handlers are replaced only by an observable fallthrough sentinel.
dispatch = local.index(' if (handleTxRoutingCommand(')
command = local[:dispatch] + ' ++fallback_calls;\n return false;\n}'
utils = (ROOT / 'src/Utils.cpp').read_text()
hex_chars = next(line for line in utils.splitlines()
if line.startswith('static const char hex_chars[]'))
identity = (ROOT / 'src/Identity.cpp').read_text()
identity_methods = '\n'.join(method(identity, signature) for signature in (
'Identity::Identity()', 'LocalIdentity::LocalIdentity()',
'bool LocalIdentity::readFrom(Stream& s)',
'size_t LocalIdentity::writeTo(uint8_t* dest, size_t max_len) const'))
harness = HARNESS.replace('@COMMAND@', command)
harness = harness.replace('@HEX_CHARS@', hex_chars)
harness = harness.replace('@TO_HEX@', method(utils, 'void Utils::toHex('))
harness = harness.replace('@IDENTITY_METHODS@', identity_methods)
with tempfile.TemporaryDirectory() as directory:
work = Path(directory)
cpp = work / 'identity.cpp'
cpp.write_text(harness)
binary = work / 'identity'
for debug, packets, private_export in product((0, 1), repeat=3):
with self.subTest(debug=debug, packets=packets, private_export=private_export):
flags = ['ARDUINO=1', 'MESH_DEBUG=' + str(debug),
'MESH_PACKET_LOGGING=' + str(packets),
'ENABLE_PRIVATE_KEY_EXPORT=' + str(private_export),
'COMPANION_FEATURE_TEXT_TERMINAL=' + str(private_export),
'COMPANION_FEATURE_OTA_CLI=' + str(packets)]
if not debug and not packets:
flags.append('MESH_USB_LOGGING_DISABLED=1')
if debug and packets:
flags += ['COMPANION_RADIO_FULL=1', 'WITH_MQTT_BRIDGE=1']
built = subprocess.run([os.environ.get('CXX', 'g++'), '-std=c++17',
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
'-fno-pie', '-no-pie', *['-D' + flag for flag in flags],
'-I', str(ROOT / 'src'), '-I', str(ROOT / 'test/mocks'),
str(cpp), '-o', str(binary)], capture_output=True, text=True)
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
ran = subprocess.run([str(binary)], capture_output=True, text=True)
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
def test_terminal_binary_and_help_route_to_the_shared_readonly_query(self):
source = (ROOT / 'examples/companion_radio/MyMesh.cpp').read_text()
terminal = method(source, 'void MyMesh::handleTerminalCommand(')
self.assertIn('handleLocalControlCommand(command, local_reply, sizeof(local_reply))', terminal)
self.assertIn('terminalOutput().printf(" %s\\r\\n", local_reply);', terminal)
self.assertIn('terminalOutput().print(" get public.key\\r\\n");', terminal)
shared = method(source, 'bool MyMesh::handleCommand(')
self.assertIn('if (handleLocalControlCommand(command, reply, reply_capacity)) return true;', shared)
self.assertIn('return handleCommand(command, 0, reply);',
(ROOT / 'examples/companion_radio/MyMesh.h').read_text())
start = source.index('} else if (mesh::companion::isRunCliFrame(')
end = source.index('} else if (cmd_frame[0] == CMD_SEND_TXT_MSG', start)
framed = source[start:end]
self.assertIn('handled = handleCommand(text, 0, reply_buf);', framed)
self.assertIn('out_frame[0] = RESP_CODE_CLI_REPLY;', framed)
self.assertIn('_serial->writeFrame(out_frame, 1 + rlen);', framed)
local = method(source, 'bool MyMesh::handleLocalControlCommand(')
branch = local[:local.index(' if (handleTxRoutingCommand(')]
self.assertIn('strcmp(command, "get public.key") == 0', branch)
self.assertNotIn('#if', branch) # Not gated by private export or logging.
self.assertNotIn('prv_key', branch)
self.assertNotIn('save', branch)
if __name__ == '__main__':
unittest.main()
@@ -194,7 +194,7 @@ int main(int argc,char** argv){
} else if(scenario==7){
// Pre-consent image: all earlier settings survive with default-off consent.
DataStore legacy;legacy.fs.files["/new_prefs"]=disk;
legacy.fs.files["/new_prefs"].resize(disk.size()-2);
legacy.fs.files["/new_prefs"].resize(disk.size()-3);
CompanionNodePrefs loaded;double lat=0,lon=0;
assert(legacy.loadPrefs(loaded,lat,lon));
assert(loaded.flood_retry_advert_enabled==0);
+50 -11
View File
@@ -50,10 +50,11 @@ struct Prefs {
struct Sensors {
double node_lat=10,node_lon=20;
std::string gps="0",interval="60",other="old";
bool accepts=true;
bool accepts=true,gps_available=true;
bool powersaving_enabled=false;
void setPowerSavingEnabled(bool enabled) { powersaving_enabled=enabled; }
const char* getSettingByKey(const char* key) {
if(!strcmp(key,"gps") && !gps_available)return nullptr;
return !strcmp(key,"gps") ? gps.c_str() : interval.c_str();
}
bool setSettingValue(const char* key,const char* value) {
@@ -114,17 +115,24 @@ struct MyMesh {
void writeOKFrame() { replies.push_back(0); }
void writeErrFrame(uint8_t error) { replies.push_back(error); }
void updateGpsTelemetryPolicy() { ++gps_policy_updates; }
void applyGpsPrefs() {
sensors.setSettingValue("gps",_prefs.gps_enabled?"1":"0");
char s[16];snprintf(s,sizeof(s),"%lu",(unsigned long)_prefs.gps_interval);
sensors.setSettingValue("gps_interval",s);
}
#if ENV_INCLUDE_GPS == 1
bool setGpsEnabled(bool);
@APPLY_GPS_PREFS@
#endif
MyMesh& terminalOutput() { return *this; }
void print(const char* value) { output+=value; }
void frame(size_t len) { @FRAMES@ }
void web(const char* key,const char* value,char* reply) { @WEB@ }
void terminal(const char* config) { @TERMINAL@ }
bool gps(const char* gps_value,char* reply) { const size_t reply_size=160;@GPS@;return false; }
bool gps(const char* gps_value,char* reply) {
#if ENV_INCLUDE_GPS == 1
const size_t reply_size=160;
@GPS@
#else
(void)gps_value;(void)reply;
#endif
return false;
}
bool hasOutbound() const { return false; }
bool millisHasNowPassed(uint32_t) const { return true; }
uint32_t futureMillis(uint32_t delay) const { return delay+100; }
@@ -155,6 +163,9 @@ struct MyMesh {
}
};
@METHODS@
#if ENV_INCLUDE_GPS == 1
@GPS_METHOD@
#endif
static void u32(std::vector<uint8_t>& bytes,uint32_t value) {
for(unsigned i=0;i<4;++i) bytes.push_back(uint8_t(value>>(8*i)));
}
@@ -218,12 +229,38 @@ int main() {
m.expectOriginal();
}
#if ENV_INCLUDE_GPS == 1
for(bool save_ok:{false,true}) {
for(bool initial:{false,true})for(bool save_ok:{false,true}) {
MyMesh m;char reply[160]={};m.storage_accepts=save_ok;
assert(m.gps("on",reply));++checks;
m._prefs.gps_enabled=m.durable.gps_enabled=initial;
sensors.gps=initial?"1":"0";
assert(m.gps(initial?"off":"on",reply));++checks;
const bool expected=save_ok?!initial:initial;
assert(error(reply)!=save_ok && m.saves==1);
assert(sensors.gps==(save_ok?"1":"0"));
if(!save_ok)m.laterSaveCannotResurrectFailure();
assert(sensors.gps==(expected?"1":"0") && sensors.interval=="60");
assert(m._prefs.gps_enabled==expected && m.durable.gps_enabled==expected);
m.storage_accepts=true;assert(m.saveAdvertName("later"));
assert(m.durable.gps_enabled==expected);
}
for(bool initial:{false,true})for(bool available:{false,true}) {
MyMesh m;char reply[160]={};
m._prefs.gps_enabled=m.durable.gps_enabled=initial;
sensors.gps=initial?"1":"0";
sensors.gps_available=available;sensors.accepts=false;
assert(m.gps(initial?"off":"on",reply));++checks;
assert(error(reply) && m.saves==0);
assert(sensors.gps==(initial?"1":"0"));
assert(m._prefs.gps_enabled==initial && m.durable.gps_enabled==initial);
}
for(const char* value:{"", "1", "ON", "on extra"}) {
MyMesh m;char reply[160]={};assert(m.gps(value,reply));++checks;
assert(error(reply) && m.saves==0 && sensors.gps=="0");m.expectOriginal();
}
#else
{
MyMesh m;char reply[160]="unchanged";
assert(!m.gps("on",reply) && !m.gps("off",reply));++checks;
assert(!strcmp(reply,"unchanged") && m.saves==0 && sensors.gps=="0");
m.expectOriginal();
}
#endif
// Checked RXPS applies roll back hardware too, and keep retrying a busy
@@ -352,6 +389,8 @@ class CompanionPrefsTransactionTests(unittest.TestCase):
'static uint32_t nextRadioApplyRetryDelay(',
)),
'@SAVE_PREFERENCE@': extract_braced(header, 'template <typename T> bool savePreference('),
'@APPLY_GPS_PREFS@': extract_braced(header, 'void applyGpsPrefs('),
'@GPS_METHOD@': extract_braced(text, 'bool MyMesh::setGpsEnabled('),
'@METHODS@': '\n'.join(extract_braced(text,sig) for sig in (
'bool MyMesh::saveAdvertName(', 'bool MyMesh::saveAdvertLocation(',
'bool MyMesh::applyAndSaveRxPowerSaving(',
+452
View File
@@ -0,0 +1,452 @@
"""Execute Companion debug persistence/CLI and bounded packet logging paths."""
from pathlib import Path
import os
import subprocess
import tempfile
import unittest
from test_companion_preferences_transaction import HARNESS as STORE_HARNESS
from test_companion_preferences_transaction import esp_recovery_helpers
from test_radio_receive_contract import method
ROOT = Path(__file__).resolve().parents[1]
PREFERENCE_HARNESS = r'''
struct ConfigText : Stream {
std::string text;size_t offset=0;
int available() override{return int(text.size()-offset);}
int read() override{return offset<text.size()?uint8_t(text[offset++]):-1;}
int peek() override{return offset<text.size()?uint8_t(text[offset]):-1;}
size_t write(uint8_t value) override{text.push_back(char(value));return 1;}
size_t write(const uint8_t* value,size_t size) override{
text.append(reinterpret_cast<const char*>(value),size);return size;
}
size_t print(unsigned int value,int base) override{
assert(base==10);const auto number=std::to_string(value);
return write(reinterpret_cast<const uint8_t*>(number.data()),number.size());
}
};
namespace mesh {
bool logging_enabled=true, debug_enabled=false;
unsigned debug_applies=0;
bool isUsbLoggingEnabled(){return logging_enabled;}
bool isUsbDebugEnabled(){return debug_enabled;}
void setUsbDebugEnabled(bool enabled){debug_enabled=enabled;++debug_applies;}
}
struct {double node_lat=47.1,node_lon=-122.2;} sensors;
struct MyMesh {
CompanionNodePrefs _prefs;
DataStore store;
DataStore* _store=&store;
@SAVE_PREFS@
@SAVE_PREFERENCE@
bool command(const char* command,char* reply,size_t reply_size){
@DEBUG_COMMANDS@
return false;
}
};
int main(){
MyMesh node;
assert(node._prefs.usb_debug_enabled==0);
strcpy(node._prefs.node_name,"durable node");node._prefs.freq=910.525f;
node._prefs.ble_pin=876543;node._prefs.bluetooth_enabled=0;
node._prefs.one_key_dm_enabled=1;node._prefs.usb_logging_enabled=1;
assert(node.savePrefs());
const auto original=node.store.fs.files["/new_prefs"];
#if defined(TBEAM_1W)
assert(original.size()==240);
#elif defined(RP2040_PLATFORM) && defined(ENABLE_WIFI_INTERFACE)
assert(original.size()==330);
#else
assert(original.size()==233);
#endif
assert(original.back()==0&&original[original.size()-2]==0);
assert(original[original.size()-3]==1&&original[158]==1);
char reply[160]={};
for(const char* malformed : {"set usb.debug","set usb.debug ",
"set usb.debug 1","set usb.debug ON","set usb.debug on ",
"set usb.debug on reboot","set usb.debug off extra", "set usb.debug off\n"}){
const auto before=node.store.fs.files["/new_prefs"];
const unsigned applied=mesh::debug_applies;
assert(node.command(malformed,reply,sizeof(reply)));
assert(!strcmp(reply,"Error: use set usb.debug <on|off>"));
assert(node._prefs.usb_debug_enabled==0&&!mesh::debug_enabled);
assert(mesh::debug_applies==applied&&node.store.fs.files["/new_prefs"]==before);
}
for(const char* unrelated : {"set usb.debugger on","get usb.debugx","get usb.debug on"})
assert(!node.command(unrelated,reply,sizeof(reply)));
assert(node.command("set usb.debug\t on",reply,sizeof(reply)));
assert(!strncmp(reply,"OK - USB debug on (saved)",24));
assert(node._prefs.usb_debug_enabled==1&&mesh::debug_enabled);
const auto enabled_disk=node.store.fs.files["/new_prefs"];
assert(enabled_disk.back()==1);
for(size_t i=0;i+1<original.size();++i)assert(enabled_disk[i]==original[i]);
assert(node.command("get usb.debug",reply,sizeof(reply)));
#if MESH_DEBUG
assert(!strcmp(reply,"usb.debug on"));
#else
assert(!strcmp(reply,"usb.debug on (core debug not compiled)"));
#endif
mesh::logging_enabled=false;
assert(node.command("get usb.debug",reply,sizeof(reply)));
#if MESH_DEBUG
assert(!strcmp(reply,"usb.debug on (USB logging off)"));
#endif
// Faults at early and newly appended bytes both preserve the committed image
// and requested runtime state. A subsequent successful save applies once.
for(int fail_after : {17,int(enabled_disk.size()-1)}){
node.store.fs.fail_write_after=fail_after;
const unsigned applied=mesh::debug_applies;
assert(node.command("set usb.debug off",reply,sizeof(reply)));
assert(!strcmp(reply,"Error: USB debug setting could not be saved"));
assert(node._prefs.usb_debug_enabled==1&&mesh::debug_enabled);
assert(mesh::debug_applies==applied);
assert(node.store.fs.files["/new_prefs"]==enabled_disk);
}
node.store.fs.fail_write_after=-1;
assert(node.command("set usb.debug off",reply,sizeof(reply)));
assert(node._prefs.usb_debug_enabled==0&&!mesh::debug_enabled);
for(uint8_t enabled : {0,1}){
node._prefs.usb_debug_enabled=enabled;assert(node.savePrefs());
assert(mesh::debug_enabled==(enabled!=0));
DataStore reboot;reboot.fs=node.store.fs;
CompanionNodePrefs loaded;loaded.usb_debug_enabled=1-enabled;
double lat=0,lon=0;assert(reboot.loadPrefs(loaded,lat,lon));
assert(loaded.usb_debug_enabled==enabled&&loaded.bluetooth_enabled==0);
assert(loaded.one_key_dm_enabled==1&&loaded.usb_logging_enabled==1);
assert(loaded.ble_pin==876543&&loaded.freq==910.525f);
}
// Existing complete tails retain their established offsets and always start
// quiet, even if the object was already using verbose runtime diagnostics.
for(unsigned size : {84u,156u,215u,226u,unsigned(original.size()-1)}){
DataStore legacy;legacy.fs.files["/new_prefs"]=enabled_disk;
legacy.fs.files["/new_prefs"].resize(size);
CompanionNodePrefs loaded;loaded.usb_debug_enabled=1;
double lat=0,lon=0;assert(legacy.loadPrefs(loaded,lat,lon));
assert(loaded.usb_debug_enabled==0&&loaded.ble_pin==876543);
assert(legacy.fs.files["/new_prefs"].size()==size);
}
// Normalize the new boolean only after the entire persisted image validates.
for(uint8_t byte : {2,255}){
DataStore corrupt;corrupt.fs.files["/new_prefs"]=enabled_disk;
corrupt.fs.files["/new_prefs"].back()=byte;
CompanionNodePrefs loaded;double lat=0,lon=0;
assert(corrupt.loadPrefsInt("/new_prefs",loaded,lat,lon));
assert(loaded.usb_debug_enabled==0);
}
DataStore extra;extra.fs.files["/new_prefs"]=enabled_disk;
extra.fs.files["/new_prefs"].push_back(0);
CompanionNodePrefs unchanged;unchanged.usb_debug_enabled=0;
double lat=1,lon=2;assert(!extra.loadPrefsInt("/new_prefs",unchanged,lat,lon));
assert(unchanged.usb_debug_enabled==0&&lat==1&&lon==2);
// Numeric dynamic configuration uses the actual preference, not a shadow
// string in the custom dictionary, and applies through the same save path.
char dynamic[8]={};
assert(node._prefs.getCustom()->setByKey("usb_debug","1"));
assert(node._prefs.usb_debug_enabled==1&&node._prefs.isDirty());
assert(node._prefs.getCustom()->getByKey("usb_debug",dynamic,sizeof(dynamic)));
assert(!strcmp(dynamic,"1"));
assert(node.savePrefs()&&mesh::debug_enabled&&!node._prefs.isDirty());
assert(node._prefs.getCustom()->setByKey("usb_debug","0"));
assert(node.savePrefs()&&!mesh::debug_enabled);
// The radio serializer exports the preference; custom JSON imports delegate
// to the same field rather than creating an unrelated string preference.
ConfigText imported;imported.text="{usb_debug:1}";
assert(node._prefs.getCustom()->getByKey("usb_debug",dynamic,sizeof(dynamic)));
auto* serializer=static_cast<DynamicConfigSerializer*>(node._prefs.getCustom());
assert(serializer->loadSerial(imported)&&node._prefs.usb_debug_enabled==1);
assert(node.savePrefs()&&mesh::debug_enabled);
ConfigText exported;assert(node._prefs.getRadioPrefs()->saveSerial(exported));
assert(exported.text.find("usb_debug:1")!=std::string::npos);
// Only an explicit 1 enables verbosity, including when a dynamic schema has
// assigned an out-of-range byte. Failed commits still leave runtime alone.
node._prefs.usb_debug_enabled=255;assert(node.savePrefs());
assert(node._prefs.usb_debug_enabled==0&&!mesh::debug_enabled);
node._prefs.usb_debug_enabled=255;node.store.fs.fail_write=true;
assert(!node.savePrefs()&&node._prefs.usb_debug_enabled==255&&!mesh::debug_enabled);
node.store.fs.fail_write=false;node._prefs.usb_debug_enabled=0;
// Bounded replies do not write beyond their supplied capacity.
char small[9];memset(small,'X',sizeof(small));
assert(node.command("get usb.debug",small,8));assert(small[7]==0&&small[8]=='X');
}
'''
PACKET_HARNESS = r'''
#include <cassert>
#include <cstring>
#include <string>
#include <vector>
#include <ctime>
#include <helpers/SerialPacketLog.h>
#include <helpers/BaseSerialInterface.h>
std::vector<std::string> events;
struct Port : Stream {
std::string output;
bool stalled=false;
int availableForWrite() override{return stalled?0:4096;}
size_t write(const uint8_t* bytes,size_t size) override{
events.push_back("ascii");output.append(reinterpret_cast<const char*>(bytes),size);return size;
}
} log_port;
namespace mesh {
bool enabled=true,debug_enabled=false;
bool isUsbLoggingEnabled(){return enabled;}
Port& usbLoggingPort(){return log_port;}
}
struct Transport {
bool connected=true;
std::vector<std::vector<uint8_t>> frames;
bool isConnected() const{return connected;}
size_t writeFrame(const uint8_t* bytes,size_t size){
events.push_back("binary");frames.emplace_back(bytes,bytes+size);return size;
}
};
struct Bridge {
bool running=true;
std::vector<std::vector<uint8_t>> raw;
float snr=0,rssi=0;
bool isRunning() const{return running;}
void storeRawRadioData(const uint8_t* bytes,int len,float s,float r){
events.push_back("mqtt");raw.emplace_back(bytes,bytes+len);snr=s;rssi=r;
}
};
@PUSH_CODE@
struct Clock {
uint32_t timestamp=1791030896;
uint32_t getCurrentTime() const{return timestamp;}
};
class DateTime {
std::tm value;
public:
explicit DateTime(uint32_t timestamp){const std::time_t time=timestamp;value=*std::gmtime(&time);}
unsigned hour() const{return value.tm_hour;}
unsigned minute() const{return value.tm_min;}
unsigned second() const{return value.tm_sec;}
unsigned day() const{return value.tm_mday;}
unsigned month() const{return value.tm_mon+1;}
unsigned year() const{return value.tm_year+1900;}
};
struct MyMesh {
Transport transport;Transport* _serial=&transport;
Bridge bridge;Bridge* _mqtt_bridge=&bridge;
uint8_t out_frame[MAX_FRAME_SIZE]={};
Clock clock;Clock* getRTCClock(){return &clock;}
#if MESH_PACKET_LOGGING && !MESH_PACKET_LOGGING_COMPACT
const char* getLogDateTime();
#else
const char* getLogDateTime(){return "";}
#endif
void logRxRaw(float,float,const uint8_t*,int);
};
@LOG_TIMESTAMP@
@LOG_RX_RAW@
void reset(){
events.clear();log_port.output.clear();log_port.stalled=false;
mesh::serialLogDroppedCount()=0;mesh::serialLogPortSeen()=false;
mesh::enabled=true;mesh::debug_enabled=false;g_mock_millis=0;
}
int main(){
const uint8_t raw[]={0x00,0xab,0xff};
MyMesh node;reset();node.logRxRaw(2.5f,-92.0f,raw,sizeof(raw));
#if MESH_PACKET_LOGGING
#if MESH_PACKET_LOGGING_COMPACT
assert(log_port.output=="R00ABFF\n");
#else
assert(log_port.output=="12:34:56 - 3/10/2026 U RAW: 00ABFF\r\n");
// A second invocation uses the current RTC and is the formatter dispatched
// by the shared RX/TX summary path as well as this RAW path.
assert(!strcmp(node.getLogDateTime(),"12:34:56 - 3/10/2026 U"));
node.clock.timestamp+=1;
assert(!strcmp(node.getLogDateTime(),"12:34:57 - 3/10/2026 U"));
node.clock.timestamp-=1;
#endif
assert(events.front()=="ascii"); // RAW precedes the unchanged downstream sinks.
#else
assert(log_port.output.empty());
#endif
assert(node.transport.frames.size()==1);
const std::vector<uint8_t> frame={PUSH_CODE_LOG_RX_DATA,10,uint8_t(-92),0x00,0xab,0xff};
assert(node.transport.frames[0]==frame);
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
assert(node.bridge.raw.size()==1&&node.bridge.raw[0]==std::vector<uint8_t>(raw,raw+sizeof(raw)));
assert(node.bridge.snr==2.5f&&node.bridge.rssi==-92.0f);
assert(events[events.size()-2]=="mqtt");
#endif
reset();mesh::enabled=false;node.logRxRaw(2.5f,-92.0f,raw,sizeof(raw));
assert(log_port.output.empty()&&node.transport.frames.size()==2);
reset();node.transport.connected=false;node.logRxRaw(2.5f,-92.0f,raw,sizeof(raw));
assert(node.transport.frames.size()==2); // Disconnected binary session receives no push.
#if MESH_PACKET_LOGGING
assert(!log_port.output.empty()); // Independent diagnostics port still receives RAW.
#endif
reset();uint8_t maximum[255];memset(maximum,0xa5,sizeof(maximum));
node.logRxRaw(0,-100,maximum,sizeof(maximum));
assert(node.transport.frames.size()==2); // Never overflow the 176-byte frame buffer.
#if MESH_PACKET_LOGGING
assert(log_port.output.size()<=SERIAL_LOG_LINE_MAX);
std::string expected_hex;for(unsigned i=0;i<255;++i)expected_hex+="A5";
#if MESH_PACKET_LOGGING_COMPACT
assert(log_port.output=="R"+expected_hex+"\n");
#else
assert(log_port.output=="12:34:56 - 3/10/2026 U RAW: "+expected_hex+"\r\n");
#endif
reset();log_port.stalled=true;node.transport.connected=true;
const uint32_t before=millis();node.logRxRaw(2.5f,-92.0f,raw,sizeof(raw));
assert(log_port.output.empty()&&millis()==before);
assert(node.transport.frames.size()==3); // A stalled host cannot block other sinks.
#endif
}
'''
class CompanionUsbLoggingTests(unittest.TestCase):
def compile_and_run(self, work, text, flags, extra=()):
source = work / 'test.cpp'
source.write_text(text)
binary = work / 'test'
built = subprocess.run([os.environ.get('CXX', 'g++'), '-std=c++17',
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
'-fno-pie', '-no-pie', *['-D' + flag for flag in flags],
'-include', str(work / 'platform_shim.h'),
'-I', str(work), '-I', str(ROOT / 'test/fixtures/radio_profiles/mocks'),
'-I', str(ROOT / 'test/mocks'), '-I', str(ROOT / 'src'),
'-I', str(ROOT / 'src/helpers'), '-I', str(ROOT), str(source),
*[str(ROOT / 'src/helpers' / name) for name in extra],
'-o', str(binary)], capture_output=True, text=True)
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
ran = subprocess.run([str(binary)], capture_output=True, text=True)
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
def prepare(self, work):
(work / 'Utils.h').write_text('''#pragma once
#include <Arduino.h>
#include <cassert>
namespace mesh { struct Utils {
static void printHex(Stream&,const uint8_t*,size_t){assert(false);}
static void fromHex(uint8_t*,size_t,const char*){assert(false);}
}; }
''')
(work / 'platform_shim.h').write_text('''#include <cstdlib>
#include <cstdio>
inline char* utoa(unsigned int value,char* output,int base){
if(base!=10)abort();sprintf(output,"%u",value);return output;
}
''')
transaction = (ROOT / 'src/helpers/ContactFileTransaction.h').read_text()
(work / 'ContactFileTransaction.h').write_text(transaction.replace(
'#include "IdentityStore.h"', '#include <helpers/IdentityStore.h>'))
def test_real_preference_io_cli_and_saved_runtime_gate(self):
store = (ROOT / 'examples/companion_radio/DataStore.cpp').read_text()
source = (ROOT / 'examples/companion_radio/MyMesh.cpp').read_text()
header = (ROOT / 'examples/companion_radio/MyMesh.h').read_text()
store_methods = esp_recovery_helpers(store) + '\n'.join(method(store, signature)
for signature in ('bool DataStore::loadPrefs(',
'bool DataStore::loadPrefsInt(', 'bool DataStore::savePrefs('))
prefix = STORE_HARNESS.split('int main(')[0].replace('@METHODS@', store_methods)
start = source.index(' if (strcmp(command, "get usb.debug") == 0)')
end = source.index(' if (strcmp(command, "get usb.logging") == 0)', start)
harness = PREFERENCE_HARNESS.replace('@DEBUG_COMMANDS@', source[start:end])
harness = harness.replace('@SAVE_PREFS@', method(header, ' bool savePrefs()'))
harness = harness.replace('@SAVE_PREFERENCE@', method(
header, 'template <typename T> bool savePreference('))
with tempfile.TemporaryDirectory() as directory:
work = Path(directory)
self.prepare(work)
for platform in ('NRF52_PLATFORM', 'STM32_PLATFORM', 'ESP32_PLATFORM',
'RP2040_PLATFORM', 'ESP32_PLATFORM,TBEAM_1W',
'RP2040_PLATFORM,ENABLE_WIFI_INTERFACE'):
for debug in (0, 1):
with self.subTest(platform=platform, debug=debug):
self.compile_and_run(work, prefix + harness,
[flag + '=1' for flag in platform.split(',')] +
['MESH_USB_LOGGING_AVAILABLE=1', 'MESH_DEBUG=' + str(debug)],
('ConfigSerializer.cpp', 'DynamicConfigSerializer.cpp',
'CommonRadioPrefs.cpp', 'TxtDataHelpers.cpp'))
def test_real_raw_emitter_master_gate_compact_and_other_transports(self):
source = (ROOT / 'examples/companion_radio/MyMesh.cpp').read_text()
code = next(line for line in source.splitlines()
if line.startswith('#define PUSH_CODE_LOG_RX_DATA'))
harness = PACKET_HARNESS.replace('@PUSH_CODE@', code)
harness = harness.replace('@LOG_TIMESTAMP@',
'#if MESH_PACKET_LOGGING && !MESH_PACKET_LOGGING_COMPACT\n' +
method(source, 'const char* MyMesh::getLogDateTime()') + '\n#endif\n')
harness = harness.replace('@LOG_RX_RAW@', method(source, 'void MyMesh::logRxRaw('))
with tempfile.TemporaryDirectory() as directory:
work = Path(directory)
self.prepare(work)
for packets, compact, mqtt in ((0, 0, 0), (0, 0, 1),
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)):
with self.subTest(packets=packets, compact=compact, mqtt=mqtt):
flags = ['MESH_PACKET_LOGGING=' + str(packets),
'MESH_PACKET_LOGGING_COMPACT=' + str(compact)]
if mqtt:
flags += ['WITH_MQTT_BRIDGE=1', 'ESP32_PLATFORM=1', 'WIFI_SSID="test"']
self.compile_and_run(work, harness, flags)
def test_begin_defaults_clamps_and_applies_debug_before_packet_master(self):
source = (ROOT / 'examples/companion_radio/MyMesh.cpp').read_text()
self.assertIn('_prefs.usb_debug_enabled = 0;', source)
self.assertIn('_prefs.usb_debug_enabled = _prefs.usb_debug_enabled == 1 ? 1 : 0;', source)
apply = source.index('mesh::setUsbDebugEnabled(_prefs.usb_debug_enabled != 0);')
self.assertLess(apply, source.index('mesh::setUsbLoggingEnabled(usb_logging_enabled);'))
header = (ROOT / 'examples/companion_radio/MyMesh.h').read_text()
self.assertIn('const char* getLogDateTime() override;', header)
def test_actual_help_chunks_keep_controls_and_bounded_native_writes(self):
source = (ROOT / 'examples/companion_radio/MyMesh.cpp').read_text()
start = source.index(' } else if (strcmp(command, "help") == 0) {')
start = source.index('{', start) + 1
end = source.index(' } else if (handleCommand(command, 0, local_reply))', start)
harness = r'''
#include <cassert>
#include <cstring>
#include <string>
struct Port {
std::string output;
void print(const char* text){assert(strlen(text)<=256);output+=text;}
void println(const char* text){print(text);output+="\r\n";}
};
struct MyMesh {
Port port;bool _terminal_mode=true;
Port& terminalOutput(){return port;}
void help(){ @HELP@ }
};
int main(){
MyMesh node;
for(bool terminal : {false,true}){
node._terminal_mode=terminal;node.port.output.clear();node.help();
const auto& text=node.port.output;
assert(text.find("Commands:\r\n stats-core / stats-radio / stats-radio-diag / stats-packets\r\n")==0);
assert(text.find(" get public.key\r\n")!=std::string::npos);
assert(text.find(" get usb.debug\r\n set usb.debug <on|off>\r\n")!=std::string::npos);
#if MESH_USB_CONSOLE_COOPERATIVE
assert(text.find(" get usb.watchdog\r\n get usb.watchdog.last\r\n set usb.watchdog <off|on|auto> (saved; default auto)\r\n")!=std::string::npos);
#else
assert(text.find("usb.watchdog")==std::string::npos);
#endif
assert(text.find(" reboot\r\n ver\r\n")!=std::string::npos);
assert(text.find(" +++MESHCORE-TERM-STOP\r\n")!=std::string::npos ? terminal : !terminal);
assert(text.find(" disconnect (closes the network terminal)\r\n")!=std::string::npos ? !terminal : terminal);
}
}
'''.replace('@HELP@', source[start:end])
features = ['MESH_USB_LOGGING_AVAILABLE', 'COMPANION_FEATURE_READER',
'COMPANION_FEATURE_MEMORY_DIAGNOSTICS', 'COMPANION_FEATURE_NOTIFICATIONS',
'COMPANION_RADIO_FULL', 'COMPANION_EXCLUSIVE_WIFI_BLE',
'WITH_WEBCONFIG', 'MESH_PRIMARY_ESPNOW', 'COMPANION_FEATURE_TEMP_RADIO',
'COMPANION_FEATURE_OTA_CLI', 'BLE_PIN_CODE', 'ESP32', 'NRF52_PLATFORM',
'EXTRAFS']
with tempfile.TemporaryDirectory() as directory:
work = Path(directory)
self.prepare(work)
for native in (0, 1):
with self.subTest(native=native):
self.compile_and_run(work, harness,
[flag + '=1' for flag in features] + ['WIFI_SSID="test"',
'MESH_USB_CONSOLE_COOPERATIVE=' + str(native)])
if __name__ == '__main__':
unittest.main()
@@ -24,7 +24,7 @@ HARNESS = r'''
#include <cstdint>
#include <helpers/FileRead.h>
#define MESH_ESP32_TINYUSB_NONBLOCKING 1
#define MESH_ESP32_USB_CONSOLE_COOPERATIVE 1
#define MESH_USB_CONSOLE_COOPERATIVE 1
#define MAX_ROUTE_HASH_BYTES 3
#define PACKET_LOG_FILE "/packet_log"
static void require(bool ok, const char* what) {
@@ -249,7 +249,7 @@ class CooperativeOutputTest(unittest.TestCase):
with self.subTest(role=role), tempfile.TemporaryDirectory() as temporary:
text = (ROOT / f"examples/{role}/MyMesh.cpp").read_text()
dump_start = text.index("void MyMesh::dumpLogFile()")
dump_end = text.index("\n#if MESH_ESP32_USB_CONSOLE_COOPERATIVE\nbool MyMesh::hasPendingSerialOutput", dump_start)
dump_end = text.index("\n#if MESH_USB_CONSOLE_COOPERATIVE\nbool MyMesh::hasPendingSerialOutput", dump_start)
pump_start = text.index("bool MyMesh::hasPendingSerialOutput()", dump_end)
pump_end = text.index("\n#endif", pump_start)
methods = text[dump_start:dump_end] + "\n" + text[pump_start:pump_end]
+1
View File
@@ -32,6 +32,7 @@ class Esp32TinyUsbNonblockingTest(unittest.TestCase):
f"-I{FIXTURE / 'mocks'}", f"-I{ROOT / 'src'}",
str(FIXTURE / "test_esp32_tinyusb_nonblocking.cpp"),
str(ROOT / "src/helpers/UsbLogging.cpp"),
str(ROOT / "src/helpers/UsbLoggingClientActivity.cpp"),
"-o", str(binary),
]
if companion:
+7 -2
View File
@@ -24,6 +24,8 @@ class Esp32TinyUsbRoleHygieneTest(unittest.TestCase):
r"return buffered_esp32_tinyusb_terminal_port;\s+"
r"#elif MESH_ESP32_HWCDC_SESSION_GUARD\s+"
r"return guarded_esp32_hwcdc_port;\s+"
r"#elif MESH_NRF52_USB_CONSOLE_COOPERATIVE\s+"
r"return buffered_primary_usb_terminal_port;\s+"
r"#else\s+return Serial;\s+#endif",
)
@@ -140,15 +142,18 @@ class Esp32TinyUsbRoleHygieneTest(unittest.TestCase):
def test_sleep_keeps_raw_flush_only_for_other_esp32_transports(self):
text = source("src/helpers/ESP32Board.cpp")
shutdown = text[text.index("void ESP32Board::shutdownPeripherals(") :
text.index("void ESP32Board::powerOff(")]
sleep = text[text.index("void ESP32Board::enterDeepSleep(") :]
self.assertIn("shutdownPeripherals();", sleep)
guard = re.search(
r"#if MESH_ESP32_USB_CONSOLE_COOPERATIVE\s+"
r"mesh::serviceUsbTerminalPort\(\);\s+"
r"#else\s+Serial\.flush\(\);\s+#endif",
sleep,
shutdown,
)
self.assertIsNotNone(guard)
self.assertEqual(sleep.count("Serial.flush();"), 1)
self.assertEqual(shutdown.count("Serial.flush();"), 1)
def test_large_file_dumps_are_bounded_and_cancelable(self):
for role in ROLES:
+5 -5
View File
@@ -30,7 +30,7 @@ class Esp32UsbSerialHygieneTest(unittest.TestCase):
r"\bSerial\.(?:print|println|printf|write)\s*\(",
relative,
)
self.assertIn("mesh::usbLoggingPort()", text, relative)
self.assertRegex(text, r"mesh::usb(?:Logging|Debug)Port\(\)", relative)
def test_v4_companion_uses_usb_serial_jtag_mode(self):
platformio = source("variants/heltec_v4/platformio.ini")
@@ -134,7 +134,7 @@ class Esp32UsbSerialHygieneTest(unittest.TestCase):
self.assertNotIn("Serial.setTxBufferSize(", helper)
self.assertIn("while (Serial.read() >= 0)", helper)
self.assertIn(
"setPlatformDebugOutputEnabled(isUsbLoggingEnabled());", purge
"setPlatformDebugOutputEnabled(isUsbDebugLoggingEnabled());", purge
)
self.assertIn(
"esp32_hwcdc_self_reset_guard.expectSelfResetBurst()", purge
@@ -325,7 +325,7 @@ class Esp32UsbSerialHygieneTest(unittest.TestCase):
)
self.assertIn("Stream& output = mesh::usbLoggingPort();", text)
def test_framework_diagnostics_follow_same_runtime_gate(self):
def test_framework_diagnostics_require_master_and_debug_gates(self):
text = source("src/helpers/UsbLogging.cpp")
self.assertIn("Serial.setDebugOutput(enabled);", text)
@@ -333,14 +333,14 @@ class Esp32UsbSerialHygieneTest(unittest.TestCase):
text.index("void setUsbLoggingEnabled(") :
text.index("bool saveUsbLoggingBootPreference(")
]
self.assertIn("setPlatformDebugOutputEnabled(enabled);", setter)
self.assertIn("setPlatformDebugOutputEnabled(isUsbDebugLoggingEnabled());", setter)
begin = text[
text.index("void beginUsbLoggingPort(") :
text.index("void serviceUsbLoggingPort(")
]
self.assertIn(
"setPlatformDebugOutputEnabled(isUsbLoggingEnabled());", begin
"setPlatformDebugOutputEnabled(isUsbDebugLoggingEnabled());", begin
)
def test_expected_fresh_nvs_state_is_silent(self):
+78
View File
@@ -25,6 +25,80 @@ function hex(text) {
assert.strictEqual(publicOnly.public.radioId, "000102030405060708090A0B0C0D0E0F");
assert.strictEqual(publicOnly.public.firmware, "1.17.1.5");
assert.strictEqual(publicOnly.acl.length, 0);
assert.strictEqual(publicOnly.public.protocol, "MGR1");
assert.strictEqual(publicOnly.public.usbLogging, null);
assert.strictEqual(publicOnly.public.usbWatchdogLast, null);
// Public MGR2 parsing is additive; actual MGR2 SIV ciphertext is checked
// cross-language by test_management_report.py using the production encoder.
const legacy = Buffer.from(decoder.EXAMPLE_PAGE, "hex");
const currentHeader = Buffer.from(legacy.subarray(0, 83)); currentHeader[3] = 0x32;
const usb = Buffer.alloc(15);
usb.writeUInt16LE(0x07ff, 0); usb[2] = 2 | (8 << 2);
usb.writeUInt32LE(604800, 3); usb.writeUInt32LE(0xffffffff, 7);
usb.writeUInt32LE(1209600, 11);
const current = Buffer.concat([currentHeader, usb, legacy.subarray(83)]);
const currentPublic = await decoder.decodeManagement(current.toString("hex"), "");
assert.strictEqual(currentPublic.public.protocol, "MGR2");
assert.strictEqual(currentPublic.public.usbWatchdogLast, null);
assert.strictEqual(currentPublic.public.usbLogging.supported, true);
assert.strictEqual(currentPublic.public.usbLogging.persistenceReady, true);
assert.strictEqual(currentPublic.public.usbLogging.loggerActive, true);
assert.strictEqual(currentPublic.public.usbLogging.stage, 2);
assert.strictEqual(currentPublic.public.usbLogging.backoffStep, 8);
assert.strictEqual(currentPublic.public.usbLogging.retrySeconds, 604800);
assert.strictEqual(currentPublic.public.usbLogging.inactiveSeconds, 0xffffffff);
assert.strictEqual(currentPublic.public.usbLogging.watchdogAuto, true);
assert.strictEqual(currentPublic.public.usbLogging.autoConnectedSeconds, 1209600);
const unsupported = Buffer.from(current); unsupported.writeUInt16LE(0, 83);
assert.strictEqual((await decoder.decodeManagement(unsupported.toString("hex"), "")).public.usbLogging.supported, false);
const openReaderOnly = Buffer.from(current); openReaderOnly[84] &= ~4;
const readerWithoutLogger = (await decoder.decodeManagement(openReaderOnly.toString("hex"), "")).public.usbLogging;
assert.strictEqual(readerWithoutLogger.hostConnected, true);
assert.strictEqual(readerWithoutLogger.readerConnected, true);
assert.strictEqual(readerWithoutLogger.loggerActive, false);
const badCount = Buffer.from(current); badCount[82] = 6;
await assert.rejects(decoder.decodeManagement(badCount.toString("hex"), ""), /bounds/);
await assert.rejects(decoder.decodeManagement(current.subarray(0, 99).toString("hex"), ""), /complete/);
for (const [offset, value] of [[84, 8], [85, 0xc0]]) {
const reserved = Buffer.from(current); reserved[offset] = value;
await assert.rejects(decoder.decodeManagement(reserved.toString("hex"), ""), /Reserved/);
}
// Public MGR3 fields use their own layout; production ciphertext for all
// three versions is authenticated cross-language by the Python host test.
const mgr3Header = Buffer.from(current.subarray(0, 98)); mgr3Header[3] = 0x33;
const event = Buffer.alloc(13); event[0] = 0xbf;
event.writeUInt32LE(1700000001, 1); event.writeUInt32LE(777, 5); event.writeUInt32LE(42, 9);
const mgr3 = Buffer.concat([mgr3Header, event, current.subarray(98)]);
const mgr3Public = await decoder.decodeManagement(mgr3.toString("hex"), "");
assert.strictEqual(mgr3Public.public.protocol, "MGR3");
assert.strictEqual(mgr3Public.public.usbLogging.loggerActive, true);
assert.deepStrictEqual(mgr3Public.public.usbWatchdogLast, {
reasons: 15, reasonNames: ["host-absent", "reader-absent", "tx-stalled", "client-inactive"],
actionCode: 3, action: "reboot-requested", persisted: true,
epoch: 1700000001, uptimeSeconds: 777, sequence: 42,
});
const none = Buffer.from(mgr3); none.fill(0, 98, 111);
assert.strictEqual((await decoder.decodeManagement(none.toString("hex"), "")).public.usbWatchdogLast, null);
for (let action = 1; action <= 4; ++action) {
const ramOnly = Buffer.from(mgr3); ramOnly[98] = 15 | (action << 4); ramOnly.writeUInt32LE(0, 99);
const decodedEvent = (await decoder.decodeManagement(ramOnly.toString("hex"), "")).public.usbWatchdogLast;
assert.strictEqual(decodedEvent.actionCode, action);
assert.strictEqual(decodedEvent.persisted, false);
assert.strictEqual(decodedEvent.epoch, 0);
}
for (const code of [0x80, 0x8f, 0xdf, 0xef, 0xff]) {
const invalid = Buffer.from(mgr3); invalid[98] = code;
await assert.rejects(decoder.decodeManagement(invalid.toString("hex"), ""), /Invalid/);
}
const invalidSequence = Buffer.from(mgr3); invalidSequence.writeUInt32LE(0, 107);
await assert.rejects(decoder.decodeManagement(invalidSequence.toString("hex"), ""), /Invalid/);
const fiveEntries = Buffer.from(mgr3); fiveEntries[82] = 5;
await assert.rejects(decoder.decodeManagement(fiveEntries.toString("hex"), ""), /bounds/);
for (const size of [99, 110, 126]) {
await assert.rejects(decoder.decodeManagement(mgr3.subarray(0, size).toString("hex"), ""), /complete/);
}
const decoded = await decoder.decodeManagement(
decoder.EXAMPLE_PAGE,
@@ -37,6 +111,10 @@ function hex(text) {
assert.strictEqual(decoded.acl[0].fingerprint, "AABBCCDDEEFF001122334455");
assert.strictEqual(decoded.acl[0].administrator, true);
assert.strictEqual(decoded.acl[0].otaSigner, true);
const wrapped = decoder.EXAMPLE_PAGE.match(/.{1,64}/g).join("\n");
assert.strictEqual((await decoder.decodeManagement(wrapped, decoder.EXAMPLE_PASSWORD)).authenticated, true);
const duplicateLines = decoder.EXAMPLE_PAGE + "\n" + decoder.EXAMPLE_PAGE;
assert.strictEqual((await decoder.decodeManagement(duplicateLines, decoder.EXAMPLE_PASSWORD)).acl.length, 1);
const groupData = "1A00" + decoder.EXAMPLE_PAGE;
const packet = await decoder.decodeManagement(groupData, decoder.EXAMPLE_PASSWORD);
+165 -4
View File
@@ -25,7 +25,7 @@ class ManagementTests(unittest.TestCase):
cls.exe = Path(cls.work.name) / "management-test"
sources = ["AES128.cpp", "AESCommon.cpp", "BlockCipher.cpp", "Crypto.cpp", "SHA256.cpp", "Hash.cpp"]
host_define = [] if os.name == "nt" else ["-DHOST_BUILD"]
sanitizers = [] if os.name == "nt" else ["-fsanitize=address,undefined", "-fno-omit-frame-pointer"]
sanitizers = [] if os.name == "nt" else ["-fsanitize=address,undefined", "-fno-sanitize-recover=all", "-fno-omit-frame-pointer"]
cmd = [shutil.which("g++") or "g++", "-std=c++17", "-O1", "-g", "-Wall", "-Wextra"] + host_define + [
*sanitizers, "-I", str(cls.crypto), "-I", str(ROOT / "src"),
str(ROOT / "src/helpers/ManagementReport.cpp"),
@@ -33,7 +33,11 @@ class ManagementTests(unittest.TestCase):
cmd += [str(cls.crypto / p) for p in sources] + ["-o", str(cls.exe)]
subprocess.run(cmd, check=True, capture_output=True, text=True)
result = subprocess.run([str(cls.exe)], check=True, capture_output=True, text=True)
cls.pages = [bytes.fromhex(line) for line in result.stdout.splitlines()]
emitted = [bytes.fromhex(line) for line in result.stdout.splitlines()]
cls.pages = [page for page in emitted if page[:4] == b"MGR3" and int.from_bytes(page[20:24], "little") == 42]
cls.mgr2_pages = [page for page in emitted if page[:4] == b"MGR2"]
cls.legacy_pages = [page for page in emitted if page[:4] == b"MGR1"]
cls.empty_event_pages = [page for page in emitted if page[:4] == b"MGR3" and int.from_bytes(page[20:24], "little") == 43]
# Compile the unchanged production runtime and transaction writer with
# a memory filesystem/radio in place of the hardware-facing headers.
fixture = ROOT / "test/fixtures/management"
@@ -61,24 +65,180 @@ class ManagementTests(unittest.TestCase):
self.assertEqual(decoded["sequence"], 42)
self.assertEqual(len(decoded["acl"]), 36)
self.assertEqual(decoded["firmware_version"], "1.17.1.5")
self.assertEqual(decoded["protocol"], "MGR3")
self.assertEqual(len(self.pages), 9)
usb = decoded["usb_logging"]
self.assertTrue(usb["supported"] and usb["recovery_deferred"])
self.assertTrue(usb["host_connected"] and usb["reader_connected"])
self.assertTrue(usb["logger_active"])
self.assertEqual(usb["stage"], 2)
self.assertEqual(usb["backoff_step"], 8)
self.assertEqual(usb["retry_seconds"], 604800)
self.assertEqual(usb["inactive_seconds"], 0xffffffff)
self.assertTrue(usb["watchdog_auto"])
self.assertEqual(usb["auto_connected_seconds"], 1209600)
self.assertEqual(decoded["usb_watchdog_last"], dict(
reasons=15, reason_names=["host-absent", "reader-absent", "tx-stalled", "client-inactive"],
action_code=3, action="reboot-requested", persisted=True,
epoch=1700000001, uptime_seconds=777, sequence=42))
self.assertNotIn("manifest_id", decoded)
def test_legacy_report_still_decodes_without_inventing_usb_status(self):
decoded = report.decode_report(self.legacy_pages, "management test password")
self.assertEqual(decoded["protocol"], "MGR1")
self.assertEqual(len(decoded["acl"]), 36)
self.assertIsNone(decoded["usb_logging"])
self.assertIsNone(decoded["usb_watchdog_last"])
with self.assertRaises(ValueError):
report.decode_report([self.legacy_pages[0]] + self.pages[1:], "management test password")
def test_mgr2_crypto_still_decodes_without_inventing_event(self):
decoded = report.decode_report(self.mgr2_pages, "management test password")
self.assertEqual(decoded["protocol"], "MGR2")
self.assertEqual(len(decoded["acl"]), 36)
self.assertEqual(len(self.mgr2_pages), 8)
self.assertEqual(decoded["usb_logging"]["retry_seconds"], 604800)
self.assertIsNone(decoded["usb_watchdog_last"])
def test_current_none_event_with_empty_acl_is_authenticated(self):
decoded = report.decode_report(self.empty_event_pages, "management test password")
self.assertEqual(decoded["protocol"], "MGR3")
self.assertEqual(decoded["acl"], [])
self.assertIsNone(decoded["usb_watchdog_last"])
def test_current_reports_decode_in_browser_with_real_production_ciphertext(self):
node = shutil.which("node")
self.assertIsNotNone(node, "node is required for cross-language management decoding")
script = """
const assert = require('assert');
globalThis.crypto = require('crypto').webcrypto;
const decoder = require(process.argv[1]);
let data = '';
process.stdin.on('data', chunk => { data += chunk; });
process.stdin.on('end', async () => {
try {
const captures = JSON.parse(data);
const pages = captures.MGR3;
for (const magic of ['MGR1', 'MGR2']) {
const legacy = await decoder.decodeManagement(captures[magic].join('\\n'), 'management test password');
assert(legacy.authenticated && legacy.complete);
assert.strictEqual(legacy.acl.length, 36);
assert.strictEqual(legacy.public.protocol, magic);
assert.strictEqual(legacy.pageCount, magic === 'MGR1' ? 6 : 8);
assert.strictEqual(legacy.public.usbWatchdogLast, null);
assert.strictEqual(legacy.public.usbLogging === null, magic === 'MGR1');
}
const empty = await decoder.decodeManagement(captures.empty.join('\\n'), 'management test password');
assert(empty.authenticated && empty.complete && !empty.acl.length);
assert.strictEqual(empty.public.protocol, 'MGR3');
assert.strictEqual(empty.public.usbWatchdogLast, null);
const result = await decoder.decodeManagement(pages.join('\\n'), 'management test password');
assert(result.authenticated && result.complete);
assert.strictEqual(result.acl.length, 36);
assert.strictEqual(result.pageCount, 9);
assert.strictEqual(result.public.protocol, 'MGR3');
assert.strictEqual(result.public.usbLogging.retrySeconds, 604800);
assert.strictEqual(result.public.usbLogging.inactiveSeconds, 0xffffffff);
assert.strictEqual(result.public.usbLogging.hostConnected, true);
assert.strictEqual(result.public.usbLogging.readerConnected, true);
assert.strictEqual(result.public.usbLogging.loggerActive, true);
assert.strictEqual(result.public.usbLogging.watchdogAuto, true);
assert.strictEqual(result.public.usbLogging.autoConnectedSeconds, 1209600);
assert.strictEqual(result.public.usbWatchdogLast.epoch, 1700000001);
assert.strictEqual(result.public.usbWatchdogLast.uptimeSeconds, 777);
assert.strictEqual(result.public.usbWatchdogLast.sequence, 42);
assert.strictEqual(result.public.usbWatchdogLast.reasons, 15);
assert.strictEqual(result.public.usbWatchdogLast.action, 'reboot-requested');
assert.strictEqual(result.public.usbWatchdogLast.persisted, true);
const raw = pages.map(page => '1900' + page + '00'.repeat((3 + Math.ceil((page.length / 2 - 3) / 16) * 16) - page.length / 2));
const routed = await decoder.decodeManagement(raw.join('\\n'), 'management test password');
assert(routed.authenticated && routed.complete);
assert.strictEqual(routed.public.envelope.route, 'flood');
const mutated = Buffer.from(pages[0], 'hex'); mutated[87] ^= 1;
await assert.rejects(decoder.decodeManagement(mutated.toString('hex'), 'management test password'), /Password is wrong/);
const changedLogger = Buffer.from(pages[0], 'hex'); changedLogger[84] ^= 4;
await assert.rejects(decoder.decodeManagement(changedLogger.toString('hex'), 'management test password'), /Password is wrong/);
for (let offset = 98; offset < 111; ++offset) {
const changedEvent = Buffer.from(pages[0], 'hex'); changedEvent[offset] ^= 1;
await assert.rejects(decoder.decodeManagement(changedEvent.toString('hex'), 'management test password'), /Password is wrong|Invalid/);
}
const changedSnapshot = Buffer.from(pages[1], 'hex'); changedSnapshot[99] ^= 1;
await assert.rejects(decoder.decodeManagement(pages[0] + '\\n' + changedSnapshot.toString('hex'), ''), /same snapshot/);
} catch (error) { console.error(error); process.exitCode = 1; }
});
"""
import json
subprocess.run([node, "-e", script, str(ROOT / "docs/_javascript/management_decoder.js")],
input=json.dumps({"MGR3": [page.hex() for page in self.pages],
"MGR2": [page.hex() for page in self.mgr2_pages],
"MGR1": [page.hex() for page in self.legacy_pages],
"empty": [page.hex() for page in self.empty_event_pages]}),
text=True, check=True, timeout=30)
def test_runtime_storage_scheduling_rollover_and_failures(self):
subprocess.run([str(self.runtime)], check=True)
def test_tampering_and_wrong_password(self):
for position in (4, 20, 28, 74, 76, 83, -1):
for position in (4, 20, 28, 74, 76, 83, 85, 86, 90, 94, *range(98, 111), 111, -1):
changed = bytearray(self.pages[0]); changed[position] ^= 1
with self.subTest(position=position), self.assertRaises(ValueError):
report.decode_report([bytes(changed)] + self.pages[1:], "management test password")
with self.assertRaises(ValueError):
report.decode_report(self.pages, "incorrect management password")
def test_logger_activity_is_authenticated_separately_from_connection(self):
changed = bytearray(self.pages[0]); changed[84] ^= 4
public = report._usb_status(changed)
self.assertTrue(public["host_connected"] and public["reader_connected"])
self.assertFalse(public["logger_active"])
with self.assertRaises(ValueError):
report.decode_page(bytes(changed), "management test password")
def test_page_omission_duplicate_mixed(self):
for pages in ([], self.pages[:-1], self.pages[1:] + [self.pages[1]], self.pages + [self.pages[0]]):
with self.assertRaises(ValueError):
report.decode_report(pages, "management test password")
def test_current_size_page_bounds_and_unknown_version_are_rejected(self):
for offset, value in ((3, ord("4")), (79, 8), (81, 6), (82, 5), (80, 37), (84, 8), (85, 0xc0)):
changed = bytearray(self.pages[0]); changed[offset] = value
with self.subTest(offset=offset), self.assertRaises(ValueError):
report.decode_page(bytes(changed), "management test password")
for payload in (self.pages[0][:-1], self.pages[0] + b"\x00", self.pages[0][:99]):
with self.assertRaises(ValueError):
report.decode_page(payload, "management test password")
def test_event_validation_and_advisory_zero_time(self):
for code in (0x80, 0x8f, 0xdf, 0xef, 0xff):
changed = bytearray(self.pages[0]); changed[98] = code
with self.subTest(code=code), self.assertRaises(ValueError):
report.decode_page(bytes(changed), "management test password")
changed = bytearray(self.pages[0]); changed[107:111] = bytes(4)
with self.assertRaises(ValueError):
report.decode_page(bytes(changed), "management test password")
changed = bytearray(self.pages[0]); changed[99:103] = bytes(4)
self.assertEqual(report._watchdog_event(changed)["epoch"], 0)
for action in range(1, 5):
changed[98] = 15 | (action << 4)
event = report._watchdog_event(changed)
self.assertEqual(event["action_code"], action)
self.assertFalse(event["persisted"])
def test_authenticated_event_mismatch_is_not_one_snapshot(self):
from Crypto.Cipher import AES
page = self.pages[1]
key = report.derive(report.password_key("management test password"), "MeshCore-MGR1-SIV", page[4:20])
cipher = AES.new(key, AES.MODE_SIV); cipher.update(page[:111])
private = cipher.decrypt_and_verify(page[111:-16], page[-16:])
header = bytearray(page[:111]); header[99] ^= 1
cipher = AES.new(key, AES.MODE_SIV); cipher.update(header)
ciphertext, tag = cipher.encrypt_and_digest(private)
changed = bytes(header) + ciphertext + tag
self.assertEqual(report.decode_page(changed, "management test password"),
report.decode_page(page, "management test password"))
with self.assertRaisesRegex(ValueError, "mixed report snapshots"):
report.decode_report([self.pages[0], changed] + self.pages[2:], "management test password")
def test_fingerprints_and_password_length(self):
a = report.fingerprint("management test password", bytes(16), bytes(32))
b = report.fingerprint("management test password", bytes([1]) + bytes(15), bytes(32))
@@ -111,7 +271,8 @@ class ManagementTests(unittest.TestCase):
padded = page + bytes(3 + ((len(page) - 3 + 15) // 16) * 16 - len(page))
message = dict(type="PACKET", direction="rx", raw=(b"\x19\x00" + padded).hex())
self.assertEqual(report.mqtt_payload(message), page)
message["raw"] = (b"\x19\x00" + padded[:-1] + b"\x01").hex()
malformed = padded[:-1] + b"\x01" if len(padded) > len(page) else padded + b"\x00"
message["raw"] = (b"\x19\x00" + malformed).hex()
self.assertIsNone(report.mqtt_payload(message))
@@ -118,6 +118,7 @@ bool canAccess(void* opaque) {
CapacityStream logging_port;
mesh::WholeRecordNonBlockingStream<> bounded_logging_port(logging_port);
bool logging_enabled = true;
bool debug_enabled = true;
} // namespace
@@ -131,6 +132,10 @@ bool isUsbLoggingEnabled() {
return logging_enabled;
}
bool isUsbDebugEnabled() {
return debug_enabled;
}
Stream& usbLoggingPort() {
return bounded_logging_port;
}
@@ -141,6 +146,7 @@ class Nrf52DebugOutputTest : public testing::Test {
protected:
void SetUp() override {
logging_enabled = true;
debug_enabled = true;
logging_port.reset(256);
}
};
@@ -196,6 +202,16 @@ TEST_F(Nrf52DebugOutputTest, DisabledLoggingDoesNotTouchThePort) {
EXPECT_EQ(logging_port.write_calls_, 0);
}
TEST_F(Nrf52DebugOutputTest, PacketOutputDoesNotEnableDebugVerbosity) {
debug_enabled = false;
MESH_DEBUG_PRINTLN("quiet");
BRIDGE_DEBUG_PRINTLN("quiet");
POWERSAVING_DEBUG_PRINTLN("quiet");
EXPECT_EQ(logging_port.available_calls_, 0);
EXPECT_EQ(logging_port.write_calls_, 0);
EXPECT_TRUE(mesh::isUsbLoggingEnabled());
}
TEST_F(Nrf52DebugOutputTest, ReentrantDebugOutputIsDroppedWithoutWaiting) {
logging_port.reenter_on_write_ = true;
+15 -3
View File
@@ -55,6 +55,10 @@ static uint32_t baud = 115200;
static void tud_cdc_get_line_coding(cdc_line_coding_t* coding) { coding->bit_rate = baud; }
static void TinyUSB_Port_EnterDFU() { dfu = true; }
namespace mesh {
static std::atomic<bool> usb_logging_tx_waiting{false};
static bool isUsbLoggingEnabled() { return true; }
static void clearUsbLoggingClientActivity() {}
@ATTEMPT@
@GATES@
static uint32_t primary_usb_terminal_taken_reset_generation = 0;
@WRITE@
@@ -152,6 +156,7 @@ int main() {
class Nrf52UsbClientTest(unittest.TestCase):
def test_stop_token_reports_shared_logging_conflict(self):
main = (ROOT / 'examples/companion_radio/main.cpp').read_text()
stop = function(main, 'if (strcmp(usb_terminal_line, USB_TERMINAL_STOP_TOKEN) == 0)')
@@ -294,8 +299,10 @@ int main() {
def test_real_usb_callbacks_and_transport_with_both_client_styles(self):
start = USB.index('static std::atomic<uint32_t> primary_usb_reset_generation')
gates = USB[start:USB.index('\n#endif', start)]
end_start = USB.index('static void endPrimaryUsbHostSession(', start)
gates = USB[start:end_start] + function(USB, 'static void endPrimaryUsbHostSession(')
source = HARNESS.replace('@GATES@', gates)
source = source.replace('@ATTEMPT@', function(USB, 'static void noteUsbLoggingTxAttempt('))
for marker, signature in (
('@WRITE@', 'static size_t writeTinyUsbCdcOnce('),
('@COMPLETE@', 'static void completePrimaryUsbSessionReset('),
@@ -308,10 +315,15 @@ int main() {
with tempfile.TemporaryDirectory() as directory:
cpp = Path(directory) / 'usb-client.cpp'
cpp.write_text(source)
for dual in (False, True):
with self.subTest(dedicated_logging=dual):
for dual, ordinary in ((False, False), (True, False), (False, True)):
with self.subTest(dedicated_logging=dual, ordinary_role=ordinary):
binary = Path(directory) / ('usb-client-' + str(dual))
flags = ['-DMESH_DUAL_CDC_LOGGING=1'] if dual else []
tested_source = source
if ordinary:
flags += ['-DUSE_TINYUSB=1']
tested_source = source.replace('#define ENABLE_USB_INTERFACE 1\n', '')
cpp.write_text(tested_source)
subprocess.run(['g++', '-std=c++17', *flags, str(cpp), '-o', str(binary)], check=True)
subprocess.run([str(binary)], check=True)
+42
View File
@@ -0,0 +1,42 @@
"""Compile the complete native nRF52 USB facade with stalled endpoint mocks."""
from pathlib import Path
import os
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = ROOT / 'test/fixtures/nrf52_usb_console'
class Nrf52UsbConsoleTest(unittest.TestCase):
def test_actual_console_and_session_callbacks(self):
compiler = shutil.which('g++') or shutil.which('clang++')
if not compiler:
self.skipTest('a host C++17 compiler is required')
sanitizer = [] if os.name == 'nt' else [
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
'-fno-pie', '-no-pie']
with tempfile.TemporaryDirectory(prefix='nrf52-usb-console-') as directory:
for companion in (False, True):
with self.subTest(companion=companion):
binary = Path(directory) / ('console-' + str(companion))
built = subprocess.run([
compiler, '-std=c++17', '-Wall', '-Wextra', '-Werror',
*sanitizer, '-DARDUINO', '-DNRF52_PLATFORM', '-DUSE_TINYUSB',
'-DMESH_DEBUG=1', *(['-DENABLE_USB_INTERFACE'] if companion else []),
'-I' + str(FIXTURE / 'mocks'), '-I' + str(ROOT / 'src'),
str(FIXTURE / 'test_console.cpp'),
*[str(ROOT / 'src/helpers' / file) for file in (
'UsbLogging.cpp', 'UsbLoggingClientActivity.cpp',
'UsbLoggingLineStateOverride.cpp')],
'-o', str(binary)], capture_output=True, text=True, timeout=60)
self.assertEqual(built.returncode, 0, built.stderr)
tested = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
self.assertEqual(tested.returncode, 0, tested.stderr)
self.assertIn('native nRF52 console transport passed', tested.stdout)
if __name__ == '__main__':
unittest.main()
+1 -1
View File
@@ -294,7 +294,7 @@ class Nrf52UsbLoggingContractTest(unittest.TestCase):
session_end = source[source.index(
"static void endPrimaryUsbHostSession("
):source.index("#endif", source.index(
):source.index("\n}\n#endif", source.index(
"static void endPrimaryUsbHostSession("
))]
self.assertIn("primary_usb_line_state_dtr.exchange(", session_end)
+37 -29
View File
@@ -171,41 +171,49 @@ TEST(ManagementRouting, GroupDataForBothRoutesKeepsPublicHeaderAndLegacyLength)
TraceTestRadio radio; TraceTestClock clock; TraceTestRNG rng; TraceTestRTC rtc;
TraceTestTables tables; StaticPoolPacketManager pool(8);
TraceTestMesh mesh(radio, clock, rng, rtc, pool, tables);
uint8_t raw[mesh::management::HEADER + mesh::management::TAG] = {};
memcpy(raw, "MGR1", 4); raw[79] = 1;
uint8_t raw[mesh::management::CURRENT_HEADER + mesh::management::TAG] = {};
const uint8_t path[] = {0x12, 0xab};
uint8_t scope_key[16]; memset(scope_key, 0x42, sizeof(scope_key));
for (bool flood : {false, true}) {
auto* p = mesh.createRawData(raw, sizeof(raw)); ASSERT_NE(nullptr, p);
ASSERT_TRUE(mesh.sendManagementData(p, flood, path, 2, 2,
flood ? scope_key : nullptr));
EXPECT_EQ(PAYLOAD_TYPE_GRP_DATA, p->getPayloadType());
EXPECT_EQ(flood, p->isRouteFlood());
EXPECT_EQ(0, memcmp(p->payload, raw, sizeof(raw)));
EXPECT_EQ(0u, (p->payload_len - 3u) % 16u);
EXPECT_TRUE(mesh::management::validPage(p->payload, p->payload_len, true));
EXPECT_EQ(flood ? 0 : 2, p->getPathHashCount());
if (!flood) EXPECT_EQ(0, memcmp(p->path, path, 2));
for (const char* magic : {"MGR1", "MGR2", "MGR3"}) {
memset(raw, 0, sizeof(raw));
memcpy(raw, magic, 4); raw[79] = 1;
const size_t size = mesh::management::headerSize(raw) + mesh::management::TAG;
for (bool flood : {false, true}) {
auto* p = mesh.createRawData(raw, size); ASSERT_NE(nullptr, p);
ASSERT_TRUE(mesh.sendManagementData(p, flood, path, 2, 2,
flood ? scope_key : nullptr));
EXPECT_EQ(PAYLOAD_TYPE_GRP_DATA, p->getPayloadType());
EXPECT_EQ(flood, p->isRouteFlood());
EXPECT_EQ(0, memcmp(p->payload, raw, size));
EXPECT_EQ(0u, (p->payload_len - 3u) % 16u);
EXPECT_TRUE(mesh::management::validPage(p->payload, p->payload_len, true));
EXPECT_EQ(flood ? 0 : 2, p->getPathHashCount());
if (!flood) EXPECT_EQ(0, memcmp(p->path, path, 2));
}
}
}
TEST(ManagementRouting, FloodForwardingDoesNotNeedAKeyAndStillHonorsFilters) {
TraceTestRadio radio; TraceTestClock clock; TraceTestRNG rng; TraceTestRTC rtc;
TraceTestTables tables; StaticPoolPacketManager pool(8);
TraceTestMesh mesh(radio, clock, rng, rtc, pool, tables);
mesh.forwardFloods = true;
mesh::Packet p;
p.header = (PAYLOAD_TYPE_GRP_DATA << PH_TYPE_SHIFT) | ROUTE_TYPE_FLOOD;
p.setPathHashSizeAndCount(1, 0);
p.payload_len = mesh::management::floodSize(mesh::management::HEADER + mesh::management::TAG);
memset(p.payload, 0, p.payload_len);
memcpy(p.payload, "MGR1", 4);
p.payload[79] = 1; // one page containing zero ACL entries
ASSERT_TRUE(mesh::management::validPage(p.payload, p.payload_len, true));
EXPECT_NE(ACTION_RELEASE, mesh.receivePacket(&p));
EXPECT_FALSE(mesh.groupPacketObserved); // never delivered as decrypted channel data
mesh.rejectFloods = true;
EXPECT_EQ(ACTION_RELEASE, mesh.receivePacket(&p));
for (const char* magic : {"MGR1", "MGR2", "MGR3"}) {
TraceTestRadio radio; TraceTestClock clock; TraceTestRNG rng; TraceTestRTC rtc;
TraceTestTables tables; StaticPoolPacketManager pool(8);
TraceTestMesh mesh(radio, clock, rng, rtc, pool, tables);
mesh.forwardFloods = true;
mesh::Packet p;
p.header = (PAYLOAD_TYPE_GRP_DATA << PH_TYPE_SHIFT) | ROUTE_TYPE_FLOOD;
p.setPathHashSizeAndCount(1, 0);
memset(p.payload, 0, sizeof(p.payload));
memcpy(p.payload, magic, 4);
p.payload_len = mesh::management::floodSize(
mesh::management::headerSize(p.payload) + mesh::management::TAG);
memset(p.payload + 4, 0, p.payload_len - 4);
p.payload[79] = 1; // one page containing zero ACL entries
ASSERT_TRUE(mesh::management::validPage(p.payload, p.payload_len, true));
EXPECT_NE(ACTION_RELEASE, mesh.receivePacket(&p));
EXPECT_FALSE(mesh.groupPacketObserved); // never delivered as decrypted channel data
mesh.rejectFloods = true;
EXPECT_EQ(ACTION_RELEASE, mesh.receivePacket(&p));
}
}
class RetryCodingRateRadio : public TraceTestRadio {
+195
View File
@@ -0,0 +1,195 @@
"""Exercise the real RAM-only USB stats lease and bounded CDC input parser."""
from pathlib import Path
import os
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
MOCKS = ROOT / 'test' / 'fixtures' / 'usb_logging_watchdog' / 'mocks'
HARNESS = r'''
#include <Arduino.h>
#include <helpers/UsbLoggingClientActivity.h>
#include <cassert>
#include <cstdio>
#include <initializer_list>
#include <string>
using Parser = mesh::UsbLoggingStatsLineParser;
constexpr uint32_t Lease = mesh::USB_LOGGING_CLIENT_LEASE_MS;
static unsigned consume(Parser& parser, const std::string& input) {
unsigned matched = 0;
for (const char byte : input) if (parser.consume(byte)) ++matched;
return matched;
}
static void check_exact_stats_command_recognition() {
assert(Lease == 15U * 60U * 1000U);
assert(!mesh::isUsbLoggingStatsCommand(nullptr));
for (const char* command : {"stats-core", "stats-radio", "stats-packets",
" \tstats-core\t ", "stats-radio\t", " stats-packets"}) {
assert(mesh::isUsbLoggingStatsCommand(command));
}
for (const char* command : {"", " ", "stats", "stats-core2", "stats-radio-diag",
"stats-packets suffix", "STATS-CORE", "get name",
"get usb.watchdog", "usb.logger heartbeat", "stats-core\r",
"stats-core\n", "stats-core\b", "stats-cor", "stats-core;"}) {
assert(!mesh::isUsbLoggingStatsCommand(command));
}
}
static void check_default_expiry_and_renewal() {
mock_watchdog_millis = 0;
mesh::clearUsbLoggingClientActivity();
assert(!mesh::isUsbLoggingClientActive());
for (const char* command : {"stats-core", "stats-radio", "stats-packets"}) {
mock_watchdog_millis = 100;
mesh::noteUsbLoggingStatsCommand(command);
assert(mesh::isUsbLoggingClientActive());
mock_watchdog_millis = 100 + Lease - 1;
assert(mesh::isUsbLoggingClientActive());
mock_watchdog_millis = 100 + Lease;
assert(!mesh::isUsbLoggingClientActive());
assert(!mesh::isUsbLoggingClientActive());
}
mesh::clearUsbLoggingClientActivity();
for (uint32_t cycle = 0; cycle != 100; ++cycle) {
mock_watchdog_millis = cycle * 300000U;
mesh::noteUsbLoggingStatsCommand("stats-core");
assert(mesh::isUsbLoggingClientActive());
mock_watchdog_millis += 300000U - 1;
assert(mesh::isUsbLoggingClientActive());
}
const auto last = 99U * 300000U;
mock_watchdog_millis = last + Lease - 1;
assert(mesh::isUsbLoggingClientActive());
mock_watchdog_millis = last + Lease;
assert(!mesh::isUsbLoggingClientActive());
// Expiration is latched. Multiple millis rollovers cannot revive an old poll.
for (uint64_t day = 1; day <= 150; ++day) {
mock_watchdog_millis = uint32_t(uint64_t(last) + day * 86400000ULL);
assert(!mesh::isUsbLoggingClientActive());
}
mock_watchdog_millis = last; // Exactly the old uint32 clock value after wrap.
assert(!mesh::isUsbLoggingClientActive());
}
static void check_invalid_input_never_extends_or_starts_a_lease() {
for (const char* command : {"stats-core extra", "stats-radio-diag", "ver", "",
"usb.logger heartbeat", "stats-packets\n"}) {
mock_watchdog_millis = 1000;
mesh::clearUsbLoggingClientActivity();
mesh::noteUsbLoggingStatsCommand(command);
assert(!mesh::isUsbLoggingClientActive());
mesh::noteUsbLoggingStatsCommand("stats-radio");
mock_watchdog_millis = 1000 + Lease - 1;
mesh::noteUsbLoggingStatsCommand(command);
assert(mesh::isUsbLoggingClientActive());
++mock_watchdog_millis;
assert(!mesh::isUsbLoggingClientActive());
}
mesh::noteUsbLoggingStatsCommand(nullptr);
assert(!mesh::isUsbLoggingClientActive());
}
static void check_session_clear_and_clock_wrap() {
mock_watchdog_millis = UINT32_MAX - 1000;
mesh::clearUsbLoggingClientActivity();
mesh::noteUsbLoggingStatsCommand(" \tstats-packets\t ");
const uint32_t started = mock_watchdog_millis;
mock_watchdog_millis = started + Lease - 1;
assert(mesh::isUsbLoggingClientActive());
mock_watchdog_millis = started + Lease;
assert(!mesh::isUsbLoggingClientActive());
mesh::noteUsbLoggingStatsCommand("stats-core");
assert(mesh::isUsbLoggingClientActive());
mesh::clearUsbLoggingClientActivity();
assert(!mesh::isUsbLoggingClientActive());
mesh::noteUsbLoggingStatsCommand("stats-radio");
assert(mesh::isUsbLoggingClientActive());
for (unsigned clear = 0; clear != 10000; ++clear) {
mesh::clearUsbLoggingClientActivity();
assert(!mesh::isUsbLoggingClientActive());
}
mesh::noteUsbLoggingStatsCommand("stats-core");
assert(mesh::isUsbLoggingClientActive());
// Deadline zero is reserved. This rare rollover expires only one ms early.
mesh::clearUsbLoggingClientActivity();
mock_watchdog_millis = uint32_t(0U - Lease);
const uint32_t zero_deadline_start = mock_watchdog_millis;
mesh::noteUsbLoggingStatsCommand("stats-core");
mock_watchdog_millis = zero_deadline_start + Lease - 2;
assert(mesh::isUsbLoggingClientActive());
mock_watchdog_millis = zero_deadline_start + Lease - 1;
assert(!mesh::isUsbLoggingClientActive());
mock_watchdog_millis = 0;
assert(!mesh::isUsbLoggingClientActive());
}
static void check_bounded_logging_endpoint_line_parser() {
Parser parser;
assert(consume(parser, "stats-core\r\n") == 1);
assert(consume(parser, "stats-radio\n") == 1);
assert(consume(parser, "stats-packets\r") == 1);
assert(consume(parser, "stats-core") == 0);
assert(consume(parser, "\n") == 1);
assert(consume(parser, " \tstats-radio \t\r\n") == 1);
assert(consume(parser, "stats-radio-diag\r\n") == 0);
assert(consume(parser, "stats-packets suffix\r\n") == 0);
assert(consume(parser, "\r\n\r\n") == 0);
assert(consume(parser, "stats-core") == 0);
parser.reset();
assert(consume(parser, "\n") == 0);
assert(consume(parser, std::string(21, ' ') + "stats-core\n") == 1); // 31 bytes.
assert(consume(parser, std::string(22, ' ') + "stats-core\n") == 0); // 32 bytes.
assert(consume(parser, std::string(1048576, 'x') + "stats-core\n") == 0);
assert(consume(parser, "stats-core\n") == 1); // Recover after rejected line.
assert(consume(parser, std::string("stats-core\0suffix\n", 18)) == 0);
assert(consume(parser, "stats-core\n") == 1);
assert(consume(parser, std::string(1, '\0') + "stats-core\n") == 0);
assert(consume(parser, "stats-core\n") == 1);
assert(consume(parser, "stats-core\b\n") == 0);
assert(consume(parser, "stats-core\x7f\n") == 0);
}
int main() {
check_exact_stats_command_recognition();
check_default_expiry_and_renewal();
check_invalid_input_never_extends_or_starts_a_lease();
check_session_clear_and_clock_wrap();
check_bounded_logging_endpoint_line_parser();
puts("USB stats client activity lease passed");
}
'''
class UsbLoggingClientActivityTest(unittest.TestCase):
def test_actual_client_activity_and_parser(self):
compiler = os.environ.get('CXX') or shutil.which('g++') or shutil.which('clang++')
if compiler is None:
self.skipTest('a host C++17 compiler is required')
sanitizer_flags = [] if os.name == 'nt' else [
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
'-fno-pie', '-no-pie']
with tempfile.TemporaryDirectory(prefix='usb-logging-client-') as directory:
source = Path(directory) / 'test.cpp'
binary = Path(directory) / 'test.exe'
source.write_text(HARNESS, encoding='utf-8')
built = subprocess.run([
compiler, '-std=c++17', '-Wall', '-Wextra', '-Werror', '-DARDUINO',
'-DESP32', '-DARDUINO_USB_MODE=0', '-DARDUINO_USB_CDC_ON_BOOT=1',
*sanitizer_flags, '-I' + str(MOCKS), '-I' + str(ROOT / 'src'),
str(source), str(ROOT / 'src' / 'helpers' / 'UsbLoggingClientActivity.cpp'),
'-o', str(binary)], capture_output=True, text=True, timeout=60)
self.assertEqual(built.returncode, 0, built.stderr)
tested = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
self.assertEqual(tested.returncode, 0, tested.stderr)
self.assertIn('USB stats client activity lease passed', tested.stdout)
if __name__ == '__main__':
unittest.main()
+501
View File
@@ -0,0 +1,501 @@
"""Compile the real USB watchdog policy and exercise its bounded timelines."""
from pathlib import Path
import os
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
HARNESS = r'''
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <initializer_list>
#include <limits>
#include <helpers/UsbLoggingWatchdogPolicy.h>
using Policy = mesh::UsbLoggingWatchdogPolicy;
using Action = Policy::Action;
using Observation = mesh::UsbLoggingObservation;
using AutoArm = mesh::UsbLoggingAutoArmPolicy;
struct Clock {
uint64_t elapsed = 0;
uint32_t initial = 0;
explicit Clock(uint32_t first = 0) : initial(first) {}
uint32_t now() const { return uint32_t(uint64_t(initial) + elapsed); }
void advance(uint64_t duration) { elapsed += duration; }
};
Observation missing() { return {true, false, false, false, 0}; }
Observation idle() { return {true, true, true, false, 0}; }
Observation pending() { return {true, true, true, true, 0}; }
static void check_intervals_and_corruption() {
const unsigned hours[] = {1, 2, 4, 8, 16, 32, 64, 128, 168};
for (uint8_t step = 0; step <= Policy::MAX_STEP; ++step) {
assert(Policy::intervalMs(step) == hours[step] * Policy::BASE_MS);
assert(Policy::nextStep(step) == (step == 8 ? 8 : step + 1));
Policy policy;
policy.begin(0, step);
assert(policy.update(0, true, missing()) == Action::None);
const auto timeout = Policy::intervalMs(step);
assert(policy.update(Policy::EARLY_RECOVERY_MS - 1, true, missing()) == Action::None);
assert(policy.update(Policy::EARLY_RECOVERY_MS, true, missing()) == Action::SoftRecovery);
policy.actionAttempted(Action::SoftRecovery);
assert(policy.update(Policy::EARLY_RECOVERY_MS + Policy::RECOVERY_GRACE_MS - 1,
true, missing()) == Action::None);
assert(policy.update(Policy::EARLY_RECOVERY_MS + Policy::RECOVERY_GRACE_MS,
true, missing()) == Action::Reenumerate);
policy.actionAttempted(Action::Reenumerate);
assert(policy.update(timeout - 1, true, missing()) == Action::None);
assert(policy.update(timeout, true, missing()) == Action::Reboot);
assert(policy.backoffStep() == step); // An action never persists a tier.
assert(policy.inactiveSeconds() == timeout / 1000);
}
for (uint8_t corrupt : {uint8_t(9), uint8_t(127), uint8_t(255)}) {
Policy policy;
policy.begin(0, corrupt);
assert(policy.backoffStep() == 8);
assert(Policy::intervalMs(corrupt) == Policy::MAX_MS);
assert(Policy::nextStep(corrupt) == 8);
assert(policy.update(0, true, missing()) == Action::None);
assert(policy.update(Policy::EARLY_RECOVERY_MS - 1, true, missing()) == Action::None);
assert(policy.update(Policy::MAX_MS - 1, true, missing()) == Action::SoftRecovery);
assert(policy.update(Policy::MAX_MS, true, missing()) == Action::SoftRecovery);
policy.backoffCommitted(corrupt);
assert(policy.backoffStep() == 8);
}
}
static void check_missing_unopened_and_unplugged() {
for (const auto observation : {missing(), Observation{true, true, false, false, 0},
Observation{true, false, true, false, 0}}) {
Policy policy;
policy.begin(500, 0);
assert(policy.update(500, true, observation) == Action::None);
for (uint32_t elapsed = 100; elapsed < Policy::BASE_MS; elapsed += 100) {
assert(policy.update(500 + elapsed, true, observation)
== (elapsed < Policy::EARLY_RECOVERY_MS ? Action::None : Action::SoftRecovery));
}
assert(policy.update(500 + Policy::BASE_MS, true, observation) == Action::SoftRecovery);
assert(policy.inactiveSeconds() == 3600);
}
Policy policy;
policy.begin(0, 0);
assert(policy.update(0, true, idle()) == Action::None);
auto unplugged = missing();
assert(policy.update(1000, true, unplugged) == Action::None);
assert(policy.update(1000 + Policy::EARLY_RECOVERY_MS - 1, true, unplugged) == Action::None);
assert(policy.update(1000 + Policy::BASE_MS, true, unplugged) == Action::SoftRecovery);
policy.actionAttempted(Action::SoftRecovery);
assert(policy.update(1001 + Policy::BASE_MS, true, idle()) == Action::None);
assert(policy.stage() == 0 && policy.inactiveSeconds() == 0);
}
static void check_idle_and_pending_stall() {
Policy policy;
policy.begin(0, 0);
for (uint32_t day = 0; day <= 80; ++day) {
assert(policy.update(day * 86400000U, true, idle()) == Action::None);
assert(!policy.stalled() && policy.inactiveSeconds() == 0);
}
Clock clock;
Policy stalled;
stalled.begin(clock.now(), 0);
auto observation = pending();
assert(stalled.update(clock.now(), true, observation) == Action::None);
clock.advance(Policy::STALL_OBSERVE_MS - 1);
assert(stalled.update(clock.now(), true, observation) == Action::None);
assert(!stalled.stalled());
clock.advance(1);
assert(stalled.update(clock.now(), true, observation) == Action::None);
assert(stalled.stalled() && stalled.inactiveSeconds() == 30);
clock.advance(Policy::EARLY_RECOVERY_MS - Policy::STALL_OBSERVE_MS - 1);
assert(stalled.update(clock.now(), true, observation) == Action::None);
clock.advance(1);
assert(stalled.update(clock.now(), true, observation) == Action::SoftRecovery);
stalled.actionAttempted(Action::SoftRecovery);
assert(stalled.stage() == 1 && stalled.stalled());
// Destructive purge can remove demand, but cannot fake completion evidence.
observation.pending = false;
clock.advance(1);
assert(stalled.update(clock.now(), true, observation) == Action::None);
assert(stalled.stalled() && stalled.stage() == 1);
clock.advance(Policy::RECOVERY_GRACE_MS - 1);
assert(stalled.update(clock.now(), true, observation) == Action::Reenumerate);
stalled.actionAttempted(Action::Reenumerate);
clock.advance(Policy::RECOVERY_GRACE_MS - 1);
assert(stalled.update(clock.now(), true, observation) == Action::None);
clock.advance(1);
assert(stalled.update(clock.now(), true, observation) == Action::None);
clock.advance(Policy::BASE_MS - clock.elapsed - 1);
assert(stalled.update(clock.now(), true, observation) == Action::None);
clock.advance(1);
assert(stalled.update(clock.now(), true, observation) == Action::Reboot);
assert(stalled.backoffStep() == 0); // Caller still must persist nextStep().
}
static void check_stages_and_deferred_retries() {
Policy policy;
Clock clock;
auto observation = missing();
policy.begin(clock.now(), 0);
assert(policy.update(clock.now(), true, observation) == Action::None);
clock.advance(Policy::EARLY_RECOVERY_MS);
assert(policy.update(clock.now(), true, observation, false) == Action::None);
assert(policy.deferred() && policy.stage() == 0);
clock.advance(1000);
assert(policy.update(clock.now(), true, observation, false) == Action::None);
assert(policy.update(clock.now(), true, observation, true) == Action::SoftRecovery);
// Backend Deferred: leave action unnoted and retry it without timer extension.
clock.advance(1);
assert(policy.update(clock.now(), true, observation) == Action::SoftRecovery);
policy.actionAttempted(Action::SoftRecovery);
assert(policy.stage() == 1);
clock.advance(Policy::RECOVERY_GRACE_MS - 1);
assert(policy.update(clock.now(), true, observation) == Action::None);
clock.advance(1);
assert(policy.update(clock.now(), true, observation, false) == Action::None);
assert(policy.deferred() && policy.stage() == 1);
assert(policy.update(clock.now(), true, observation) == Action::Reenumerate);
clock.advance(1);
assert(policy.update(clock.now(), true, observation) == Action::Reenumerate);
policy.actionAttempted(Action::Reenumerate);
assert(policy.stage() == 2);
clock.advance(Policy::RECOVERY_GRACE_MS - 1);
assert(policy.update(clock.now(), true, observation) == Action::None);
clock.advance(1);
assert(policy.update(clock.now(), true, observation, false) == Action::None);
assert(policy.deferred() && policy.backoffStep() == 0);
assert(policy.update(clock.now(), true, observation) == Action::None);
clock.advance(Policy::BASE_MS - clock.elapsed);
assert(policy.update(clock.now(), true, observation) == Action::Reboot);
clock.advance(1);
assert(policy.update(clock.now(), true, observation) == Action::Reboot);
policy.backoffCommitted(Policy::nextStep(policy.backoffStep()));
assert(policy.backoffStep() == 1);
policy.actionAttempted(Action::Reboot);
assert(policy.stage() == 3);
}
static void check_progress_aborts_and_counter_wrap() {
for (unsigned stage = 1; stage <= 2; ++stage) {
Policy policy;
Clock clock;
auto observation = pending();
policy.begin(clock.now(), 0);
assert(policy.update(clock.now(), true, observation) == Action::None);
clock.advance(Policy::BASE_MS);
assert(policy.update(clock.now(), true, observation) == Action::SoftRecovery);
policy.actionAttempted(Action::SoftRecovery);
if (stage == 2) {
clock.advance(Policy::RECOVERY_GRACE_MS);
assert(policy.update(clock.now(), true, observation) == Action::Reenumerate);
policy.actionAttempted(Action::Reenumerate);
}
observation.pending = false;
clock.advance(1);
assert(policy.update(clock.now(), true, observation) == Action::None);
assert(policy.stage() == stage && policy.stalled());
++observation.tx_progress; // Only the backend's actual ACK advances this.
assert(policy.update(clock.now(), true, observation, false) == Action::None);
assert(policy.stage() == 0 && !policy.stalled() && policy.inactiveSeconds() == 0);
}
Policy policy;
auto observation = pending();
observation.tx_progress = UINT32_MAX;
policy.begin(0, 0);
policy.update(0, true, observation);
policy.update(Policy::STALL_OBSERVE_MS, true, observation);
assert(policy.stalled());
observation.tx_progress = 0;
assert(policy.update(Policy::STALL_OBSERVE_MS + 1, true, observation) == Action::None);
assert(!policy.stalled() && policy.inactiveSeconds() == 0);
assert(policy.update(Policy::STALL_OBSERVE_MS * 2, true, observation) == Action::None);
assert(!policy.stalled());
}
static void check_healthy_reset_requires_continuity_and_commit() {
Policy policy;
policy.begin(0, 4);
assert(policy.update(0, true, idle()) == Action::None);
assert(policy.update(Policy::HEALTHY_RESET_MS - 1, true, idle()) == Action::None);
assert(policy.update(Policy::HEALTHY_RESET_MS, true, idle(), false) == Action::None);
assert(policy.deferred() && policy.backoffStep() == 4);
assert(policy.update(Policy::HEALTHY_RESET_MS, true, idle()) == Action::ResetBackoff);
assert(policy.backoffStep() == 4); // Persistence not acknowledged yet.
assert(policy.update(Policy::HEALTHY_RESET_MS + 1, true, idle()) == Action::ResetBackoff);
policy.backoffCommitted(0);
assert(policy.update(Policy::HEALTHY_RESET_MS + 2, true, idle()) == Action::None);
policy.begin(0, 7);
assert(policy.update(0, true, idle()) == Action::None);
assert(policy.update(300000, true, missing()) == Action::None);
assert(policy.update(400000, true, idle()) == Action::None);
assert(policy.update(400000 + Policy::HEALTHY_RESET_MS - 1, true, idle()) == Action::None);
assert(policy.update(400000 + Policy::HEALTHY_RESET_MS, true, idle()) == Action::ResetBackoff);
}
static void check_disabled_unsupported_and_reenable() {
Policy policy;
policy.begin(0, 2);
assert(policy.update(0, true, missing()) == Action::None);
assert(policy.update(Policy::BASE_MS * 2, false, missing()) == Action::None);
assert(policy.inactiveSeconds() == 0 && policy.stage() == 0);
const auto start = Policy::BASE_MS * 2 + 1;
assert(policy.update(start, true, missing()) == Action::None);
assert(policy.update(start + Policy::EARLY_RECOVERY_MS - 1, true, missing()) == Action::None);
assert(policy.update(start + Policy::EARLY_RECOVERY_MS, true, missing()) == Action::SoftRecovery);
assert(policy.backoffStep() == 2);
policy.actionAttempted(Action::SoftRecovery);
assert(policy.update(start + Policy::BASE_MS * 4 + 1, false, missing()) == Action::None);
assert(policy.stage() == 0 && !policy.stalled() && !policy.deferred());
Observation unsupported;
policy.begin(0, 0);
assert(policy.update(0, true, unsupported) == Action::None);
assert(policy.update(Policy::MAX_MS, true, unsupported) == Action::None);
assert(policy.inactiveSeconds() == 0);
assert(policy.update(Policy::MAX_MS + 1, true, missing()) == Action::None);
assert(policy.update(Policy::MAX_MS + Policy::EARLY_RECOVERY_MS, true, missing()) == Action::None);
assert(policy.update(Policy::MAX_MS + Policy::EARLY_RECOVERY_MS + 1, true, missing()) == Action::SoftRecovery);
}
static void check_millis_wrap_long_run_and_old_samples() {
Policy policy;
Clock clock(UINT32_MAX - 1500);
policy.begin(clock.now(), 8);
assert(policy.update(clock.now(), true, missing()) == Action::None);
for (unsigned day = 1; day <= 150; ++day) {
clock.advance(86400000UL);
const auto action = policy.update(clock.now(), true, missing());
assert(action == Action::SoftRecovery);
assert(policy.inactiveSeconds() == day * 86400U);
}
// Status saturates instead of rolling over after 136 years of elapsed time.
for (unsigned day = 151; day <= 49712; ++day) {
clock.advance(86400000UL);
assert(policy.update(clock.now(), true, missing()) == Action::SoftRecovery);
}
assert(policy.inactiveSeconds() == UINT32_MAX);
policy.begin(1000, 0);
policy.update(1000, true, missing());
policy.update(1100, true, missing());
policy.update(1090, true, missing()); // Brief out-of-order sample is ignored.
assert(policy.inactiveSeconds() == 0);
assert(policy.update(1000 + Policy::EARLY_RECOVERY_MS - 1, true, missing()) == Action::None);
assert(policy.update(1000 + Policy::EARLY_RECOVERY_MS, true, missing()) == Action::SoftRecovery);
}
static void check_pending_resets_after_real_progress() {
Policy policy;
auto observation = pending();
policy.begin(0, 0);
policy.update(0, true, observation);
assert(policy.update(Policy::EARLY_RECOVERY_MS - 1, true, observation) == Action::None);
assert(policy.stalled());
observation.tx_progress = 1;
assert(policy.update(Policy::EARLY_RECOVERY_MS - 1, true, observation) == Action::None);
assert(!policy.stalled());
assert(policy.update(Policy::EARLY_RECOVERY_MS * 2 - 2, true, observation) == Action::None);
assert(policy.update(Policy::EARLY_RECOVERY_MS * 2 - 1, true, observation) == Action::SoftRecovery);
}
static void check_pre_recovery_purge_cannot_erase_a_stall() {
Policy policy;
auto observation = pending();
policy.begin(0, 0);
assert(policy.update(0, true, observation) == Action::None);
assert(policy.update(Policy::STALL_OBSERVE_MS, true, observation) == Action::None);
assert(policy.stalled());
observation.pending = false; // An external queue/session cleanup, not ACK.
assert(policy.update(Policy::STALL_OBSERVE_MS + 1, true, observation) == Action::None);
assert(policy.stalled() && policy.inactiveSeconds() == 30);
assert(policy.update(Policy::BASE_MS, true, observation) == Action::SoftRecovery);
policy.actionAttempted(Action::SoftRecovery);
assert(policy.update(Policy::BASE_MS + 1, true, observation) == Action::None);
assert(policy.stalled() && policy.stage() == 1);
++observation.tx_progress;
assert(policy.update(Policy::BASE_MS + 2, true, observation) == Action::None);
assert(!policy.stalled() && policy.stage() == 0);
}
static void check_auto_qualification_is_continuous() {
AutoArm policy;
policy.begin(100);
assert(!policy.update(100, true, true));
assert(policy.connectedSeconds() == 0);
assert(!policy.update(100 + AutoArm::QUALIFICATION_MS - 1, true, true));
assert(policy.connectedSeconds() == AutoArm::QUALIFICATION_MS / 1000 - 1);
assert(policy.update(100 + AutoArm::QUALIFICATION_MS, true, true));
assert(policy.connectedSeconds() == 14U * 86400U);
// Disconnect, DTR closure, observed stall, disabled master, unsupported
// transport and non-durable storage all map to qualifying=false. None can
// accumulate separate cable sessions into the required continuous period.
for (unsigned reason = 0; reason != 6; ++reason) {
policy.begin(0);
assert(!policy.update(0, true, true));
assert(!policy.update(AutoArm::QUALIFICATION_MS - 1, true, true));
assert(!policy.update(AutoArm::QUALIFICATION_MS, false, true));
assert(policy.connectedSeconds() == 0);
const uint32_t restarted = AutoArm::QUALIFICATION_MS + 1;
assert(!policy.update(restarted, true, true));
assert(!policy.update(restarted + AutoArm::QUALIFICATION_MS - 1, true, true));
assert(policy.update(restarted + AutoArm::QUALIFICATION_MS, true, true));
}
policy.begin(0);
assert(!policy.update(0, true, true));
assert(!policy.update(AutoArm::QUALIFICATION_MS - 1, true, true));
assert(!policy.update(AutoArm::QUALIFICATION_MS, true, false));
assert(policy.connectedSeconds() == 0);
assert(!policy.update(AutoArm::QUALIFICATION_MS + 1, true, true));
assert(!policy.update(AutoArm::QUALIFICATION_MS * 2, true, true));
assert(policy.update(AutoArm::QUALIFICATION_MS * 2 + 1, true, true));
// A reboot always loses the unprovable connected interval.
policy.begin(AutoArm::QUALIFICATION_MS * 2 + 1);
assert(!policy.update(AutoArm::QUALIFICATION_MS * 2 + 1, true, true));
assert(policy.connectedSeconds() == 0);
}
static void check_auto_clock_wrap_and_saturation() {
AutoArm policy;
Clock clock(UINT32_MAX - 1000);
policy.begin(clock.now());
assert(!policy.update(clock.now(), true, true));
for (unsigned day = 1; day <= 150; ++day) {
clock.advance(86400000UL);
assert(policy.update(clock.now(), true, true) == (day >= 14));
assert(policy.connectedSeconds() == day * 86400U);
}
for (unsigned day = 151; day <= 49712; ++day) {
clock.advance(86400000UL);
assert(policy.update(clock.now(), true, true));
}
assert(policy.connectedSeconds() == UINT32_MAX);
assert(!policy.update(clock.now(), false, true));
assert(policy.connectedSeconds() == 0);
policy.begin(1000);
assert(!policy.update(1000, true, true));
assert(!policy.update(1100, true, true));
assert(!policy.update(1090, true, true));
assert(policy.connectedSeconds() == 0);
assert(!policy.update(1000 + AutoArm::QUALIFICATION_MS - 1, true, true));
assert(policy.update(1000 + AutoArm::QUALIFICATION_MS, true, true));
}
static void check_auto_stall_observation_does_not_arm_recovery() {
Policy policy;
auto observation = pending();
policy.begin(0, 0);
assert(policy.update(0, false, observation) == Action::None);
assert(policy.update(Policy::STALL_OBSERVE_MS, false, observation) == Action::None);
assert(policy.stalled() && policy.inactiveSeconds() == 0);
assert(policy.update(Policy::BASE_MS * 20, false, observation) == Action::None);
assert(policy.stalled() && policy.stage() == 0);
// Promotion to On starts a new complete interval, never inherits Auto age.
const uint32_t activation = Policy::BASE_MS * 20 + 1;
assert(policy.update(activation, true, observation) == Action::None);
assert(policy.update(activation + Policy::EARLY_RECOVERY_MS - 1, true, observation) == Action::None);
assert(policy.update(activation + Policy::EARLY_RECOVERY_MS, true, observation) == Action::SoftRecovery);
}
static void check_auto_usb_only_recovery_vetoes_board_reset() {
Policy policy;
policy.begin(0, 0);
auto observation = missing();
assert(policy.update(0, true, observation, true, false) == Action::None);
assert(policy.update(Policy::EARLY_RECOVERY_MS, true, observation, false, false) == Action::None);
assert(policy.deferred());
assert(policy.update(Policy::EARLY_RECOVERY_MS, true, observation, true, false) == Action::SoftRecovery);
policy.actionAttempted(Action::SoftRecovery);
const uint32_t reenum = Policy::EARLY_RECOVERY_MS + Policy::RECOVERY_GRACE_MS;
assert(policy.update(reenum, true, observation, true, false) == Action::Reenumerate);
policy.actionAttempted(Action::Reenumerate);
for (uint32_t day = 1; day <= 80; ++day) {
assert(policy.update(day * 86400000U, true, observation, true, false) == Action::None);
assert(policy.stage() == 2 && policy.backoffStep() == 0);
}
assert(policy.update(80U * 86400000U, true, observation, false, true) == Action::None);
assert(policy.deferred());
assert(policy.update(80U * 86400000U, true, observation, true, true) == Action::Reboot);
}
static void check_ack_without_effective_reader_cannot_extend_outage() {
for (auto observation : {Observation{true, true, false, false, 0},
Observation{true, false, true, false, 0}}) {
Policy policy;
policy.begin(0, 0);
assert(policy.update(0, true, observation) == Action::None);
for (uint32_t elapsed = 10000; elapsed < Policy::EARLY_RECOVERY_MS; elapsed += 10000) {
++observation.tx_progress;
assert(policy.update(elapsed, true, observation) == Action::None);
}
++observation.tx_progress;
assert(policy.update(Policy::EARLY_RECOVERY_MS, true, observation) == Action::SoftRecovery);
assert(policy.inactiveSeconds() == 300);
policy.actionAttempted(Action::SoftRecovery);
++observation.tx_progress;
const auto reenum = Policy::EARLY_RECOVERY_MS + Policy::RECOVERY_GRACE_MS;
assert(policy.update(reenum, true, observation) == Action::Reenumerate);
policy.actionAttempted(Action::Reenumerate);
++observation.tx_progress;
assert(policy.update(Policy::BASE_MS - 1, true, observation) == Action::None);
++observation.tx_progress;
assert(policy.update(Policy::BASE_MS, true, observation) == Action::Reboot);
}
}
int main() {
check_intervals_and_corruption();
check_missing_unopened_and_unplugged();
check_idle_and_pending_stall();
check_stages_and_deferred_retries();
check_progress_aborts_and_counter_wrap();
check_healthy_reset_requires_continuity_and_commit();
check_disabled_unsupported_and_reenable();
check_millis_wrap_long_run_and_old_samples();
check_pending_resets_after_real_progress();
check_pre_recovery_purge_cannot_erase_a_stall();
check_auto_qualification_is_continuous();
check_auto_clock_wrap_and_saturation();
check_auto_stall_observation_does_not_arm_recovery();
check_auto_usb_only_recovery_vetoes_board_reset();
check_ack_without_effective_reader_cannot_extend_outage();
puts("USB watchdog policy timelines passed");
}
'''
class UsbLoggingWatchdogTest(unittest.TestCase):
def test_production_policy_timelines(self):
compiler = os.environ.get('CXX') or shutil.which('g++') or shutil.which('clang++')
if compiler is None:
self.skipTest('a host C++17 compiler is required')
sanitizer_flags = [] if os.name == 'nt' else [
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
'-fno-pie', '-no-pie']
with tempfile.TemporaryDirectory(prefix='usb-watchdog-policy-') as directory:
work = Path(directory)
cpp = work / 'test.cpp'
binary = work / ('test.exe' if os.name == 'nt' else 'test')
cpp.write_text(HARNESS, encoding='utf-8')
built = subprocess.run([
compiler, '-std=c++17', '-Wall', '-Wextra', '-Werror',
*sanitizer_flags, '-I' + str(ROOT / 'src'),
str(cpp), '-o', str(binary)], capture_output=True, text=True, timeout=60)
self.assertEqual(built.returncode, 0, built.stderr)
tested = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
self.assertEqual(tested.returncode, 0, tested.stderr)
self.assertIn('USB watchdog policy timelines passed', tested.stdout)
if __name__ == '__main__':
unittest.main()
+287
View File
@@ -0,0 +1,287 @@
"""Execute the actual role ownership vetoes for USB watchdog recovery."""
from itertools import product
from pathlib import Path
import os
import subprocess
import tempfile
import unittest
from test_radio_receive_contract import method
ROOT = Path(__file__).resolve().parents[1]
COMMON_HARNESS = r'''
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
struct Board {
bool updating=false, testing=false;
bool isOTAUpdateRunning() const{return updating;}
bool isRadioTestActive() const{return testing;}
} board;
struct RadioDriver {
bool observing=false, calibrating=false;
bool isWatchdogObserving() const{return observing;}
bool isCalibratingNoiseFloor() const{return calibrating;}
} radio_driver;
namespace mesh {
struct Status {bool reader_connected=true, stalled=false;};
static Status status;
Status usbLoggingStatus(){return status;}
}
struct Store {
bool dirty=false;
bool hasPendingContactWrites() const{return dirty;}
};
struct SerialInterface {
bool pending=false, connected=false;
bool hasPendingIO() const{return pending;}
bool isConnected() const{return connected;}
};
struct RoleMesh {
uint32_t dirty_contacts_expiry=0, _ota_update_at=0, set_radio_at=0,
_scheduled_reboot_at=0;
bool outbound=false, temporary=false, ota_apply=false,
saved_radio_apply_pending=false, temp_radio_handoff_pending=false,
command_radio_apply_pending=false, binary_trace_pending=false,
_iter_started=false, terminal=true;
void* pending_serial_reply_route=nullptr;
void* sign_data=nullptr;
Store store;
Store* _store=&store;
SerialInterface serial;
SerialInterface* _serial=&serial;
bool hasOutbound() const{return outbound;}
bool isAnyTempRadioActive() const{return temporary;}
bool hasPendingOtaApply() const{return ota_apply;}
bool isTerminalMode() const{return terminal;}
#if MESH_USB_CONSOLE_COOPERATIVE
bool functional_output=false;
bool hasPendingSerialOutput() const{return functional_output;}
#endif
@ACCESSOR_DECLARATION@
} the_mesh;
@ACCESSOR_DEFINITION@
static char command[32]={};
static bool command_overflow=false;
#if defined(ENABLE_USB_INTERFACE)
static SerialInterface usb_serial_interface;
static bool usb_connected=false;
static bool usb_mota_mode=false;
static unsigned usb_terminal_line_len=0;
static bool usb_terminal_discard_line=false;
bool isUsbTerminalDataConnected(){return usb_connected;}
#endif
@CALLBACK@
void reset(){
board=Board{};radio_driver=RadioDriver{};mesh::status=mesh::Status{};
the_mesh=RoleMesh{};
// Assignment copies the fixture's dependency pointers; point them back at
// the live fixture objects, not the temporary used to reset scalar state.
the_mesh._store=&the_mesh.store;the_mesh._serial=&the_mesh.serial;
command[0]=0;command_overflow=false;
#if defined(ENABLE_USB_INTERFACE)
usb_serial_interface=SerialInterface{};usb_connected=false;
usb_mota_mode=false;usb_terminal_line_len=0;usb_terminal_discard_line=false;
#endif
}
int main(){
reset();assert(the_mesh.canRecoverUsbLogging());
assert(usbLoggingRecoverySafe(nullptr));
// Each protection must survive a physically absent/stalled reader. That
// exception is only for stale ASCII work, never radio, flash or protocol.
for(unsigned state=0;state<3;++state){
reset();mesh::status.reader_connected=state!=1;
mesh::status.stalled=state==2;
board.updating=true;assert(!usbLoggingRecoverySafe(nullptr));board.updating=false;
board.testing=true;assert(!usbLoggingRecoverySafe(nullptr));board.testing=false;
radio_driver.observing=true;assert(!usbLoggingRecoverySafe(nullptr));
radio_driver.observing=false;
radio_driver.calibrating=true;assert(!usbLoggingRecoverySafe(nullptr));
radio_driver.calibrating=false;
for(auto member : {&RoleMesh::outbound,&RoleMesh::temporary,&RoleMesh::ota_apply,
&RoleMesh::saved_radio_apply_pending}){
the_mesh.*member=true;
assert(!the_mesh.canRecoverUsbLogging());
assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.*member=false;
}
the_mesh.dirty_contacts_expiry=1;
assert(!the_mesh.canRecoverUsbLogging());assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.dirty_contacts_expiry=0;
@ROLE_VETOES@
}
@COMMAND_VETOES@
@COMPANION_VETOES@
reset();assert(usbLoggingRecoverySafe(nullptr));
}
'''
class UsbLoggingWatchdogRoleTests(unittest.TestCase):
def test_actual_ordinary_role_callbacks_with_and_without_native_console(self):
for role, header, specific in (
('simple_repeater', 'MyMesh.h', '''
the_mesh.temp_radio_handoff_pending=true;
assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.temp_radio_handoff_pending=false;
the_mesh._ota_update_at=1;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh._ota_update_at=0;
'''),
('simple_room_server', 'MyMesh.h', '''
the_mesh.set_radio_at=1;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.set_radio_at=0;
the_mesh._ota_update_at=1;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh._ota_update_at=0;
'''),
('simple_sensor', 'SensorMesh.h', '''
the_mesh.set_radio_at=1;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.set_radio_at=0;
''')):
directory = ROOT / 'examples' / role
accessor = method((directory / header).read_text(),
'bool canRecoverUsbLogging() const')
callback = method((directory / 'main.cpp').read_text(),
'static bool usbLoggingRecoverySafe(void*)')
command_checks = '''
reset();command[0]='x';assert(!usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=false;assert(usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=true;mesh::status.stalled=true;
assert(usbLoggingRecoverySafe(nullptr));
reset();
'''
if role == 'simple_repeater':
command_checks += '''
command_overflow=true;assert(!usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=false;assert(usbLoggingRecoverySafe(nullptr));
reset();
'''
if role != 'simple_sensor':
command_checks += '''
#if MESH_USB_CONSOLE_COOPERATIVE
the_mesh.functional_output=true;assert(!usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=false;assert(usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=true;mesh::status.stalled=true;
assert(usbLoggingRecoverySafe(nullptr));
reset();
#endif
'''
harness = self._harness(accessor, '', callback, specific,
command_checks, '')
for cooperative in (0, 1):
with self.subTest(role=role, cooperative=cooperative):
self._compile_and_run(harness, cooperative=cooperative)
def test_actual_companion_protocol_flash_radio_and_command_ownership(self):
directory = ROOT / 'examples/companion_radio'
accessor = method((directory / 'MyMesh.cpp').read_text(),
'bool MyMesh::canRecoverUsbLogging() const')
accessor = accessor.replace('MyMesh::', 'RoleMesh::', 1)
callback = method((directory / 'main.cpp').read_text(),
'static bool usbLoggingRecoverySafe(void*)')
role_checks = r'''
for(auto member : {&RoleMesh::command_radio_apply_pending,
&RoleMesh::binary_trace_pending}){
the_mesh.*member=true;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.*member=false;
}
the_mesh.store.dirty=true;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.store.dirty=false;
the_mesh._scheduled_reboot_at=1;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh._scheduled_reboot_at=0;
int route=1;
the_mesh.pending_serial_reply_route=&route;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.pending_serial_reply_route=nullptr;
the_mesh.sign_data=&route;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.sign_data=nullptr;
the_mesh.serial.pending=true;assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.serial.pending=false;
the_mesh._iter_started=true;the_mesh.serial.connected=true;
assert(!usbLoggingRecoverySafe(nullptr));
the_mesh.serial.connected=false;assert(usbLoggingRecoverySafe(nullptr));
the_mesh._iter_started=false;
the_mesh._serial=nullptr;assert(usbLoggingRecoverySafe(nullptr));
the_mesh._serial=&the_mesh.serial;
'''
companion_checks = r'''
#if defined(ENABLE_USB_INTERFACE)
// A live binary client or buffered protocol transaction is always protected,
// even if the dedicated logging CDC is disconnected or its output is stuck.
for(unsigned state=0;state<3;++state){
reset();mesh::status.reader_connected=state!=1;mesh::status.stalled=state==2;
the_mesh.terminal=false;usb_connected=true;
assert(!usbLoggingRecoverySafe(nullptr));
usb_connected=false;assert(usbLoggingRecoverySafe(nullptr));
usb_serial_interface.pending=true;assert(!usbLoggingRecoverySafe(nullptr));
usb_serial_interface.pending=false;
#if COMPANION_FEATURE_USB_MOTA_SOURCE
usb_mota_mode=true;assert(!usbLoggingRecoverySafe(nullptr));
usb_mota_mode=false;
#endif
}
reset();usb_terminal_line_len=1;assert(!usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=false;assert(usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=true;mesh::status.stalled=true;
assert(usbLoggingRecoverySafe(nullptr));
reset();usb_terminal_discard_line=true;assert(!usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=false;assert(usbLoggingRecoverySafe(nullptr));
mesh::status.reader_connected=true;mesh::status.stalled=true;
assert(usbLoggingRecoverySafe(nullptr));
#endif
'''
harness = self._harness('bool canRecoverUsbLogging() const;', accessor,
callback, role_checks, '', companion_checks)
for usb, mota in product((0, 1), repeat=2):
with self.subTest(usb=usb, mota=mota):
self._compile_and_run(harness, cooperative=usb, usb=usb, mota=mota)
def test_every_role_loads_durable_state_and_services_recovery_in_normal_loop(self):
for role in ('companion_radio', 'simple_repeater', 'simple_room_server',
'simple_sensor'):
with self.subTest(role=role):
source = (ROOT / 'examples' / role / 'main.cpp').read_text()
setup = method(source, 'void setup()')
loop = method(source[source.rindex('\nvoid loop()'):], 'void loop()')
self.assertIn('loadUsbLoggingWatchdog(', setup)
self.assertTrue('!volatile_primary_fs' in setup or
'!store.isVolatilePrimaryFS()' in setup)
self.assertEqual(setup.count('[]() -> uint32_t { return rtc_clock.getCurrentTime(); }'), 2)
self.assertNotIn('getCurrentTimeUnique()', setup)
self.assertIn('mesh::serviceUsbLoggingPort();', loop)
self.assertIn('if (mesh::serviceUsbLoggingWatchdog(usbLoggingRecoverySafe)) '
'board.reboot();', loop)
self.assertIn('!mesh::isUsbLoggingWatchdogArmed()', source)
@staticmethod
def _harness(declaration, definition, callback, role, commands, companion):
return (COMMON_HARNESS.replace('@ACCESSOR_DECLARATION@', declaration)
.replace('@ACCESSOR_DEFINITION@', definition)
.replace('@CALLBACK@', callback).replace('@ROLE_VETOES@', role)
.replace('@COMMAND_VETOES@', commands)
.replace('@COMPANION_VETOES@', companion))
def _compile_and_run(self, harness, cooperative, usb=0, mota=0):
with tempfile.TemporaryDirectory() as directory:
work = Path(directory)
cpp = work / 'roles.cpp'
cpp.write_text(harness)
binary = work / 'roles'
flags = ['-DMESH_USB_CONSOLE_COOPERATIVE=' + str(cooperative),
'-DCOMPANION_FEATURE_USB_MOTA_SOURCE=' + str(mota)]
if usb:
flags.append('-DENABLE_USB_INTERFACE=1')
built = subprocess.run([os.environ.get('CXX', 'g++'), '-std=c++17',
'-Wall', '-Wextra', '-Werror', '-Wno-unused-variable',
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
'-fno-pie', '-no-pie', *flags, str(cpp), '-o', str(binary)],
capture_output=True, text=True)
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
ran = subprocess.run([str(binary)], capture_output=True, text=True)
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
if __name__ == '__main__':
unittest.main()
+99
View File
@@ -0,0 +1,99 @@
"""Compile the actual watchdog runtime with isolated filesystem/USB boundaries."""
from pathlib import Path
import os
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = ROOT / 'test' / 'fixtures' / 'usb_logging_watchdog'
class UsbLoggingWatchdogRuntimeTest(unittest.TestCase):
def test_production_runtime_and_persistence(self):
compiler = os.environ.get('CXX') or shutil.which('g++') or shutil.which('clang++')
if compiler is None:
self.skipTest('a host C++17 compiler is required')
sanitizer_flags = [] if os.name == 'nt' else [
'-fsanitize=address,undefined', '-fno-sanitize-recover=all',
'-fno-pie', '-no-pie']
with tempfile.TemporaryDirectory(prefix='usb-watchdog-runtime-') as directory:
source = ROOT / 'src' / 'helpers' / 'UsbLoggingWatchdog.cpp'
for ota in (False, True):
with self.subTest(ota_enabled=ota):
binary = Path(directory) / ('ota' + str(ota) + '.exe')
compile_source = source
if ota:
# Copy this translation unit byte-for-byte. Its quoted
# OTA include can then use an isolated dependency fake,
# while every tested production statement is unchanged.
compile_source = Path(directory) / 'UsbLoggingWatchdog.cpp'
compile_source.write_bytes(source.read_bytes())
built = subprocess.run([
compiler, '-std=c++17', '-Wall', '-Wextra', '-Werror', '-pthread',
*sanitizer_flags, '-DARDUINO', '-DESP32', '-DESP32_PLATFORM',
'-DARDUINO_USB_MODE=0', '-DARDUINO_USB_CDC_ON_BOOT=1',
*(['-DENABLE_OTA'] if ota else []),
'-I' + str(FIXTURE / 'mocks'),
'-I' + str(ROOT / 'src' / 'helpers'), '-I' + str(ROOT / 'src'),
str(compile_source), str(FIXTURE / 'test_usb_logging_watchdog_runtime.cpp'),
'-o', str(binary)], capture_output=True, text=True, timeout=60)
self.assertEqual(built.returncode, 0, built.stderr)
tested = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
self.assertEqual(tested.returncode, 0, tested.stderr)
self.assertIn('USB watchdog runtime and persistence passed', tested.stdout)
def test_unsupported_transport_has_no_recovery_or_storage_work(self):
compiler = os.environ.get('CXX') or shutil.which('g++') or shutil.which('clang++')
if compiler is None:
self.skipTest('a host C++17 compiler is required')
with tempfile.TemporaryDirectory(prefix='usb-watchdog-unsupported-') as directory:
harness = Path(directory) / 'unsupported.cpp'
harness.write_text(r'''
#include <cassert>
#include <cstring>
#include <helpers/UsbLoggingWatchdog.h>
#include <helpers/UsbLoggingClientActivity.h>
namespace mesh { bool isUsbLoggingEnabled() { return true; } }
static bool safe(void*) { assert(false); return false; }
int main() {
mesh::loadUsbLoggingWatchdog(nullptr, true);
const auto status = mesh::usbLoggingStatus();
assert(status.logging_enabled && !status.supported && !status.watchdog_auto);
assert(!status.logger_active && !status.persistence_ready);
assert(!mesh::serviceUsbLoggingWatchdog(safe));
assert(!mesh::isUsbLoggingWatchdogArmed());
assert(!mesh::isUsbLoggingWatchdogUpdateActive());
mesh::noteUsbLoggingStatsCommand("stats-core");
assert(!mesh::isUsbLoggingClientActive());
mesh::clearUsbLoggingClientActivity();
char reply[160] = {};
for (const char* command : {"get usb.watchdog", "get usb.watchdog.last", "set usb.watchdog auto",
"set usb.watchdog on", "set usb.watchdog off"}) {
assert(mesh::handleUsbLoggingWatchdogCommand(command, reply, sizeof(reply)));
assert(strstr(reply, "unsupported"));
}
assert(!mesh::handleUsbLoggingWatchdogCommand("get usb.watchdogx", reply, sizeof(reply)));
assert(!mesh::handleUsbLoggingWatchdogCommand("set usb.debug on", reply, sizeof(reply)));
assert(!mesh::handleUsbLoggingWatchdogCommand(nullptr, reply, sizeof(reply)));
assert(!mesh::handleUsbLoggingWatchdogCommand("get usb.watchdog", reply, 0));
}
''', encoding='ascii')
binary = Path(directory) / 'unsupported.exe'
built = subprocess.run([
compiler, '-std=c++17', '-Wall', '-Wextra', '-Werror',
'-DARDUINO', '-DESP32', '-DESP32_PLATFORM',
'-I' + str(FIXTURE / 'mocks'),
'-I' + str(ROOT / 'src' / 'helpers'), '-I' + str(ROOT / 'src'),
str(ROOT / 'src' / 'helpers' / 'UsbLoggingWatchdog.cpp'),
str(ROOT / 'src' / 'helpers' / 'UsbLoggingClientActivity.cpp'),
str(harness), '-o', str(binary)], capture_output=True, text=True, timeout=60)
self.assertEqual(built.returncode, 0, built.stderr)
tested = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
self.assertEqual(tested.returncode, 0, tested.stderr)
if __name__ == '__main__':
unittest.main()
+233
View File
@@ -0,0 +1,233 @@
"""Compile the production USB watchdog event codec without Arduino or PIO."""
from pathlib import Path
import os
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
HARNESS = r'''
#include <helpers/UsbLoggingWatchdogEvent.h>
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
using Event = mesh::UsbLoggingWatchdogEvent;
constexpr size_t Size = mesh::USB_WATCHDOG_EVENT_SIZE;
static_assert(Size == 13, "The management extension is exactly thirteen bytes");
static bool same(const Event& lhs, const Event& rhs) {
return lhs.reasons == rhs.reasons && lhs.action == rhs.action
&& lhs.epoch == rhs.epoch && lhs.uptime_seconds == rhs.uptime_seconds
&& lhs.sequence == rhs.sequence && lhs.persisted == rhs.persisted;
}
static Event populated() {
Event event;
event.reasons = Event::HOST_ABSENT | Event::TX_STALLED | Event::CLIENT_INACTIVE;
event.action = Event::REBOOT_REQUESTED;
event.epoch = 0x12345678;
event.uptime_seconds = 0x90abcdef;
event.sequence = 0x10203040;
event.persisted = true;
return event;
}
static void exact_little_endian_and_bounds() {
const auto event = populated();
std::array<uint8_t, Size + 2> output;
output.fill(0x5a);
mesh::encodeUsbWatchdogEvent(output.data() + 1, event);
const uint8_t expected[Size] = {
0xbd, 0x78, 0x56, 0x34, 0x12, 0xef, 0xcd, 0xab, 0x90,
0x40, 0x30, 0x20, 0x10};
assert(output.front() == 0x5a && output.back() == 0x5a);
assert(!std::memcmp(output.data() + 1, expected, Size));
Event decoded;
assert(mesh::validUsbWatchdogEvent(expected));
assert(mesh::decodeUsbWatchdogEvent(expected, decoded));
assert(same(event, decoded));
}
static void zero_sequence_has_one_canonical_encoding() {
auto event = populated();
event.sequence = 0;
uint8_t output[Size];
std::memset(output, 0x5a, Size);
mesh::encodeUsbWatchdogEvent(output, event);
for (const auto byte : output) assert(byte == 0);
auto decoded = populated();
assert(mesh::validUsbWatchdogEvent(output));
assert(mesh::decodeUsbWatchdogEvent(output, decoded));
assert(same(decoded, Event()));
mesh::encodeUsbWatchdogEvent(output, Event());
for (const auto byte : output) assert(byte == 0);
}
static void roundtrip_every_reason_action_and_persistence_bit() {
unsigned count = 0;
for (uint8_t reasons = 1; reasons <= 15; ++reasons) {
for (uint8_t action = Event::SOFT_RECOVERY; action <= Event::REBOOT_CANCELLED; ++action) {
for (unsigned persisted = 0; persisted != 2; ++persisted) {
auto event = populated();
event.reasons = reasons;
event.action = action;
event.persisted = persisted;
uint8_t output[Size];
mesh::encodeUsbWatchdogEvent(output, event);
assert(output[0] == (reasons | (action << 4) | (persisted << 7)));
auto decoded = Event();
assert(mesh::validUsbWatchdogEvent(output));
assert(mesh::decodeUsbWatchdogEvent(output, decoded));
assert(same(event, decoded));
++count;
}
}
}
assert(count == 120);
}
static void assert_invalid_preserves_destination(const uint8_t* bytes) {
auto destination = populated();
const auto before = destination;
assert(!mesh::validUsbWatchdogEvent(bytes));
assert(!mesh::decodeUsbWatchdogEvent(bytes, destination));
assert(same(before, destination));
}
static void invalid_zero_sequence_mixed_data_and_invalid_actions() {
// With sequence zero, every other wire bit must be zero too. This covers
// reasons, action, persistence, timestamp and uptime independently.
for (size_t index = 0; index != 9; ++index) {
for (unsigned bit = 0; bit != 8; ++bit) {
uint8_t bytes[Size] = {};
bytes[index] = uint8_t(1U << bit);
assert_invalid_preserves_destination(bytes);
}
}
uint8_t bytes[Size];
mesh::encodeUsbWatchdogEvent(bytes, populated());
bytes[0] = Event::HOST_ABSENT; // A nonempty record cannot use NONE.
assert_invalid_preserves_destination(bytes);
bytes[0] = Event::SOFT_RECOVERY << 4; // Nor can it have no reason.
assert_invalid_preserves_destination(bytes);
for (unsigned action = 5; action <= 7; ++action) {
for (unsigned persisted = 0; persisted != 2; ++persisted) {
bytes[0] = uint8_t(Event::HOST_ABSENT | (action << 4) | (persisted << 7));
assert_invalid_preserves_destination(bytes);
}
}
}
static void epoch_zero_and_uint32_boundaries_are_valid() {
for (unsigned maximum = 0; maximum != 2; ++maximum) {
auto event = populated();
event.epoch = maximum ? UINT32_MAX : 0;
event.uptime_seconds = maximum ? UINT32_MAX : 0;
event.sequence = maximum ? UINT32_MAX : 1;
event.persisted = maximum;
uint8_t bytes[Size];
mesh::encodeUsbWatchdogEvent(bytes, event);
Event decoded;
assert(mesh::validUsbWatchdogEvent(bytes));
assert(mesh::decodeUsbWatchdogEvent(bytes, decoded));
assert(same(event, decoded));
for (size_t index = 1; index != 9; ++index) {
assert(bytes[index] == (maximum ? 0xff : 0));
}
}
}
static void wire_validation_matches_decode_for_every_flag_and_sequence_byte() {
for (unsigned code = 0; code <= 255; ++code) {
for (size_t sequence_byte = 9; sequence_byte < Size; ++sequence_byte) {
uint8_t bytes[Size] = {};
bytes[0] = uint8_t(code);
bytes[1] = 0x5a; // Arbitrary advisory clock and uptime are allowed.
bytes[8] = 0xa5;
bytes[sequence_byte] = 0x80;
const unsigned reasons = code & 15;
const unsigned action = (code >> 4) & 7;
const bool expected = reasons != 0 && action >= 1 && action <= 4;
assert(mesh::validUsbWatchdogEvent(bytes) == expected);
Event decoded;
assert(mesh::decodeUsbWatchdogEvent(bytes, decoded) == expected);
}
}
}
static void action_names_are_stable_and_unknown_is_none() {
const char* expected[] = {"none", "soft-recovery", "reenumerate",
"reboot-requested", "reboot-cancelled"};
for (unsigned action = 0; action <= 255; ++action) {
assert(!std::strcmp(mesh::usbWatchdogActionName(uint8_t(action)),
action < 5 ? expected[action] : "none"));
}
}
int main(int argc, char** argv) {
assert(argc == 2);
if (!std::strcmp(argv[1], "bytes")) exact_little_endian_and_bounds();
else if (!std::strcmp(argv[1], "empty")) zero_sequence_has_one_canonical_encoding();
else if (!std::strcmp(argv[1], "roundtrip")) roundtrip_every_reason_action_and_persistence_bit();
else if (!std::strcmp(argv[1], "invalid")) invalid_zero_sequence_mixed_data_and_invalid_actions();
else if (!std::strcmp(argv[1], "limits")) epoch_zero_and_uint32_boundaries_are_valid();
else if (!std::strcmp(argv[1], "validation")) wire_validation_matches_decode_for_every_flag_and_sequence_byte();
else if (!std::strcmp(argv[1], "names")) action_names_are_stable_and_unknown_is_none();
else return 2;
std::puts("USB watchdog event codec passed");
}
'''
class UsbWatchdogEventTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
compiler = os.environ.get('CXX') or shutil.which('g++') or shutil.which('clang++')
if not compiler:
raise unittest.SkipTest('a host C++ compiler is required')
cls.directory = tempfile.TemporaryDirectory(prefix='mesh-usb-watchdog-event-')
cls.addClassCleanup(cls.directory.cleanup)
source = Path(cls.directory.name) / 'event.cpp'
source.write_text(HARNESS, encoding='ascii')
cls.executable = Path(cls.directory.name) / 'event'
command = [compiler, '-std=c++17', '-Wall', '-Wextra', '-Werror', '-pedantic',
'-O2', '-I', str(ROOT / 'src'), str(source), '-o', str(cls.executable)]
result = subprocess.run(command, capture_output=True, text=True, timeout=60)
if result.returncode:
raise AssertionError(result.stdout + result.stderr)
def run_case(self, name):
result = subprocess.run([str(self.executable), name], capture_output=True,
text=True, timeout=10)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn('USB watchdog event codec passed', result.stdout)
def test_exact_thirteen_byte_little_endian_layout_and_output_bounds(self):
self.run_case('bytes')
def test_empty_event_canonical_encoding_resets_decoded_destination(self):
self.run_case('empty')
def test_all_reason_masks_actions_and_persistence_bits_roundtrip(self):
self.run_case('roundtrip')
def test_malformed_records_fail_without_mutating_destination(self):
self.run_case('invalid')
def test_zero_epoch_and_maximum_uint32_fields_are_valid(self):
self.run_case('limits')
def test_action_names(self):
self.run_case('names')
def test_wire_only_validation_matches_decode_for_all_flags_and_sequence_bytes(self):
self.run_case('validation')
if __name__ == '__main__':
unittest.main()