-
Notifications
You must be signed in to change notification settings - Fork 3
/
attr_test.go
1010 lines (944 loc) · 30.7 KB
/
attr_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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cgolmnl_test
import (
. "github.com/chamaken/cgolmnl"
. "github.com/chamaken/cgolmnl/testlib"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"fmt"
"math/rand"
"os"
"syscall"
"time"
"unsafe"
"testing"
)
//
// use testing
func TestNlattrPointer(t *testing.T) {
ab := NewNlattrBuf(16)
ab.SetLen(4)
nla := NlattrPointer(*ab)
if nla.Len != 4 {
t.Errorf("len - want: %d, but: %d", 4, nla.Len)
}
ab.SetLen(10)
if nla.Len != 10 {
t.Errorf("len - want: %d, but: %d", 10, nla.Len)
}
ab.SetType(2)
if nla.Type != 2 {
t.Errorf("type - want: %d, but: %d", 2, nla.Type)
}
}
func TestAttrGetType(t *testing.T) {
ab := NewNlattrBuf(16)
ab.SetType(2)
nla := NlattrPointer(*ab)
if nla.GetType() != 2 {
t.Errorf("type - want: %d, but: %d", 2, nla.GetType())
}
nla.Type = 2 | NLA_F_NESTED
if nla.GetType() != 2 {
t.Errorf("type - want: %d, but: %d", 2, nla.GetType())
}
}
func TestAttrParse(t *testing.T) {
cb := func(attr *Nlattr, data interface{}) (int, syscall.Errno) {
// fmt.Printf("attr.Len: %d, data: %d\n", (*attr).Len, data.(int))
return MNL_CB_OK, 0
}
nlh, _ := NewNlmsg(4096)
val := 0x12
nlh.PutU8(uint16(MNL_TYPE_U8), 0x10)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x11)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x12)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x13)
ret, err := nlh.Parse(0, cb, val)
if ret != MNL_CB_OK {
t.Errorf("type - want: %d, but: %d", MNL_CB_OK, ret)
}
if err != nil {
t.Errorf("type - want: %v, but: %v", nil, err)
}
}
// used testing
//
var _ = Describe("Attr", func() {
fmt.Fprintf(os.Stdout, "Hello, attr 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
// for nlattr validation
valid_len = map[AttrDataType][2]uint16{
MNL_TYPE_UNSPEC: {0, 0},
MNL_TYPE_U8: {1, 1},
MNL_TYPE_U16: {2, 2},
MNL_TYPE_U32: {4, 4},
MNL_TYPE_U64: {8, 8},
MNL_TYPE_STRING: {64, 64},
MNL_TYPE_FLAG: {0, 0},
MNL_TYPE_MSECS: {8, 8},
MNL_TYPE_NESTED: {32, 32},
MNL_TYPE_NESTED_COMPAT: {32, 32},
MNL_TYPE_NUL_STRING: {64, 64},
MNL_TYPE_BINARY: {64, 64},
// mnl.TYPE_MAX : {, ),
}
invalid_len = map[AttrDataType][2]uint16{
MNL_TYPE_U8: {2, 3},
MNL_TYPE_U16: {3, 4},
MNL_TYPE_U32: {5, 6},
MNL_TYPE_U64: {9, 10},
}
)
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)
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)
})
Context("NlattrPointer", func() {
It("should share uint16 4 len", func() {
abuf.SetLen(4)
Expect(nla.Len).To(Equal(uint16(4)))
})
It("should share uint16 2 type", func() {
abuf.SetType(2)
Expect(nla.Type).To(Equal(uint16(2)))
})
})
Context("AttrGetType", func() {
It("should share uint16 2 len", func() {
abuf.SetType(2)
Expect(nla.GetType()).To(Equal(uint16(2)))
})
It("should be 2 even NLA_F_NESTED", func() {
abuf.SetType(2 | NLA_F_NESTED)
Expect(nla.GetType()).To(Equal(uint16(2)))
})
})
Context("AttrGetLen", func() {
It("should be 10", func() {
abuf.SetLen(16)
Expect(nla.GetLen()).To(Equal(uint16(16)))
})
})
Context("AttrGetPayloadLen", func() {
It("should be 123 - MNL_ATTR_HDRLEN (not aligned)", func() {
rand_abuf.SetLen(123)
Expect(rand_nla.PayloadLen()).To(Equal(uint16(123 - MNL_ATTR_HDRLEN)))
})
})
Context("AttrGetPayload", func() {
It("should have same pointer", func() {
abuf_value := *((*[]byte)(abuf))
Expect(nla.Payload()).To(Equal(unsafe.Pointer(&abuf_value[MNL_ATTR_HDRLEN])))
})
})
Context("AttrGetPayloadBytes", func() {
It("should have same value", func() {
rand_abuf_value := *((*[]byte)(rand_abuf))
alen := uint16(91)
rand_abuf.SetLen(alen)
Expect(rand_nla.PayloadBytes()).To(Equal(rand_abuf_value[MNL_ATTR_HDRLEN:alen]))
})
})
Context("AttrOk", func() {
It("has invalid len and all false", func() {
abuf.SetLen(3)
Expect(nla.Ok(3)).To(Equal(false))
Expect(nla.Ok(4)).To(Equal(false))
Expect(nla.Ok(5)).To(Equal(false))
})
It("have valid len and should fail only shorten", func() {
abuf.SetLen(8)
Expect(nla.Ok(7)).To(Equal(false))
Expect(nla.Ok(8)).To(Equal(true))
Expect(nla.Ok(9)).To(Equal(true))
})
})
Context("AttrNext", func() {
It("next should share the same buf", func() {
rand_abuf.SetLen(256)
next_buf := (*(*[]byte)(rand_abuf))[256:]
next_abuf := NlattrBuf(next_buf)
next_abuf.SetLen(128)
Expect(rand_nla.Next().Len).To(Equal(uint16(128)))
next_nla := rand_nla.Next()
Expect(next_nla.PayloadBytes()).To(Equal(next_buf[MNL_ATTR_HDRLEN:128]))
})
})
Context("AttrTypeValid", func() {
It("should be valid if type is le param", func() {
var i uint16
for i = 0; i < uint16(MNL_TYPE_MAX); i++ {
abuf.SetType(i)
err := nla.TypeValid(uint16(MNL_TYPE_MAX))
Expect(err).To(BeNil())
}
})
It("should error gt param", func() {
abuf.SetType(uint16(MNL_TYPE_MAX + 1))
err := nla.TypeValid(uint16(MNL_TYPE_MAX))
Expect(err.(syscall.Errno)).To(Equal(syscall.EOPNOTSUPP))
})
})
Context("AttrValidate", func() {
It("should be invalid because of type", func() {
err := nla.Validate(MNL_TYPE_MAX)
Expect(err.(syscall.Errno)).To(Equal(syscall.EINVAL))
err = nla.Validate2(MNL_TYPE_MAX, 1)
Expect(err.(syscall.Errno)).To(Equal(syscall.EINVAL))
})
It("should be valid", func() {
for t := range valid_len {
abuf.SetLen(uint16(MNL_ATTR_HDRLEN) + valid_len[t][0])
err := nla.Validate(t)
Expect(err).To(BeNil())
err = nla.Validate2(t, Size_t(valid_len[t][1]))
Expect(err).To(BeNil())
}
})
It("should be invalid by mnl_attr_data_type_len, ERANGE", func() {
for t := range invalid_len {
abuf.SetLen(uint16(MNL_ATTR_HDRLEN) + invalid_len[t][0])
err := nla.Validate(t)
Expect(err.(syscall.Errno)).To(Equal(syscall.ERANGE))
}
})
It("should be invalid MNL_TYPE_FLAG", func() {
abuf.SetLen(uint16(MNL_ATTR_HDRLEN + 1))
err := nla.Validate(MNL_TYPE_FLAG)
Expect(err.(syscall.Errno)).To(Equal(syscall.ERANGE))
})
It("should be invalid MNL_TYPE_NUL_STRING", func() {
abuf.SetLen(256)
(*abuf)[abuf.Len()-1] = 1
err := nla.Validate(MNL_TYPE_NUL_STRING)
Expect(err.(syscall.Errno)).To(Equal(syscall.EINVAL))
abuf.SetLen(uint16(MNL_ATTR_HDRLEN))
err = nla.Validate(MNL_TYPE_NUL_STRING)
Expect(err.(syscall.Errno)).To(Equal(syscall.ERANGE))
})
It("should be invalid MNL_TYPE_STRING", func() {
abuf.SetLen(uint16(MNL_ATTR_HDRLEN))
err := nla.Validate(MNL_TYPE_STRING)
Expect(err.(syscall.Errno)).To(Equal(syscall.ERANGE))
})
It("should be valid MNL_TYPE_NESTED with 0 payload", func() {
abuf.SetLen(uint16(MNL_ATTR_HDRLEN))
err := nla.Validate(MNL_TYPE_NESTED)
Expect(err).To(BeNil())
})
It("should be invalid MNL_TYPE_NESTED", func() {
abuf.SetLen(uint16(MNL_ATTR_HDRLEN*2 - 1))
err := nla.Validate(MNL_TYPE_NESTED)
Expect(err.(syscall.Errno)).To(Equal(syscall.ERANGE))
})
})
Context("AttrParse", func() {
cb := func(val uint8) func(*Nlattr, interface{}) (int, syscall.Errno) {
return func(attr *Nlattr, data interface{}) (int, syscall.Errno) {
if data != nil {
return MNL_CB_ERROR, data.(syscall.Errno)
}
preval := val
val += 1
if preval == attr.U8() && data == nil {
return MNL_CB_OK, 0
}
return MNL_CB_STOP, 0
}
}(0x10)
// XXX: using functions defined here nlmsg.go
It("should return MNL_CB_OK, nil", func() {
nlh, _ := NewNlmsg(512)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x10)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x11)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x12)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x13)
ret, err := nlh.Parse(0, cb, nil)
Expect(ret).To(Equal(MNL_CB_OK))
Expect(err).To(BeNil())
nlh, _ = NewNlmsg(512)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x14)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x15)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x16)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x17)
ret, err = nlh.Parse(0, cb, nil)
Expect(ret).To(Equal(MNL_CB_OK))
Expect(err).To(BeNil())
})
It("should return MNL_CB_STOP, nil", func() {
nlh, _ := NewNlmsg(512)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x18)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x19)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x1a)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x00)
ret, err := nlh.Parse(0, cb, nil)
Expect(ret).To(Equal(MNL_CB_STOP))
Expect(err).To(BeNil())
})
It("should return MNL_CB_ERROR, nil", func() {
nlh, _ := NewNlmsg(512)
nlh.PutU8(uint16(MNL_TYPE_U8), 0x00)
ret, err := nlh.Parse(0, cb, syscall.Errno(3))
Expect(ret).To(Equal(MNL_CB_ERROR))
Expect(err).To(Equal(syscall.Errno(3)))
})
})
Describe("Attribute callback", func() {
cb_f := func(val uint16) func(*Nlattr, interface{}) (int, syscall.Errno) {
return func(attr *Nlattr, data interface{}) (int, syscall.Errno) {
if !data.(bool) {
return MNL_CB_STOP, 0
}
if val != attr.Type {
return MNL_CB_ERROR, 123
}
val += 1
return MNL_CB_OK, 0
}
}
Context("AttrParseNested", func() {
// XXX: using functions defined here nlmsg.go
nlh, _ := NewNlmsg(512)
nested := nlh.NestStart(1)
nlh.PutU8(uint16(2), 10)
nlh.PutU8(uint16(3), 20)
nlh.PutU8(uint16(4), 30)
nlh.NestEnd(nested)
It("should return MNL_CB_OK, nil", func() {
ret, err := nested.ParseNested(cb_f(2), true)
Expect(ret).To(Equal(MNL_CB_OK))
Expect(err).To(BeNil())
})
It("should return MNL_CB_STOP, nil", func() {
ret, err := nested.ParseNested(cb_f(2), false)
Expect(ret).To(Equal(MNL_CB_STOP))
Expect(err).To(BeNil())
})
It("should return MNL_CB_ERROR, nil", func() {
ret, err := nested.ParseNested(cb_f(0), true)
Expect(ret).To(Equal(MNL_CB_ERROR))
Expect(err).To(Equal(syscall.Errno(123)))
})
})
Context("AttrParsePayload", func() {
// again, using functions defined here, nlmsg.go
nlh, _ := NewNlmsg(512)
nlh.PutU8(uint16(2), 10)
nlh.PutU8(uint16(3), 20)
nlh.PutU8(uint16(4), 30)
It("should return MNL_CB_OK, nil", func() {
ret, err := AttrParsePayload(nlh.PayloadBytes(), cb_f(2), true)
Expect(ret).To(Equal(MNL_CB_OK))
Expect(err).To(BeNil())
})
It("should return MNL_CB_STOP, nil", func() {
ret, err := AttrParsePayload(nlh.PayloadBytes(), cb_f(2), false)
Expect(ret).To(Equal(MNL_CB_STOP))
Expect(err).To(BeNil())
})
It("should return MNL_CB_ERROR, nil", func() {
ret, err := AttrParsePayload(nlh.PayloadBytes(), cb_f(0), true)
Expect(ret).To(Equal(MNL_CB_ERROR))
Expect(err).To(Equal(syscall.Errno(123)))
})
})
})
Context("AttrGetU8", func() {
It("should be 0x11", func() {
abuf.SetLen(5)
abuf.SetType(uint16(MNL_TYPE_U8))
SetUint8(*abuf, 4, 0x11)
Expect(nla.U8()).To(Equal(uint8(0x11)))
})
})
Context("AttrGetU16", func() {
It("should be 0x1234", func() {
abuf.SetLen(6)
abuf.SetType(uint16(MNL_TYPE_U16))
SetUint16(*abuf, 4, 0x1234)
Expect(nla.U16()).To(Equal(uint16(0x1234)))
})
})
Context("AttrGetU32", func() {
It("should be 0x12345678", func() {
abuf.SetLen(8)
abuf.SetType(uint16(MNL_TYPE_U32))
SetUint32(*abuf, 4, 0x12345678)
Expect(nla.U32()).To(Equal(uint32(0x12345678)))
})
})
Context("AttrGetU64", func() {
It("should be 0x123456789abcdef", func() {
abuf.SetLen(12)
abuf.SetType(uint16(MNL_TYPE_U64))
SetUint64(*abuf, 4, 0x123456789abcdef)
Expect(nla.U64()).To(Equal(uint64(0x123456789abcdef)))
})
})
Context("AttrGetStr", func() {
It("should be abcDEF", func() {
abuf.SetLen(11)
abuf.SetType(uint16(MNL_TYPE_STRING))
for i, c := range []byte("abcDEF") {
(*abuf)[i+4] = c
}
(*abuf)[11] = 0
Expect(nla.Str()).To(Equal("abcDEF"))
})
})
Context("AttrPut", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
nlh.Put(3, SizeofNlmsg, unsafe.Pointer(rand_nlh.Nlmsghdr))
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + 16", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MNL_NLMSG_HDRLEN))
})
It("attr type should be 3", func() {
Expect(_tbuf.Type()).To(Equal(uint16(3)))
})
It("attr len should be 4 + MNL_NLMSG_HDRLEN", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + MNL_NLMSG_HDRLEN)))
})
It("attr contents should be equal", func() {
Expect(([]byte)(_tbuf)[MNL_ATTR_HDRLEN:int(MNL_ATTR_HDRLEN+MNL_NLMSG_HDRLEN)]).To(Equal((*(*[]byte)(rand_hbuf))[:MNL_NLMSG_HDRLEN]))
})
})
Context("AttrPutPtr", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
nlh.PutPtr(3, rand_nlh.Nlmsghdr)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + 16", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MNL_NLMSG_HDRLEN))
})
It("attr type should be 3", func() {
Expect(_tbuf.Type()).To(Equal(uint16(3)))
})
It("attr len should be 4 + MNL_NLMSG_HDRLEN", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + MNL_NLMSG_HDRLEN)))
})
It("attr contents should be equal", func() {
Expect(([]byte)(_tbuf)[MNL_ATTR_HDRLEN:int(MNL_ATTR_HDRLEN+MNL_NLMSG_HDRLEN)]).To(Equal((*(*[]byte)(rand_hbuf))[:MNL_NLMSG_HDRLEN]))
})
})
Context("AttrPutBytes", func() {
b := []byte{1, 2, 3}
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
nlh.PutBytes(1, b)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(3)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(len(b)))))
})
It("attr type should be 1", func() {
Expect(_tbuf.Type()).To(Equal(uint16(1)))
})
It("attr len should be 4 + 3", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + uint32(len(b)))))
})
It("attr contents should be equal", func() {
Expect(([]byte)(_tbuf)[MNL_ATTR_HDRLEN : int(MNL_ATTR_HDRLEN)+len(b)]).To(Equal(b))
})
})
Context("AttrPutU8", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
nlh.PutU8(uint16(MNL_TYPE_U8), 7)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(1)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(1))))
})
It("attr type should be MNL_TYPE_U8", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_U8)))
})
It("attr len should be 4 + 1", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + 1)))
})
It("value should be 7", func() {
Expect(_tbuf[MNL_ATTR_HDRLEN]).To(Equal(uint8(7)))
})
})
Context("AttrPutU16", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
nlh.PutU16(uint16(MNL_TYPE_U16), 12345)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(2)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(2))))
})
It("attr type should be MNL_TYPE_U16", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_U16)))
})
It("attr len should be 4 + 2", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + 2)))
})
It("valud should be 12345", func() {
Expect(GetUint16(_tbuf, uint(MNL_ATTR_HDRLEN))).To(Equal(uint16(12345)))
})
})
Context("AttrPutU32", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
nlh.PutU32(uint16(MNL_TYPE_U32), 0x12345678)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(4)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(4))))
})
It("attr type should be MNL_TYPE_U32", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_U32)))
})
It("attr len should be 4 + 4", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + 4)))
})
It("valud should be 0x12345678", func() {
Expect(GetUint32(_tbuf, uint(MNL_ATTR_HDRLEN))).To(Equal(uint32(0x12345678)))
})
})
Context("AttrPutU64", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
nlh.PutU64(uint16(MNL_TYPE_U64), 0x123456789abcdef0)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(8)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(8))))
})
It("attr type should be MNL_TYPE_U64", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_U64)))
})
It("attr len should be 4 + 8", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + 8)))
})
It("value should be 0x123456789abcdef0", func() {
Expect(GetUint64(_tbuf, uint(MNL_ATTR_HDRLEN))).To(Equal(uint64(0x123456789abcdef0)))
})
})
Context("AttrPutStr", func() {
var _tbuf NlattrBuf
s := "abcdEFGH"
BeforeEach(func() {
nlh.PutHeader()
nlh.PutStr(uint16(MNL_TYPE_STRING), s)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(len(s))", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(len(s)))))
})
It("attr type should be MNL_TYPE_STRING", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_STRING)))
})
It("attr len should be 4 + len(s)", func() {
Expect(_tbuf.Len()).To(Equal(uint16(int(MNL_ATTR_HDRLEN) + len(s))))
})
It("value should be equal to s", func() {
Expect(([]byte)(_tbuf[MNL_ATTR_HDRLEN : int(MNL_ATTR_HDRLEN)+len(s)])).To(Equal(([]byte)(s)))
})
It("next byte should not be NULL", func() {
t := [333]byte{} // 256 <= size <= 512
for i, _ := range t {
t[i] = 'a'
}
nlh.PutStr(uint16(MNL_TYPE_STRING), string(t[:]))
Expect(_tbuf[int(MNL_ATTR_HDRLEN)+len(s)+1]).NotTo(BeZero())
})
})
Context("AttrPutStrz", func() {
var _tbuf NlattrBuf
s := "abcdEFGH"
BeforeEach(func() {
nlh.PutHeader()
nlh.PutStrz(uint16(MNL_TYPE_NUL_STRING), s)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(len(s) + 1)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(len(s)+1))))
})
It("attr type should be MNL_TYPE_NUL_STRING", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_NUL_STRING)))
})
It("attr len should be 4 + len(s) + 1", func() {
Expect(_tbuf.Len()).To(Equal(uint16(int(MNL_ATTR_HDRLEN) + len(s) + 1)))
})
It("value should be equal to s", func() {
Expect(([]byte)(_tbuf[MNL_ATTR_HDRLEN : int(MNL_ATTR_HDRLEN)+len(s)])).To(Equal(([]byte)(s)))
})
It("next byte should not be NULL", func() {
t := [333]byte{} // 256 <= size <= 512
for i, _ := range t {
t[i] = 'a'
}
nlh.PutStr(uint16(MNL_TYPE_STRING), string(t[:]))
Expect(_tbuf[int(MNL_ATTR_HDRLEN)+len(s)+1]).To(BeZero())
})
})
Context("AttrNestStart", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
nlh.NestStart(1)
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("header len should be MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN", func() {
Expect(hbuf.Len()).To(Equal(uint32(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN)))
})
It("attribute type has NLA_F_NESTED", func() {
Expect(_tbuf.Type() & NLA_F_NESTED).To(Equal(uint16(NLA_F_NESTED)))
})
It("attribute type has set value", func() {
Expect(_tbuf.Type() & 1).To(Equal(uint16(1)))
})
})
Context("AttrPutCheck", func() {
b := []byte{1, 2, 3}
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
if nlh.PutCheck(1, 3, unsafe.Pointer(&b[0])) == false {
panic("invalid test assumption")
}
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(3)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(len(b)))))
})
It("attr type should be 1", func() {
Expect(_tbuf.Type()).To(Equal(uint16(1)))
})
It("attr len should be 4 + 3", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + uint32(len(b)))))
})
It("attr contents should be equal", func() {
Expect(([]byte)(_tbuf)[MNL_ATTR_HDRLEN : int(MNL_ATTR_HDRLEN)+len(b)]).To(Equal(b))
})
It("buflen just MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN should return false and nothing has changed", func() {
_hbuf := NewNlmsgBuf(int(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN))
nlh, _ := NewNlmsgBytes(*_hbuf)
nlh.PutHeader()
pb := make([]byte, len(*_hbuf))
copy(pb, *_hbuf)
Expect(nlh.PutCheck(1, 3, unsafe.Pointer(&b[0]))).To(Equal(false))
Expect(*(*[]byte)(_hbuf)).To(BeEquivalentTo(pb))
})
})
Context("AttrPutU8Check", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
if nlh.PutU8Check(uint16(MNL_TYPE_U8), 7) == false {
panic("invalid test assumption")
}
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(1)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(1))))
})
It("attr type should be MNL_TYPE_U8", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_U8)))
})
It("attr len should be 4 + 1", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + 1)))
})
It("value should be 7", func() {
Expect(_tbuf[MNL_ATTR_HDRLEN]).To(Equal(uint8(7)))
})
It("buflen just MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN should return false and nothing has changed", func() {
_hbuf := NewNlmsgBuf(int(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN))
nlh, _ := NewNlmsgBytes(*_hbuf)
nlh.PutHeader()
pb := make([]byte, len(*_hbuf))
copy(pb, *_hbuf)
Expect(nlh.PutU8Check(uint16(MNL_TYPE_U8), 7)).To(Equal(false))
Expect(*(*[]byte)(_hbuf)).To(BeEquivalentTo(pb))
})
})
Context("AttrPutU16Check", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
if nlh.PutU16Check(uint16(MNL_TYPE_U16), 12345) == false {
panic("invalid test assumption")
}
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(2)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(2))))
})
It("attr type should be MNL_TYPE_U16", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_U16)))
})
It("attr len should be 4 + 2", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + 2)))
})
It("valud should be 12345", func() {
Expect(GetUint16(_tbuf, uint(MNL_ATTR_HDRLEN))).To(Equal(uint16(12345)))
})
It("buflen just MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN should return false and nothing has changed", func() {
_hbuf := NewNlmsgBuf(int(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN))
nlh, _ := NewNlmsgBytes(*_hbuf)
nlh.PutHeader()
pb := make([]byte, len(*_hbuf))
copy(pb, *_hbuf)
Expect(nlh.PutU16Check(uint16(MNL_TYPE_U16), 12345)).To(Equal(false))
Expect(*(*[]byte)(_hbuf)).To(BeEquivalentTo(pb))
})
})
Context("AttrPutU32Check", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
if nlh.PutU32Check(uint16(MNL_TYPE_U32), 0x12345678) == false {
panic("invalid test assumption")
}
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(4)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(4))))
})
It("attr type should be MNL_TYPE_U32", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_U32)))
})
It("attr len should be 4 + 4", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + 4)))
})
It("valud should be 0x12345678", func() {
Expect(GetUint32(_tbuf, uint(MNL_ATTR_HDRLEN))).To(Equal(uint32(0x12345678)))
})
It("buflen just MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN should return false and nothing has changed", func() {
_hbuf := NewNlmsgBuf(int(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN))
nlh, _ := NewNlmsgBytes(*_hbuf)
nlh.PutHeader()
pb := make([]byte, len(*_hbuf))
copy(pb, *_hbuf)
Expect(nlh.PutU32Check(uint16(MNL_TYPE_U32), 0x12345678)).To(Equal(false))
Expect(*(*[]byte)(_hbuf)).To(BeEquivalentTo(pb))
})
})
Context("AttrPutU64Check", func() {
var _tbuf NlattrBuf
BeforeEach(func() {
nlh.PutHeader()
if nlh.PutU64Check(uint16(MNL_TYPE_U64), 0x123456789abcdef0) == false {
panic("invalid test assumption")
}
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(8)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(8))))
})
It("attr type should be MNL_TYPE_U64", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_U64)))
})
It("attr len should be 4 + 8", func() {
Expect(_tbuf.Len()).To(Equal(uint16(MNL_ATTR_HDRLEN + 8)))
})
It("value should be 0x123456789abcdef0", func() {
Expect(GetUint64(_tbuf, uint(MNL_ATTR_HDRLEN))).To(Equal(uint64(0x123456789abcdef0)))
})
It("buflen just MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN should return false and nothing has changed", func() {
_hbuf := NewNlmsgBuf(int(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN))
nlh, _ := NewNlmsgBytes(*_hbuf)
nlh.PutHeader()
pb := make([]byte, len(*_hbuf))
copy(pb, *_hbuf)
Expect(nlh.PutU64Check(uint16(MNL_TYPE_U64), 0x123456789abcdef0)).To(Equal(false))
Expect(*(*[]byte)(_hbuf)).To(BeEquivalentTo(pb))
})
})
Context("AttrPutStrCheck", func() {
var _tbuf NlattrBuf
s := "abcdEFGH"
BeforeEach(func() {
nlh.PutHeader()
if nlh.PutStrCheck(uint16(MNL_TYPE_STRING), s) == false {
panic("invalid test assumption")
}
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(len(s))", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(len(s)))))
})
It("attr type should be MNL_TYPE_STRING", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_STRING)))
})
It("attr len should be 4 + len(s)", func() {
Expect(_tbuf.Len()).To(Equal(uint16(int(MNL_ATTR_HDRLEN) + len(s))))
})
It("value should be equal to s", func() {
Expect(([]byte)(_tbuf[MNL_ATTR_HDRLEN : int(MNL_ATTR_HDRLEN)+len(s)])).To(Equal(([]byte)(s)))
})
It("next byte should not be NULL", func() {
t := [333]byte{} // 256 <= size <= 512
for i, _ := range t {
t[i] = 'a'
}
nlh.PutStr(uint16(MNL_TYPE_STRING), string(t[:]))
Expect(_tbuf[int(MNL_ATTR_HDRLEN)+len(s)+1]).NotTo(BeZero())
})
It("buflen just MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN should return false and nothing has changed", func() {
_hbuf := NewNlmsgBuf(int(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN))
nlh, _ := NewNlmsgBytes(*_hbuf)
nlh.PutHeader()
pb := make([]byte, len(*_hbuf))
copy(pb, *_hbuf)
Expect(nlh.PutStrCheck(uint16(MNL_TYPE_STRING), s)).To(Equal(false))
Expect(*(*[]byte)(_hbuf)).To(BeEquivalentTo(pb))
})
})
Context("AttrPutStrzCheck", func() {
var _tbuf NlattrBuf
s := "abcdEFGH"
BeforeEach(func() {
nlh.PutHeader()
if nlh.PutStrzCheck(uint16(MNL_TYPE_NUL_STRING), s) == false {
panic("invalid test assumption")
}
_tbuf = NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
})
It("nlh len should be 16 + 4 + align(len(s) + 1)", func() {
Expect(nlh.Len).To(Equal(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN + MnlAlign(uint32(len(s)+1))))
})
It("attr type should be MNL_TYPE_NUL_STRING", func() {
Expect(_tbuf.Type()).To(Equal(uint16(MNL_TYPE_NUL_STRING)))
})
It("attr len should be 4 + len(s) + 1", func() {
Expect(_tbuf.Len()).To(Equal(uint16(int(MNL_ATTR_HDRLEN) + len(s) + 1)))
})
It("value should be equal to s", func() {
Expect(([]byte)(_tbuf[MNL_ATTR_HDRLEN : int(MNL_ATTR_HDRLEN)+len(s)])).To(Equal(([]byte)(s)))
})
It("next byte should not be NULL", func() {
t := [333]byte{} // 256 <= size <= 512
for i, _ := range t {
t[i] = 'a'
}
nlh.PutStr(uint16(MNL_TYPE_STRING), string(t[:]))
Expect(_tbuf[int(MNL_ATTR_HDRLEN)+len(s)+1]).To(BeZero())
})
It("buflen just MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN should return false and nothing has changed", func() {
_hbuf := NewNlmsgBuf(int(MNL_NLMSG_HDRLEN + MNL_ATTR_HDRLEN))
nlh, _ := NewNlmsgBytes(*_hbuf)
nlh.PutHeader()
pb := make([]byte, len(*_hbuf))
copy(pb, *_hbuf)
Expect(nlh.PutStrzCheck(uint16(MNL_TYPE_NUL_STRING), s)).To(Equal(false))
Expect(*(*[]byte)(_hbuf)).To(BeEquivalentTo(pb))
})
})
Context("AttrNestEnd", func() {
It("attr len should be updated", func() {
nlh.PutHeader()
_nla := nlh.NestStart(1) // payload len: aligned
nlh.PutU8(uint16(MNL_TYPE_U8), 0x12) // 1: 4
nlh.PutU16(uint16(MNL_TYPE_U16), 0x3456) // 2: 4
nlh.PutU32(uint16(MNL_TYPE_U32), 0x3456789a) // 4: 4
nlh.PutU64(uint16(MNL_TYPE_U64), 0xbcdef0123456789a) // 8: 8
nlh.PutStr(uint16(MNL_TYPE_STRING), "bcdef") // 5: 8
nlh.PutStrz(uint16(MNL_TYPE_NUL_STRING), "01234567") // 9: 12 = 40 + MNL_ATTR_HDRLEN * 7
nlh.NestEnd(_nla)
_abuf := NlattrBuf((*hbuf)[MNL_NLMSG_HDRLEN:])
Expect(_abuf.Len()).To(Equal(uint16(68)))
})
})
Context("AttrNestCancel", func() {
It("nlh len should be updated", func() {
nlh.PutHeader()
_nla := nlh.NestStart(1) // payload len: aligned
nlh.PutU8(uint16(MNL_TYPE_U8), 0x12) // 1: 4
nlh.PutU16(uint16(MNL_TYPE_U16), 0x3456) // 2: 4
nlh.PutU32(uint16(MNL_TYPE_U32), 0x3456789a) // 4: 4
nlh.PutU64(uint16(MNL_TYPE_U64), 0xbcdef0123456789a) // 8: 8
nlh.PutStr(uint16(MNL_TYPE_STRING), "bcdef") // 5: 8
nlh.PutStrz(uint16(MNL_TYPE_NUL_STRING), "01234567") // 9: 12 = 40 + MNL_ATTR_HDRLEN * 7
Expect(hbuf.Len()).To(Equal(uint32(MNL_NLMSG_HDRLEN + 68)))
nlh.NestCancel(_nla)
Expect(hbuf.Len()).To(Equal(uint32(MNL_NLMSG_HDRLEN)))
})
})
Context("Attributes", func() {
It("has valid 4 attrs", func() {
nlh.PutHeader()
nlh.PutU8(uint16(0), 0x10)
nlh.PutU8(uint16(1), 0x11)
nlh.PutU8(uint16(2), 0x12)
nlh.PutU8(uint16(3), 0x13)
i := 0
for attr := range nlh.Attributes(0) {
Expect(attr.Type).To(Equal(uint16(i)))
Expect(attr.U8()).To(Equal(uint8(0x10 + i)))
i += 1
}
})
})
Context("Nesteds", func() {
It("nested 4 valid attributes", func() {
// XXX: using functions defined here nlmsg.go
nlh.PutHeader()
nested := nlh.NestStart(1)
nlh.PutU8(uint16(0), 0)
nlh.PutU8(uint16(1), 10)
nlh.PutU8(uint16(2), 20)
nlh.PutU8(uint16(3), 30)
nlh.NestEnd(nested)
i := 0
for attr := range nested.Nesteds() {
Expect(attr.Type).To(Equal(uint16(i)))
Expect(attr.U8()).To(Equal(uint8(10 * i)))
i += 1
}
})
})
Context("PayloadAttributes", func() {
It("has 4 valid attributes", func() {
nlh.PutHeader()
nlh.PutU8(uint16(1), 10)
nlh.PutU8(uint16(2), 20)
nlh.PutU8(uint16(3), 30)
nlh.PutU8(uint16(4), 40)