forked from security/hakurei
container: remove custom cmd initialisation
This part of the interface is very unintuitive and only used for testing, even in testing it is inelegant and can be done better. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -1,22 +1,17 @@
|
||||
package dbus_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hakurei.app/container"
|
||||
"hakurei.app/helper"
|
||||
"hakurei.app/internal"
|
||||
"hakurei.app/internal/hlog"
|
||||
"hakurei.app/system/dbus"
|
||||
)
|
||||
|
||||
@@ -64,20 +59,23 @@ func TestFinalise(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProxyStartWaitCloseString(t *testing.T) {
|
||||
oldWaitDelay := helper.WaitDelay
|
||||
helper.WaitDelay = 16 * time.Second
|
||||
t.Cleanup(func() { helper.WaitDelay = oldWaitDelay })
|
||||
|
||||
t.Run("sandbox", func(t *testing.T) {
|
||||
proxyName := dbus.ProxyName
|
||||
dbus.ProxyName = os.Args[0]
|
||||
t.Cleanup(func() { dbus.ProxyName = proxyName })
|
||||
testProxyFinaliseStartWaitCloseString(t, true)
|
||||
})
|
||||
t.Run("sandbox", func(t *testing.T) { testProxyFinaliseStartWaitCloseString(t, true) })
|
||||
t.Run("direct", func(t *testing.T) { testProxyFinaliseStartWaitCloseString(t, false) })
|
||||
}
|
||||
|
||||
func testProxyFinaliseStartWaitCloseString(t *testing.T, useSandbox bool) {
|
||||
{
|
||||
oldWaitDelay := helper.WaitDelay
|
||||
helper.WaitDelay = 16 * time.Second
|
||||
t.Cleanup(func() { helper.WaitDelay = oldWaitDelay })
|
||||
}
|
||||
|
||||
{
|
||||
proxyName := dbus.ProxyName
|
||||
dbus.ProxyName = os.Args[0]
|
||||
t.Cleanup(func() { dbus.ProxyName = proxyName })
|
||||
}
|
||||
|
||||
var p *dbus.Proxy
|
||||
|
||||
t.Run("string for nil proxy", func(t *testing.T) {
|
||||
@@ -122,35 +120,12 @@ func testProxyFinaliseStartWaitCloseString(t *testing.T, useSandbox bool) {
|
||||
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
if !useSandbox {
|
||||
p = dbus.NewDirect(ctx, final, nil)
|
||||
} else {
|
||||
p = dbus.New(ctx, final, nil)
|
||||
}
|
||||
|
||||
p.CommandContext = func(ctx context.Context) (cmd *exec.Cmd) {
|
||||
return exec.CommandContext(ctx, os.Args[0], "-test.v",
|
||||
"-test.run=TestHelperInit", "--", "init")
|
||||
}
|
||||
p.CmdF = func(v any) {
|
||||
if useSandbox {
|
||||
z := v.(*container.Container)
|
||||
if z.Args[0] != dbus.ProxyName {
|
||||
panic(fmt.Sprintf("unexpected argv0 %q", os.Args[0]))
|
||||
}
|
||||
z.Args = append([]string{os.Args[0], "-test.run=TestHelperStub", "--"}, z.Args[1:]...)
|
||||
} else {
|
||||
cmd := v.(*exec.Cmd)
|
||||
if cmd.Args[0] != dbus.ProxyName {
|
||||
panic(fmt.Sprintf("unexpected argv0 %q", os.Args[0]))
|
||||
}
|
||||
cmd.Err = nil
|
||||
cmd.Path = os.Args[0]
|
||||
cmd.Args = append([]string{os.Args[0], "-test.run=TestHelperStub", "--"}, cmd.Args[1:]...)
|
||||
}
|
||||
}
|
||||
p.FilterF = func(v []byte) []byte { return bytes.SplitN(v, []byte("TestHelperInit\n"), 2)[1] }
|
||||
output := new(strings.Builder)
|
||||
if !useSandbox {
|
||||
p = dbus.NewDirect(ctx, final, output)
|
||||
} else {
|
||||
p = dbus.New(ctx, final, output)
|
||||
}
|
||||
|
||||
t.Run("invalid wait", func(t *testing.T) {
|
||||
wantErr := "dbus: not started"
|
||||
@@ -176,9 +151,9 @@ func testProxyFinaliseStartWaitCloseString(t *testing.T, useSandbox bool) {
|
||||
}
|
||||
|
||||
t.Run("string", func(t *testing.T) {
|
||||
wantSubstr := fmt.Sprintf("%s -test.run=TestHelperStub -- --args=3 --fd=4", os.Args[0])
|
||||
wantSubstr := fmt.Sprintf("%s --args=3 --fd=4", os.Args[0])
|
||||
if useSandbox {
|
||||
wantSubstr = fmt.Sprintf(`argv: ["%s" "-test.run=TestHelperStub" "--" "--args=3" "--fd=4"], filter: true, rules: 0, flags: 0x1, presets: 0xf`, os.Args[0])
|
||||
wantSubstr = fmt.Sprintf(`argv: ["%s" "--args=3" "--fd=4"], filter: true, rules: 0, flags: 0x1, presets: 0xf`, os.Args[0])
|
||||
}
|
||||
if got := p.String(); !strings.Contains(got, wantSubstr) {
|
||||
t.Errorf("String: %q, want %q",
|
||||
@@ -203,11 +178,3 @@ func testProxyFinaliseStartWaitCloseString(t *testing.T, useSandbox bool) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelperInit(t *testing.T) {
|
||||
if len(os.Args) != 5 || os.Args[4] != "init" {
|
||||
return
|
||||
}
|
||||
container.SetOutput(hlog.Output{})
|
||||
container.Init(hlog.Prepare, internal.InstallOutput)
|
||||
}
|
||||
|
||||
@@ -36,9 +36,6 @@ func (p *Proxy) Start() error {
|
||||
|
||||
if !p.useSandbox {
|
||||
p.helper = helper.NewDirect(ctx, p.name, p.final, true, argF, func(cmd *exec.Cmd) {
|
||||
if p.CmdF != nil {
|
||||
p.CmdF(cmd)
|
||||
}
|
||||
if p.output != nil {
|
||||
cmd.Stdout, cmd.Stderr = p.output, p.output
|
||||
}
|
||||
@@ -56,7 +53,7 @@ func (p *Proxy) Start() error {
|
||||
}
|
||||
|
||||
var libPaths []string
|
||||
if entries, err := ldd.ExecFilter(ctx, p.CommandContext, p.FilterF, toolPath); err != nil {
|
||||
if entries, err := ldd.Exec(ctx, toolPath); err != nil {
|
||||
return err
|
||||
} else {
|
||||
libPaths = ldd.Path(entries)
|
||||
@@ -69,15 +66,10 @@ func (p *Proxy) Start() error {
|
||||
z.SeccompFlags |= seccomp.AllowMultiarch
|
||||
z.SeccompPresets |= seccomp.PresetStrict
|
||||
z.Hostname = "hakurei-dbus"
|
||||
z.CommandContext = p.CommandContext
|
||||
if p.output != nil {
|
||||
z.Stdout, z.Stderr = p.output, p.output
|
||||
}
|
||||
|
||||
if p.CmdF != nil {
|
||||
p.CmdF(z)
|
||||
}
|
||||
|
||||
// these lib paths are unpredictable, so mount them first so they cannot cover anything
|
||||
for _, name := range libPaths {
|
||||
z.Bind(name, name, 0)
|
||||
|
||||
17
system/dbus/proc_test.go
Normal file
17
system/dbus/proc_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package dbus_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"hakurei.app/container"
|
||||
"hakurei.app/helper"
|
||||
"hakurei.app/internal"
|
||||
"hakurei.app/internal/hlog"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
container.TryArgv0(hlog.Output{}, hlog.Prepare, internal.InstallOutput)
|
||||
helper.InternalHelperStub()
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
@@ -37,10 +36,6 @@ type Proxy struct {
|
||||
useSandbox bool
|
||||
|
||||
name string
|
||||
CmdF func(any)
|
||||
|
||||
CommandContext func(ctx context.Context) (cmd *exec.Cmd)
|
||||
FilterF func([]byte) []byte
|
||||
|
||||
mu, pmu sync.RWMutex
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package dbus_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"hakurei.app/helper"
|
||||
)
|
||||
|
||||
func TestHelperStub(t *testing.T) { helper.InternalHelperStub() }
|
||||
Reference in New Issue
Block a user