From 560cb626a1a8f7d4bfca5d30f262cc90fb5d4608 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Fri, 10 Apr 2026 20:39:06 +0900 Subject: hst: remove enablement json adapter The go116 behaviour of built-in new function makes this cleaner. Signed-off-by: Ophestra --- hst/config_test.go | 14 +++++++------- hst/enablement.go | 52 ++++++++++++++++++++------------------------------ hst/enablement_test.go | 21 +++++++++----------- hst/hst.go | 2 +- 4 files changed, 38 insertions(+), 51 deletions(-) (limited to 'hst') diff --git a/hst/config_test.go b/hst/config_test.go index 22d764b5..7c6ade8c 100644 --- a/hst/config_test.go +++ b/hst/config_test.go @@ -64,44 +64,44 @@ func TestConfigValidate(t *testing.T) { }}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrEnviron, Msg: `invalid environment variable "TERM\x00"`}}, - {"insecure pulse", &hst.Config{Enablements: hst.NewEnablements(hst.EPulse), Container: &hst.ContainerConfig{ + {"insecure pulse", &hst.Config{Enablements: new(hst.EPulse), Container: &hst.ContainerConfig{ Home: fhs.AbsTmp, Shell: fhs.AbsTmp, Path: fhs.AbsTmp, }}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrInsecure, Msg: "enablement PulseAudio is insecure and no longer supported"}}, - {"direct wayland", &hst.Config{Enablements: hst.NewEnablements(hst.EWayland), DirectWayland: true, Container: &hst.ContainerConfig{ + {"direct wayland", &hst.Config{Enablements: new(hst.EWayland), DirectWayland: true, Container: &hst.ContainerConfig{ Home: fhs.AbsTmp, Shell: fhs.AbsTmp, Path: fhs.AbsTmp, }}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrInsecure, Msg: "direct_wayland is insecure and no longer supported"}}, - {"direct wayland allow", &hst.Config{Enablements: hst.NewEnablements(hst.EWayland), DirectWayland: true, Container: &hst.ContainerConfig{ + {"direct wayland allow", &hst.Config{Enablements: new(hst.EWayland), DirectWayland: true, Container: &hst.ContainerConfig{ Home: fhs.AbsTmp, Shell: fhs.AbsTmp, Path: fhs.AbsTmp, }}, hst.VAllowInsecure, nil}, - {"direct pipewire", &hst.Config{Enablements: hst.NewEnablements(hst.EPipeWire), DirectPipeWire: true, Container: &hst.ContainerConfig{ + {"direct pipewire", &hst.Config{Enablements: new(hst.EPipeWire), DirectPipeWire: true, Container: &hst.ContainerConfig{ Home: fhs.AbsTmp, Shell: fhs.AbsTmp, Path: fhs.AbsTmp, }}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrInsecure, Msg: "direct_pipewire is insecure and no longer supported"}}, - {"direct pipewire allow", &hst.Config{Enablements: hst.NewEnablements(hst.EPipeWire), DirectPipeWire: true, Container: &hst.ContainerConfig{ + {"direct pipewire allow", &hst.Config{Enablements: new(hst.EPipeWire), DirectPipeWire: true, Container: &hst.ContainerConfig{ Home: fhs.AbsTmp, Shell: fhs.AbsTmp, Path: fhs.AbsTmp, }}, hst.VAllowInsecure, nil}, - {"direct pulse", &hst.Config{Enablements: hst.NewEnablements(hst.EPulse), DirectPulse: true, Container: &hst.ContainerConfig{ + {"direct pulse", &hst.Config{Enablements: new(hst.EPulse), DirectPulse: true, Container: &hst.ContainerConfig{ Home: fhs.AbsTmp, Shell: fhs.AbsTmp, Path: fhs.AbsTmp, }}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrInsecure, Msg: "direct_pulse is insecure and no longer supported"}}, - {"direct pulse allow", &hst.Config{Enablements: hst.NewEnablements(hst.EPulse), DirectPulse: true, Container: &hst.ContainerConfig{ + {"direct pulse allow", &hst.Config{Enablements: new(hst.EPulse), DirectPulse: true, Container: &hst.ContainerConfig{ Home: fhs.AbsTmp, Shell: fhs.AbsTmp, Path: fhs.AbsTmp, diff --git a/hst/enablement.go b/hst/enablement.go index 71ddecab..c820ea67 100644 --- a/hst/enablement.go +++ b/hst/enablement.go @@ -7,12 +7,12 @@ import ( "syscall" ) -// Enablement represents an optional host service to export to the target user. -type Enablement byte +// Enablements denotes optional host service to export to the target user. +type Enablements byte const ( // EWayland exposes a Wayland pathname socket via security-context-v1. - EWayland Enablement = 1 << iota + EWayland Enablements = 1 << iota // EX11 adds the target user via X11 ChangeHosts and exposes the X11 // pathname socket. EX11 @@ -28,8 +28,8 @@ const ( EM ) -// String returns a string representation of the flags set on [Enablement]. -func (e Enablement) String() string { +// String returns a string representation of the flags set on [Enablements]. +func (e Enablements) String() string { switch e { case 0: return "(no enablements)" @@ -47,7 +47,7 @@ func (e Enablement) String() string { buf := new(strings.Builder) buf.Grow(32) - for i := Enablement(1); i < EM; i <<= 1 { + for i := Enablements(1); i < EM; i <<= 1 { if e&i != 0 { buf.WriteString(", " + i.String()) } @@ -60,12 +60,6 @@ func (e Enablement) String() string { } } -// NewEnablements returns the address of [Enablement] as [Enablements]. -func NewEnablements(e Enablement) *Enablements { return (*Enablements)(&e) } - -// Enablements is the [json] adapter for [Enablement]. -type Enablements Enablement - // enablementsJSON is the [json] representation of [Enablements]. type enablementsJSON = struct { Wayland bool `json:"wayland,omitempty"` @@ -75,24 +69,21 @@ type enablementsJSON = struct { Pulse bool `json:"pulse,omitempty"` } -// Unwrap returns the underlying [Enablement]. -func (e *Enablements) Unwrap() Enablement { +// Unwrap returns the value pointed to by e. +func (e *Enablements) Unwrap() Enablements { if e == nil { return 0 } - return Enablement(*e) + return *e } -func (e *Enablements) MarshalJSON() ([]byte, error) { - if e == nil { - return nil, syscall.EINVAL - } +func (e Enablements) MarshalJSON() ([]byte, error) { return json.Marshal(&enablementsJSON{ - Wayland: Enablement(*e)&EWayland != 0, - X11: Enablement(*e)&EX11 != 0, - DBus: Enablement(*e)&EDBus != 0, - PipeWire: Enablement(*e)&EPipeWire != 0, - Pulse: Enablement(*e)&EPulse != 0, + Wayland: e&EWayland != 0, + X11: e&EX11 != 0, + DBus: e&EDBus != 0, + PipeWire: e&EPipeWire != 0, + Pulse: e&EPulse != 0, }) } @@ -106,22 +97,21 @@ func (e *Enablements) UnmarshalJSON(data []byte) error { return err } - var ve Enablement + *e = 0 if v.Wayland { - ve |= EWayland + *e |= EWayland } if v.X11 { - ve |= EX11 + *e |= EX11 } if v.DBus { - ve |= EDBus + *e |= EDBus } if v.PipeWire { - ve |= EPipeWire + *e |= EPipeWire } if v.Pulse { - ve |= EPulse + *e |= EPulse } - *e = Enablements(ve) return nil } diff --git a/hst/enablement_test.go b/hst/enablement_test.go index 5ba9b710..166c3d93 100644 --- a/hst/enablement_test.go +++ b/hst/enablement_test.go @@ -13,7 +13,7 @@ func TestEnablementString(t *testing.T) { t.Parallel() testCases := []struct { - flags hst.Enablement + flags hst.Enablements want string }{ {0, "(no enablements)"}, @@ -59,13 +59,13 @@ func TestEnablements(t *testing.T) { sData string }{ {"nil", nil, "null", `{"value":null,"magic":3236757504}`}, - {"zero", hst.NewEnablements(0), `{}`, `{"value":{},"magic":3236757504}`}, - {"wayland", hst.NewEnablements(hst.EWayland), `{"wayland":true}`, `{"value":{"wayland":true},"magic":3236757504}`}, - {"x11", hst.NewEnablements(hst.EX11), `{"x11":true}`, `{"value":{"x11":true},"magic":3236757504}`}, - {"dbus", hst.NewEnablements(hst.EDBus), `{"dbus":true}`, `{"value":{"dbus":true},"magic":3236757504}`}, - {"pipewire", hst.NewEnablements(hst.EPipeWire), `{"pipewire":true}`, `{"value":{"pipewire":true},"magic":3236757504}`}, - {"pulse", hst.NewEnablements(hst.EPulse), `{"pulse":true}`, `{"value":{"pulse":true},"magic":3236757504}`}, - {"all", hst.NewEnablements(hst.EM - 1), `{"wayland":true,"x11":true,"dbus":true,"pipewire":true,"pulse":true}`, `{"value":{"wayland":true,"x11":true,"dbus":true,"pipewire":true,"pulse":true},"magic":3236757504}`}, + {"zero", new(hst.Enablements(0)), `{}`, `{"value":{},"magic":3236757504}`}, + {"wayland", new(hst.EWayland), `{"wayland":true}`, `{"value":{"wayland":true},"magic":3236757504}`}, + {"x11", new(hst.EX11), `{"x11":true}`, `{"value":{"x11":true},"magic":3236757504}`}, + {"dbus", new(hst.EDBus), `{"dbus":true}`, `{"value":{"dbus":true},"magic":3236757504}`}, + {"pipewire", new(hst.EPipeWire), `{"pipewire":true}`, `{"value":{"pipewire":true},"magic":3236757504}`}, + {"pulse", new(hst.EPulse), `{"pulse":true}`, `{"value":{"pulse":true},"magic":3236757504}`}, + {"all", new(hst.EM - 1), `{"wayland":true,"x11":true,"dbus":true,"pipewire":true,"pulse":true}`, `{"value":{"wayland":true,"x11":true,"dbus":true,"pipewire":true,"pulse":true},"magic":3236757504}`}, } for _, tc := range testCases { @@ -137,7 +137,7 @@ func TestEnablements(t *testing.T) { }) t.Run("val", func(t *testing.T) { - if got := hst.NewEnablements(hst.EWayland | hst.EPulse).Unwrap(); got != hst.EWayland|hst.EPulse { + if got := new(hst.EWayland | hst.EPulse).Unwrap(); got != hst.EWayland|hst.EPulse { t.Errorf("Unwrap: %v", got) } }) @@ -146,9 +146,6 @@ func TestEnablements(t *testing.T) { t.Run("passthrough", func(t *testing.T) { t.Parallel() - if _, err := (*hst.Enablements)(nil).MarshalJSON(); !errors.Is(err, syscall.EINVAL) { - t.Errorf("MarshalJSON: error = %v", err) - } if err := (*hst.Enablements)(nil).UnmarshalJSON(nil); !errors.Is(err, syscall.EINVAL) { t.Errorf("UnmarshalJSON: error = %v", err) } diff --git a/hst/hst.go b/hst/hst.go index d2dd7611..95eca8ca 100644 --- a/hst/hst.go +++ b/hst/hst.go @@ -72,7 +72,7 @@ func Template() *Config { return &Config{ ID: "org.chromium.Chromium", - Enablements: NewEnablements(EWayland | EDBus | EPipeWire), + Enablements: new(EWayland | EDBus | EPipeWire), SessionBus: &BusConfig{ See: nil, -- cgit v1.3.1