forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
synproxy_test.go
169 lines (137 loc) · 6.6 KB
/
synproxy_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// +build linux
package synproxy
import (
"io/ioutil"
"os"
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
)
func TestSynproxyFileNormal(t *testing.T) {
testSynproxyFileData(t, synproxyFileNormal, synproxyResultNormal)
}
func TestSynproxyFileOverflow(t *testing.T) {
testSynproxyFileData(t, synproxyFileOverflow, synproxyResultOverflow)
}
func TestSynproxyFileExtended(t *testing.T) {
testSynproxyFileData(t, synproxyFileExtended, synproxyResultNormal)
}
func TestSynproxyFileAltered(t *testing.T) {
testSynproxyFileData(t, synproxyFileAltered, synproxyResultNormal)
}
func TestSynproxyFileHeaderMismatch(t *testing.T) {
tmpfile := makeFakeSynproxyFile([]byte(synproxyFileHeaderMismatch))
defer os.Remove(tmpfile)
k := Synproxy{
statFile: tmpfile,
}
acc := testutil.Accumulator{}
err := k.Gather(&acc)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid number of columns in data")
}
func TestSynproxyFileInvalidHex(t *testing.T) {
tmpfile := makeFakeSynproxyFile([]byte(synproxyFileInvalidHex))
defer os.Remove(tmpfile)
k := Synproxy{
statFile: tmpfile,
}
acc := testutil.Accumulator{}
err := k.Gather(&acc)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid value")
}
func TestNoSynproxyFile(t *testing.T) {
tmpfile := makeFakeSynproxyFile([]byte(synproxyFileNormal))
// Remove file to generate "no such file" error
os.Remove(tmpfile)
k := Synproxy{
statFile: tmpfile,
}
acc := testutil.Accumulator{}
err := k.Gather(&acc)
assert.Error(t, err)
}
// Valid Synproxy file
const synproxyFileNormal = `entries syn_received cookie_invalid cookie_valid cookie_retrans conn_reopened
00000000 00007a88 00002af7 00007995 00000000 00000000
00000000 0000892c 000015e3 00008852 00000000 00000000
00000000 00007a80 00002ccc 0000796a 00000000 00000000
00000000 000079f7 00002bf5 0000790a 00000000 00000000
00000000 00007a08 00002c9a 00007901 00000000 00000000
00000000 00007cfc 00002b36 000078fd 00000000 00000000
00000000 000079c2 00002c2b 000078d6 00000000 00000000
00000000 0000798a 00002ba8 000078a0 00000000 00000000`
const synproxyFileOverflow = `entries syn_received cookie_invalid cookie_valid cookie_retrans conn_reopened
00000000 80000001 e0000000 80000001 00000000 00000000
00000000 80000003 f0000009 80000003 00000000 00000000`
const synproxyFileHeaderMismatch = `entries syn_received cookie_invalid cookie_valid cookie_retrans
00000000 00000002 00000000 00000002 00000000 00000000
00000000 00000004 00000015 00000004 00000000 00000000
00000000 00000003 00000000 00000003 00000000 00000000
00000000 00000002 00000000 00000002 00000000 00000000
00000000 00000003 00000009 00000003 00000000 00000000
00000000 00000003 00000009 00000003 00000000 00000000
00000000 00000001 00000000 00000001 00000000 00000000
00000000 00000003 00000009 00000003 00000000 00000000`
const synproxyFileInvalidHex = `entries syn_received cookie_invalid cookie_valid cookie_retrans conn_reopened
entries 00000002 00000000 00000002 00000000 00000000
00000000 00000003 00000009 00000003 00000000 00000000`
const synproxyFileExtended = `entries syn_received cookie_invalid cookie_valid cookie_retrans conn_reopened new_counter
00000000 00007a88 00002af7 00007995 00000000 00000000 00000000
00000000 0000892c 000015e3 00008852 00000000 00000000 00000000
00000000 00007a80 00002ccc 0000796a 00000000 00000000 00000000
00000000 000079f7 00002bf5 0000790a 00000000 00000000 00000000
00000000 00007a08 00002c9a 00007901 00000000 00000000 00000000
00000000 00007cfc 00002b36 000078fd 00000000 00000000 00000000
00000000 000079c2 00002c2b 000078d6 00000000 00000000 00000000
00000000 0000798a 00002ba8 000078a0 00000000 00000000 00000000`
const synproxyFileAltered = `entries cookie_invalid cookie_valid syn_received conn_reopened
00000000 00002af7 00007995 00007a88 00000000
00000000 000015e3 00008852 0000892c 00000000
00000000 00002ccc 0000796a 00007a80 00000000
00000000 00002bf5 0000790a 000079f7 00000000
00000000 00002c9a 00007901 00007a08 00000000
00000000 00002b36 000078fd 00007cfc 00000000
00000000 00002c2b 000078d6 000079c2 00000000
00000000 00002ba8 000078a0 0000798a 00000000`
var synproxyResultNormal = map[string]interface{}{
"entries": uint32(0x00000000),
"syn_received": uint32(0x0003e27b),
"cookie_invalid": uint32(0x0001493e),
"cookie_valid": uint32(0x0003d7cf),
"cookie_retrans": uint32(0x00000000),
"conn_reopened": uint32(0x00000000),
}
var synproxyResultOverflow = map[string]interface{}{
"entries": uint32(0x00000000),
"syn_received": uint32(0x00000004),
"cookie_invalid": uint32(0xd0000009),
"cookie_valid": uint32(0x00000004),
"cookie_retrans": uint32(0x00000000),
"conn_reopened": uint32(0x00000000),
}
func testSynproxyFileData(t *testing.T, fileData string, telegrafData map[string]interface{}) {
tmpfile := makeFakeSynproxyFile([]byte(fileData))
defer os.Remove(tmpfile)
k := Synproxy{
statFile: tmpfile,
}
acc := testutil.Accumulator{}
err := k.Gather(&acc)
assert.NoError(t, err)
acc.AssertContainsFields(t, "synproxy", telegrafData)
}
func makeFakeSynproxyFile(content []byte) string {
tmpfile, err := ioutil.TempFile("", "synproxy_test")
if err != nil {
panic(err)
}
if _, err := tmpfile.Write(content); err != nil {
panic(err)
}
if err := tmpfile.Close(); err != nil {
panic(err)
}
return tmpfile.Name()
}