-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsocket_test.go
130 lines (116 loc) · 3.67 KB
/
socket_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cgolmnl_test
import (
. "github.com/chamaken/cgolmnl"
. "github.com/chamaken/cgolmnl/testlib"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"fmt"
"os"
"syscall"
)
func socketContexts(nl *Socket) func() {
return func() {
Context("Socket Descriptor", func() {
It("should be a socket", func() {
buf := &syscall.Stat_t{}
fd := nl.Fd()
_ = syscall.Fstat(fd, buf)
Expect(IsSock(buf.Mode)).To(BeTrue())
})
It("should be closed and become invalid fd", func() {
fd := nl.Fd()
Expect(nl.Close()).To(BeNil())
Expect(IsValidFd(fd)).To(BeFalse())
nl, _ = NewSocket(NETLINK_NETFILTER) // for AfterEach
})
})
Context("Bind and Port", func() {
It("port should be zero", func() {
Expect(nl.Portid()).To(BeZero())
})
It("portid shuld be same as bind and not bind again", func() {
err := nl.Bind(0, 65432) // may fail
Expect(err).To(BeNil())
Expect(nl.Portid()).To(Equal(uint32(65432)))
err = nl.Bind(0, MNL_SOCKET_AUTOPID)
Expect(err).To(Equal(syscall.EINVAL))
})
})
Context("Send and Recv", func() {
var nlh *Nlmsg
BeforeEach(func() {
_ = nl.Bind(0, MNL_SOCKET_AUTOPID)
nlh, _ = NewNlmsg(int(MNL_NLMSG_HDRLEN))
nlh.Type = NLMSG_NOOP
nlh.Flags = NLM_F_ECHO | NLM_F_ACK
nlh.Pid = nl.Portid()
nlh.Seq = 1234
})
It("should sendto and recv err message", func() {
b1, _ := nlh.MarshalBinary()
nsent, err := nl.Sendto(b1)
Expect(err).To(BeNil())
Expect(nsent).To(Equal(Ssize_t(MNL_NLMSG_HDRLEN)))
b2 := make([]byte, 256)
nrecv, err := nl.Recvfrom(b2)
Expect(err).To(BeNil())
Expect(nrecv).To(Equal(Ssize_t(36))) // nlmsghdr + nlmsgerr
// nlr := NlmsgBytes(b2[:nrecv])
nlr, _ := NewNlmsg(int(nrecv))
nlr.Len = uint32(nrecv)
nlr.UnmarshalBinary(b2[:nrecv])
Expect(nlr.Len).To(Equal(uint32(36)))
Expect(nlr.Type).To(Equal(uint16(NLMSG_ERROR)))
nle := (*Nlmsgerr)(nlr.Payload())
Expect(nle.Error).To(Equal(-int32(syscall.EPERM)))
Expect(nle.Msg.Len).To(Equal(MNL_NLMSG_HDRLEN))
Expect(nle.Msg.Flags).To(Equal(uint16(NLM_F_ECHO | NLM_F_ACK)))
Expect(nle.Msg.Pid).To(Equal(nl.Portid()))
Expect(nle.Msg.Seq).To(Equal(uint32(1234)))
})
It("should send_nlmsg and recv err message", func() {
nsent, err := nl.SendNlmsg(nlh)
Expect(err).To(BeNil())
Expect(nsent).To(Equal(Ssize_t(MNL_NLMSG_HDRLEN)))
b2 := make([]byte, 256)
nrecv, err := nl.Recvfrom(b2)
Expect(err).To(BeNil())
Expect(nrecv).To(Equal(Ssize_t(36)))
nlr, _ := NewNlmsg(int(nrecv))
nlr.Len = uint32(nrecv)
nlr.UnmarshalBinary(b2[:nrecv])
Expect(nlr.Len).To(Equal(uint32(36)))
Expect(nlr.Type).To(Equal(uint16(NLMSG_ERROR)))
nle := (*Nlmsgerr)(nlr.Payload())
Expect(nle.Error).To(Equal(-int32(syscall.EPERM)))
Expect(nle.Msg.Len).To(Equal(MNL_NLMSG_HDRLEN))
Expect(nle.Msg.Flags).To(Equal(uint16(NLM_F_ECHO | NLM_F_ACK)))
Expect(nle.Msg.Pid).To(Equal(nl.Portid()))
Expect(nle.Msg.Seq).To(Equal(uint32(1234)))
})
})
Context("option set and get", func() {
It("should set/get NETLINK_BROADCAST_ERROR by Cint", func() {
Expect(nl.SetsockoptCint(NETLINK_BROADCAST_ERROR, 1)).To(BeNil())
ret, err := nl.Sockopt(NETLINK_BROADCAST_ERROR, SizeofCint)
Expect(err).To(BeNil())
Expect(len(ret)).To(Equal(SizeofCint))
// assume at least 32bit
Expect(Endian.Uint32(ret[0:SizeofCint])).To(Equal(uint32(1)))
})
})
}
}
var _ = Describe("Socket", func() {
fmt.Fprintf(os.Stdout, "Hello, socket tester!\n") // to import os, sys for debugging
var (
nl *Socket
)
BeforeEach(func() {
nl, _ = NewSocket(NETLINK_NETFILTER)
})
AfterEach(func() {
nl.Close()
})
socketContexts(nl)
})