aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOphestra Umiker <cat@ophivana.moe>2024-09-29 15:49:32 +0900
committerOphestra Umiker <cat@ophivana.moe>2024-09-29 15:49:32 +0900
commitd1415305ae6635bfdeec034452cfd61f44c6f1df (patch)
tree99fbac6f3bba6e53ff2d2b7becd03866bddeb6d3
parent98f9fdb7cc949469d34a1428c3dd093ebf6aa4eb (diff)
dbus: test child process handling behaviour via helper stub
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
-rw-r--r--dbus/dbus_test.go125
-rw-r--r--dbus/stub_test.go11
2 files changed, 107 insertions, 29 deletions
diff --git a/dbus/dbus_test.go b/dbus/dbus_test.go
index 5ea14670..28f3c244 100644
--- a/dbus/dbus_test.go
+++ b/dbus/dbus_test.go
@@ -43,6 +43,20 @@ func TestNew(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{})
if err := ep.Seal(nil, nil); !errors.Is(err, dbus.ErrConfig) {
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) {
- 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) {
+func TestProxy_Start_Wait_Close_String(t *testing.T) {
for id, tc := range testCasePairs() {
// this test does not test errors
if tc[0].wantErr {
continue
}
- t.Run("strings for "+id, func(t *testing.T) {
- p := dbus.New(binPath, tc[0].bus, tc[1].bus)
-
- // test unsealed behaviour
- want := "(unsealed dbus proxy)"
+ t.Run("string for nil proxy", func(t *testing.T) {
+ var p *dbus.Proxy
+ want := "(invalid 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)
- }
+ t.Run("proxy for "+id, func(t *testing.T) {
+ helper.InternalReplaceExecCommand(t)
+ p := dbus.New(binPath, tc[0].bus, tc[1].bus)
- // test sealed behaviour
- want = strings.Join(append(tc[0].want, tc[1].want...), " ")
- if got := p.String(); got != want {
- t.Errorf("String() = %v, want %v",
- got, want)
- }
+ 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)
+ }
+ })
+ })
+ })
})
}
}
diff --git a/dbus/stub_test.go b/dbus/stub_test.go
new file mode 100644
index 00000000..0dd80bdc
--- /dev/null
+++ b/dbus/stub_test.go
@@ -0,0 +1,11 @@
+package dbus_test
+
+import (
+ "testing"
+
+ "git.ophivana.moe/cat/fortify/helper"
+)
+
+func TestHelperChildStub(t *testing.T) {
+ helper.InternalChildStub()
+}