-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpf_test.go
94 lines (91 loc) · 2.46 KB
/
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
82
83
84
85
86
87
88
89
90
91
92
93
94
package lcm
import (
"testing"
"golang.org/x/net/bpf"
"gotest.tools/v3/assert"
)
func TestShortMessageChannelFilter(t *testing.T) {
for _, tt := range []struct {
name string
program []bpf.Instruction
packet []byte
expected int
}{
{
name: "accepted 1",
program: shortMessageChannelFilter("foo", "barbaz"),
packet: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // UDP header
0x4c, 0x43, 0x30, 0x32, // magic
0x00, 0x00, 0x00, 0x01, // sequence number
'f', 'o', 'o', 0, // channel
},
expected: 0xffff,
},
{
name: "accepted query parameters",
program: shortMessageChannelFilter("foo", "barbaz"),
packet: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // UDP header
0x4c, 0x43, 0x30, 0x32, // magic
0x00, 0x00, 0x00, 0x01, // sequence number
'b', 'a', 'r', 'b', 'a', 'z', '?', 'm', '=', '1', 0, // channel
},
expected: 0xffff,
},
{
name: "accepted 2",
program: shortMessageChannelFilter("foo", "barbaz"),
packet: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // UDP header
0x4c, 0x43, 0x30, 0x32, // magic
0x00, 0x00, 0x00, 0x01, // sequence number
'b', 'a', 'r', 'b', 'a', 'z', 0, 0, // channel
},
expected: 0xffff,
},
{
name: "accepted 3",
program: shortMessageChannelFilter("foo", "tutan"),
packet: append(
[]byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // UDP header
0x4c, 0x43, 0x30, 0x32, // magic
0x00, 0x00, 0x00, 0x01, // sequence number
},
[]byte("tutan\x00")..., // channel
),
expected: 0xffff,
},
{
name: "rejected due to wrong c1hannel",
program: shortMessageChannelFilter("foo", "barbaz"),
packet: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // UDP header
0x4c, 0x43, 0x30, 0x32, // magic
0x00, 0x00, 0x00, 0x01, // sequence number
'b', 'a', 'r', 0, // channel
},
expected: 0,
},
{
name: "rejected due to wrong header magic",
program: shortMessageChannelFilter("foo", "barbaz"),
packet: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // UDP header
0x4c, 0x43, 0x30, 0x00, // wrong magic
0x00, 0x00, 0x00, 0x01, // sequence number
'f', 'o', 'o', 0, // channel
},
expected: 0,
},
} {
t.Run(tt.name, func(t *testing.T) {
vm, err := bpf.NewVM(tt.program)
assert.NilError(t, err)
n, err := vm.Run(tt.packet)
assert.NilError(t, err)
assert.Equal(t, tt.expected, n)
})
}
}