-
Notifications
You must be signed in to change notification settings - Fork 48
/
f_bpf_test.go
81 lines (76 loc) · 1.85 KB
/
f_bpf_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
package tc
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestBpf(t *testing.T) {
tests := map[string]struct {
val Bpf
err1 error
err2 error
}{
"simple": {val: Bpf{
Ops: bytesPtr([]byte{0x6, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff}),
OpsLen: uint16Ptr(0x1),
ClassID: uint32Ptr(0x10001),
Flags: uint32Ptr(0x1),
}},
"da obj /tmp/bpf.o sec foo": {val: Bpf{
FD: uint32Ptr(8), Name: stringPtr("bpf.o:[foo]"),
Flags: uint32Ptr(0x1), FlagsGen: uint32Ptr(0x2),
}},
"all options": {val: Bpf{
Ops: bytesPtr([]byte{0x6, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff}),
OpsLen: uint16Ptr(0x1),
ClassID: uint32Ptr(0x10001),
FD: uint32Ptr(42),
Name: stringPtr("testing"),
Tag: bytesPtr([]byte{0xAA, 0x55}),
ID: uint32Ptr(42),
}},
"filter add dev XXX ingress bpf bytecode '1,6 0 0 4294967295,' flowid 1:1 action drop": {
val: Bpf{
Ops: bytesPtr([]byte{0x6, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff}),
OpsLen: uint16Ptr(1),
ClassID: uint32Ptr(0x10001),
Action: &Action{
Kind: "gact",
Gact: &Gact{
Parms: &GactParms{
Action: 2, // drop
},
},
},
},
},
}
for name, testcase := range tests {
t.Run(name, func(t *testing.T) {
data, err1 := marshalBpf(&testcase.val)
if err1 != nil {
if errors.Is(err1, testcase.err1) {
return
}
t.Fatalf("Unexpected error: %v", err1)
}
val := Bpf{}
err2 := unmarshalBpf(data, &val)
if err2 != nil {
if errors.Is(err2, testcase.err2) {
return
}
t.Fatalf("Unexpected error: %v", err2)
}
if diff := cmp.Diff(testcase.val, val); diff != "" {
t.Fatalf("Bpf missmatch (-want +got):\n%s", diff)
}
})
}
t.Run("nil", func(t *testing.T) {
_, err := marshalBpf(nil)
if !errors.Is(err, ErrNoArg) {
t.Fatalf("unexpected error: %v", err)
}
})
}