-
Notifications
You must be signed in to change notification settings - Fork 21
/
nflog_linux_integration_test.go
94 lines (77 loc) · 1.9 KB
/
nflog_linux_integration_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
//go:build integration && linux
// +build integration,linux
package nflog
import (
"context"
"testing"
"time"
)
func TestLinuxNflog(t *testing.T) {
// Set configuration parameters
config := Config{
Group: 100,
Copymode: CopyPacket,
}
// Open a socket to the netfilter log subsystem
nf, err := Open(&config)
if err != nil {
t.Fatalf("failed to open nflog socket: %v", err)
}
defer nf.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// hook function that is called for every received packet by the nflog group
hook := func(a Attribute) int {
// Just print out the payload of the nflog packet
t.Logf("%v\n", *a.Payload)
return 0
}
errFunc := func(e error) int {
t.Logf("received error on hook: %v", e)
return 0
}
// Register your function to listen on nflog group 100
err = nf.RegisterWithErrorFunc(ctx, hook, errFunc)
if err != nil {
t.Fatalf("failed to register hook function: %v", err)
}
// Block till the context expires
<-ctx.Done()
}
func startNflog(ctx context.Context, t *testing.T, group uint16) (func(), error) {
t.Helper()
config := Config{
Group: group,
Copymode: CopyPacket,
}
nf, err := Open(&config)
if err != nil {
return func() {}, err
}
fn := func(a Attribute) int {
t.Logf("--nflog-group %d\t%v\n", group, *a.Payload)
return 1
}
err = nf.Register(ctx, fn)
if err != nil {
return func() {}, err
}
return func() { nf.Close() }, nil
}
func TestLinuxMultiNflog(t *testing.T) {
var cleanUp []func()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for i := 32; i <= 42; i++ {
function, err := startNflog(ctx, t, uint16(i))
if err != nil {
t.Fatalf("failed to open nflog socket for group %d: %v", i, err)
}
cleanUp = append(cleanUp, function)
}
// Block till the context expires
<-ctx.Done()
for _, function := range cleanUp {
function()
}
}