From d1415305ae6635bfdeec034452cfd61f44c6f1df Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Sun, 29 Sep 2024 15:49:32 +0900 Subject: [PATCH] dbus: test child process handling behaviour via helper stub Signed-off-by: Ophestra Umiker --- dbus/dbus_test.go | 129 +++++++++++++++++++++++++++++++++++----------- dbus/stub_test.go | 11 ++++ 2 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 dbus/stub_test.go diff --git a/dbus/dbus_test.go b/dbus/dbus_test.go index 5ea1467..28f3c24 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)" - 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...), " ") + 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) } }) + + 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) + } + }) + }) + }) + }) } } diff --git a/dbus/stub_test.go b/dbus/stub_test.go new file mode 100644 index 0000000..0dd80bd --- /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() +}