package web import ( "encoding/json" "path/filepath" "strings" "testing" "time" "github.com/yuanyuanxiang/SimpleRemoter/server/go/auditquery" "github.com/yuanyuanxiang/SimpleRemoter/server/go/hub" "github.com/yuanyuanxiang/SimpleRemoter/server/go/logger" "github.com/yuanyuanxiang/SimpleRemoter/server/go/wsauth" ) // The scanner has its own tests in package auditquery; this file covers the // WebSocket command handler and its reply shape. // newAuditTestHubWithoutAuditFile builds a hub whose logger has no dedicated // audit file, mirroring a server started with YAMA_AUDIT_LOG=off. func newAuditTestHubWithoutAuditFile(t *testing.T, dir string) (*wsHub, string) { t.Helper() lg := logger.New(logger.Config{ Level: logger.LevelInfo, Console: false, File: filepath.Join(dir, "server.log"), }) t.Cleanup(func() { _ = lg.Close() }) wh := newWSHub(wsauth.New(), hub.New(), nil, lg) t.Cleanup(wh.stop) return wh, "" } func readQueuedJSON(t *testing.T, c *wsClient) map[string]any { t.Helper() select { case m := <-c.send: var v map[string]any if err := json.Unmarshal(m.data, &v); err != nil { t.Fatalf("queued reply is not JSON: %q: %v", m.data, err) } return v case <-time.After(2 * time.Second): t.Fatal("timed out waiting for a queued reply") return nil } } func TestQueryAuditHandlerAdminOnly(t *testing.T) { wh, _, _ := auditTestHub(t) viewer, tok := auditTestClient(wh, "viewer") raw, _ := json.Marshal(map[string]any{"token": tok}) wh.handleQueryAudit(viewer, raw) msg := readQueuedJSON(t, viewer) if msg["cmd"] != "audit_result" || msg["ok"] != false { t.Fatalf("viewer reply = %+v", msg) } if _, ok := msg["events"]; ok { t.Error("denied viewer got events") } } func TestQueryAuditHandlerReturnsEvents(t *testing.T) { wh, _, _ := auditTestHub(t) // Two records written directly through the same logger the hub queries. wh.log.Audit("web.login.success", map[string]any{"actor": "admin", "ip": "203.0.113.1"}) wh.log.Audit("remote.screen.start", map[string]any{"actor": "admin", "device_id": "dev1"}) admin, tok := auditTestClient(wh, "admin") raw, _ := json.Marshal(map[string]any{"token": tok, "limit": 10}) wh.handleQueryAudit(admin, raw) msg := readQueuedJSON(t, admin) if msg["cmd"] != "audit_result" || msg["ok"] != true { t.Fatalf("reply = %+v", msg) } events, ok := msg["events"].([]any) if !ok || len(events) < 2 { t.Fatalf("events = %#v, want at least the two written records", msg["events"]) } // requireAuth writes a web.session.resume record for this same request, so // that one legitimately appears newer. Assert the two written records are // present and still newest-first relative to each other. var names []string for _, e := range events { if m, ok := e.(map[string]any); ok { names = append(names, auditquery.StringField(m, "event")) } } joined := strings.Join(names, ",") if !strings.Contains(joined, "remote.screen.start") || !strings.Contains(joined, "web.login.success") { t.Fatalf("events = %v, want both written records", names) } if strings.Index(joined, "remote.screen.start") > strings.Index(joined, "web.login.success") { t.Fatalf("events = %v, want newest-first order", names) } if msg["truncated"] != false || msg["file_exists"] != true { t.Errorf("meta = %+v", msg) } } func TestQueryAuditHandlerDisabledFile(t *testing.T) { dir := t.TempDir() wh, _ := newAuditTestHubWithoutAuditFile(t, dir) admin, tok := auditTestClient(wh, "admin") raw, _ := json.Marshal(map[string]any{"token": tok}) wh.handleQueryAudit(admin, raw) msg := readQueuedJSON(t, admin) if msg["ok"] != false { t.Fatalf("reply = %+v, want ok:false when the audit file is disabled", msg) } }