You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
subgraph-oz/ipc/ipc_test.go

100 lines
1.9 KiB

package ipc
10 years ago
import (
"os"
10 years ago
"sync"
"testing"
)
type TestMsg struct {
t int "Test"
}
type testConnection struct {
10 years ago
server *MsgServer
client *MsgConn
10 years ago
wg sync.WaitGroup
called bool
}
type testServer struct {
conn *MsgConn
10 years ago
wg sync.WaitGroup
}
10 years ago
const testSocket = "@test"
10 years ago
var testFactory = NewMsgFactory(new(TestMsg))
func testConnect(handler func(*TestMsg, *Message) error) (*testConnection, error) {
10 years ago
tc := &testConnection{}
wrapper := func(tm *TestMsg, msg *Message) error {
err := handler(tm, msg)
tc.called = true
tc.wg.Done()
return err
}
10 years ago
s, err := NewServer(testSocket, testFactory, nil, wrapper)
10 years ago
if err != nil {
return nil, err
}
10 years ago
c, err := Connect(testSocket, testFactory, nil)
10 years ago
if err != nil {
return nil, err
}
10 years ago
tc.server = s
tc.client = c
tc.wg.Add(1)
go tc.server.Run()
return tc, nil
}
func runTest(t *testing.T, handler func(*TestMsg, *Message) error, tester func(*testConnection)) {
10 years ago
tc, err := testConnect(handler)
if err != nil {
t.Error("error setting up test connection:", err)
}
tester(tc)
tc.wait()
if !tc.called {
t.Error("handler function not called")
}
}
func (tc *testConnection) wait() {
tc.wg.Wait()
tc.client.Close()
tc.server.Close()
}
func TestUcred(t *testing.T) {
handler := func(tm *TestMsg, msg *Message) error {
uid := uint32(os.Getuid())
gid := uint32(os.Getgid())
pid := int32(os.Getpid())
u := msg.Ucred
if u.Uid != uid || u.Gid != gid || u.Pid != pid {
t.Errorf("ucred (%d/%d/%d) does not match process (%d/%d/%d)", u.Uid, u.Gid, u.Pid, uid, gid, pid)
}
return nil
}
runTest(t, handler, func(tc *testConnection) {
tc.client.SendMsg(&TestMsg{})
})
}
func TestPassFDs(t *testing.T) {
10 years ago
fds := []int{1, 2}
handler := func(tm *TestMsg, msg *Message) error {
if len(msg.Fds) != len(fds) {
t.Errorf("Expecting %d descriptors, got %d", len(fds), len(msg.Fds))
}
return nil
}
runTest(t, handler, func(tc *testConnection) {
tc.client.SendMsg(&TestMsg{}, fds...)
})
10 years ago
}