dbus: test child process handling behaviour via helper stub

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra 2024-09-29 15:49:32 +09:00
parent 98f9fdb7cc
commit d1415305ae
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 109 additions and 31 deletions

View File

@ -43,6 +43,20 @@ func TestNew(t *testing.T) {
} }
func TestProxy_Seal(t *testing.T) { func TestProxy_Seal(t *testing.T) {
t.Run("double seal panic", func(t *testing.T) {
defer func() {
want := "dbus proxy sealed twice"
if r := recover(); r != want {
t.Errorf("Seal: panic = %q, want %q",
r, want)
}
}()
p := dbus.New(binPath, [2]string{}, [2]string{})
_ = p.Seal(dbus.NewConfig("", true, false), nil)
_ = p.Seal(dbus.NewConfig("", true, false), nil)
})
ep := dbus.New(binPath, [2]string{}, [2]string{}) ep := dbus.New(binPath, [2]string{}, [2]string{})
if err := ep.Seal(nil, nil); !errors.Is(err, dbus.ErrConfig) { if err := ep.Seal(nil, nil); !errors.Is(err, dbus.ErrConfig) {
t.Errorf("Seal(nil, nil) error = %v, want %v", t.Errorf("Seal(nil, nil) error = %v, want %v",
@ -87,47 +101,100 @@ func TestProxy_Seal(t *testing.T) {
} }
} }
func TestProxy_Seal_Panic(t *testing.T) { func TestProxy_Start_Wait_Close_String(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Seal: did not panic from repeated seal")
}
}()
p := dbus.New(binPath, [2]string{}, [2]string{})
_ = p.Seal(dbus.NewConfig("", true, false), nil)
_ = p.Seal(dbus.NewConfig("", true, false), nil)
}
func TestProxy_String(t *testing.T) {
for id, tc := range testCasePairs() { for id, tc := range testCasePairs() {
// this test does not test errors // this test does not test errors
if tc[0].wantErr { if tc[0].wantErr {
continue continue
} }
t.Run("strings for "+id, func(t *testing.T) { t.Run("string for nil proxy", func(t *testing.T) {
p := dbus.New(binPath, tc[0].bus, tc[1].bus) var p *dbus.Proxy
want := "(invalid dbus proxy)"
// test unsealed behaviour
want := "(unsealed dbus proxy)"
if got := p.String(); got != want {
t.Errorf("String() = %v, want %v",
got, want)
}
if err := p.Seal(tc[0].c, tc[1].c); err != nil {
t.Errorf("Seal(%p, %p) error = %v, wantErr %v",
tc[0].c, tc[1].c,
err, tc[0].wantErr)
}
// test sealed behaviour
want = strings.Join(append(tc[0].want, tc[1].want...), " ")
if got := p.String(); got != want { if got := p.String(); got != want {
t.Errorf("String() = %v, want %v", t.Errorf("String() = %v, want %v",
got, want) got, want)
} }
}) })
t.Run("proxy for "+id, func(t *testing.T) {
helper.InternalReplaceExecCommand(t)
p := dbus.New(binPath, tc[0].bus, tc[1].bus)
t.Run("unsealed behaviour of "+id, func(t *testing.T) {
t.Run("unsealed string of "+id, func(t *testing.T) {
want := "(unsealed dbus proxy)"
if got := p.String(); got != want {
t.Errorf("String() = %v, want %v",
got, want)
return
}
})
t.Run("unsealed wait of "+id, func(t *testing.T) {
wantErr := "proxy not started"
if err := p.Wait(); err == nil || err.Error() != wantErr {
t.Errorf("Wait() error = %v, wantErr %v",
err, wantErr)
}
})
})
t.Run("seal with "+id, func(t *testing.T) {
if err := p.Seal(tc[0].c, tc[1].c); err != nil {
t.Errorf("Seal(%p, %p) error = %v, wantErr %v",
tc[0].c, tc[1].c,
err, tc[0].wantErr)
return
}
})
t.Run("sealed behaviour of "+id, func(t *testing.T) {
want := strings.Join(append(tc[0].want, tc[1].want...), " ")
if got := p.String(); got != want {
t.Errorf("String() = %v, want %v",
got, want)
return
}
t.Run("sealed start of "+id, func(t *testing.T) {
if err := p.Start(nil, nil); err != nil {
t.Errorf("Start(nil, nil) error = %v",
err)
}
t.Run("started string of "+id, func(t *testing.T) {
wantSubstr := binPath + " --args=3 --fd=4"
if got := p.String(); !strings.Contains(got, wantSubstr) {
t.Errorf("String() = %v, want %v",
p.String(), wantSubstr)
return
}
})
t.Run("sealed closing of "+id+" without status", func(t *testing.T) {
wantPanic := "attempted to close helper with no status pipe"
defer func() {
if r := recover(); r != wantPanic {
t.Errorf("Close() panic = %v, wantPanic %v",
r, wantPanic)
}
}()
if err := p.Close(); err != nil {
t.Errorf("Close() error = %v",
err)
}
})
t.Run("started wait of "+id, func(t *testing.T) {
if err := p.Wait(); err != nil {
t.Errorf("Wait() error = %v",
err)
}
})
})
})
})
} }
} }

11
dbus/stub_test.go Normal file
View File

@ -0,0 +1,11 @@
package dbus_test
import (
"testing"
"git.ophivana.moe/cat/fortify/helper"
)
func TestHelperChildStub(t *testing.T) {
helper.InternalChildStub()
}