forked from ti-mo/conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_test.go
83 lines (60 loc) · 2.27 KB
/
string_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
package conntrack
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestProtoLookup(t *testing.T) {
// Existing proto
if got := protoLookup(6); got != "tcp" {
t.Fatalf("unexpected string representation of proto 6: %s", got)
}
// Non-existent proto
if got := protoLookup(255); got != "255" {
t.Fatalf("unexpected string representation of proto 255: %s", got)
}
}
func TestEventString(t *testing.T) {
tpl := Tuple{
IP: IPTuple{
SourceAddress: net.IPv4(1, 2, 3, 4),
DestinationAddress: net.ParseIP("fe80::1"),
},
Proto: ProtoTuple{
SourcePort: 54321,
DestinationPort: 80,
},
}
// Empty event
e := Event{}
assert.Equal(t, "[EventUnknown] <Empty Event>", e.String())
// Event with Flow
ef := Event{Flow: &Flow{}}
ef.Flow.Status.Value = StatusAssured
ef.Flow.TupleOrig = tpl
ef.Flow.CountersOrig.Bytes = 42
ef.Flow.CountersOrig.Packets = 1
ef.Flow.Labels = []byte{0xf0, 0xf0}
ef.Flow.LabelsMask = []byte{0xff, 0xff}
ef.Flow.Mark = 0xf000baaa
ef.Flow.SeqAdjOrig = SequenceAdjust{OffsetBefore: 80, OffsetAfter: 747811, Position: 42}
ef.Flow.SeqAdjReply = SequenceAdjust{OffsetBefore: 123, OffsetAfter: 456, Position: 889999}
ef.Flow.SecurityContext = "selinux_t"
assert.Equal(t,
"[EventUnknown] (Unreplied) Timeout: 0, <0, Src: 1.2.3.4:54321, Dst: [fe80::1]:80>, Zone 0, Acct: [orig: 1 pkts/42 B] [orig: 0 pkts/0 B], Label: <0xf0f0/0xffff>, Mark: <0xf000baaa>, SeqAdjOrig: [dir: orig, pos: 42, before: 80, after: 747811], SeqAdjReply: [dir: orig, pos: 889999, before: 123, after: 456], SecCtx: selinux_t",
ef.String())
// Event with Expect
ee := Event{Type: EventExpDestroy, Expect: &Expect{}}
ee.Expect.TupleMaster = tpl
ee.Expect.Tuple = tpl
ee.Expect.Mask = tpl
ee.Expect.HelpName = "ftp"
ee.Expect.Class = 0x42
assert.Equal(t,
"[EventExpDestroy] Timeout: 0, Master: <0, Src: 1.2.3.4:54321, Dst: [fe80::1]:80>, Tuple: <0, Src: 1.2.3.4:54321, Dst: [fe80::1]:80>, Mask: <0, Src: 1.2.3.4:54321, Dst: [fe80::1]:80>, Zone: 0, Helper: 'ftp', Class: 0x42",
ee.String())
}
func TestStatsString(t *testing.T) {
s := Stats{CPUID: 42, Found: 2, SearchRestart: 999}
assert.Equal(t, "<CPU 42 - Found: 2, Invalid: 0, Ignore: 0, Insert: 0, InsertFailed: 0, Drop: 0, EarlyDrop: 0, Error: 0, SearchRestart: 999>", s.String())
}