aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/app/app_stub_linux_test.go
blob: 0efc585d3ad3a636c44a3cc6b412120d8327b3ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package app_test

import (
	"fmt"
	"io/fs"
	"log"
	"os/user"
	"strconv"

	"hakurei.app/hst"
)

// fs methods are not implemented using a real FS
// to help better understand filesystem access behaviour
type stubNixOS struct {
	lookPathErr map[string]error
	usernameErr map[string]error
}

func (s *stubNixOS) Getuid() int                              { return 1971 }
func (s *stubNixOS) Getgid() int                              { return 100 }
func (s *stubNixOS) TempDir() string                          { return "/tmp" }
func (s *stubNixOS) MustExecutable() string                   { return "/run/wrappers/bin/hakurei" }
func (s *stubNixOS) Exit(code int)                            { panic("called exit on stub with code " + strconv.Itoa(code)) }
func (s *stubNixOS) EvalSymlinks(path string) (string, error) { return path, nil }
func (s *stubNixOS) Uid(aid int) (int, error)                 { return 1000000 + 0*10000 + aid, nil }

func (s *stubNixOS) Println(v ...any)               { log.Println(v...) }
func (s *stubNixOS) Printf(format string, v ...any) { log.Printf(format, v...) }

func (s *stubNixOS) LookupEnv(key string) (string, bool) {
	switch key {
	case "SHELL":
		return "/run/current-system/sw/bin/zsh", true
	case "TERM":
		return "xterm-256color", true
	case "WAYLAND_DISPLAY":
		return "wayland-0", true
	case "PULSE_COOKIE":
		return "", false
	case "HOME":
		return "/home/ophestra", true
	case "XDG_CONFIG_HOME":
		return "/home/ophestra/xdg/config", true
	default:
		panic(fmt.Sprintf("attempted to access unexpected environment variable %q", key))
	}
}

func (s *stubNixOS) LookPath(file string) (string, error) {
	if s.lookPathErr != nil {
		if err, ok := s.lookPathErr[file]; ok {
			return "", err
		}
	}

	switch file {
	case "zsh":
		return "/run/current-system/sw/bin/zsh", nil
	default:
		panic(fmt.Sprintf("attempted to look up unexpected executable %q", file))
	}
}

func (s *stubNixOS) LookupGroup(name string) (*user.Group, error) {
	switch name {
	case "video":
		return &user.Group{Gid: "26", Name: "video"}, nil
	default:
		return nil, user.UnknownGroupError(name)
	}
}

func (s *stubNixOS) ReadDir(name string) ([]fs.DirEntry, error) {
	switch name {
	case "/":
		return stubDirEntries("bin", "boot", "dev", "etc", "home", "lib",
			"lib64", "nix", "proc", "root", "run", "srv", "sys", "tmp", "usr", "var")
	case "/run":
		return stubDirEntries("agetty.reload", "binfmt", "booted-system",
			"credentials", "cryptsetup", "current-system", "dbus", "host", "keys",
			"libvirt", "libvirtd.pid", "lock", "log", "lvm", "mount", "NetworkManager",
			"nginx", "nixos", "nscd", "opengl-driver", "pppd", "resolvconf", "sddm",
			"store", "syncoid", "system", "systemd", "tmpfiles.d", "udev", "udisks2",
			"user", "utmp", "virtlogd.pid", "wrappers", "zed.pid", "zed.state")
	case "/etc":
		return stubDirEntries("alsa", "bashrc", "binfmt.d", "dbus-1", "default",
			"ethertypes", "fonts", "fstab", "fuse.conf", "group", "host.conf", "hostid",
			"hostname", "hostname.CHECKSUM", "hosts", "inputrc", "ipsec.d", "issue", "kbd",
			"libblockdev", "locale.conf", "localtime", "login.defs", "lsb-release", "lvm",
			"machine-id", "man_db.conf", "modprobe.d", "modules-load.d", "mtab", "nanorc",
			"netgroup", "NetworkManager", "nix", "nixos", "NIXOS", "nscd.conf", "nsswitch.conf",
			"opensnitchd", "os-release", "pam", "pam.d", "passwd", "pipewire", "pki", "polkit-1",
			"profile", "protocols", "qemu", "resolv.conf", "resolvconf.conf", "rpc", "samba",
			"sddm.conf", "secureboot", "services", "set-environment", "shadow", "shells", "ssh",
			"ssl", "static", "subgid", "subuid", "sudoers", "sysctl.d", "systemd", "terminfo",
			"tmpfiles.d", "udev", "udisks2", "UPower", "vconsole.conf", "X11", "zfs", "zinputrc",
			"zoneinfo", "zprofile", "zshenv", "zshrc")
	default:
		panic(fmt.Sprintf("attempted to read unexpected directory %q", name))
	}
}

func (s *stubNixOS) Stat(name string) (fs.FileInfo, error) {
	switch name {
	case "/var/run/nscd":
		return nil, nil
	case "/run/user/1971/pulse":
		return nil, nil
	case "/run/user/1971/pulse/native":
		return stubFileInfoMode(0666), nil
	case "/home/ophestra/.pulse-cookie":
		return stubFileInfoIsDir(true), nil
	case "/home/ophestra/xdg/config/pulse/cookie":
		return stubFileInfoIsDir(false), nil
	default:
		panic(fmt.Sprintf("attempted to stat unexpected path %q", name))
	}
}

func (s *stubNixOS) Open(name string) (fs.File, error) {
	switch name {
	default:
		panic(fmt.Sprintf("attempted to open unexpected file %q", name))
	}
}

func (s *stubNixOS) Paths() hst.Paths {
	return hst.Paths{
		SharePath:   "/tmp/hakurei.1971",
		RuntimePath: "/run/user/1971",
		RunDirPath:  "/run/user/1971/hakurei",
	}
}