Files
groundstation/internal/display/diag_test.go
T

151 lines
5.6 KiB
Go

package display
import (
"bytes"
"regexp"
"strings"
"testing"
)
func TestFormatDiagLineRendersTheExactStableSchema(t *testing.T) {
got := FormatDiagLine("first_redraw", "osd",
"trigger=initial", "ops=3", "regions=3", "bounds=196x106+28+15",
"snap=none", "video=off", "stack=top")
want := "display_diag kind=first_redraw role=osd trigger=initial ops=3 regions=3 bounds=196x106+28+15 snap=none video=off stack=top"
if got != want {
t.Fatalf("line=%q want %q", got, want)
}
// A start record has no role: the role field is omitted entirely.
got = FormatDiagLine("start", "", "pid=123", "vcs=unknown")
want = "display_diag kind=start pid=123 vcs=unknown"
if got != want {
t.Fatalf("line=%q want %q", got, want)
}
}
func TestDiagCollectCommandTargetsTheStableTagWithATailCap(t *testing.T) {
// The documented collection command must filter the journal on the
// stable tag and cap the output, so it stays pasteable.
if !strings.Contains(DiagCollectCommand, DiagTag) {
t.Fatalf("collect command %q must target the stable tag %q", DiagCollectCommand, DiagTag)
}
if !strings.Contains(DiagCollectCommand, "tail") || !strings.Contains(DiagCollectCommand, "--no-pager") {
t.Fatalf("collect command %q must cap with tail and disable paging", DiagCollectCommand)
}
}
func TestDiagDeduplicatesIdenticalStateTransitions(t *testing.T) {
var buf bytes.Buffer
d := NewDiag(&buf)
for i := 0; i < 3; i++ {
d.Emit("snap", "osd", "state=fresh")
}
d.Emit("snap", "osd", "state=stale")
d.Emit("snap", "osd", "state=stale")
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
if len(lines) != 2 {
t.Fatalf("lines=%d (%v) want one fresh and one stale record", len(lines), lines)
}
if !strings.Contains(lines[0], "state=fresh") || !strings.Contains(lines[1], "state=stale") {
t.Fatalf("lines=%v want state transitions only", lines)
}
}
func TestDiagCapsRecordsAndForcesOnlyTheSummary(t *testing.T) {
var buf bytes.Buffer
d := NewDiag(&buf)
d.cap = 3
for i := 0; i < 10; i++ {
d.Emit("snap", "osd", "state="+[]string{"fresh", "stale"}[i%2])
}
d.EmitForced("summary", "osd", "flushes=1")
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
if len(lines) != 4 {
t.Fatalf("lines=%d (%v) want the cap plus one forced summary", len(lines), lines)
}
if !strings.Contains(lines[len(lines)-1], "kind=summary") {
t.Fatalf("last line=%q want the forced summary", lines[len(lines)-1])
}
}
func TestDiagNilReceiverIsSafe(t *testing.T) {
var d *Diag
d.Emit("snap", "osd", "state=fresh")
d.EmitForced("summary", "osd", "flushes=0")
}
func TestDiagValueSanitizesNewlinesAndBoundsLength(t *testing.T) {
// A hostile or multiline error can never inject journal lines into the
// display_diag stream: line breaks collapse to spaces and the value is
// length-bounded.
hostile := "line one\nline two\rinjected line\n" + strings.Repeat("x", 400)
got := diagValue(hostile)
if strings.ContainsAny(got, "\n\r") {
t.Fatalf("sanitized value=%q still carries line breaks", got)
}
if len(got) != diagValueMaxLen {
t.Fatalf("len=%d want the %d-byte bound", len(got), diagValueMaxLen)
}
if !strings.HasPrefix(got, "line one line two injected line ") {
t.Fatalf("sanitized value=%q must preserve content with spaces", got)
}
if diagValue("") != "" {
t.Fatal("an empty value stays empty")
}
}
func TestVCSRevisionReturnsASafeSingleToken(t *testing.T) {
// The revision is either the embedded build revision or the explicit
// unknown marker: a single token without whitespace either way, safe
// for the key=value schema under any build mode.
got := VCSRevision()
if got == "" || strings.ContainsAny(got, " \t\n\r") {
t.Fatalf("vcs revision=%q must be a single safe token", got)
}
}
func TestDiagLinesCarryNoSensitiveValues(t *testing.T) {
var buf bytes.Buffer
d := NewDiag(&buf)
// Everything the diagnostics can ever emit: process metadata, role and
// window identifiers, geometry and shape extents, state enums, stack
// position, bounded counters and operation errors. No RC channel
// values, telemetry payloads, file contents, environment or
// credentials have a field in the schema.
d.EmitForced("start", "", "pid=4321", "vcs=0123abcd")
d.Emit("open", "osd", "window=0x600001", "root=0x2a0", "cfg=1920x1080",
"root_geom=1920x1080", "client_geom=1920x1080", "refresh=1s",
"type=DOCK", "wm_state=FULLSCREEN+ABOVE", "shape_init=empty/empty")
d.Emit("first_redraw", "osd", "trigger=initial", "ops=3", "regions=3",
"bounds=196x106+28+15", "snap=none", "video=off", "stack=top")
d.Emit("query", "osd", "shape=196x106+28+15", "geom=1920x1080+0+26",
"parent=0x1a00001", "stack=1/3")
d.Emit("snap", "osd", "state=stale")
d.Emit("video", "osd", "state=on")
d.Emit("stack", "osd", "target=video/0x2c00002")
d.Emit("reconnect", "osd", "event=open_failed", "error=connect refused", "retry_delay=500ms")
d.Emit("error", "osd", "op=shape", "err=bounding shape: BadWindow")
d.EmitForced("summary", "osd", "redraws=12", "flushes=12", "errors=1", "snap=stale", "video=on")
schema := regexp.MustCompile(`^display_diag kind=[a-z_]+( role=[a-z]+)?( [a-z_0-9]+="?[^\s"]+( [^\s"]+)*"?)?$`)
for _, line := range strings.Split(strings.TrimSpace(buf.String()), "\n") {
if !strings.HasPrefix(line, DiagTag+" ") {
t.Fatalf("line=%q must carry the stable tag", line)
}
if !schema.MatchString(line) {
t.Fatalf("line=%q does not match the whitelisted schema", line)
}
for _, forbidden := range []string{"channel", "channels", "gps", "battery", "attitude", "Xauthority", "env", "password"} {
if strings.Contains(strings.ToLower(line), forbidden+"=") {
t.Fatalf("line=%q must never carry a %q field", line, forbidden)
}
}
}
}