forked from florianl/go-conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conntrack_linux_integration_test.go
143 lines (120 loc) · 3.52 KB
/
conntrack_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
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
//go:build integration && linux
// +build integration,linux
package conntrack
import (
"context"
"net"
"os/exec"
"testing"
"time"
)
func TestLinuxConntrackUpdatePing(t *testing.T) {
// ping is needed to create a session, we can work with
_, err := exec.LookPath("ping")
if err != nil {
t.Fatalf("could not find ping binary")
}
ctx, cancel := context.WithCancel(context.Background())
// Create a session
cmd := exec.CommandContext(ctx, "ping", "127.0.0.2")
if err := cmd.Start(); err != nil {
t.Fatalf("could not start ping to 127.0.0.2: %v", err)
}
defer cancel()
// Give the kernel some time, to track the session
time.Sleep(2 * time.Second)
nfct, err := Open(&Config{})
if err != nil {
t.Fatalf("could not open socket: %v", err)
}
defer nfct.Close()
cons, err := nfct.Dump(Conntrack, IPv4)
if err != nil {
t.Fatalf("could not dump sessions: %v", err)
}
var pingSession Con
for _, c := range cons {
if c.Origin == nil || c.Origin.Proto == nil || c.Origin.Proto.Number == nil || *c.Origin.Proto.Number != 1 {
continue
}
if (*c.Origin.Src).Equal(net.ParseIP("127.0.0.1")) && (*c.Origin.Dst).Equal(net.ParseIP("127.0.0.2")) {
pingSession = c
break
}
}
if pingSession.Mark == nil {
t.Logf("could not identify ping session")
return
}
origMark := *pingSession.Mark
*pingSession.Mark = 0xFF00AA11
// Update the conntrack entry
if err := nfct.Update(Conntrack, IPv4, pingSession); err != nil {
t.Fatalf("could not update conntrack entry: %v", err)
}
c, err := nfct.Get(Conntrack, IPv4, pingSession)
if err != nil {
t.Fatalf("could not get session: %v", err)
}
if len(c) != 1 {
t.Fatalf("could not get updated session")
}
if origMark == *c[0].Mark {
t.Fatalf("could not update mark of the session")
}
t.Logf("original mark 0x%x vs modified mark 0x%x\n", origMark, *c[0].Mark)
}
func TestLinuxConntrackDeleteEntry(t *testing.T) {
// ping is needed to create a session, we can work with
_, err := exec.LookPath("ping")
if err != nil {
t.Fatalf("Could not find ping binary")
}
ctx, cancel := context.WithCancel(context.Background())
// Create a session
cmd := exec.CommandContext(ctx, "ping", "-i 2", "127.0.0.4")
if err := cmd.Start(); err != nil {
t.Fatalf("Could not start ping to 127.0.0.4: %v", err)
}
defer cancel()
// Give the kernel some time, to track the session
time.Sleep(3 * time.Second)
nfct, err := Open(&Config{})
if err != nil {
t.Fatalf("Could not open socket: %v", err)
}
defer nfct.Close()
conns, err := nfct.Dump(Conntrack, IPv4)
if err != nil {
t.Fatalf("Could not dump sessions: %v", err)
}
var origConntrackID uint32
for _, c := range conns {
if c.Origin == nil || c.Origin.Src == nil || c.Origin.Dst == nil {
continue
}
if (*c.Origin.Src).Equal(net.ParseIP("127.0.0.1")) && (*c.Origin.Dst).Equal(net.ParseIP("127.0.0.4")) {
origConntrackID = *c.ID
if err := nfct.Delete(Conntrack, IPv4, c); err != nil {
t.Fatalf("could not delete session: %v", err)
}
break
}
}
// there will be a session for the ping, as it is still running.
// But as we deleted the original session, there has to be a new AttrID
conns2, err2 := nfct.Dump(Conntrack, IPv4)
if err2 != nil {
t.Fatalf("could not dump sessions: %v", err)
}
for _, c := range conns2 {
if c.Origin == nil || c.Origin.Src == nil || c.Origin.Dst == nil {
continue
}
if (*c.Origin.Src).Equal(net.ParseIP("127.0.0.1")) && (*c.Origin.Dst).Equal(net.ParseIP("127.0.0.4")) {
if *c.ID == origConntrackID {
t.Fatalf("original ping session was not deleted")
}
}
}
}