package gnome_test import ( "testing" "proxyctl/internal/gnome" "proxyctl/internal/profile" ) type identityEnvironment struct { current profile.UserIdentity users map[string]profile.UserIdentity env map[string]string } func (e identityEnvironment) CurrentUser() (profile.UserIdentity, error) { return e.current, nil } func (e identityEnvironment) LookupUser(name string) (profile.UserIdentity, error) { return e.users[name], nil } func (e identityEnvironment) LookupEnv(name string) (string, bool) { v, ok := e.env[name] return v, ok } func TestConfigurationTargetsSudoInvokingUsersSessionAndState(t *testing.T) { alice := profile.UserIdentity{Username: "alice", HomeDir: "/home/alice", UID: 1000, GID: 1001} got, err := gnome.ResolveConfiguration(identityEnvironment{current: profile.UserIdentity{Username: "root", HomeDir: "/root", UID: 0, GID: 0}, users: map[string]profile.UserIdentity{"alice": alice}, env: map[string]string{"SUDO_USER": "alice", "XDG_STATE_HOME": "/root/state", "XDG_RUNTIME_DIR": "/run/user/0", "DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/0/bus"}}) if err != nil { t.Fatal(err) } if got.UID != 1000 || got.GID != 1001 || got.HomeDir != "/home/alice" || got.ConfigDir != "/home/alice/.config" || got.RuntimeDir != "/run/user/1000" || got.StateFile != "/home/alice/.local/state/proxyctl/gnome.json" { t.Fatalf("%#v", got) } } func TestConfigurationKeepsDirectUsersSafeAbsoluteXDGPaths(t *testing.T) { user := profile.UserIdentity{Username: "alice", HomeDir: "/home/alice", UID: 1000, GID: 1001} got, err := gnome.ResolveConfiguration(identityEnvironment{current: user, env: map[string]string{"XDG_CONFIG_HOME": "/home/alice/config", "XDG_STATE_HOME": "/home/alice/state", "XDG_RUNTIME_DIR": "/run/user/1000", "DBUS_SESSION_BUS_ADDRESS": "unix:path=/custom/bus"}}) if err != nil { t.Fatal(err) } if got.ConfigDir != "/home/alice/config" || got.StateFile != "/home/alice/state/proxyctl/gnome.json" || got.RuntimeDir != "/run/user/1000" || got.SessionBus != "unix:path=/custom/bus" { t.Fatalf("%#v", got) } }