-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnlmsg_test.go
294 lines (265 loc) · 7.91 KB
/
nlmsg_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package cgolmnl_test
import (
. "github.com/chamaken/cgolmnl"
. "github.com/chamaken/cgolmnl/testlib"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"math/rand"
// "syscall"
"fmt"
"os"
"time"
"unsafe"
)
var _ = Describe("Attr", func() {
fmt.Fprintf(os.Stdout, "Hello, nlmsg tester!\n") // to import os, sys for debugging
var (
BUFLEN = 512
r *rand.Rand
// Nlmsg
hbuf *NlmsgBuf
nlh *Nlmsg
rand_hbuf *NlmsgBuf
rand_nlh *Nlmsg
// Nlattr
abuf *NlattrBuf
nla *Nlattr
rand_abuf *NlattrBuf
rand_nla *Nlattr
)
BeforeEach(func() {
r = rand.New(rand.NewSource(time.Now().Unix()))
hbuf = NewNlmsgBuf(BUFLEN)
nlh, _ = NewNlmsgBytes(*hbuf)
rand_hbuf = NewNlmsgBuf(BUFLEN)
for i := 0; i < BUFLEN; i++ {
(*(*[]byte)(rand_hbuf))[i] = byte(r.Int() % 256)
}
rand_nlh, _ = NewNlmsgBytes(*rand_hbuf)
rand_hbuf.SetLen(uint32(BUFLEN))
abuf = NewNlattrBuf(BUFLEN)
nla = NlattrPointer(*abuf)
rand_abuf = NewNlattrBuf(BUFLEN)
for i := 0; i < BUFLEN; i++ {
(*(*[]byte)(rand_abuf))[i] = byte(r.Int() % 256)
}
rand_nla = NlattrPointer(*rand_abuf)
rand_abuf.SetLen(uint16(BUFLEN))
})
Context("NlmsgBytes", func() {
It("should share uint32 0x12345678 len", func() {
hbuf.SetLen(0x12345678)
Expect(nlh.Len).To(Equal(uint32(0x12345678)))
})
It("should share uint16 0x9abc type", func() {
hbuf.SetType(0x9abc)
Expect(nlh.Type).To(Equal(uint16(0x9abc)))
})
It("should share uint16 0xdef0 flags", func() {
hbuf.SetFlags(0xdef0)
Expect(nlh.Flags).To(Equal(uint16(0xdef0)))
})
It("should share uint32 0x23456789 flags", func() {
hbuf.SetSeq(0x23456789)
Expect(nlh.Seq).To(Equal(uint32(0x23456789)))
})
It("should share uint32 0xabcdef01 pid", func() {
hbuf.SetPid(0xabcdef01)
Expect(nlh.Pid).To(Equal(uint32(0xabcdef01)))
})
})
Context("NlmsgSize", func() {
It("should return 19", func() {
Expect(NlmsgSize(3)).To(Equal(Size_t(19)))
})
})
Context("NlmsgPayloadLen", func() {
It("should return aligned 123 - MNL_NLMSG_HDRLEN", func() {
hbuf.SetLen(MnlAlign(123))
Expect(nlh.PayloadLen()).To(Equal(Size_t((MnlAlign(123) - MNL_NLMSG_HDRLEN))))
})
})
Context("NlmsgPutExtraHeader", func() {
var p unsafe.Pointer
BeforeEach(func() {
rand_hbuf.SetLen(MnlAlign(256))
p = rand_nlh.PutExtraHeader(123)
})
It("nlmsg_len should be added aligned 123", func() {
Expect(rand_nlh.Len).To(Equal(uint32(256 + MnlAlign(123))))
})
It("contents should be all 0", func() {
for i := 256; i < 256+int(MnlAlign(123)); i++ {
Expect((*rand_hbuf)[i]).To(BeZero())
}
})
})
Context("NlmsgGetPayload", func() {
It("points buffer[MNL_NLMSG_HDRLEN]", func() {
rand_hbuf.SetLen(MnlAlign(384))
Expect(rand_nlh.Payload()).To(Equal(unsafe.Pointer(&(*rand_hbuf)[MNL_NLMSG_HDRLEN])))
})
})
Context("NlmsgGetPayloadBytes", func() {
It("length should be len - MNL_NLMSG_HDRLEN", func() {
rand_hbuf.SetLen(MnlAlign(384))
Expect(len(rand_nlh.PayloadBytes())).To(Equal(int(MnlAlign(384) - MNL_NLMSG_HDRLEN)))
})
It("contents should be the same", func() {
rand_hbuf.SetLen(MnlAlign(384))
Expect(rand_nlh.PayloadBytes()).To(Equal((*(*[]byte)(rand_hbuf))[MNL_NLMSG_HDRLEN:MnlAlign(384)]))
})
})
Context("NlmsgGetPayloadOffset", func() {
It("points buffer[MNL_NLMSG_HDRLEN + offset]", func() {
Expect(rand_nlh.PayloadOffset(191)).To(Equal(unsafe.Pointer(&(*rand_hbuf)[MNL_NLMSG_HDRLEN+MnlAlign(191)])))
})
})
Context("NlmsgGetPayloadOffsetBytes", func() {
It("length should be len - MNL_NLMSG_HDRLEN - offset", func() {
Expect(len(rand_nlh.PayloadOffsetBytes(191))).To(Equal(BUFLEN - int(MNL_NLMSG_HDRLEN+MnlAlign(191))))
})
It("contets should be the same", func() {
Expect(rand_nlh.PayloadOffsetBytes(191)).To(Equal((*(*[]byte)(rand_hbuf))[int(MNL_NLMSG_HDRLEN+MnlAlign(191)):]))
})
})
Context("NlmsgOk", func() {
Describe("length: 16", func() {
BeforeEach(func() {
hbuf.SetLen(16)
})
It("param 15 should be false", func() {
Expect(nlh.Ok(15)).To(Equal(false))
})
It("param 16 shoule be true", func() {
Expect(nlh.Ok(16)).To(Equal(true))
})
It("param 17 shoule be true", func() {
Expect(nlh.Ok(17)).To(Equal(true))
})
})
Describe("length: 8", func() {
BeforeEach(func() {
hbuf.SetLen(8)
})
It("param 7 should be false", func() {
Expect(nlh.Ok(7)).To(Equal(false))
})
It("param 8 shoule be false", func() {
Expect(nlh.Ok(8)).To(Equal(false))
})
It("param 9 shoule be false", func() {
Expect(nlh.Ok(9)).To(Equal(false))
})
})
Describe("length: 32", func() {
BeforeEach(func() {
hbuf.SetLen(32)
})
It("param 31 should be false", func() {
Expect(nlh.Ok(31)).To(Equal(false))
})
It("param 32 shoule be true", func() {
Expect(nlh.Ok(32)).To(Equal(true))
})
It("param 33 shoule be true", func() {
Expect(nlh.Ok(33)).To(Equal(true))
})
})
})
Context("NlmsgNext", func() {
It("should have 3 valid (empty) messages", func() {
hbuf.SetLen(MnlAlign(256))
SetUint32(*hbuf, uint(MnlAlign(256)), 128)
SetUint32(*hbuf, uint(MnlAlign(256)+MnlAlign(128)), 64)
next_nlh, rest := nlh.Next(BUFLEN)
Expect(rest).To(Equal(BUFLEN - 256))
Expect(next_nlh.Len).To(Equal(MnlAlign(128)))
Expect(next_nlh.Ok(rest)).To(BeTrue())
next_nlh, rest = next_nlh.Next(rest)
Expect(rest).To(Equal(BUFLEN - 256 - 128))
Expect(next_nlh.Len).To(Equal(MnlAlign(64)))
Expect(next_nlh.Ok(rest)).To(BeTrue())
next_nlh, rest = next_nlh.Next(rest)
Expect(rest).To(Equal(BUFLEN - 256 - 128 - 64))
Expect(next_nlh.Ok(rest)).To(BeFalse())
})
})
Context("NlmsgGetPayloadTail", func() {
It("address should be the same", func() {
hbuf.SetLen(MnlAlign(323))
Expect(nlh.PayloadTail()).To(Equal(unsafe.Pointer(&(*hbuf)[MnlAlign(323)])))
})
})
Context("NlmsgSeqOk", func() {
It("should be equal to but accept 0", func() {
hbuf.SetSeq(0x12345678)
Expect(nlh.SeqOk(0x12345678)).To(BeTrue())
Expect(nlh.SeqOk(0x12345)).To(BeFalse())
Expect(nlh.SeqOk(0)).To(BeTrue())
hbuf.SetSeq(0)
Expect(nlh.SeqOk(0x12345678)).To(BeTrue())
})
})
Context("NlmsgPortidOk", func() {
It("should be equal to but accept 0", func() {
hbuf.SetPid(0x12345678)
Expect(nlh.PortidOk(0x12345678)).To(BeTrue())
Expect(nlh.PortidOk(0x12345)).To(BeFalse())
Expect(nlh.PortidOk(0)).To(BeTrue())
hbuf.SetPid(0)
Expect(nlh.PortidOk(0x12345678)).To(BeTrue())
})
})
Context("NlmsgBatches", func() {
// init
var b *NlmsgBatch
BeforeEach(func() {
buf := make([]byte, 301)
b, _ = NewNlmsgBatch(buf, 163)
})
It("should indicate initial, empty states", func() {
Expect(b.Size()).To(BeZero())
Expect(len(b.HeadBytes())).To(BeZero())
Expect(b.IsEmpty()).To(BeTrue())
})
It("has apropriate length in filling buffer", func() {
// filling buf
for i := 1; i < 11; i++ {
_nlh := b.CurrentNlmsg()
_nlh.PutHeader()
Expect(b.Next()).To(BeTrue())
Expect(b.Size()).To(Equal(Size_t(int(MNL_NLMSG_HDRLEN) * i)))
Expect(len(b.HeadBytes())).To(Equal(int(MNL_NLMSG_HDRLEN) * i))
Expect(b.IsEmpty()).To(BeFalse())
}
})
It("next should indicate false after filling up", func() {
for i := 0; i < 11; i++ {
_nlh := b.CurrentNlmsg()
_nlh.PutHeader()
b.Next()
}
Expect(b.Next()).To(BeFalse())
Expect(b.Size()).To(Equal(Size_t(int(MNL_NLMSG_HDRLEN) * 10)))
Expect(len(b.HeadBytes())).To(Equal(int(MNL_NLMSG_HDRLEN) * 10))
Expect(b.IsEmpty()).To(BeFalse())
})
It("should one header after filling up and reset", func() {
for i := 0; i < 11; i++ {
_nlh := b.CurrentNlmsg()
_nlh.PutHeader()
b.Next()
}
b.Reset()
Expect(b.Size()).To(Equal(Size_t(MNL_NLMSG_HDRLEN)))
Expect(len(b.HeadBytes())).To(Equal(int(MNL_NLMSG_HDRLEN)))
Expect(b.IsEmpty()).To(BeFalse())
Expect(b.Next()).To(BeTrue())
b.Reset()
Expect(b.Size()).To(BeZero())
Expect(len(b.HeadBytes())).To(BeZero())
Expect(b.IsEmpty()).To(BeTrue())
})
})
})