mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 06:14:23 +00:00
The poller view returns it as latestReadingRequested, null when the reading does not say.
62 lines
4.0 KiB
Go
62 lines
4.0 KiB
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package polling
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
// Readings from poll firmware that signs a final requested part (builds from 2026-12-01), from its own test
|
|
// suite: a telemetry reading of a requested target and a status reading of the operator's own.
|
|
const (
|
|
firmwareRequestedTelemetry = `{"v":2,"origin":"Bench","origin_id":"2152F8D19B791D24453242E15F2EAB6CB7CFFA7B6A5ED30097960E069881DB12","timestamp":"2025-11-28T14:26:43.000000+00:00","plugin":"meshcore.observer_fw/test","readings":[{"node_id":"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd","battery_mv":3940,"source":"poll","reply_hops":0,"method":"telemetry","sampled_at":"2025-11-28T14:26:40.000000+00:00","snr":9.2,"rssi":-87,"temperature_c":16.5,"lpp":"0174018a016700a5","tag":1764339990,"packet_hash":"1021324354657687","requested":true}],"sig":"37ada91788f562f15f9e4f6b577633eff2b88de8fcc910ac07d85bd861b1bc257a45a3da4c9fb7dc7fad249bcc388c1e3d162c4aa4a9183704d743bb47339801"}`
|
|
firmwareLocalStatus = `{"v":1,"origin":"Bench","origin_id":"2152F8D19B791D24453242E15F2EAB6CB7CFFA7B6A5ED30097960E069881DB12","timestamp":"2025-11-28T14:26:43.000000+00:00","plugin":"meshcore.observer_fw/test","readings":[{"node_id":"0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a","battery_mv":4038,"source":"poll","reply_hops":0,"method":"status","sampled_at":"2025-11-28T14:26:40.000000+00:00","snr":-6.5,"rssi":-101,"stats":{"uptime_secs":788089,"noise_floor":-116,"last_rssi":-114,"last_snr":1.25,"tx_queue":0,"packets_recv":81234,"packets_sent":20276,"recv_flood":76198,"recv_direct":5012,"sent_flood":19211,"sent_direct":1065,"tx_air_secs":18301,"rx_air_secs":73835,"flood_dups":56298,"direct_dups":36,"recv_errors":28074,"err_events":2},"tag":1764339991,"packet_hash":"A0A1A2A3A4A5A6A7"}],"sig":"c9f26fc112af643046c7df52ce7a25f502e9faef389d1a1b158543dfd1f4047eadb0e001e441272723700886bf010b842260feb4010cd3ad8aa57930c5cd7501"}`
|
|
)
|
|
|
|
func TestReadingsSignedWithRequestedPartVerify(t *testing.T) {
|
|
for name, src := range map[string]string{"requested": firmwareRequestedTelemetry, "local": firmwareLocalStatus} {
|
|
rs, err := ParseReadings([]byte(src))
|
|
if err != nil || len(rs) != 1 || !rs[0].Signed {
|
|
t.Fatalf("%s: %v %+v", name, err, rs)
|
|
}
|
|
if got := rs[0].Requested; got == nil || *got != (name == "requested") {
|
|
t.Errorf("%s: requested %v", name, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Firmware older than the flag and unsigned readings do not say, whatever an unsigned one carries.
|
|
func TestRequestedIsUnknownWithoutTheSignedPart(t *testing.T) {
|
|
unsigned := editReading(t, firmwareRequestedTelemetry, func(top, _ map[string]any) { delete(top, "sig") })
|
|
for name, raw := range map[string][]byte{"old build": []byte(firmwareTelemetry), "unsigned": unsigned} {
|
|
rs, err := ParseReadings(raw)
|
|
if err != nil || len(rs) != 1 || rs[0].Requested != nil {
|
|
t.Errorf("%s: %v %+v", name, err, rs)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The flag is signed, and when present it is the literal true.
|
|
func TestRequestedIsBoundByTheSignature(t *testing.T) {
|
|
for name, edit := range map[string]func(top, r map[string]any){
|
|
"removed": func(_, r map[string]any) { delete(r, "requested") },
|
|
"false": func(_, r map[string]any) { r["requested"] = false },
|
|
"number": func(_, r map[string]any) { r["requested"] = 1 },
|
|
"string": func(_, r map[string]any) { r["requested"] = "true" },
|
|
"null": func(_, r map[string]any) { r["requested"] = nil },
|
|
} {
|
|
if _, err := ParseReadings(editReading(t, firmwareRequestedTelemetry, edit)); !errors.Is(err, ErrReadingSignature) {
|
|
t.Errorf("requested %s: %v", name, err)
|
|
}
|
|
}
|
|
// Added to a reading signed without it, by a new build or an old one.
|
|
for name, src := range map[string]string{"new build": firmwareLocalStatus, "old build": firmwareTelemetry} {
|
|
raw := editReading(t, src, func(_, r map[string]any) { r["requested"] = true })
|
|
if _, err := ParseReadings(raw); !errors.Is(err, ErrReadingSignature) {
|
|
t.Errorf("requested added, %s: %v", name, err)
|
|
}
|
|
}
|
|
}
|