mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 05:04:31 +00:00
Whether a claim may replace what an observer has stored, own or foreign, is now decided in one place under the observer's seat lock: an older claim never undoes a newer one, the claim spacing holds across both kinds, and an unsigned claim is refused while a signed one is live. An observer that turns foreign loses every assignment in the same transaction, requested ones its claim does not name included. A server that starts checking drops the claims that were not placed under its current host and broker list. A foreign reading never counts as a repeater's newest; one from an observer this server assigned the target stays its own; with authority approved it needs approval to be trusted. A null sig or poll_url is refused. A packet's broker set only grows. The foreign reading lookups get a partial index (093), the MQTT 5 connected flag follows the current connection only, and queued messages are charged for their source.
54 lines
3.0 KiB
Go
54 lines
3.0 KiB
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package polling
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAdmitClaim(t *testing.T) {
|
|
now := time.Date(2026, 11, 28, 12, 0, 0, 0, time.UTC)
|
|
at := func(min int) time.Time { return now.Add(time.Duration(min) * time.Minute) }
|
|
old := now.Add(-time.Hour) // received long enough ago for the spacing
|
|
own := func(made time.Time, signed bool) StoredClaim {
|
|
return StoredClaim{ClaimedAt: made, ReceivedAt: old, Signed: signed}
|
|
}
|
|
foreign := func(made time.Time, signed bool) StoredClaim {
|
|
return StoredClaim{Foreign: true, ClaimedAt: made, ReceivedAt: old, Signed: signed}
|
|
}
|
|
check := now.Add(-ClaimTTL)
|
|
for name, tc := range map[string]struct {
|
|
c Claim
|
|
own bool
|
|
stored []StoredClaim
|
|
want bool
|
|
}{
|
|
"nothing stored": {Claim{Timestamp: at(0)}, true, nil, true},
|
|
"newer own over own": {Claim{Timestamp: at(2)}, true, []StoredClaim{own(at(0), false)}, true},
|
|
"same own again": {Claim{Timestamp: at(0)}, true, []StoredClaim{own(at(0), false)}, false},
|
|
"newer foreign over own": {Claim{Timestamp: at(2)}, false, []StoredClaim{own(at(0), false)}, true},
|
|
"same claim, foreign route second": {Claim{Timestamp: at(0)}, false, []StoredClaim{own(at(0), false)}, false},
|
|
"same claim, own route second": {Claim{Timestamp: at(0)}, true, []StoredClaim{foreign(at(0), false)}, true},
|
|
"delayed older own after newer foreign": {Claim{Timestamp: at(0)}, true, []StoredClaim{foreign(at(2), false)}, false},
|
|
"newer own over foreign": {Claim{Timestamp: at(4)}, true, []StoredClaim{foreign(at(2), false)}, true},
|
|
"older foreign after newer foreign": {Claim{Timestamp: at(0)}, false, []StoredClaim{foreign(at(2), false)}, false},
|
|
"unsigned after signed, checked": {Claim{Timestamp: at(4), SignedSince: check}, true, []StoredClaim{foreign(at(2), true)}, false},
|
|
"unsigned after signed, not checked": {Claim{Timestamp: at(4)}, true, []StoredClaim{own(at(2), true)}, true},
|
|
"signed after signed, checked": {Claim{Timestamp: at(4), Signed: true, SignedSince: check}, false, []StoredClaim{own(at(2), true)}, true},
|
|
"unsigned after an expired signed one": {Claim{Timestamp: at(4), SignedSince: check}, true,
|
|
[]StoredClaim{{ClaimedAt: at(-200), ReceivedAt: now.Add(-3 * time.Hour), Signed: true}}, true},
|
|
"too soon after the other kind": {Claim{Timestamp: at(4)}, false,
|
|
[]StoredClaim{{ClaimedAt: at(2), ReceivedAt: now.Add(-5 * time.Second)}}, false},
|
|
"too soon after a foreign one": {Claim{Timestamp: at(4)}, true,
|
|
[]StoredClaim{{Foreign: true, ClaimedAt: at(2), ReceivedAt: now.Add(-5 * time.Second)}}, false},
|
|
"same claim by the own route is never too soon": {Claim{Timestamp: at(2)}, true,
|
|
[]StoredClaim{{Foreign: true, ClaimedAt: at(2), ReceivedAt: now.Add(-5 * time.Millisecond)}}, true},
|
|
} {
|
|
if got := AdmitClaim(tc.c, tc.own, now, tc.stored); got != tc.want {
|
|
t.Errorf("%s: %v, want %v", name, got, tc.want)
|
|
}
|
|
}
|
|
}
|