Files
proxy/internal/shell/configuration_test.go

52 lines
2.1 KiB
Go

package shell_test
import (
"path/filepath"
"testing"
"proxyctl/internal/profile"
"proxyctl/internal/shell"
)
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) {
value, ok := e.env[name]
return value, ok
}
func TestShellConfigurationUsesSudoInvokingUserAndRejectsRootXDGOverrides(t *testing.T) {
owner := profile.UserIdentity{Username: "alice", HomeDir: "/home/alice", UID: 1000, GID: 1000}
got, err := shell.ResolveConfiguration(identityEnvironment{
current: profile.UserIdentity{Username: "root", HomeDir: "/root", UID: 0, GID: 0}, users: map[string]profile.UserIdentity{"alice": owner},
env: map[string]string{"SUDO_USER": "alice", "SHELL": "/bin/zsh", "XDG_CONFIG_HOME": "/root/config", "XDG_STATE_HOME": "/root/state"},
})
if err != nil {
t.Fatal(err)
}
if got.Shell != "/bin/zsh" || got.StartupFile != "/home/alice/.zshrc" || got.EnvironmentFile != "/home/alice/.config/proxyctl/shell.env" || got.RecoveryFile != "/home/alice/.local/state/proxyctl/shell.json" || got.UID != 1000 || !got.OwnFiles {
t.Fatalf("sudo invoking-user configuration = %#v", got)
}
}
func TestShellConfigurationUsesDirectRootIdentityAndAbsoluteXDG(t *testing.T) {
got, err := shell.ResolveConfiguration(identityEnvironment{
current: profile.UserIdentity{Username: "root", HomeDir: "/root", UID: 0, GID: 0},
env: map[string]string{"SHELL": "/bin/bash", "XDG_CONFIG_HOME": "/var/root-config", "XDG_STATE_HOME": "/var/root-state"},
})
if err != nil {
t.Fatal(err)
}
if got.StartupFile != "/root/.bashrc" || got.EnvironmentFile != filepath.Join("/var/root-config", "proxyctl", "shell.env") || got.RecoveryFile != filepath.Join("/var/root-state", "proxyctl", "shell.json") || got.UID != 0 {
t.Fatalf("direct-root configuration = %#v", got)
}
}