From b607cf1df6fb3d52f62b6f9a96bb46a4d2eee5ca Mon Sep 17 00:00:00 2001 From: Ophestra Date: Fri, 20 Nov 2026 01:04:49 +0900 Subject: internal/testsuite: accept flags in hakurei helper This avoids changing function signature when adding new flags. Signed-off-by: Ophestra --- cmd/hakurei/testsuite/main.go | 50 +++++++++++++++++------------------ cmd/hakurei/testsuite/sandbox/main.go | 2 +- internal/testsuite/testsuite.go | 31 +++++++++++++++------- 3 files changed, 46 insertions(+), 37 deletions(-) diff --git a/cmd/hakurei/testsuite/main.go b/cmd/hakurei/testsuite/main.go index 7f8a14b3..220b8a2a 100644 --- a/cmd/hakurei/testsuite/main.go +++ b/cmd/hakurei/testsuite/main.go @@ -75,15 +75,14 @@ func checkHakurei( dbusEnv string, name, long string, - scan, verbose, insecure bool, - retry int, + flags, retry int, processConfig func(c *hst.Config), f func(p hst.State, pid int) error, checkOutput func(output string) bool, wantCode ...int, ) { - if !scan && f != nil { + if flags&testsuite.SHScan == 0 && f != nil { log.Fatalf("(%s) cannot inspect without scanning", name) } if checkOutput == nil { @@ -137,12 +136,11 @@ func checkHakurei( checkHakurei( ctx, wg, cred, dbusEnv, name, long, - scan, verbose, insecure, - retry-1, + flags, retry-1, processConfig, f, checkOutput, wantCode..., ) - }, scan, false, verbose, insecure, c, + }, flags, c, ) if f != nil { @@ -151,7 +149,7 @@ func checkHakurei( } } - if scan { + if flags&testsuite.SHScan != 0 { testsuite.MustRun( cred, testsuite.SessionEnv(dbusEnv), "hakurei", "kill", p.ID.String(), @@ -351,7 +349,7 @@ func main() { output, ) } - }, false, false, false, false, &hst.Config{ + }, 0, &hst.Config{ Container: &hst.ContainerConfig{ Shell: absNonexistent, Home: absNonexistent, @@ -366,13 +364,13 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "indirect-termination", "indirect termination", - true, false, false, 0, nil, nil, nil, + testsuite.SHScan, 0, nil, nil, nil, ) checkHakurei( ctx, &wg, &cred, dbusEnv, "immediate-termination", "immediate termination", - true, false, false, 0, func(c *hst.Config) { + testsuite.SHScan, 0, func(c *hst.Config) { c.Container.WaitDelay = -1 }, nil, nil, 254, ) @@ -380,7 +378,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "shim-poke", "unexpected shim notification", - true, true, false, 0, nil, func(p hst.State, pid int) error { + testsuite.SHScan|testsuite.SHVerbose, 0, nil, func(p hst.State, pid int) error { if err := syscall.Kill(p.ShimPID, syscall.SIGCONT); err != nil { return os.NewSyscallError("kill", err) } @@ -396,7 +394,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "reject-deprecated", "reject insecure configuration", - false, false, false, 0, func(c *hst.Config) { + 0, 0, func(c *hst.Config) { c.Enablements = new(hst.EPulse) }, nil, func(output string) bool { return output == "hakurei: enablement PulseAudio is insecure "+ @@ -407,7 +405,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "pipewire-pulse", "pipewire proxying behaviour", - false, false, false, 0, func(c *hst.Config) { + 0, 0, func(c *hst.Config) { c.Enablements = new(hst.EPipeWire) c.Container.Path = prefix.Append("bin", "pactl") c.Container.Args = []string{"pactl", "info"} @@ -451,7 +449,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "pipewire-security", "pipewire SecurityContext", - false, false, false, 32, func(c *hst.Config) { + 0, 32, func(c *hst.Config) { c.Enablements = new(hst.EPipeWire) c.Container.Path = prefix.Append("bin", "pactl") c.Container.Args = []string{ @@ -468,7 +466,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "pipewire-direct-failure", "pipewire cover direct access", - false, false, false, 0, func(c *hst.Config) { + 0, 0, func(c *hst.Config) { c.Enablements = new(hst.EPipeWire) c.Container.Path = prefix.Append("bin", "pw-dump") c.Container.Args = []string{"pw-dump"} @@ -480,7 +478,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "pipewire-direct", "pipewire direct access", - false, false, true, 0, func(c *hst.Config) { + testsuite.SHInsecure, 0, func(c *hst.Config) { c.Enablements = new(hst.EPipeWire) c.DirectPipeWire = true c.Container.Path = prefix.Append("bin", "pw-dump") @@ -493,7 +491,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "wayland-client", "wayland client", - true, false, false, 0, func(c *hst.Config) { + testsuite.SHScan, 0, func(c *hst.Config) { c.Enablements = new(hst.EWayland) c.Container.Path = prefix.Append("bin", "foot") c.Container.Args = []string{"foot"} @@ -511,7 +509,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "wayland-security-context", "wayland security-context-v1", - false, false, false, 0, func(c *hst.Config) { + 0, 0, func(c *hst.Config) { c.Enablements = new(hst.EWayland) c.Container.Path = prefix.Append("bin", "wayland-info") c.Container.Args = []string{"wayland-info"} @@ -526,7 +524,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "wlr-screencopy-unstable-v1", "direct wlr-screencopy-unstable-v1", - false, false, true, 0, func(c *hst.Config) { + testsuite.SHInsecure, 0, func(c *hst.Config) { c.Enablements = new(hst.EWayland) c.DirectWayland = true c.Container.Path = prefix.Append("bin", "wayland-info") @@ -544,7 +542,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "direct-wayland-client", "direct wayland client", - true, false, true, 0, func(c *hst.Config) { + testsuite.SHScan|testsuite.SHInsecure, 0, func(c *hst.Config) { c.Enablements = new(hst.EWayland) c.DirectWayland = true c.Container.Path = prefix.Append("bin", "foot") @@ -564,7 +562,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "xwayland-client", "xwayland client", - false, false, false, 32, func(c *hst.Config) { + 0, 32, func(c *hst.Config) { c.Enablements = new(hst.EX11) c.Container.Path = prefix.Append("bin", "xset") c.Container.Args = []string{"xset", "q"} @@ -577,7 +575,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "dbus", "proxied dbus call", - false, false, false, 0, func(c *hst.Config) { + 0, 0, func(c *hst.Config) { c.Enablements = new(hst.EDBus) c.SessionBus = &hst.BusConfig{ Talk: []string{"org.freedesktop.Notifications"}, @@ -596,7 +594,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "dbus reject", "rejected dbus call", - false, false, false, 0, func(c *hst.Config) { + 0, 0, func(c *hst.Config) { c.Enablements = new(hst.EDBus) c.SessionBus = &hst.BusConfig{Filter: true} c.Container.Path = prefix.Append("bin", "notify-send") @@ -722,7 +720,7 @@ func main() { checkHakurei( ctx, &wg, &cred, dbusEnv, "setscheduler-"+strconv.Itoa(int(policy)), "setscheduler "+strconv.Quote(policy.String()), - true, false, false, 0, func(c *hst.Config) { + testsuite.SHScan, 0, func(c *hst.Config) { c.SchedPolicy = _policy }, func(p hst.State, pid int) error { got := -1 @@ -842,7 +840,7 @@ func main() { "--numeric", "/tmp/hakurei.0/runtime/"+strconv.Itoa(c.Identity), )) - }, true, false, false, false, c, + }, testsuite.SHScan, c, ) validatePresent("after first process") @@ -852,7 +850,7 @@ func main() { defer close(done) onDone(err, output) validatePresent("after second process termination") - }, true, false, false, false, c, + }, testsuite.SHScan, c, ) validatePresent("after second process") diff --git a/cmd/hakurei/testsuite/sandbox/main.go b/cmd/hakurei/testsuite/sandbox/main.go index 681718a7..4eb36f8c 100644 --- a/cmd/hakurei/testsuite/sandbox/main.go +++ b/cmd/hakurei/testsuite/sandbox/main.go @@ -359,7 +359,7 @@ func main() { if err != nil { log.Fatalf("test case %s: %v\n%s", name, err, output) } - }, true, true, false, false, &tc.Hakurei, + }, testsuite.SHScan|testsuite.SHWait, &tc.Hakurei, ) if err != nil { diff --git a/internal/testsuite/testsuite.go b/internal/testsuite/testsuite.go index 0b7594ba..aada7721 100644 --- a/internal/testsuite/testsuite.go +++ b/internal/testsuite/testsuite.go @@ -716,6 +716,17 @@ func LookupStore(cred *syscall.Credential, id hst.ID, p *hst.State) error { return <-done } +const ( + // SHScan enables startup notification and proc scanning for [StartHakurei]. + SHScan = 1 << iota + // SHWait blocks [StartHakurei] on notify pipe. + SHWait + // SHVerbose enables hakurei verbose output in [StartHakurei]. + SHVerbose + // SHInsecure enables insecure configuration in [StartHakurei]. + SHInsecure +) + // StartHakurei starts a hakurei container configured by c. func StartHakurei( ctx context.Context, @@ -724,19 +735,19 @@ func StartHakurei( dbusEnv string, processCommand func(cmd *exec.Cmd), onDone func(err error, output string), - scan, wait, verbose, insecure bool, + flags int, c *hst.Config, ) (p hst.State, pid int, err error) { var hakureiFlags string - if verbose { + if flags&SHVerbose != 0 { hakureiFlags += " -v" } - if insecure { + if flags&SHInsecure != 0 { hakureiFlags += " --insecure" } var notifySuffix string - if wait { + if flags&SHWait != 0 { notifySuffix += " 1>&3" } @@ -796,7 +807,7 @@ func StartHakurei( return } - if !scan && len(cmd.ExtraFiles) != 3 { + if flags&SHScan == 0 && len(cmd.ExtraFiles) != 3 { err = errors.Join(notify.Close(), ident.Close()) return } @@ -804,7 +815,7 @@ func StartHakurei( var id hst.ID if _, err = io.ReadFull(ident, id[:]); err != nil { _, _ = notify.Close(), ident.Close() - if !scan && errors.Is(err, io.EOF) { + if flags&SHScan == 0 && errors.Is(err, io.EOF) { err = nil } return @@ -813,12 +824,12 @@ func StartHakurei( return } - if !scan { + if flags&SHScan == 0 { err = notify.Close() return } - if wait { + if flags&SHWait != 0 { if _, err = io.ReadFull(notify, make([]byte, 8)); err != nil { _ = notify.Close() return @@ -918,7 +929,7 @@ func MustStartHakurei( cred *syscall.Credential, dbusEnv string, onDone func(err error, output string), - scan, wait, verbose, insecure bool, + flags int, c *hst.Config, ) (p hst.State, pid int) { _ctx, cancel := context.WithCancel(ctx) @@ -929,7 +940,7 @@ func MustStartHakurei( nil, func(err error, output string) { defer cancel() onDone(err, output) - }, scan, wait, verbose, insecure, c, + }, flags, c, ) if err != nil { log.Printf( -- cgit v1.3.1