aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/stub_test.go
diff options
context:
space:
mode:
authorOphestra Umiker <cat@ophivana.moe>2024-09-29 14:40:01 +0900
committerOphestra Umiker <cat@ophivana.moe>2024-09-29 14:40:01 +0900
commitd530a9e9f9bd85ea18a688d879d0558cd56fb8bd (patch)
treeae23026d042642a01cf21b930e64f8545ce1142a /helper/stub_test.go
parent0e7849fac2fa42bc23dc97e419d8c00b06c8db37 (diff)
helper: stub helper for tests
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'helper/stub_test.go')
-rw-r--r--helper/stub_test.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/helper/stub_test.go b/helper/stub_test.go
new file mode 100644
index 00000000..9f85ac65
--- /dev/null
+++ b/helper/stub_test.go
@@ -0,0 +1,75 @@
+package helper_test
+
+import (
+ "io"
+ "os"
+ "strconv"
+ "syscall"
+ "testing"
+
+ "git.ophivana.moe/cat/fortify/helper"
+)
+
+func TestHelperChildStub(t *testing.T) {
+ // this test mocks the helper process
+ if os.Getenv(helper.FortifyHelper) != "1" {
+ return
+ }
+ // simulate args pipe behaviour
+ func() {
+ f := os.NewFile(3, "|0")
+ if f == nil {
+ panic("attempted to start helper without args pipe")
+ }
+
+ if _, err := io.Copy(os.Stdout, f); err != nil {
+ panic("cannot read args: " + err.Error())
+ }
+ }()
+
+ var wait chan struct{}
+
+ // simulate status pipe behaviour
+ if os.Getenv(helper.FortifyStatus) == "1" {
+ wait = make(chan struct{})
+ go func() {
+ f := os.NewFile(4, "|1")
+ if f == nil {
+ panic("attempted to start with status reporting without status pipe")
+ }
+
+ if _, err := f.Write([]byte{'x'}); err != nil {
+ panic("cannot write to status pipe: " + err.Error())
+ }
+
+ // wait for status pipe close
+ var epoll int
+ if fd, err := syscall.EpollCreate1(0); err != nil {
+ panic("cannot open epoll fd: " + err.Error())
+ } else {
+ defer func() {
+ if err = syscall.Close(fd); err != nil {
+ panic("cannot close epoll fd: " + err.Error())
+ }
+ }()
+ epoll = fd
+ }
+ if err := syscall.EpollCtl(epoll, syscall.EPOLL_CTL_ADD, int(f.Fd()), &syscall.EpollEvent{}); err != nil {
+ panic("cannot add status pipe to epoll: " + err.Error())
+ }
+ events := make([]syscall.EpollEvent, 1)
+ if _, err := syscall.EpollWait(epoll, events, -1); err != nil {
+ panic("cannot poll status pipe: " + err.Error())
+ }
+ if events[0].Events != syscall.EPOLLERR {
+ panic(strconv.Itoa(int(events[0].Events)))
+
+ }
+ close(wait)
+ }()
+ }
+
+ if wait != nil {
+ <-wait
+ }
+}