mirror of
https://github.com/meshtastic/firmware.git
synced 2026-09-17 00:52:45 -04:00
* Add native test coverage for the UptimeClock monotonic seam
src/UptimeClock.{h,cpp} shipped without a dedicated test suite. Port the six
tests from the monotonic-time branch (test/test_time), retargeted to the
renamed header.
The wrap test crosses 0xFFFFFFFF via advanceTestMillis() rather than a second
setTestMillis(): setTestMillis() sets clockSourceChanged, which makes
getMillis64() rebase its accumulator and swallow the wrap.
* NextHopRouter: fix 49.7-day millis() rollover in retransmission timing
Resolves the "FIXME, handle 51 day rolloever here!!!" in
NextHopRouter::doRetransmissions() by switching the retransmission-due
comparison from plain unsigned <= to a signed-difference cast.
The previous p.nextTxMsec <= now comparison silently breaks across the
~49.7 day millis() wraparound: pending retransmissions either stall
for the remainder of the wrap window, or all fire simultaneously at
the rollover boundary. Long-running router/infrastructure nodes do hit
this in practice.
The replacement (int32_t)(p.nextTxMsec - now) <= 0 is the standard
Arduino/embedded idiom for rollover-safe deadline checks and behaves
identically to the original for any non-wrap timing.
* Address Copilot review: use unsigned half-range for rollover-safe retransmit check
Review feedback from @Copilot on PR #10227: casting a uint32_t
subtraction to int32_t is implementation-defined in C++ when the
unsigned value exceeds INT32_MAX (even though it works on typical
two's-complement targets).
Switch to the fully well-defined unsigned half-range form:
nextTxMsec is in the past-or-equal iff (now - nextTxMsec) has not
wrapped past 2^31 ms. Future offsets < 2^31 ms wrap into the top
half and read as 'not yet'.
Same semantics as the signed-cast version on every two's-complement
platform we care about, but portable to any conforming C++ impl.
* Use monotonic time for airtime windows
* Document monotonic airtime windows
* Fix test_packet_signing sentinel that #10227's rollover fix inverts
test_C3_invalid_repeated_packet_cannot_ack_or_change_retry_state parked a
pending packet at nextTxMsec = UINT32_MAX to mean "never retransmit", then
asserted that a rejected repeated packet leaves the retry state untouched.
NextHopRouter::doRetransmissions() now tests whether a retransmit is due with
an unsigned half-range compare, (uint32_t)(now - nextTxMsec) < 0x80000000u,
so that retransmission timing survives the ~49.7 day millis() wrap. Under it
now - 0xFFFFFFFF == now + 1, a small positive delta, so UINT32_MAX reads as
~1ms in the past: the retransmit fires and rewrites nextTxMsec, and the test
failed with "Expected 4294967295 Was 6247".
Use a representable future time instead. Production is unaffected either way -
nextTxMsec is only ever written as millis() + d, and UINT32_MAX came from the
test harness alone - so the sentinel is what needs to go, not the comparison.
Special-casing UINT32_MAX in the retransmit path would keep a value that reads
as "expired" under any wrap-correct compare.
The value is held in a local because millis() advances across
runPipelineIngress(), so recomputing it at the assertion would compare against
a different number.
Reported upstream on meshtastic/firmware#10227, whose branch predates this test.
* Make Throttle time-injectable and add hasElapsed()
Throttle backs ~94 call sites, which makes it the highest-leverage place in
the tree to put the clock seam: reading Time::getMillis() instead of millis()
in its three call sites turns all of them into time-injectable code at once,
without touching any of them. The 32-bit millis() wrap is not otherwise
reachable from a native test.
The read is behaviour-preserving - Time::getMillis() returns millis() unless a
test injects a clock - and the full native suite passes with it live.
Also add hasElapsed(), the complement of isWithinTimespanMs(), because 51 of
the 94 call sites are spelled !isWithinTimespanMs and read poorly. Its
boundary is inclusive (>=) since isWithinTimespanMs uses <; both are
documented. It deliberately does not treat lastExecutionMs == 0 as "never
run": call sites pair that test with the interval check themselves, and
absorbing a sentinel into the one helper every module depends on is exactly
the value-overloading hazard being removed elsewhere.
Migrating the existing !isWithinTimespanMs sites is cosmetic and deliberately
left out of this commit.
test/test_throttle/ covers window semantics, both boundaries, the complement
identity, execute()'s first-run and throttled paths, and - the point of the
exercise - a window opened before the wrap closing correctly after it,
including at the 24h interval that is the longest in the tree.
* Stop disarmed deadline sentinels reaching the comparison
Two deadline variables encoded "inactive" as a magic value that only reads as
"never" because the comparison against it is a naive millis() compare. Under
any rollover-correct comparison both invert to "expired ~49 days ago", so they
have to be untangled before those comparisons can be fixed.
Power::reboot() set rebootAtMsec = -1 on platforms with no reboot
implementation, intending "never fire". Every reader already treats 0 as the
disarm value - powerCommandsCheck() tests `if (rebootAtMsec && ...)`, and
AdminModule writes 0 to cancel - so -1 was both wrong and unnecessary. Use 0.
Left as UINT32_MAX it would reboot-loop the moment the comparison is corrected.
ExternalNotificationModule's nag window compared against nagCycleCutoff, which
holds UINT32_MAX once stopped and 1 at boot. isNagging is the real armed flag,
so test it first and short-circuit: a disarmed cutoff can no longer reach the
arithmetic, while an idle module still takes the same sleep path that the
boot-time value of 1 was relying on.
Note this fixes the sentinel only. The comparison itself is still a naive
`nagCycleCutoff < millis()` and remains on the list to convert.
* Fix millis() rollover in every deadline and interval comparison
Roughly 20 sites compared against millis() directly - `millis() > deadline`,
`deadline < millis()`, `last + interval < millis()`. All of them break for
about 24 days after the 32-bit millis() wrap: depending on which side of the
wrap each value sits, the action either stalls for weeks or fires immediately
and repeatedly. The longest affected interval is the 12 hour NTP renewal, a
~50x margin against the wrap, so none of these needed the range - only the
correct comparison.
Add Throttle::deadlinePassed(deadlineMs) for sites that store an absolute
deadline they cannot re-express as "interval since an event". It uses the same
unsigned half-range test as NextHopRouter::doRetransmissions() rather than
introducing a competing signed-cast idiom, and unlike the signed cast it is
defined for every input. Sites that do store an event use the existing
isWithinTimespanMs / hasElapsed. Nothing gained new state.
Because both helpers read Time::getMillis(), every converted site is now
reachable from a native test that drives the clock across the wrap; the
comparison itself is covered directly in test/test_throttle/.
Sentinel handling is the reason this could not be a mechanical rewrite. The
disarm convention is not uniform: 0 means "inactive" for rebootAtMsec,
shutdownAtMsec, alertBannerUntil, fixHoldEnds, suppressUntilMs and
touchResumeBlockUntilMs; 0 means "due now" for ntp_renew, which is forced to 0
at link-up; UINT32_MAX means "inactive" for nagCycleCutoff; and
alertBannerUntil == 0 in isOverlayBannerShowing() means "show indefinitely".
Every inactive marker is arithmetically far in the past, so a correct
comparison fires on it - each site tests its sentinel before the arithmetic,
and keeps the meaning it had.
Two sites carried a second bug found on the way:
BME680Sensor tested (stateUpdateCounter * STATE_SAVE_PERIOD) < millis(). With
a 6 hour period and a uint16_t counter that product overflows uint32_t after
about 198 saves, independently of the millis() wrap. It now measures the
interval since the last save.
EInkDynamicDisplay had `if (previousRunMs > millis()) return;` as a millis()
overflow guard, which skipped rate limiting entirely for the whole post-wrap
period - the bug it meant to prevent. Every check below it already goes
through Throttle, so the guard is removed rather than fixed.
MotionSensor's calibration countdown is converted to a signed delta rather
than deadlinePassed, because it needs the remaining magnitude and not a
boolean; that matches the already-correct check in the same file.
* Remove getMillis64() and use Throttle for the NodeInfo reply window
getMillis64() had exactly one caller and no callers in tests. It also carried
obligations that made it the wrong shape for this firmware: a wrap accumulator
in mutable statics, which is not ISR-safe, and which must be polled at least
once every ~49.7 days or it silently misses a wrap and returns a time ~49 days
short.
Its one caller only wanted to know whether a 12 hour suppression window had
elapsed - which Throttle answers correctly across the wrap without any
accumulator. NodeInfoModule now stores Time::getMillis() in lastNodeInfoSeen
and tests the window with Throttle::isWithinTimespanMs, so the map holds
milliseconds rather than seconds derived from a 64-bit read.
USERPREFS_NODEINFO_REPLY_SUPPRESS_SECS is user-overridable and now feeds a
multiply by 1000, so a static_assert rejects any value too large to express in
milliseconds instead of letting it wrap.
clockSourceChanged goes too. It existed solely to rebase getMillis64()'s
accumulator when a test swapped clock sources, and it made the wrap untestable
through the injection API: setTestMillis() set the flag, so a wrap crossed by
two setTestMillis() calls was swallowed. With the accumulator gone the flag has
nothing to rebase, and the injection API is a plain settable clock.
The three getMillis64 tests are dropped as they no longer describe anything.
One test replaces them, pinning that advanceTestMillis() wraps past
0xFFFFFFFF rather than saturating, since the Throttle wrap tests rely on it.
Also fix eviction in pruneLastNodeInfoCache(): it picked the entry with the
smallest stored stamp, which is the wrong victim once some stamps sit on the
far side of the wrap. It now evicts the largest elapsed time.
* Add CI guard and docs rule against naive millis() comparisons
Fixing the existing sites does not stop the next one being added. The
millis-deadline-check job rejects millis() placed directly next to a comparison
operator, in either order, anywhere in src/. It lives in test_native.yml
alongside suite-count-check, which sets the precedent for a repo-hygiene guard
that CI enforces and bin/run-tests.sh does not.
The correct idioms all subtract before comparing, so none of them match the
pattern. Line comments are stripped first, so documentation is free to name the
broken form - as the guard's own comment and the coding conventions both do.
Writing the check before finishing the sweep turned out to be worth it: it
found roughly 14 sites that a by-hand audit of deadline variables had missed,
including two extra nagCycleCutoff compares, both boot-screen timeouts, and a
6 hour sensor save interval that was also overflowing a uint32_t multiply.
.github/millis-deadline-allowlist.txt covers the cases that are genuinely not
deadline tests. Both current entries are uptime thresholds - "has the device
been up N ms" - with no stored deadline and no event to measure from: a 30s
button holdoff against phantom shutdown from floating pins, and a 10s window
for the OEM boot logo. Each re-crosses its threshold once per wrap, which is
harmless for boot-holdoff logic and not worth new state to avoid. Entries are
keyed on file plus exact source text, without line numbers, so an edit above an
entry does not silently invalidate it.
Locally the guard reports 19 matches before the sweep and 2 after, both
allowlisted.
The Throttle bullet in the coding conventions is rewritten from "prefer
Throttle for rate limiting" to "never compare against millis() directly", lists
all four helpers with when to use which, names the CI guard, and documents the
sentinel hazard with the rebootAtMsec = -1 case that would have become a reboot
loop. Mirrored into AGENTS.md; CLAUDE.md gets a pointer row.
* Trim rollover comments to what the code needs
The comments added with the millis() rollover fixes carried too much of the
investigation that produced them: how many sites were found, which document
recorded them, what the old code used to do. That belongs in the commit history,
not in the source, and some of it was already stale - Power::reboot() still
described the check it disarms as "a naive millis() > deadline" when that
comparison had been fixed in the same series.
What stays is the non-obvious part at each site: which sentinel value the
variable overloads and what it means there, since that differs between call
sites and is what a correct comparison gets wrong. 0 means "not scheduled" for
rebootAtMsec, "renew now" for ntp_renew, and "show indefinitely" in
isOverlayBannerShowing().
Exposition is kept where it earns its place: the Throttle helpers, the uptime
clock's note on why there is no 64-bit variant, and the tests. The Throttle
docs lose only the site count and the "longest interval in the firmware"
statistic, both of which would age badly; the range trade-off between the two
forms is what a caller actually needs.
Comments only - no code changed, verified by diff.
* possible fixes
* Address review feedback on the rollover fixes
- BME680Sensor: checkpoint lastStateSaveMs after a successful write instead of
at the interval test. The first save (IAQ accuracy >= 2) left it at 0, timing
the next save from boot, and stamping before the write deferred the retry a
full period when the write failed. Reads Time::getMillis(), the same clock
Throttle compares against.
- Throttle: add deadlinePassedAt(now, deadline) for loops that snapshot the
clock once and test many deadlines; deadlinePassed() now delegates to it.
NextHopRouter::doRetransmissions() uses it, replacing the inline half-range
compare adopted from #10227 (nightjoker7) - same arithmetic, credited at the
call site - and takes its snapshot from Time::getMillis() so setNextTx()
deadlines and the due test cannot diverge under an injected test clock.
- test_native.yml: set -euo pipefail in the millis-deadline guard, matching the
sibling suite-count job. Without -e a partially failed scan could report "no
violations" from truncated output.
- test_packet_signing: build the not-due deadline from Time::getMillis() rather
than millis(), so the test and the router read one clock.
- test_throttle: cover deadlinePassedAt(), and correct a wrapped-value comment
(0xFFFFFF00 + 400 is 0x00000090, not 0x00000094).
Two review comments were declined: the AirTime mutex (every airTime-> caller
runs in the single cooperative loop, WebServerThread included) and the
MotionSensor 0-sentinel countdown (the calibration frame is only installed
while a window is open).
clod helped out here
* Correct the described failure window of a naive millis() compare
The comments and agent docs said a bare `millis() > deadline` "breaks for ~24
days after the wrap". That figure belongs to the fix, not the bug: it is the
half-range limit of deadlinePassed(), which reads deadlines more than 2^31 ms
ahead as already passed, and the range over which a UINT32_MAX sentinel reads
as passed.
The naive compare's actual failure is an inversion lasting only while the
deadline sits on the far side of the wrap, so it is bounded by the interval:
the action fires immediately and loses its wait, or blocks for about the wait
it should have performed - days for the nRF52 flash-corruption backoff,
one skipped cycle for a seconds-long retransmit timer.
Comments and docs only; the ~24.8 day statements that correctly describe
deadlinePassed()'s own range are left as they were.
clod helped out here
* Restore a monotonic uptime clock and consolidate the wrap counters
Time::getMillisMonotonic() is the getMillis64() shape - a 32-bit wrap
counter carried across reads - promoted to the shared timebase, with
Time::getUptimeSecs() as the derived whole-seconds view. This deliberately
reverses the earlier removal of getMillis64(), and the distinction matters:
removal was right for a lazily-read accumulator with one rare caller, where
a 49.7-day gap between reads silently swallowed a wrap. Here every read is
the poll and AirTime::runOnce() guarantees one per second; the missed-wrap
contract is pinned by a test rather than left as a footnote.
Three private wrap counters collapse into it:
- AirTime::syncNow() takes its seconds from Time::getUptimeSecs() and drops
its lastSyncMsec checkpoint; window rotation is unchanged.
- DeviceTelemetryModule loses refreshUptime()/uptimeWrapCount/uptimeLastMs;
uptime_seconds comes from Time::getUptimeSecs(), which also removes the
0.296s-per-wrap truncation of (0xFFFFFFFF / 1000) * wraps. Its two
interval checks move to Throttle::hasElapsed().
- HostMetricsModule's copies of those members were never read (its uptime
comes from /proc/uptime) - deleted.
Not ISR-safe (unguarded mutable carry): ISRs keep using getMillis(), which
stays a pure read. Audited: no interrupt-context file reads getTime(),
getValidTime(), or the new accessors.
test/native-suite-count 44 -> 45: the bump for test_uptime_clock was lost
in a branch history rewrite, leaving every later value off by one -
run-tests.sh reports AMBER and CI's suite-count-check fails on the current
push until this correction.
* Anchor the wall clock in monotonic milliseconds
getTime() computed elapsed-since-time-set as a 32-bit millis() delta, so a
node that took time once and stayed up past 49.7 days reported a wall clock
one full cycle in the past - and last_heard, rx_time, message and position
stamps all inherited it. The anchor is now the 64-bit monotonic count
(timeStartMsec -> timeStartMs64) and the elapsed term is computed in 64-bit,
so the wall clock is exact at any uptime.
All six anchor writers follow: the five hardware-RTC read branches and
perhapsSetRTC(), which keeps a truncated 32-bit copy of the same instant for
its Throttle-checked rate-limit stamps. The test seams anchor the same way.
Two native regression tests drive getTime() across the wrap through the
Time seam - one anchored before the wrap and read after it, one anchored
after a counted wrap - with the test epoch derived from BUILD_EPOCH so the
plausibility window cannot rot as the build date advances.
* Stamp the rx_time placeholder in monotonic uptime seconds
computeRxTimeStamp() stamped Time::getMillis() when the clock was untrusted,
and reconcilePendingRxTimes() back-calculated with a 32-bit millis() delta -
correct within one wrap, but a placeholder older than 49.7 days aliased to a
small elapsed value and reconciled to a plausible-but-wrong recent epoch:
the exact failure has_rx_time exists to prevent, reachable by an ordinary
unattended router whose phone connects two months in.
The placeholder is now Time::getUptimeSecs(). Both stamps come off the
monotonic counter, so the elapsed term is exact at any age and the aliasing
window is gone outright rather than widened. If elapsed somehow exceeds the
epoch itself, the packet stays un-dated (absent, never wrong) instead of
clamping to a pre-1970 value. Defence in depth: a placeholder that leaks
needs ~50 years of uptime to cross MIN_PLAUSIBLE_EPOCH, where milliseconds
took 18.3 days.
The stream-API reconciliation tests keep their scenarios with the placeholder
unit switched, and ScopedTimeFixture resets the monotonic carry so uptime
seconds are deterministic per case.
* Date nodes heard before the clock arrives, without polluting last_heard
A node first heard while the wall clock was untrusted got no last_heard at
all, and nothing backfilled it once time arrived - the phone showed "Last
heard: unknown" for a node it had just announced. The arrival instant now
waits in a RAM-only sidecar (NodeNum -> uptime seconds, 32 slots,
reuse-oldest - the RouteHealth shape) and is converted to a real epoch on
the clock-becoming-trusted transition, beside the existing rx_time
reconciliation. last_heard itself never holds anything but a real epoch or
0: it persists to flash and the warm tier, where an uptime-relative value
would be meaningless after reboot.
The sidecar's write sites are updateFrom()'s no-trusted-clock path (the
rx_time placeholder already carries the arrival instant, so this is a store,
not a second clock read) and addFromContact's anti-eviction stamps, which
previously wrote a bare getTime() - boot-relative seconds on a clockless
node, the exact value lastHeardIsWallClock() exists to catch. Eviction
ranking honours the stamps: heard-this-boot outranks every stored epoch,
ordered among themselves, so a stamped contact is not the first victim.
PhoneAPI re-reads last_heard at nodeinfo send time: a record prefetched
before the clock became trusted can carry 0 while the store has since been
backfilled, and re-reading at the pop makes handshake ordering (time-set vs
node-list download) irrelevant. Backfill never moves last_heard backwards
and skips the pathological elapsed-exceeds-epoch case. A node evicted to
the warm tier before time arrives is still absorbed with last_heard 0 -
same as before, bounded to the untrusted window.
* Update the agent docs for the monotonic timebase
The conventions bullet asserted there is deliberately no 64-bit millis; the
monotonic uptime clock restored for timestamps changes that contract. State
the split explicitly: Throttle for deadlines and intervals (no carry state),
Time::getMillisMonotonic()/getUptimeSecs() for timestamps, polled by
construction and not ISR-safe.
* Publish the monotonic wrap carry from a single writer
getMillisMonotonic() was a read-modify-write on two unguarded statics, and it
is reached off the main loop: the nRF52 Bluefruit task via
onFromRadioAuthorize() -> PhoneAPI::getFromRadio -> getValidTime(), and the
portduino civetweb workers via the same path. Two readers interleaving inside
the wrap window could each increment the carry, putting every uptime and
wall-clock reading 2^32 ms ahead for the rest of the boot - a permanent ~49.7
day jump in rx_time, last_heard and ClientNotification.time.
Readers no longer write. serviceMonotonic() publishes a snapshot behind a
seqlock and is the only writer; a reader adds its own unsigned elapsed time to
that snapshot, which is exact across the wrap, so it never inspects the
boundary and cannot miscount it. The main loop publishes every iteration, so
the once-per-49.7-days obligation now has the whole window of margin instead of
resting on an instruction-wide race.
AirTime was the guaranteed poller and is now a pure reader, so the two airtime
wrap tests step the clock the way loop() does. The test clock itself is atomic
so a suite can drive it from one thread while others read.
* Re-arm the GPS ephemeris hold when none is in force
The rollover sweep guarded the hold re-arm with `fixHoldEnds != 0 &&`, which
reads like the sentinel rule but inverts this site. The comparison it replaced,
`(fixHoldEnds + GPS_THREAD_INTERVAL) < millis()`, was always true when nothing
was armed - that was the point, since 0 means "not holding" and so is a reason
to arm. With the guard, a publish that cleared the hold without sleeping (the
`shouldPublish && !tooLong && !holdExpired` path, which does not call down())
left hasValidLocation set and prev_fixQual non-zero, so no disjunct held:
nothing re-armed, nothing published, and the receiver stayed powered at the
200ms poll until searchedTooLong() fired.
State the question positively instead. fixHoldInForce() is the only place the
sentinel is interpreted, and both of runOnce()'s decisions derive from it - the
asymmetry is now visible rather than implied, since arming does not require a
prior hold but expiring does. Its `!= 0` test is not redundant with the
arithmetic: deadlinePassed() is an unsigned half-range test, so past 2^31 ms of
uptime the sentinel reads as a deadline ~24.9 days in the future.
Kept beside its caller rather than in a header; the native test build compiles
GPS.cpp, so the suite declares the prototypes.
Also converts the getACK() wait to isWithinTimespanMs(start, interval): it has
both the start instant and the interval in hand, which gives the full 49.7-day
range instead of 24.8 days ahead, and takes its anchor from Time::getMillis()
so the wait is injectable.
* Date the NodeInfo reply window in uptime seconds
The 12h reply-suppression stamp regressed from wrap-immune 64-bit seconds to
raw 32-bit milliseconds, and pruneLastNodeInfoCache() evicts only by node count
and DB membership - never by age. A stable mesh under the node cap therefore
keeps every stamp indefinitely, and once uptime passes 49.7 days an old one
aliases back into the window: `now - stamp` computes as ~0 and a legitimate
NodeInfo request goes unanswered for up to 12h. It self-heals and repeats once
per wrap cycle.
Store Time::getUptimeSecs() instead, which does not wrap for 136 years, and
drop the millisecond conversion the previous shape needed. Entries past the
window are now evicted too: they can only ever decide "don't suppress".
N8-N11 cover the window from both sides, and N10 pins the regression - it needs
a full 2^32 ms of uptime to elapse, not merely a crossing of the boundary,
because that is when a millisecond stamp reads as "answered this instant".
tearDown() now restores the injected clock and C14's region and TX bucket. A
failing assertion aborts the test body, so restoring at the end of it leaked
that state into every later case.
* Update the agent docs for the single-writer clock and sentinel direction
Two rules the preceding three commits changed.
The monotonic clock is no longer maintained by whoever happens to read it:
serviceMonotonic() is the only writer, readers are pure, and calling it from
anywhere but the main loop reintroduces the double-count.
The sentinel guidance gained the half it was missing. It named UINT32_MAX as a
sentinel while prescribing an idiom that only covers 0, and it assumed the
sentinel always means "suppress" - at the GPS fix-hold site it meant "fire",
which is how that regression passed review looking like the rule.
* Name the fix-hold expiry predicate and arm it from the injected clock
holdJustExpired() gives the second reading of the fixHoldEnds sentinel a
name beside the first, so both are pinned by test/test_gps_fix_hold/ and
neither can be respelled at the call site. The old inline form could not
be tested: written as a literal, its guard folds at compile time and the
assertion asserts nothing.
The arm site used bare millis() while the evaluation reads the Throttle
clock; same value in production, but it kept that write out of reach of
Time::setTestMillis(). Remap a deadline that lands on 0, which would
otherwise read as no hold at all.
* Share the extend formula between the clock's reader and writer
getMillisMonotonic() and serviceMonotonic() carried byte-identical wrap
arithmetic. A one-sided edit to either would drift the published carry
from what readers report, so keep one copy.
* Trim the NodeInfo dedup comment to the house limit
* todo note for potential future imrpovments
* fix some simple deadlines
* Trim the hold-expiry test comment to the house limit
* Fix non-blocking uptime publication and pre-clock recency edges (#29)
* fix(time): avoid blocking monotonic readers
* test(time): make paused-publisher check deterministic
* fix(time): address review portability gaps
* Init the eviction sentinel to the newest possible recency
EvictionRecency{} is {0, false}, which evictionRecencyOlder() ranks as older than
every candidate: without the oldestIndex/oldestBoringIndex guards nothing would
ever be selected and a full node DB would stop evicting entirely.
Init to the genuine maximum instead, so the sentinel is correct on its own. The
index guards stay: two independent reasons the scan is right beats one.
* Keep the deadline-guard check name branch protection matches
The guard was widened to cover Time::getMillis() and unqualified getMillis(),
and renamed to suit. Upstream branch protection matches required checks by name,
so a rename means the old name never reports and merges block on a check that
will never arrive.
Widen the guard, keep the name; the descriptive text carries the broader scope.
* Correct native-suite-count to 47 after the develop merge
Upstream #11293 added test_nmea_wpl and took develop's count to 43; this branch
had independently reached 46. Merging develop resolved the counter textually,
keeping 46, while the directory set became the union of both sides at 47.
The suite-count CI gate fails on the mismatch, and it gates the native test jobs,
so the tests themselves were being skipped.
* test(uptime): make the wrap fall where the comment says it does
The concurrent-reader case started at 0xFFFFF000, leaving 0x1000 to the wrap, so
the 0x800 advance annotated "cross the wrap" fell short and the wrap actually
happened during the following 60s advance.
Start at 0xFFFFF800 instead, so the first advance lands exactly on the wrap while
the readers are running and the second is the ordinary time after it - the shape
both comments already described. Total elapsed is unchanged, so the closing
assertion still holds.
* Respond to human comments
* Did I ever tell you about the time I went to Shelbyville? I wore an onion on my belt, which was the style at the time.
* Convert the I2S nag deadline develop dragged in
The HAS_I2S_SPEAKER_NRF52 RTTTL block arrived from develop with a raw
nagCycleCutoff >= millis(), which the deadline guard rejects. Use the same
Throttle::deadlinePassed() form as the two sibling paths in this function.
* Arm the LittleFS format guard with a flag, not a zero timestamp
preFSBegin() runs in the first millisecond of boot, so millis() can legitimately
return 0 there. Both readers of last_format_ms treated 0 as "nothing formatted
this boot", which would skip the repeat-corruption escalation and let a dead
flash reformat-loop instead of reporting FLASH_CORRUPTION_UNRECOVERABLE.
* Note the single-thread contract on AirTime
* Note the AirTime locking TODO, and tighten the thread note
The two constant getters are not constrained, and getSilentMinutes() reads the
buckets without rotating them, so "the accessors mutate" was not accurate.
* trunk: ignore trufflehog false positives on millis-wrap test constants
test_throttle and test_uptime_clock pin dense clusters of hex boundary
constants (0xFFFFFF00u and neighbors) to exercise 32-bit millis()
rollover. trufflehog's Lob detector stitches nearby hex literals into
one candidate string, and the result happens to match a Lob API key
shape - not a secret, just test fixtures.
Same pattern already used for the gitleaks/nodedb-fixture false
positive in this file.
---------
Co-authored-by: nightjoker7 <mattdeering7@gmail.com>
Co-authored-by: Clive Blackledge <clive@ansible.org>
Co-authored-by: Benjamin Faershtein <119711889+RCGV1@users.noreply.github.com>
Co-authored-by: Thomas Göttgens <tgoettgens@gmail.com>
Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
749 lines
30 KiB
C++
749 lines
30 KiB
C++
#include "MeshTypes.h"
|
|
#include "SerialConsole.h"
|
|
#include "TestUtil.h"
|
|
#include "UptimeClock.h"
|
|
#include "configuration.h"
|
|
#include "gps/RTC.h"
|
|
#include "mesh-pb-constants.h"
|
|
#include "mesh/MeshService.h"
|
|
#include "mesh/NodeDB.h"
|
|
#include "mesh/StreamAPI.h"
|
|
#include "mesh/StreamFrameWriter.h"
|
|
#include <algorithm>
|
|
#include <cstdarg>
|
|
#include <cstdint>
|
|
#include <ctime>
|
|
#include <deque>
|
|
#include <limits>
|
|
#include <unity.h>
|
|
#include <vector>
|
|
|
|
/// Output-only stream whose write quotas deterministically simulate backpressure.
|
|
class ScriptedStream : public Stream
|
|
{
|
|
public:
|
|
/// Report that no input bytes are queued.
|
|
int available() override { return 0; }
|
|
/// Return end-of-input for the output-only stream.
|
|
int read() override { return -1; }
|
|
/// Return end-of-input without consuming data.
|
|
int peek() override { return -1; }
|
|
/// Report the configured output capacity.
|
|
int availableForWrite() override { return availableCapacity; }
|
|
|
|
/// Route single-byte writes through the quota-aware buffer writer.
|
|
size_t write(uint8_t value) override { return write(&value, 1); }
|
|
|
|
/// Accept at most the next scripted quota and capture accepted bytes.
|
|
size_t write(const uint8_t *buffer, size_t size) override
|
|
{
|
|
requestedLengths.push_back(size);
|
|
size_t quota = size;
|
|
if (!writeQuotas.empty()) {
|
|
quota = writeQuotas.front();
|
|
writeQuotas.pop_front();
|
|
}
|
|
size_t accepted = std::min(quota, size);
|
|
output.insert(output.end(), buffer, buffer + accepted);
|
|
return accepted;
|
|
}
|
|
|
|
/// Record flush calls without changing captured output.
|
|
void flush() override { flushCount++; }
|
|
|
|
/// Set the maximum bytes accepted by the next write call.
|
|
void queueWrite(size_t quota) { writeQuotas.push_back(quota); }
|
|
|
|
int availableCapacity = std::numeric_limits<int>::max();
|
|
unsigned flushCount = 0;
|
|
std::deque<size_t> writeQuotas;
|
|
std::vector<size_t> requestedLengths;
|
|
std::vector<uint8_t> output;
|
|
};
|
|
|
|
/// Print sink that records bytes emitted by the real SerialConsole.
|
|
class RecordingPrint : public Print
|
|
{
|
|
public:
|
|
/// Capture one output byte.
|
|
size_t write(uint8_t value) override
|
|
{
|
|
output.push_back(value);
|
|
return 1;
|
|
}
|
|
|
|
std::vector<uint8_t> output;
|
|
};
|
|
|
|
/// Installs a MeshService for a test and restores the previous global service.
|
|
class ScopedMeshService
|
|
{
|
|
public:
|
|
/// Install the scoped service.
|
|
ScopedMeshService() : previous(service) { service = &instance; }
|
|
/// Restore the prior service after StreamAPI fixtures are destroyed.
|
|
~ScopedMeshService() { service = previous; }
|
|
|
|
private:
|
|
MeshService instance;
|
|
MeshService *previous;
|
|
};
|
|
|
|
/// Exposes generic StreamAPI hooks and records frame-write behavior.
|
|
class StreamAPITestShim : public StreamAPI
|
|
{
|
|
public:
|
|
/// Construct the shim over a scripted stream.
|
|
explicit StreamAPITestShim(Stream *stream) : StreamAPI(stream) {}
|
|
|
|
/// Keep connection-timeout handling inactive during tests.
|
|
bool checkIsConnected() override { return true; }
|
|
|
|
/// Invoke the generic transport implementation rather than this shim's capture hook.
|
|
bool writeBaseFrame(uint8_t *buf, size_t len, bool bestEffort = false) { return StreamAPI::writeFrame(buf, len, bestEffort); }
|
|
|
|
bool finishReady = true;
|
|
bool allowWrite = true;
|
|
unsigned finishCalls = 0;
|
|
unsigned frameWriteCalls = 0;
|
|
unsigned failureCalls = 0;
|
|
size_t failedFrameLen = 0;
|
|
size_t failedWrittenLen = 0;
|
|
std::vector<uint8_t> capturedPayload;
|
|
|
|
protected:
|
|
/// Record the pending-frame gate and return its configured state.
|
|
bool finishPendingFrame() override
|
|
{
|
|
finishCalls++;
|
|
return finishReady;
|
|
}
|
|
|
|
/// Apply the configured generic write-readiness result.
|
|
bool canWriteFrame(size_t) override { return allowWrite; }
|
|
|
|
/// Capture generic short-write failure metadata.
|
|
void onFrameWriteFailed(size_t frameLen, size_t writtenLen) override
|
|
{
|
|
failureCalls++;
|
|
failedFrameLen = frameLen;
|
|
failedWrittenLen = writtenLen;
|
|
}
|
|
|
|
/// Capture one encoded PhoneAPI payload without writing it.
|
|
bool writeFrame(uint8_t *buf, size_t len, bool bestEffort) override
|
|
{
|
|
(void)bestEffort;
|
|
frameWriteCalls++;
|
|
capturedPayload.assign(buf + 4, buf + 4 + len);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
/// Minimal PhoneAPI transport for config-stream tests.
|
|
class PhoneAPITestShim : public PhoneAPI
|
|
{
|
|
protected:
|
|
bool checkIsConnected() override { return true; }
|
|
};
|
|
|
|
/// Exposes framed-log hooks and records best-effort writes.
|
|
class LogHookStreamAPI : public StreamAPI
|
|
{
|
|
public:
|
|
/// Construct the log shim over a scripted stream.
|
|
explicit LogHookStreamAPI(Stream *stream) : StreamAPI(stream) {}
|
|
|
|
/// Keep connection-timeout handling inactive during tests.
|
|
bool checkIsConnected() override { return true; }
|
|
|
|
/// Encode a formatted log through StreamAPI's production log path.
|
|
void emitTestLog(const char *format, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, format);
|
|
emitLogRecord(meshtastic_LogRecord_Level_INFO, "test", format, args);
|
|
va_end(args);
|
|
}
|
|
|
|
bool allowLogEncoding = false;
|
|
unsigned frameWriteCalls = 0;
|
|
bool lastBestEffort = false;
|
|
|
|
protected:
|
|
/// Apply the configured log-encoding gate.
|
|
bool canEncodeLogRecord() override { return allowLogEncoding; }
|
|
|
|
/// Record whether the encoded log was marked best-effort.
|
|
bool writeFrame(uint8_t *, size_t, bool bestEffort) override
|
|
{
|
|
frameWriteCalls++;
|
|
lastBestEffort = bestEffort;
|
|
return true;
|
|
}
|
|
};
|
|
|
|
/// Assert byte-for-byte equality between expected and captured stream output.
|
|
static void assertBytesEqual(const std::vector<uint8_t> &expected, const std::vector<uint8_t> &actual)
|
|
{
|
|
TEST_ASSERT_EQUAL_UINT(expected.size(), actual.size());
|
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(expected.data(), actual.data(), expected.size());
|
|
}
|
|
|
|
/// Verify retries append only the unwritten tail and reproduce the frame exactly once.
|
|
void test_frame_writer_continues_only_unwritten_tail()
|
|
{
|
|
ScriptedStream stream;
|
|
StreamFrameWriter writer;
|
|
std::vector<uint8_t> frame = {0x94, 0xc3, 0x00, 0x06, 1, 2, 3, 4, 5, 6};
|
|
stream.queueWrite(3);
|
|
stream.queueWrite(2);
|
|
stream.queueWrite(frame.size());
|
|
|
|
TEST_ASSERT_FALSE(writer.writeFrame(stream, frame.data(), frame.size(), false));
|
|
TEST_ASSERT_FALSE(writer.isIdle());
|
|
TEST_ASSERT_FALSE(writer.finishPendingFrame(stream));
|
|
TEST_ASSERT_FALSE(writer.isIdle());
|
|
TEST_ASSERT_TRUE(writer.finishPendingFrame(stream));
|
|
TEST_ASSERT_TRUE(writer.isIdle());
|
|
|
|
std::vector<size_t> expectedRequests = {10, 7, 5};
|
|
TEST_ASSERT_EQUAL_UINT(expectedRequests.size(), stream.requestedLengths.size());
|
|
TEST_ASSERT_EQUAL_UINT64_ARRAY(expectedRequests.data(), stream.requestedLengths.data(), expectedRequests.size());
|
|
assertBytesEqual(frame, stream.output);
|
|
TEST_ASSERT_EQUAL_UINT(0, stream.flushCount);
|
|
}
|
|
|
|
/// Verify a replacement session receives a complete old frame before its new frame.
|
|
void test_frame_writer_completes_retained_tail_before_new_session_frame()
|
|
{
|
|
ScriptedStream stream;
|
|
StreamFrameWriter writer;
|
|
std::vector<uint8_t> oldFrame = {0x94, 0xc3, 0x00, 0x03, 0xa1, 0xa2, 0xa3};
|
|
std::vector<uint8_t> newFrame = {0x94, 0xc3, 0x00, 0x02, 0xb1, 0xb2};
|
|
stream.queueWrite(3);
|
|
stream.queueWrite(oldFrame.size());
|
|
stream.queueWrite(newFrame.size());
|
|
|
|
TEST_ASSERT_FALSE(writer.writeFrame(stream, oldFrame.data(), oldFrame.size(), false));
|
|
TEST_ASSERT_FALSE(writer.isIdle());
|
|
|
|
// A replacement client starts without discarding the accepted old prefix.
|
|
TEST_ASSERT_TRUE(writer.writeFrame(stream, newFrame.data(), newFrame.size(), false));
|
|
TEST_ASSERT_TRUE(writer.isIdle());
|
|
|
|
std::vector<uint8_t> expected = oldFrame;
|
|
expected.insert(expected.end(), newFrame.begin(), newFrame.end());
|
|
assertBytesEqual(expected, stream.output);
|
|
}
|
|
|
|
/// Verify a required main frame remains ordered behind a partial log frame.
|
|
void test_frame_writer_defers_main_behind_partial_log()
|
|
{
|
|
ScriptedStream stream;
|
|
StreamFrameWriter writer;
|
|
std::vector<uint8_t> logFrame = {0x94, 0xc3, 0x00, 0x02, 0xa1, 0xa2};
|
|
std::vector<uint8_t> mainFrame = {0x94, 0xc3, 0x00, 0x03, 0xb1, 0xb2, 0xb3};
|
|
stream.queueWrite(2);
|
|
stream.queueWrite(0);
|
|
stream.queueWrite(logFrame.size());
|
|
stream.queueWrite(mainFrame.size());
|
|
|
|
TEST_ASSERT_FALSE(writer.writeFrame(stream, logFrame.data(), logFrame.size(), true));
|
|
TEST_ASSERT_FALSE(writer.isIdle());
|
|
TEST_ASSERT_FALSE(writer.writeFrame(stream, mainFrame.data(), mainFrame.size(), false));
|
|
TEST_ASSERT_FALSE(writer.isIdle());
|
|
TEST_ASSERT_FALSE(writer.finishPendingFrame(stream));
|
|
TEST_ASSERT_FALSE(writer.isIdle());
|
|
TEST_ASSERT_TRUE(writer.finishPendingFrame(stream));
|
|
TEST_ASSERT_TRUE(writer.isIdle());
|
|
|
|
std::vector<uint8_t> expected = logFrame;
|
|
expected.insert(expected.end(), mainFrame.begin(), mainFrame.end());
|
|
assertBytesEqual(expected, stream.output);
|
|
TEST_ASSERT_EQUAL_UINT(4, stream.requestedLengths.size());
|
|
TEST_ASSERT_EQUAL_UINT(0, stream.flushCount);
|
|
}
|
|
|
|
/// Verify best-effort output starts only when the complete frame fits.
|
|
void test_frame_writer_rejects_best_effort_without_full_capacity()
|
|
{
|
|
ScriptedStream stream;
|
|
StreamFrameWriter writer;
|
|
std::vector<uint8_t> frame = {0x94, 0xc3, 0x00, 0x02, 1, 2};
|
|
stream.availableCapacity = frame.size() - 1;
|
|
|
|
TEST_ASSERT_FALSE(writer.writeFrame(stream, frame.data(), frame.size(), true));
|
|
TEST_ASSERT_TRUE(writer.isIdle());
|
|
TEST_ASSERT_EQUAL_UINT(0, stream.requestedLengths.size());
|
|
|
|
stream.availableCapacity = frame.size();
|
|
TEST_ASSERT_TRUE(writer.writeFrame(stream, frame.data(), frame.size(), true));
|
|
TEST_ASSERT_TRUE(writer.isIdle());
|
|
TEST_ASSERT_EQUAL_UINT(1, stream.requestedLengths.size());
|
|
assertBytesEqual(frame, stream.output);
|
|
}
|
|
|
|
/// Verify each zero-progress continuation makes one bounded write attempt.
|
|
void test_frame_writer_zero_progress_is_one_bounded_attempt()
|
|
{
|
|
ScriptedStream stream;
|
|
StreamFrameWriter writer;
|
|
std::vector<uint8_t> frame = {0x94, 0xc3, 0x00, 0x02, 1, 2};
|
|
stream.queueWrite(1);
|
|
stream.queueWrite(0);
|
|
stream.queueWrite(0);
|
|
stream.queueWrite(frame.size());
|
|
|
|
TEST_ASSERT_FALSE(writer.writeFrame(stream, frame.data(), frame.size(), false));
|
|
TEST_ASSERT_EQUAL_UINT(1, stream.requestedLengths.size());
|
|
TEST_ASSERT_FALSE(writer.finishPendingFrame(stream));
|
|
TEST_ASSERT_EQUAL_UINT(2, stream.requestedLengths.size());
|
|
TEST_ASSERT_FALSE(writer.finishPendingFrame(stream));
|
|
TEST_ASSERT_EQUAL_UINT(3, stream.requestedLengths.size());
|
|
TEST_ASSERT_TRUE(writer.finishPendingFrame(stream));
|
|
TEST_ASSERT_EQUAL_UINT(4, stream.requestedLengths.size());
|
|
assertBytesEqual(frame, stream.output);
|
|
}
|
|
|
|
/// Verify generic StreamAPI framing and successful-write flush behavior.
|
|
void test_stream_api_full_write_frames_and_flushes()
|
|
{
|
|
ScopedMeshService scopedService;
|
|
ScriptedStream stream;
|
|
StreamAPITestShim api(&stream);
|
|
uint8_t frame[7] = {0, 0, 0, 0, 0x11, 0x22, 0x33};
|
|
|
|
TEST_ASSERT_TRUE(api.writeBaseFrame(frame, 3));
|
|
|
|
std::vector<uint8_t> expected = {0x94, 0xc3, 0x00, 0x03, 0x11, 0x22, 0x33};
|
|
assertBytesEqual(expected, stream.output);
|
|
TEST_ASSERT_EQUAL_UINT(1, stream.requestedLengths.size());
|
|
TEST_ASSERT_EQUAL_UINT(1, stream.flushCount);
|
|
TEST_ASSERT_EQUAL_UINT(0, api.failureCalls);
|
|
}
|
|
|
|
/// Verify generic transports report short writes without flushing or retrying.
|
|
void test_stream_api_short_write_reports_failure_without_flush()
|
|
{
|
|
ScopedMeshService scopedService;
|
|
ScriptedStream stream;
|
|
StreamAPITestShim api(&stream);
|
|
uint8_t frame[7] = {0, 0, 0, 0, 0x11, 0x22, 0x33};
|
|
stream.queueWrite(5);
|
|
|
|
TEST_ASSERT_FALSE(api.writeBaseFrame(frame, 3));
|
|
|
|
TEST_ASSERT_EQUAL_UINT(1, stream.requestedLengths.size());
|
|
TEST_ASSERT_EQUAL_UINT(0, stream.flushCount);
|
|
TEST_ASSERT_EQUAL_UINT(1, api.failureCalls);
|
|
TEST_ASSERT_EQUAL_UINT(7, api.failedFrameLen);
|
|
TEST_ASSERT_EQUAL_UINT(5, api.failedWrittenLen);
|
|
}
|
|
|
|
/// Verify retained output blocks PhoneAPI from dequeuing the next payload.
|
|
void test_stream_api_finishes_pending_before_advancing_phone_api()
|
|
{
|
|
ScopedMeshService scopedService;
|
|
ScriptedStream stream;
|
|
StreamAPITestShim api(&stream);
|
|
api.sendConfigComplete();
|
|
api.sendNotification(meshtastic_LogRecord_Level_WARNING, 42, "still queued");
|
|
api.finishReady = false;
|
|
|
|
api.runOncePart(nullptr, 0);
|
|
TEST_ASSERT_EQUAL_UINT(1, api.finishCalls);
|
|
TEST_ASSERT_EQUAL_UINT(0, api.frameWriteCalls);
|
|
|
|
api.finishReady = true;
|
|
api.runOncePart(nullptr, 0);
|
|
TEST_ASSERT_EQUAL_UINT(2, api.finishCalls);
|
|
TEST_ASSERT_EQUAL_UINT(1, api.frameWriteCalls);
|
|
|
|
meshtastic_FromRadio decoded = meshtastic_FromRadio_init_zero;
|
|
TEST_ASSERT_TRUE(
|
|
pb_decode_from_bytes(api.capturedPayload.data(), api.capturedPayload.size(), &meshtastic_FromRadio_msg, &decoded));
|
|
TEST_ASSERT_EQUAL_UINT(meshtastic_FromRadio_clientNotification_tag, decoded.which_payload_variant);
|
|
TEST_ASSERT_EQUAL_UINT32(42, decoded.clientNotification.reply_id);
|
|
}
|
|
|
|
/// Verify framed logs honor the encoding gate and use best-effort writes.
|
|
void test_stream_api_gates_logs_and_marks_them_best_effort()
|
|
{
|
|
ScopedMeshService scopedService;
|
|
ScriptedStream stream;
|
|
LogHookStreamAPI api(&stream);
|
|
|
|
api.emitTestLog("blocked %u", 1U);
|
|
TEST_ASSERT_EQUAL_UINT(0, api.frameWriteCalls);
|
|
|
|
api.allowLogEncoding = true;
|
|
api.emitTestLog("allowed %u", 2U);
|
|
TEST_ASSERT_EQUAL_UINT(1, api.frameWriteCalls);
|
|
TEST_ASSERT_TRUE(api.lastBestEffort);
|
|
}
|
|
|
|
/// Verify the real SerialConsole emits no unframed bytes in protobuf mode.
|
|
void test_serial_console_suppresses_raw_output_in_protobuf_mode()
|
|
{
|
|
RecordingPrint sink;
|
|
const bool oldHasLora = config.has_lora;
|
|
const bool oldHasSecurity = config.has_security;
|
|
const bool oldSerialEnabled = config.security.serial_enabled;
|
|
const bool oldDebugLogApiEnabled = config.security.debug_log_api_enabled;
|
|
|
|
config.has_lora = true;
|
|
config.has_security = true;
|
|
config.security.serial_enabled = true;
|
|
config.security.debug_log_api_enabled = false;
|
|
console->setDestination(&sink);
|
|
|
|
console->write('A');
|
|
const bool rawBeforeProtobuf = sink.output.size() == 1 && sink.output[0] == 'A';
|
|
sink.output.clear();
|
|
|
|
const uint8_t emptyToRadio = 0;
|
|
console->handleToRadio(&emptyToRadio, 0);
|
|
console->write('B');
|
|
console->write('\n');
|
|
console->log(MESHTASTIC_LOG_LEVEL_ERROR, "must stay framed");
|
|
const bool emptyAfterProtobuf = sink.output.empty();
|
|
|
|
console->setDestination(&Serial);
|
|
config.has_lora = oldHasLora;
|
|
config.has_security = oldHasSecurity;
|
|
config.security.serial_enabled = oldSerialEnabled;
|
|
config.security.debug_log_api_enabled = oldDebugLogApiEnabled;
|
|
|
|
TEST_ASSERT_TRUE(rawBeforeProtobuf);
|
|
TEST_ASSERT_TRUE(emptyAfterProtobuf);
|
|
}
|
|
|
|
// Build a phone->radio ADMIN_APP packet carrying `admin`, with an arbitrary wire `from`.
|
|
static meshtastic_MeshPacket makeAdminPacket(NodeNum from, const meshtastic_AdminMessage &admin)
|
|
{
|
|
meshtastic_MeshPacket p = meshtastic_MeshPacket_init_zero;
|
|
p.from = from;
|
|
p.which_payload_variant = meshtastic_MeshPacket_decoded_tag;
|
|
p.decoded.portnum = meshtastic_PortNum_ADMIN_APP;
|
|
p.decoded.payload.size =
|
|
pb_encode_to_bytes(p.decoded.payload.bytes, sizeof(p.decoded.payload.bytes), &meshtastic_AdminMessage_msg, &admin);
|
|
return p;
|
|
}
|
|
|
|
// The lockdown admin gate must decide on the connection's authorization, not the wire `from`. A
|
|
// client that sets from != 0 previously skipped the gate, so an unauthorized connection could run
|
|
// admin. classifyLocalAdminPacket ignores `from`, so the same spoofed packet is still dropped.
|
|
static void test_lockdown_admin_gate_ignores_wire_from(void)
|
|
{
|
|
meshtastic_AdminMessage setter = meshtastic_AdminMessage_init_zero;
|
|
setter.which_payload_variant = meshtastic_AdminMessage_set_owner_tag;
|
|
meshtastic_MeshPacket spoofed = makeAdminPacket(0x12345678, setter); // from != 0, the bypass
|
|
|
|
meshtastic_AdminMessage out;
|
|
TEST_ASSERT_EQUAL_MESSAGE((int)PhoneAPI::LocalAdminGate::DropUnauthorized,
|
|
(int)PhoneAPI::classifyLocalAdminPacket(spoofed, /*adminAuthorized=*/false, out),
|
|
"unauthorized admin with from != 0 must still be dropped");
|
|
// Control: an authorized connection's identical packet passes through.
|
|
TEST_ASSERT_EQUAL_MESSAGE((int)PhoneAPI::LocalAdminGate::AuthorizedPassThrough,
|
|
(int)PhoneAPI::classifyLocalAdminPacket(spoofed, /*adminAuthorized=*/true, out),
|
|
"authorized admin must not be dropped");
|
|
|
|
// lockdown_auth is the authentication itself, so it is delivered inline regardless of from/auth.
|
|
meshtastic_AdminMessage la = meshtastic_AdminMessage_init_zero;
|
|
la.which_payload_variant = meshtastic_AdminMessage_lockdown_auth_tag;
|
|
meshtastic_MeshPacket authPkt = makeAdminPacket(0x99, la);
|
|
TEST_ASSERT_EQUAL((int)PhoneAPI::LocalAdminGate::LockdownAuth,
|
|
(int)PhoneAPI::classifyLocalAdminPacket(authPkt, /*adminAuthorized=*/false, out));
|
|
|
|
// A non-admin packet is outside the gate entirely.
|
|
meshtastic_MeshPacket text = meshtastic_MeshPacket_init_zero;
|
|
text.which_payload_variant = meshtastic_MeshPacket_decoded_tag;
|
|
text.decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
|
|
TEST_ASSERT_EQUAL((int)PhoneAPI::LocalAdminGate::NotAdmin,
|
|
(int)PhoneAPI::classifyLocalAdminPacket(text, /*adminAuthorized=*/false, out));
|
|
}
|
|
|
|
// An ADMIN_APP packet whose payload is not a decodable AdminMessage must fall through to the
|
|
// normal reject path (NotAdmin), never be acted on as an admin command. The authorized control
|
|
// proves the decode-failure check runs before the auth branch, so it can't pass for the wrong reason.
|
|
static void test_lockdown_admin_gate_rejects_undecodable_admin(void)
|
|
{
|
|
meshtastic_MeshPacket p = meshtastic_MeshPacket_init_zero;
|
|
p.which_payload_variant = meshtastic_MeshPacket_decoded_tag;
|
|
p.decoded.portnum = meshtastic_PortNum_ADMIN_APP;
|
|
// Length-delimited field (tag 0x0A) claiming 16 bytes with none following: pb_decode fails.
|
|
p.decoded.payload.bytes[0] = 0x0A;
|
|
p.decoded.payload.bytes[1] = 0x10;
|
|
p.decoded.payload.size = 2;
|
|
|
|
meshtastic_AdminMessage out;
|
|
TEST_ASSERT_EQUAL_MESSAGE((int)PhoneAPI::LocalAdminGate::NotAdmin,
|
|
(int)PhoneAPI::classifyLocalAdminPacket(p, /*adminAuthorized=*/false, out),
|
|
"undecodable ADMIN_APP payload must fall through to the reject path");
|
|
TEST_ASSERT_EQUAL_MESSAGE((int)PhoneAPI::LocalAdminGate::NotAdmin,
|
|
(int)PhoneAPI::classifyLocalAdminPacket(p, /*adminAuthorized=*/true, out),
|
|
"undecodable ADMIN_APP payload must not pass through even when authorized");
|
|
}
|
|
|
|
static void test_want_config_includes_status_message_module_config(void)
|
|
{
|
|
ScopedMeshService scopedService;
|
|
NodeDB testNodeDB;
|
|
NodeDB *const savedNodeDB = nodeDB;
|
|
nodeDB = &testNodeDB;
|
|
const auto savedModuleConfig = moduleConfig;
|
|
moduleConfig.has_statusmessage = true;
|
|
strncpy(moduleConfig.statusmessage.node_status, "Ready", sizeof(moduleConfig.statusmessage.node_status) - 1);
|
|
moduleConfig.statusmessage.node_status[sizeof(moduleConfig.statusmessage.node_status) - 1] = '\0';
|
|
|
|
meshtastic_ToRadio request = meshtastic_ToRadio_init_zero;
|
|
request.which_payload_variant = meshtastic_ToRadio_want_config_id_tag;
|
|
request.want_config_id = SPECIAL_NONCE_ONLY_CONFIG;
|
|
uint8_t requestBytes[meshtastic_ToRadio_size];
|
|
const size_t requestSize = pb_encode_to_bytes(requestBytes, sizeof(requestBytes), &meshtastic_ToRadio_msg, &request);
|
|
|
|
PhoneAPITestShim api;
|
|
api.handleToRadio(requestBytes, requestSize);
|
|
|
|
bool foundStatusMessageConfig = false;
|
|
for (unsigned i = 0; i < 64 && !foundStatusMessageConfig; ++i) {
|
|
uint8_t responseBytes[meshtastic_FromRadio_size];
|
|
const size_t responseSize = api.getFromRadio(responseBytes);
|
|
meshtastic_FromRadio response = meshtastic_FromRadio_init_zero;
|
|
TEST_ASSERT_TRUE(pb_decode_from_bytes(responseBytes, responseSize, &meshtastic_FromRadio_msg, &response));
|
|
if (response.which_payload_variant == meshtastic_FromRadio_moduleConfig_tag &&
|
|
response.moduleConfig.which_payload_variant == meshtastic_ModuleConfig_statusmessage_tag) {
|
|
foundStatusMessageConfig = true;
|
|
TEST_ASSERT_EQUAL_STRING("Ready", response.moduleConfig.payload_variant.statusmessage.node_status);
|
|
}
|
|
}
|
|
|
|
api.close();
|
|
moduleConfig = savedModuleConfig;
|
|
nodeDB = savedNodeDB;
|
|
TEST_ASSERT_TRUE(foundStatusMessageConfig);
|
|
}
|
|
|
|
/// Queue a packet as Router::dispatchReceived would have, before any time source existed.
|
|
static void queuePendingTimePlaceholderPacket(NodeNum from, uint32_t placeholderUptimeSecs)
|
|
{
|
|
meshtastic_MeshPacket pending = meshtastic_MeshPacket_init_zero;
|
|
pending.which_payload_variant = meshtastic_MeshPacket_decoded_tag;
|
|
pending.decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
|
|
pending.from = from;
|
|
pending.to = NODENUM_BROADCAST;
|
|
pending.rx_time = placeholderUptimeSecs; // computeRxTimeStamp() stamps Time::getUptimeSecs()
|
|
pending.has_rx_time = false;
|
|
service->sendToPhone(packetPool.allocCopy(pending));
|
|
}
|
|
|
|
static void startHandshake(PhoneAPITestShim &api)
|
|
{
|
|
meshtastic_ToRadio request = meshtastic_ToRadio_init_zero;
|
|
request.which_payload_variant = meshtastic_ToRadio_want_config_id_tag;
|
|
request.want_config_id = SPECIAL_NONCE_ONLY_CONFIG;
|
|
uint8_t requestBytes[meshtastic_ToRadio_size];
|
|
const size_t requestSize = pb_encode_to_bytes(requestBytes, sizeof(requestBytes), &meshtastic_ToRadio_msg, &request);
|
|
api.handleToRadio(requestBytes, requestSize);
|
|
}
|
|
|
|
/// Drain the config stream looking for the first packet from `from`; false if never delivered.
|
|
static bool drainHandshakeForPacketFrom(PhoneAPITestShim &api, NodeNum from, meshtastic_MeshPacket &outPacket)
|
|
{
|
|
for (unsigned i = 0; i < 256; ++i) {
|
|
uint8_t responseBytes[meshtastic_FromRadio_size];
|
|
const size_t responseSize = api.getFromRadio(responseBytes);
|
|
if (responseSize == 0)
|
|
return false;
|
|
meshtastic_FromRadio response = meshtastic_FromRadio_init_zero;
|
|
TEST_ASSERT_TRUE(pb_decode_from_bytes(responseBytes, responseSize, &meshtastic_FromRadio_msg, &response));
|
|
if (response.which_payload_variant == meshtastic_FromRadio_packet_tag && response.packet.from == from) {
|
|
outPacket = response.packet;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// Swaps in a scratch NodeDB and the injected clock, restoring both plus the RTC on destruction.
|
|
/// Unity's TEST_ASSERT longjmps out on failure, so cleanup must not live at the end of the test.
|
|
class ScopedTimeFixture
|
|
{
|
|
public:
|
|
ScopedTimeFixture(uint32_t startMillis) : previous(nodeDB)
|
|
{
|
|
resetRTCStateForTests();
|
|
Time::resetMonotonicForTests(); // uptime-seconds placeholders assume no carried wrap
|
|
nodeDB = &instance;
|
|
Time::setTestMillis(startMillis);
|
|
}
|
|
~ScopedTimeFixture()
|
|
{
|
|
nodeDB = previous;
|
|
Time::useRealClock();
|
|
resetRTCStateForTests();
|
|
}
|
|
|
|
private:
|
|
NodeDB instance;
|
|
NodeDB *previous;
|
|
};
|
|
|
|
// Time given at the start of the handshake, before the queued packet is drained: reconciliation
|
|
// (fired by the RTC quality crossing hook in RTC.cpp) rewrites the placeholder in place.
|
|
static void test_time_given_at_handshake_start_reconciles_queued_packet(void)
|
|
{
|
|
ScopedMeshService scopedService;
|
|
ScopedTimeFixture timeFixture(5000);
|
|
|
|
const NodeNum sender = 0x12345678;
|
|
queuePendingTimePlaceholderPacket(sender, 2); // "received" at uptime 2s, 3s before the fixture's 5000ms now
|
|
|
|
PhoneAPITestShim api;
|
|
startHandshake(api);
|
|
|
|
struct timeval networkTime;
|
|
networkTime.tv_sec = time(NULL) + SEC_PER_DAY;
|
|
networkTime.tv_usec = 0;
|
|
TEST_ASSERT_EQUAL_INT(RTCSetResultSuccess, perhapsSetRTC(RTCQualityFromNet, &networkTime));
|
|
|
|
meshtastic_MeshPacket delivered;
|
|
TEST_ASSERT_TRUE_MESSAGE(drainHandshakeForPacketFrom(api, sender, delivered),
|
|
"queued packet was not delivered during the handshake");
|
|
TEST_ASSERT_TRUE(delivered.has_rx_time);
|
|
TEST_ASSERT_UINT32_WITHIN(2, (uint32_t)networkTime.tv_sec - 3, delivered.rx_time);
|
|
|
|
api.close();
|
|
}
|
|
|
|
// Time given at the end - after the queued packet already left via the handshake: the delivered
|
|
// copy keeps its unresolved placeholder, since reconciliation can only rewrite what's still queued.
|
|
static void test_time_given_at_handshake_end_does_not_rewrite_already_sent_packet(void)
|
|
{
|
|
ScopedMeshService scopedService;
|
|
ScopedTimeFixture timeFixture(5000);
|
|
|
|
const NodeNum sender = 0x12345678;
|
|
queuePendingTimePlaceholderPacket(sender, 2);
|
|
|
|
PhoneAPITestShim api;
|
|
startHandshake(api);
|
|
|
|
// rx_time is proto3 optional, so has_rx_time false omits it from the wire entirely: the
|
|
// decoded copy reads back 0 and the placeholder itself never left the device.
|
|
meshtastic_MeshPacket delivered;
|
|
TEST_ASSERT_TRUE_MESSAGE(drainHandshakeForPacketFrom(api, sender, delivered),
|
|
"queued packet was not delivered during the handshake");
|
|
TEST_ASSERT_FALSE(delivered.has_rx_time);
|
|
TEST_ASSERT_EQUAL_UINT32(0u, delivered.rx_time);
|
|
|
|
// Time-giving transaction happens only now, at the end of the handshake.
|
|
struct timeval networkTime;
|
|
networkTime.tv_sec = time(NULL) + SEC_PER_DAY;
|
|
networkTime.tv_usec = 0;
|
|
TEST_ASSERT_EQUAL_INT(RTCSetResultSuccess, perhapsSetRTC(RTCQualityFromNet, &networkTime));
|
|
|
|
// The already-delivered copy is a value, not a queue reference - untouched either way.
|
|
TEST_ASSERT_FALSE(delivered.has_rx_time);
|
|
TEST_ASSERT_EQUAL_UINT32(0u, delivered.rx_time);
|
|
|
|
api.close();
|
|
}
|
|
|
|
// The NodeDB half of the same transition: a node heard while the clock was untrusted gets no
|
|
// last_heard at all (the arrival instant waits in the RAM sidecar as uptime seconds), and the
|
|
// clock-valid hook backfills it to the real epoch of the sighting - so the phone reads
|
|
// "last heard: unknown" only until time arrives, never a boot-relative value.
|
|
static void test_node_heard_before_time_gets_last_heard_backfilled(void)
|
|
{
|
|
ScopedMeshService scopedService;
|
|
ScopedTimeFixture timeFixture(5000);
|
|
|
|
const NodeNum sender = 0x22334455;
|
|
meshtastic_MeshPacket heard = meshtastic_MeshPacket_init_zero;
|
|
heard.which_payload_variant = meshtastic_MeshPacket_decoded_tag;
|
|
heard.decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
|
|
heard.from = sender;
|
|
heard.to = NODENUM_BROADCAST;
|
|
heard.rx_time = 2; // uptime-seconds placeholder: "arrived at uptime 2s"
|
|
heard.has_rx_time = false;
|
|
nodeDB->updateFrom(heard);
|
|
|
|
const meshtastic_NodeInfoLite *info = nodeDB->getMeshNode(sender);
|
|
TEST_ASSERT_NOT_NULL(info);
|
|
TEST_ASSERT_EQUAL_UINT32(0u, info->last_heard); // absent, never a boot-relative stamp
|
|
|
|
struct timeval networkTime;
|
|
networkTime.tv_sec = time(NULL) + SEC_PER_DAY;
|
|
networkTime.tv_usec = 0;
|
|
TEST_ASSERT_EQUAL_INT(RTCSetResultSuccess, perhapsSetRTC(RTCQualityFromNet, &networkTime));
|
|
|
|
// Heard at uptime 2s, clock arrived at uptime 5s: the sighting dates to nowEpoch - 3.
|
|
TEST_ASSERT_UINT32_WITHIN(2, (uint32_t)networkTime.tv_sec - 3, info->last_heard);
|
|
}
|
|
|
|
// Uptime zero is a valid arrival instant during the first second of boot. It must not be confused
|
|
// with an absent sidecar record when network time arrives.
|
|
static void test_node_heard_during_first_uptime_second_gets_last_heard_backfilled(void)
|
|
{
|
|
ScopedMeshService scopedService;
|
|
ScopedTimeFixture timeFixture(500);
|
|
|
|
const NodeNum sender = 0x33445566;
|
|
TEST_ASSERT_NOT_NULL(nodeDB->getOrCreateMeshNode(sender));
|
|
meshtastic_MeshPacket heard = meshtastic_MeshPacket_init_zero;
|
|
heard.which_payload_variant = meshtastic_MeshPacket_decoded_tag;
|
|
heard.decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
|
|
heard.from = sender;
|
|
heard.to = NODENUM_BROADCAST;
|
|
heard.rx_time = 0; // received during uptime second zero
|
|
heard.has_rx_time = false;
|
|
nodeDB->updateFrom(heard);
|
|
|
|
const meshtastic_NodeInfoLite *info = nodeDB->getMeshNode(sender);
|
|
TEST_ASSERT_NOT_NULL(info);
|
|
TEST_ASSERT_EQUAL_UINT32(0u, info->last_heard);
|
|
|
|
struct timeval networkTime;
|
|
networkTime.tv_sec = time(NULL) + SEC_PER_DAY;
|
|
networkTime.tv_usec = 0;
|
|
TEST_ASSERT_EQUAL_INT(RTCSetResultSuccess, perhapsSetRTC(RTCQualityFromNet, &networkTime));
|
|
|
|
TEST_ASSERT_UINT32_WITHIN(1, (uint32_t)networkTime.tv_sec, info->last_heard);
|
|
}
|
|
|
|
/// Unity per-test setup; fixtures are local to each test.
|
|
void setUp(void) {}
|
|
/// Unity per-test teardown; fixtures clean themselves up.
|
|
void tearDown(void) {}
|
|
|
|
/// Initialize the native environment and run the stream regression suite.
|
|
void setup()
|
|
{
|
|
initializeTestEnvironment();
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_frame_writer_continues_only_unwritten_tail);
|
|
RUN_TEST(test_frame_writer_completes_retained_tail_before_new_session_frame);
|
|
RUN_TEST(test_frame_writer_defers_main_behind_partial_log);
|
|
RUN_TEST(test_frame_writer_rejects_best_effort_without_full_capacity);
|
|
RUN_TEST(test_frame_writer_zero_progress_is_one_bounded_attempt);
|
|
RUN_TEST(test_stream_api_full_write_frames_and_flushes);
|
|
RUN_TEST(test_stream_api_short_write_reports_failure_without_flush);
|
|
RUN_TEST(test_stream_api_finishes_pending_before_advancing_phone_api);
|
|
RUN_TEST(test_stream_api_gates_logs_and_marks_them_best_effort);
|
|
RUN_TEST(test_lockdown_admin_gate_ignores_wire_from);
|
|
RUN_TEST(test_lockdown_admin_gate_rejects_undecodable_admin);
|
|
RUN_TEST(test_want_config_includes_status_message_module_config);
|
|
RUN_TEST(test_time_given_at_handshake_start_reconciles_queued_packet);
|
|
RUN_TEST(test_time_given_at_handshake_end_does_not_rewrite_already_sent_packet);
|
|
RUN_TEST(test_node_heard_before_time_gets_last_heard_backfilled);
|
|
RUN_TEST(test_node_heard_during_first_uptime_second_gets_last_heard_backfilled);
|
|
// usingProtobufs intentionally has no reset path, so this must run last.
|
|
RUN_TEST(test_serial_console_suppresses_raw_output_in_protobuf_mode);
|
|
exit(UNITY_END());
|
|
}
|
|
|
|
/// Unused Arduino loop required by the native Unity runner.
|
|
void loop() {}
|