-
Notifications
You must be signed in to change notification settings - Fork 3
/
drain_test.go
316 lines (306 loc) · 11.5 KB
/
drain_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package loggingdrain
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAddLogMessage(t *testing.T) {
t.Run("test add message", func(t *testing.T) {
rawLogs := []string{
"Dec 10 07:07:38 LabSZ sshd[24206]: input_userauth_request: invalid user test9 [preauth]",
"Dec 10 07:08:28 LabSZ sshd[24208]: input_userauth_request: invalid user webmaster [preauth]",
"Dec 10 09:12:32 LabSZ sshd[24490]: Failed password for invalid user ftpuser from 0.0.0.0 port 62891 ssh2",
"Dec 10 09:12:35 LabSZ sshd[24492]: Failed password for invalid user pi from 0.0.0.0 port 49289 ssh2",
"Dec 10 09:12:44 LabSZ sshd[24501]: Failed password for invalid user ftpuser from 0.0.0.0 port 60836 ssh2",
"Dec 10 07:28:03 LabSZ sshd[24245]: input_userauth_request: invalid user pgadmin [preauth]",
}
expected := []string{
"Dec 10 07:07:38 LabSZ sshd[24206]: input_userauth_request: invalid user test9 [preauth]",
"Dec 10 [*] LabSZ [*] input_userauth_request: invalid user [*] [preauth]",
"Dec 10 09:12:32 LabSZ sshd[24490]: Failed password for invalid user ftpuser from 0.0.0.0 port 62891 ssh2",
"Dec 10 [*] LabSZ [*] Failed password for invalid user [*] from 0.0.0.0 port [*] ssh2",
"Dec 10 [*] LabSZ [*] Failed password for invalid user [*] from 0.0.0.0 port [*] ssh2",
"Dec 10 [*] LabSZ [*] input_userauth_request: invalid user [*] [preauth]",
}
drain := newDrain()
for i, rawLog := range rawLogs {
cluster, _ := drain.addLogMessage(rawLog)
assert.Equal(t, expected[i], cluster.getTemplate())
}
})
t.Run("test empty msg", func(t *testing.T) {
drain := newDrain()
for i := 0; i < 3; i++ {
drain.addLogMessage("")
}
assert.Equal(t, 1, len(drain.idToCluster.Keys()))
})
t.Run("add short message", func(t *testing.T) {
model := newDrain()
_, updateType := model.addLogMessage("hello")
assert.Equal(t, CLUSTER_UPDATE_TYPE_NEW_CLUSTER, updateType)
_, updateType = model.addLogMessage("hello")
assert.Equal(t, CLUSTER_UPDATE_TYPE_NONE, updateType)
_, updateType = model.addLogMessage("otherword")
assert.Equal(t, CLUSTER_UPDATE_TYPE_NEW_CLUSTER, updateType)
})
t.Run("add log message sim75", func(t *testing.T) {
rawLogs := []string{
"Dec 10 07:07:38 LabSZ sshd[24206]: input_userauth_request: invalid user test9 [preauth]",
"Dec 10 07:08:28 LabSZ sshd[24208]: input_userauth_request: invalid user webmaster [preauth]",
"Dec 10 09:12:32 LabSZ sshd[24490]: Failed password for invalid user ftpuser from 0.0.0.0 port 62891 ssh2",
"Dec 10 09:12:35 LabSZ sshd[24492]: Failed password for invalid user pi from 0.0.0.0 port 49289 ssh2",
"Dec 10 09:12:44 LabSZ sshd[24501]: Failed password for invalid user ftpuser from 0.0.0.0 port 60836 ssh2",
"Dec 10 07:28:03 LabSZ sshd[24245]: input_userauth_request: invalid user pgadmin [preauth]",
}
expected := []string{
"Dec 10 07:07:38 LabSZ sshd[24206]: input_userauth_request: invalid user test9 [preauth]",
"Dec 10 07:08:28 LabSZ sshd[24208]: input_userauth_request: invalid user webmaster [preauth]",
"Dec 10 09:12:32 LabSZ sshd[24490]: Failed password for invalid user ftpuser from 0.0.0.0 port 62891 ssh2",
"Dec 10 [*] LabSZ [*] Failed password for invalid user [*] from 0.0.0.0 port [*] ssh2",
"Dec 10 [*] LabSZ [*] Failed password for invalid user [*] from 0.0.0.0 port [*] ssh2",
"Dec 10 07:28:03 LabSZ sshd[24245]: input_userauth_request: invalid user pgadmin [preauth]",
}
drain := newDrain(withSim(0.75))
for i, rawLog := range rawLogs {
cluster, _ := drain.addLogMessage(rawLog)
assert.Equal(t, expected[i], cluster.getTemplate())
}
})
t.Run("test maxCluster", func(t *testing.T) {
rawLogs := []string{
"A format 1",
"A format 2",
"B format 1",
"B format 2",
"A format 3",
}
expected := []string{
"A format 1",
"A format [*]",
"B format 1",
"B format [*]",
"A format 3",
}
drain := newDrain(withMaxClusters(1))
for i, rawLog := range rawLogs {
cluster, _ := drain.addLogMessage(rawLog)
assert.Equal(t, expected[i], cluster.getTemplate())
}
})
t.Run("test lru", func(t *testing.T) {
rawLogs := []string{
"A A A",
"A A B",
"A B A",
"A B B",
"A C A",
"A C B",
"A B A",
"A A A",
}
expected := []string{
// lru: []
"A A A",
// lru: ["A A A"]
"A A [*]",
// lru: ["A A *"]
"A B A",
// lru: ["A B A", "A A *"]
"A B [*]",
// lru: ["A B *", "A A *"]
"A C A",
// lru: ["A C A", "A B *"]
"A C [*]",
// lru: ["A C *", "A B *"]
"A B [*]",
// Message "B A A" was normalized because the template "B A *" is
// still present in the cache.
// lru: ["B A *", "C A *"]
"A A A",
// Message "A A A" was not normalized because the template "C A A"
// pushed out the template "A A *" from the cache.
// lru: ["A A A", "C A *"]
}
drain := newDrain(withMaxClusters(2))
for i, rawLog := range rawLogs {
cluster, _ := drain.addLogMessage(rawLog)
assert.Equal(t, expected[i], cluster.getTemplate())
}
})
}
func TestDepth5AddLogMessage(t *testing.T) {
t.Run("test add message", func(t *testing.T) {
rawLogs := []string{
"Dec 10 07:07:38 LabSZ sshd[24206]: input_userauth_request: invalid user test9 [preauth]",
"Dec 10 07:08:28 LabSZ sshd[24208]: input_userauth_request: invalid user webmaster [preauth]",
"Dec 10 09:12:32 LabSZ sshd[24490]: Failed password for invalid user ftpuser from 0.0.0.0 port 62891 ssh2",
"Dec 10 09:12:35 LabSZ sshd[24492]: Failed password for invalid user pi from 0.0.0.0 port 49289 ssh2",
"Dec 10 09:12:44 LabSZ sshd[24501]: Failed password for invalid user ftpuser from 0.0.0.0 port 60836 ssh2",
"Dec 10 07:28:03 LabSZ sshd[24245]: input_userauth_request: invalid user pgadmin [preauth]",
}
expected := []string{
"Dec 10 07:07:38 LabSZ sshd[24206]: input_userauth_request: invalid user test9 [preauth]",
"Dec 10 [*] LabSZ [*] input_userauth_request: invalid user [*] [preauth]",
"Dec 10 09:12:32 LabSZ sshd[24490]: Failed password for invalid user ftpuser from 0.0.0.0 port 62891 ssh2",
"Dec 10 [*] LabSZ [*] Failed password for invalid user [*] from 0.0.0.0 port [*] ssh2",
"Dec 10 [*] LabSZ [*] Failed password for invalid user [*] from 0.0.0.0 port [*] ssh2",
"Dec 10 [*] LabSZ [*] input_userauth_request: invalid user [*] [preauth]",
}
drain := newDrain(withDepth(5))
for i, rawLog := range rawLogs {
cluster, _ := drain.addLogMessage(rawLog)
assert.Equal(t, expected[i], cluster.getTemplate())
}
})
t.Run("add log message sim75", func(t *testing.T) {
rawLogs := []string{
"Dec 10 07:07:38 LabSZ sshd[24206]: input_userauth_request: invalid user test9 [preauth]",
"Dec 10 07:08:28 LabSZ sshd[24208]: input_userauth_request: invalid user webmaster [preauth]",
"Dec 10 09:12:32 LabSZ sshd[24490]: Failed password for invalid user ftpuser from 0.0.0.0 port 62891 ssh2",
"Dec 10 09:12:35 LabSZ sshd[24492]: Failed password for invalid user pi from 0.0.0.0 port 49289 ssh2",
"Dec 10 09:12:44 LabSZ sshd[24501]: Failed password for invalid user ftpuser from 0.0.0.0 port 60836 ssh2",
"Dec 10 07:28:03 LabSZ sshd[24245]: input_userauth_request: invalid user pgadmin [preauth]",
}
expected := []string{
"Dec 10 07:07:38 LabSZ sshd[24206]: input_userauth_request: invalid user test9 [preauth]",
"Dec 10 07:08:28 LabSZ sshd[24208]: input_userauth_request: invalid user webmaster [preauth]",
"Dec 10 09:12:32 LabSZ sshd[24490]: Failed password for invalid user ftpuser from 0.0.0.0 port 62891 ssh2",
"Dec 10 [*] LabSZ [*] Failed password for invalid user [*] from 0.0.0.0 port [*] ssh2",
"Dec 10 [*] LabSZ [*] Failed password for invalid user [*] from 0.0.0.0 port [*] ssh2",
"Dec 10 07:28:03 LabSZ sshd[24245]: input_userauth_request: invalid user pgadmin [preauth]",
}
drain := newDrain(withSim(0.75))
for i, rawLog := range rawLogs {
cluster, _ := drain.addLogMessage(rawLog)
assert.Equal(t, expected[i], cluster.getTemplate())
}
})
}
func TestSeqDistance(t *testing.T) {
testData := []struct {
seq1 string
seq2 string
includeParams bool
relVal float32
paramCount int64
err error
}{
{
seq1: "abc 123 ooo",
seq2: "abc 456 ooo",
includeParams: true,
relVal: 0.6666667,
paramCount: 0,
err: nil,
},
{
seq1: "456 123 ooo",
seq2: "abc 456 ooo",
includeParams: true,
relVal: 0.33333334,
paramCount: 0,
err: nil,
},
{
seq1: "abc 123 [*]",
seq2: "abc 456 ooo",
includeParams: true,
relVal: 0.6666667,
paramCount: 1,
err: nil,
},
{
seq1: "A A [*]",
seq2: "A B A",
includeParams: false,
relVal: 0.33333334,
paramCount: 1,
err: nil,
},
}
for _, data := range testData {
t.Run(fmt.Sprintf("compare seq1: %s, seq2: %s, relVal: %v, paramCount: %v",
data.seq1, data.seq2, data.relVal, data.paramCount), func(t *testing.T) {
drain := newDrain()
relVal, paramCount, _ := drain.getSeqDistance(
getStringTokens(data.seq1), getStringTokens(data.seq2), data.includeParams)
assert.Equal(t, data.relVal, relVal, "relVal not equals")
assert.Equal(t, data.paramCount, paramCount, "paramCount not equals")
})
}
}
func TestFastMatch(t *testing.T) {
testData := []struct {
clusters []*LogCluster
tokens []string
bestMatchId int64
}{
{
clusters: []*LogCluster{
{
id: 1,
logTemplateTokens: []string{
"abc", "123", "345",
},
},
{
id: 2,
logTemplateTokens: []string{
"abc", "bcd", "345",
},
},
},
tokens: []string{"abc", "bcd", "123"},
bestMatchId: 2,
},
}
drain := newDrain()
for _, data := range testData {
for _, cluster := range data.clusters {
drain.idToCluster.Add(cluster.id, nil)
}
}
for i, data := range testData {
t.Run(fmt.Sprintf("test %v group", i), func(t *testing.T) {
bestMatchCluster := drain.fastMatch(data.clusters, data.tokens, drain.sim, true)
assert.NotNil(t, bestMatchCluster)
assert.Equal(t, data.bestMatchId, bestMatchCluster.id)
})
}
}
func TestAddSeqToPrefixTree(t *testing.T) {
t.Run("add log template to empty tree", func(t *testing.T) {
drain := newDrain(withDepth(5))
rootNode := newRootTreeNode()
logTemplate := []string{"abc", "aaa", "bcd", "def"}
logCluster := newLogCluster(1, logTemplate)
drain.addSeqToPrefixTree(rootNode, logCluster)
assert.Equal(t, 1, len(rootNode.lengthNodeChildren))
assert.Equal(t, rootNode.lengthNodeChildren[len(logTemplate)].length, len(logTemplate))
lengthNode := rootNode.lengthNodeChildren[len(logTemplate)]
assert.NotNil(t, lengthNode.tokenNodeChildren[logTemplate[0]])
firstInternalLayerNode := lengthNode.tokenNodeChildren[logTemplate[0]]
assert.NotNil(t, firstInternalLayerNode.tokenNodeChildren[logTemplate[1]])
secondInternalLayerNode := firstInternalLayerNode.tokenNodeChildren[logTemplate[1]]
assert.Equal(t, 1, len(secondInternalLayerNode.clusters))
assert.Equal(t, logCluster, secondInternalLayerNode.clusters[0])
})
}
func TestMatch(t *testing.T) {
t.Run("test match", func(t *testing.T) {
drain := newDrain()
drain.addLogMessage("aa aa aa")
drain.addLogMessage("aa aa bb")
drain.addLogMessage("aa aa cc")
drain.addLogMessage("xx yy zz")
c := drain.match("aa aa tt", SEARCH_STRATEGY_NEVER)
assert.Equal(t, int64(1), c.id)
c = drain.match("xx yy zz", SEARCH_STRATEGY_NEVER)
assert.Equal(t, int64(2), c.id)
c = drain.match("xx yy rr", SEARCH_STRATEGY_NEVER)
assert.Nil(t, c)
c = drain.match("nothing", SEARCH_STRATEGY_NEVER)
assert.Nil(t, c)
})
}