|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/step-security/dev-machine-guard/internal/launchd" |
| 11 | + "github.com/step-security/dev-machine-guard/internal/systemd" |
| 12 | +) |
| 13 | + |
| 14 | +func TestFileExists(t *testing.T) { |
| 15 | + dir := t.TempDir() |
| 16 | + present := filepath.Join(dir, "marker") |
| 17 | + if err := os.WriteFile(present, []byte("x"), 0o600); err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + |
| 21 | + cases := []struct { |
| 22 | + name string |
| 23 | + path string |
| 24 | + want bool |
| 25 | + }{ |
| 26 | + {"existing file", present, true}, |
| 27 | + {"missing file", filepath.Join(dir, "nope"), false}, |
| 28 | + {"empty path", "", false}, |
| 29 | + {"directory", dir, false}, // dirs intentionally don't count as installs |
| 30 | + } |
| 31 | + |
| 32 | + for _, tc := range cases { |
| 33 | + t.Run(tc.name, func(t *testing.T) { |
| 34 | + if got := fileExists(tc.path); got != tc.want { |
| 35 | + t.Fatalf("fileExists(%q) = %v, want %v", tc.path, got, tc.want) |
| 36 | + } |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TestDetectInvocationMethod_HostMachine exercises the detector against the |
| 42 | +// real machine. The result is whatever the current dev box reports; we can |
| 43 | +// only assert the value is one of the two valid wire-format strings. |
| 44 | +func TestDetectInvocationMethod_HostMachine(t *testing.T) { |
| 45 | + got := DetectInvocationMethod() |
| 46 | + if got != InvocationInstall && got != InvocationOneTime { |
| 47 | + t.Fatalf("DetectInvocationMethod returned %q, want %q or %q", |
| 48 | + got, InvocationInstall, InvocationOneTime) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// TestDetectInvocationMethod_RespondsToFilesystem covers the darwin/linux |
| 53 | +// path that stats a scheduler artifact. On Windows the check shells out to |
| 54 | +// schtasks, which we can't safely stub without an executor seam — skip there. |
| 55 | +// |
| 56 | +// Sandboxes HOME (Unix) and USERPROFILE (Windows-safe no-op on Unix) under |
| 57 | +// t.TempDir() so launchd.UserPlistPath / systemd.TimerUnitPath compute paths |
| 58 | +// that live entirely inside the temp tree. Without this the test would write |
| 59 | +// markers (and MkdirAll-created parent dirs) into the developer's real |
| 60 | +// ~/Library/LaunchAgents or ~/.config/systemd/user — leaving stray files |
| 61 | +// behind on CI and risking a tiny TOCTOU window against a real install. |
| 62 | +func TestDetectInvocationMethod_RespondsToFilesystem(t *testing.T) { |
| 63 | + if runtime.GOOS == "windows" { |
| 64 | + t.Skip("windows uses schtasks /query, not filesystem") |
| 65 | + } |
| 66 | + |
| 67 | + tempHome := t.TempDir() |
| 68 | + t.Setenv("HOME", tempHome) |
| 69 | + t.Setenv("USERPROFILE", tempHome) // no-op on Unix but cheap and keeps the seam consistent |
| 70 | + |
| 71 | + // Resolve the platform's expected artifact path AFTER the env override |
| 72 | + // so os.UserHomeDir() returns tempHome. |
| 73 | + var path string |
| 74 | + switch runtime.GOOS { |
| 75 | + case "darwin": |
| 76 | + path = launchd.UserPlistPath() |
| 77 | + case "linux": |
| 78 | + path = systemd.TimerUnitPath() |
| 79 | + default: |
| 80 | + t.Skipf("no scheduler artifact path on %s", runtime.GOOS) |
| 81 | + } |
| 82 | + if path == "" { |
| 83 | + t.Skip("could not resolve scheduler artifact path on this host") |
| 84 | + } |
| 85 | + if !strings.HasPrefix(path, tempHome) { |
| 86 | + t.Fatalf("resolved path %q escaped tempHome %q — env sandbox is not effective", path, tempHome) |
| 87 | + } |
| 88 | + |
| 89 | + // Fresh temp home — detector starts at one_time, flips to install when |
| 90 | + // the marker appears, flips back when it's removed. |
| 91 | + if got := DetectInvocationMethod(); got != InvocationOneTime { |
| 92 | + t.Fatalf("on clean temp home, detector returned %q, want %q", |
| 93 | + got, InvocationOneTime) |
| 94 | + } |
| 95 | + |
| 96 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 97 | + t.Fatalf("prepare scheduler artifact dir: %v", err) |
| 98 | + } |
| 99 | + if err := os.WriteFile(path, []byte("x"), 0o600); err != nil { |
| 100 | + t.Fatalf("write fake scheduler artifact: %v", err) |
| 101 | + } |
| 102 | + // No explicit cleanup: everything lives under t.TempDir() and is |
| 103 | + // removed by the testing framework when the test ends. |
| 104 | + |
| 105 | + if got := DetectInvocationMethod(); got != InvocationInstall { |
| 106 | + t.Fatalf("after creating %q, detector returned %q, want %q", |
| 107 | + path, got, InvocationInstall) |
| 108 | + } |
| 109 | + |
| 110 | + // Remove the marker mid-test and re-check — confirms detection is not |
| 111 | + // cached and reflects current filesystem state. |
| 112 | + if err := os.Remove(path); err != nil { |
| 113 | + t.Fatalf("remove fake artifact: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + if got := DetectInvocationMethod(); got != InvocationOneTime { |
| 117 | + t.Fatalf("after removing %q, detector returned %q, want %q", |
| 118 | + path, got, InvocationOneTime) |
| 119 | + } |
| 120 | +} |
0 commit comments