Files
felhom.eu/hub/internal/api/review_fixes_test.go
T

81 lines
3.5 KiB
Go

package api
import (
"net/http"
"strings"
"testing"
"time"
)
// Review of 2026-11-14. The trust stanza is the BOX's: only a word of the closed list and a real date may reach the
// event (and its details, which the mail appends). The first version passed `mode` and `ca_not_after` through.
func TestProxmoxTrust_OnlyClosedWordsReachTheEvent(t *testing.T) {
h, st := trustSetup(t)
bad := `{"mode":"<b>MODE</b> call +36…","problem":"FREE TEXT problem","detail":"x","ca_days_left":3,"ca_not_after":"send money to X"}`
if rr := do(h, http.MethodPost, "/host-report", "HKEY", trustReport(bad)); rr.Code != 200 {
t.Fatalf("report: %d", rr.Code)
}
evs := r1383Events(t, st, eventProxmoxTrustProblem)
if len(evs) != 1 {
t.Fatalf("events = %d", len(evs))
}
all := evs[0].Message + evs[0].DetailsJSON
for _, leak := range []string{"MODE", "<b>", "send money", "FREE TEXT"} {
if strings.Contains(all, leak) {
t.Errorf("the box's own text %q reached the event: %s", leak, all)
}
}
if !strings.Contains(all, "mode: other") || !strings.Contains(all, `"cause":"other"`) || !strings.Contains(all, `"ca_not_after":""`) {
t.Errorf("unknown words must read as other / empty: %s", all)
}
// and a real stanza is unchanged
h2, st2 := trustSetup(t)
good := `{"mode":"fingerprint","problem":"ca_copy_does_not_verify","ca_days_left":3560,"ca_not_after":"2036-08-17T15:44:42Z"}`
do(h2, http.MethodPost, "/host-report", "HKEY", trustReport(good))
e := r1383Events(t, st2, eventProxmoxTrustProblem)[0]
if !strings.Contains(e.Message, "mode: fingerprint") || !strings.Contains(e.Message, "2036-08-17T15:44:42Z") {
t.Errorf("a real stanza lost its words: %s", e.Message)
}
}
// The report's own guest list is the box's word too, so „only a guest the report lists” bounds nothing by itself. One
// box holds at most guestPowerMaxMarks give-up marks; a mark for a guest the box no longer lists is dropped — and a
// dropped mark leaves NO row behind (a box could otherwise fill the settings table with empty rows).
func TestGuestPowerGaveUp_ABoxCannotFillTheSettings(t *testing.T) {
h, st := trustSetup(t)
marks := func() int { return len(st.OSAlarmKeys("guest_power:h1:")) }
rows := func() int {
var n int
for _, k := range []string{"guest_power:h1:"} {
n += len(st.OSAlarmKeys(k))
}
return n
}
for round := 0; round < 5; round++ { // five reports, each with sixteen NEW invented guests, all "stopped"
known, gave := map[int]bool{}, []hostGuestPowerGaveUp{}
for i := 0; i < 16; i++ {
vmid := 7000 + round*100 + i
known[vmid] = true
gave = append(gave, hostGuestPowerGaveUp{VMID: vmid, Since: "2026-11-14T02:00:00Z", Attempts: 3})
}
h.checkGuestPowerGaveUp("h1", "c1", gave, map[int]bool{}, known)
if m := marks(); m > guestPowerMaxMarks {
t.Fatalf("round %d: %d give-up marks for one box, want at most %d", round, m, guestPowerMaxMarks)
}
}
if n := len(r1383Events(t, st, eventGuestPowerGaveUp)); n > 5*guestPowerMaxMarks {
t.Fatalf("%d events for five reports", n)
}
// the box now reports only its real guest: every invented mark is gone, and so is its row
h.checkGuestPowerGaveUp("h1", "c1", nil, map[int]bool{100: true}, map[int]bool{100: true})
if marks() != 0 || rows() != 0 {
t.Fatalf("marks for guests the box no longer lists: %d (rows %d), want 0", marks(), rows())
}
// a cleared mark leaves no row at all
_ = st.SetOSAlarmRaised("probe:x", time.Now())
_ = st.SetOSAlarmRaised("probe:x", time.Time{})
if !st.OSAlarmRaised("probe:x").IsZero() || len(st.OSAlarmKeys("probe:")) != 0 {
t.Fatalf("a cleared alarm is still there")
}
}