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

104 lines
2.0 KiB

package ipc
10 years ago
import (
"os"
10 years ago
"sync"
"testing"
)
type TestMsg struct {
t int "Test"
}
type testConnection struct {
server *MsgConn
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) {
s := NewMsgConn(testFactory, testSocket)
c := NewMsgConn(testFactory, testSocket)
tc := &testConnection{
server: s,
client: c,
}
wrapper := func(tm *TestMsg, msg *Message) error {
err := handler(tm, msg)
tc.called = true
tc.wg.Done()
return err
}
if err := s.AddHandlers(wrapper); err != nil {
return nil, err
}
if err := s.Listen(); err != nil {
return nil, err
}
if err := c.Connect(); err != nil {
return nil, err
}
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
}