| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | func TestMacOSGuestDiagnosticCollectorBoundsAndFilters(t *testing.T) { |
| 16 | if runtime.GOOS == "windows" { |
| 17 | t.Skip("macOS guest collector requires POSIX directory descriptors and file ownership") |
| 18 | } |
| 19 | if _, err := exec.LookPath("uv"); err != nil { |
| 20 | t.Skip("uv is required for the guest collector") |
| 21 | } |
| 22 | home := t.TempDir() |
| 23 | directory := filepath.Join(home, "Library", "Logs", "Cage") |
| 24 | if err := os.MkdirAll(directory, 0700); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | var events strings.Builder |
| 28 | stamp := time.Now().UTC().Format(time.RFC3339) |
| 29 | for index := 0; index < 150; index++ { |
| 30 | fmt.Fprintf(&events, "{\"time\":%q,\"source\":\"window\",\"reason\":\"frame_unavailable\"}\n", stamp) |
| 31 | } |
| 32 | if err := os.WriteFile(filepath.Join(directory, "window-1.jsonl"), []byte(events.String()), 0600); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | events.Reset() |
| 36 | fmt.Fprintf(&events, "{\"time\":%q,\"source\":\"window\",\"reason\":\"after_rotation\"}\n", stamp) |
| 37 | fmt.Fprintln(&events, `{"time":"2020-01-01T00:00:00Z","source":"window","reason":"old_event"}`) |
| 38 | fmt.Fprintf(&events, "{\"time\":%q,\"source\":\"window\",\"reason\":\"password=private-secret\"}\n", time.Now().UTC().Format(time.RFC3339)) |
| 39 | fmt.Fprintln(&events, "invalid JSON") |
| 40 | fmt.Fprintln(&events, `{"time":123,"source":"window","reason":"invalid_timestamp"}`) |
| 41 | if err := os.WriteFile(filepath.Join(directory, "window.jsonl"), []byte(events.String()), 0600); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | cmd := exec.Command("uv", "run", "--no-project", "python", "-I", "-c", macOSDiagnosticCollector) |
| 45 | cmd.Env = append(os.Environ(), "HOME="+home, "USERPROFILE="+home) |
| 46 | output, err := cmd.Output() |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | var result struct { |
| 51 | Version int `json:"version"` |
| 52 | Lines string `json:"lines"` |
| 53 | } |
| 54 | if json.Unmarshal(output, &result) != nil || result.Version != 1 || strings.Count(result.Lines, "\n") != 150 || len(result.Lines) > 48<<10 || strings.Contains(result.Lines, "private-secret") || strings.Contains(result.Lines, "old_event") { |
| 55 | t.Fatalf("guest diagnostics escaped the bounded fixed schema: %s", output) |
| 56 | } |
| 57 | if !strings.Contains(result.Lines, "after_rotation") { |
| 58 | t.Fatal("missing newest event from current file after same-second rotation") |
| 59 | } |
| 60 | } |