aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/app/app_stub_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/app/app_stub_test.go')
-rw-r--r--internal/app/app_stub_test.go177
1 files changed, 121 insertions, 56 deletions
diff --git a/internal/app/app_stub_test.go b/internal/app/app_stub_test.go
index f386eb01..ff218492 100644
--- a/internal/app/app_stub_test.go
+++ b/internal/app/app_stub_test.go
@@ -1,34 +1,24 @@
-package app_test
+package app
import (
"fmt"
"io/fs"
"log"
+ "os/exec"
"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 (k *stubNixOS) new(func(k syscallDispatcher)) { panic("not implemented") }
-func (s *stubNixOS) Println(v ...any) { log.Println(v...) }
-func (s *stubNixOS) Printf(format string, v ...any) { log.Printf(format, v...) }
+func (k *stubNixOS) getuid() int { return 1971 }
+func (k *stubNixOS) getgid() int { return 100 }
-func (s *stubNixOS) LookupEnv(key string) (string, bool) {
+func (k *stubNixOS) lookupEnv(key string) (string, bool) {
switch key {
case "SHELL":
return "/run/current-system/sw/bin/zsh", true
@@ -40,6 +30,8 @@ func (s *stubNixOS) LookupEnv(key string) (string, bool) {
return "", false
case "HOME":
return "/home/ophestra", true
+ case "XDG_RUNTIME_DIR":
+ return "/run/user/1971", true
case "XDG_CONFIG_HOME":
return "/home/ophestra/xdg/config", true
default:
@@ -47,35 +39,29 @@ func (s *stubNixOS) LookupEnv(key string) (string, bool) {
}
}
-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) {
+func (k *stubNixOS) stat(name string) (fs.FileInfo, error) {
switch name {
- case "video":
- return &user.Group{Gid: "26", Name: "video"}, nil
+ 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:
- return nil, user.UnknownGroupError(name)
+ panic(fmt.Sprintf("attempted to stat unexpected path %q", name))
}
}
-func (s *stubNixOS) ReadDir(name string) ([]fs.DirEntry, error) {
+func (k *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",
@@ -83,6 +69,7 @@ func (s *stubNixOS) ReadDir(name string) ([]fs.DirEntry, error) {
"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",
@@ -96,39 +83,117 @@ func (s *stubNixOS) ReadDir(name string) ([]fs.DirEntry, error) {
"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
+func (k *stubNixOS) tempdir() string { return "/tmp/" }
+
+func (k *stubNixOS) evalSymlinks(path string) (string, error) {
+ switch path {
+ case "/run/user/1971":
+ return "/run/user/1971", nil
+ case "/tmp/hakurei.0":
+ return "/tmp/hakurei.0", nil
+ case "/run/dbus":
+ return "/run/dbus", nil
+ case "/dev/kvm":
+ return "/dev/kvm", nil
+ case "/etc/":
+ return "/etc/", nil
+ case "/bin":
+ return "/bin", nil
+ case "/boot":
+ return "/boot", nil
+ case "/home":
+ return "/home", nil
+ case "/lib":
+ return "/lib", nil
+ case "/lib64":
+ return "/lib64", nil
+ case "/nix":
+ return "/nix", nil
+ case "/root":
+ return "/root", nil
+ case "/run":
+ return "/run", nil
+ case "/srv":
+ return "/srv", nil
+ case "/sys":
+ return "/sys", nil
+ case "/usr":
+ return "/usr", nil
+ case "/var":
+ return "/var", nil
+ case "/dev/dri":
+ return "/dev/dri", nil
+ case "/usr/bin/":
+ return "/usr/bin/", nil
+ case "/nix/store":
+ return "/nix/store", nil
+ case "/run/current-system":
+ return "/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-nixos-system-satori-25.05.99999999.aaaaaaa", nil
+ case "/sys/block":
+ return "/sys/block", nil
+ case "/sys/bus":
+ return "/sys/bus", nil
+ case "/sys/class":
+ return "/sys/class", nil
+ case "/sys/dev":
+ return "/sys/dev", nil
+ case "/sys/devices":
+ return "/sys/devices", nil
+ case "/run/opengl-driver":
+ return "/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-graphics-drivers", nil
+ case "/var/lib/persist/module/hakurei/0/1":
+ return "/var/lib/persist/module/hakurei/0/1", nil
default:
- panic(fmt.Sprintf("attempted to stat unexpected path %q", name))
+ panic(fmt.Sprintf("attempted to evaluate unexpected path %q", path))
+ }
+}
+
+func (k *stubNixOS) lookPath(file string) (string, error) {
+ if k.lookPathErr != nil {
+ if err, ok := k.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) Open(name string) (fs.File, error) {
+func (k *stubNixOS) lookupGroupId(name string) (string, error) {
switch name {
+ case "video":
+ return "26", nil
default:
- panic(fmt.Sprintf("attempted to open unexpected file %q", name))
+ return "", user.UnknownGroupError(name)
}
}
-func (s *stubNixOS) Paths() hst.Paths {
- return hst.Paths{
- SharePath: m("/tmp/hakurei.1971"),
- RuntimePath: m("/run/user/1971"),
- RunDirPath: m("/run/user/1971/hakurei"),
+func (k *stubNixOS) cmdOutput(cmd *exec.Cmd) ([]byte, error) {
+ switch cmd.Path {
+ case "/proc/nonexistent/hsu":
+ return []byte{'0'}, nil
+ default:
+ panic(fmt.Sprintf("unexpected cmd %#v", cmd))
}
}
+
+func (k *stubNixOS) overflowUid() int { return 65534 }
+func (k *stubNixOS) overflowGid() int { return 65534 }
+
+func (k *stubNixOS) mustHsuPath() string { return "/proc/nonexistent/hsu" }
+
+func (k *stubNixOS) fatalf(format string, v ...any) { panic(fmt.Sprintf(format, v...)) }
+
+func (k *stubNixOS) isVerbose() bool { return true }
+func (k *stubNixOS) verbose(v ...any) { log.Print(v...) }
+func (k *stubNixOS) verbosef(format string, v ...any) { log.Printf(format, v...) }