diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-02 08:51:47 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-02 08:51:47 +0200 |
| commit | 24a9cd22ac9e6436c4d45b8a2e112e7094c83113 (patch) | |
| tree | 1d83559f07f413fddb8d50dd6bd12656ea8cb86e /internal/flamegraph/liveserver_open_test.go | |
| parent | 717cf6a47cd19cc284b023825a3bac3272ebe171 (diff) | |
Launch live browser opener as invoking sudo user
Diffstat (limited to 'internal/flamegraph/liveserver_open_test.go')
| -rw-r--r-- | internal/flamegraph/liveserver_open_test.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/internal/flamegraph/liveserver_open_test.go b/internal/flamegraph/liveserver_open_test.go index 0a42a61..aa9340a 100644 --- a/internal/flamegraph/liveserver_open_test.go +++ b/internal/flamegraph/liveserver_open_test.go @@ -2,6 +2,9 @@ package flamegraph import ( "errors" + "os" + "os/exec" + "strconv" "testing" ) @@ -102,3 +105,75 @@ func TestMaybeOpenLiveBrowserPropagatesOpenError(t *testing.T) { t.Fatalf("expected launch failed error, got %v", err) } } + +func TestOpenBrowserURLReturnsErrorWhenCommandExitsNonZero(t *testing.T) { + err := openBrowserURL("http://localhost:1234/", "false") + if err == nil { + t.Fatalf("expected non-nil error") + } +} + +func TestOpenBrowserURLReturnsNilWhenCommandExitsZero(t *testing.T) { + err := openBrowserURL("http://localhost:1234/", "true") + if err != nil { + t.Fatalf("expected nil error, got %v", err) + } +} + +func TestApplySudoInvokerContextWithEnvSetsCredential(t *testing.T) { + cmd := exec.Command("echo") + uid := os.Getuid() + gid := os.Getgid() + env := []string{ + "SUDO_UID=" + strconv.Itoa(uid), + "SUDO_GID=" + strconv.Itoa(gid), + "SUDO_USER=tester", + "HOME=/root", + } + + applySudoInvokerContextWithEnv(cmd, 0, env) + + if cmd.SysProcAttr == nil || cmd.SysProcAttr.Credential == nil { + t.Fatalf("expected process credentials to be configured") + } + if got := cmd.SysProcAttr.Credential.Uid; got != uint32(uid) { + t.Fatalf("credential uid = %d, want %d", got, uint32(uid)) + } + if got := cmd.SysProcAttr.Credential.Gid; got != uint32(gid) { + t.Fatalf("credential gid = %d, want %d", got, uint32(gid)) + } + if got, ok := lookupEnvValue(cmd.Env, "USER"); !ok || got != "tester" { + t.Fatalf("USER env = %q (ok=%v), want %q", got, ok, "tester") + } + if got, ok := lookupEnvValue(cmd.Env, "LOGNAME"); !ok || got != "tester" { + t.Fatalf("LOGNAME env = %q (ok=%v), want %q", got, ok, "tester") + } +} + +func TestApplySudoInvokerContextWithEnvSkipsWhenNotRoot(t *testing.T) { + cmd := exec.Command("echo") + env := []string{ + "SUDO_UID=1000", + "SUDO_GID=1000", + "SUDO_USER=tester", + } + + applySudoInvokerContextWithEnv(cmd, 1000, env) + + if cmd.SysProcAttr != nil { + t.Fatalf("expected credentials to remain nil for non-root euid") + } + if cmd.Env != nil { + t.Fatalf("expected environment to remain nil for non-root euid") + } +} + +func TestNotifyLiveWarningUsesCallback(t *testing.T) { + var got string + notifyLiveWarning(func(message string) { + got = message + }, "open failed") + if got != "open failed" { + t.Fatalf("warning callback got %q, want %q", got, "open failed") + } +} |
