@@ -15,15 +15,15 @@ import (
15
15
log "github.com/sirupsen/logrus"
16
16
)
17
17
18
- func makeUnicornFlowEvent () types.Entry {
18
+ func makeUnicornFlowEvent (proto string ) types.Entry {
19
19
e := types.Entry {
20
20
SrcIP : fmt .Sprintf ("10.%d.%d.%d" , rand .Intn (250 ), rand .Intn (250 ), rand .Intn (250 )),
21
21
SrcPort : []int64 {1 , 2 , 3 , 4 , 5 }[rand .Intn (5 )],
22
22
DestIP : fmt .Sprintf ("10.0.0.%d" , rand .Intn (250 )),
23
23
DestPort : []int64 {11 , 12 , 13 , 14 , 15 }[rand .Intn (5 )],
24
24
Timestamp : time .Now ().Format (types .SuricataTimestampFormat ),
25
25
EventType : "flow" ,
26
- Proto : "TCP" ,
26
+ Proto : proto ,
27
27
BytesToClient : int64 (rand .Intn (10000 )),
28
28
BytesToServer : int64 (rand .Intn (10000 )),
29
29
PktsToClient : int64 (rand .Intn (100 )),
@@ -101,7 +101,7 @@ func TestUnicornAggregatorNoSubmission(t *testing.T) {
101
101
dsub := & testSubmitter {
102
102
Data : make ([]string , 0 ),
103
103
}
104
- f := MakeUnicornAggregator (dsub , 100 * time .Millisecond , false )
104
+ f := MakeUnicornAggregator (dsub , 100 * time .Millisecond , false , false )
105
105
f .Run ()
106
106
107
107
time .Sleep (1 * time .Second )
@@ -128,12 +128,12 @@ func TestUnicornAggregator(t *testing.T) {
128
128
dsub := & testSubmitter {
129
129
Data : make ([]string , 0 ),
130
130
}
131
- f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false )
131
+ f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false , false )
132
132
f .Run ()
133
133
134
134
createdFlows := make (map [string ]int )
135
135
for i := 0 ; i < 200000 ; i ++ {
136
- ev := makeUnicornFlowEvent ()
136
+ ev := makeUnicornFlowEvent ("TCP" )
137
137
if ev .BytesToClient > 0 {
138
138
key := fmt .Sprintf ("%s_%s_%d" , ev .SrcIP , ev .DestIP , ev .DestPort )
139
139
createdFlows [key ]++
@@ -189,7 +189,7 @@ func TestUnicornAggregatorWithTestdata(t *testing.T) {
189
189
dsub := & testSubmitter {
190
190
Data : make ([]string , 0 ),
191
191
}
192
- f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false )
192
+ f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false , false )
193
193
f .EnableTestFlow ("1.2.3.4" , "5.6.7.8" , 33333 )
194
194
f .Run ()
195
195
@@ -239,7 +239,7 @@ func TestUnicornAggregatorWithDispatch(t *testing.T) {
239
239
dsub := & testSubmitter {
240
240
Data : make ([]string , 0 ),
241
241
}
242
- f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false )
242
+ f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false , false )
243
243
feedWaitChan := make (chan bool )
244
244
outChan := make (chan types.Entry )
245
245
@@ -256,17 +256,21 @@ func TestUnicornAggregatorWithDispatch(t *testing.T) {
256
256
f .Run ()
257
257
258
258
createdFlows := make (map [string ]int )
259
- for i := 0 ; i < 200000 ; i ++ {
260
- ev := makeUnicornFlowEvent ()
261
- if ev .BytesToClient > 0 {
259
+ for i := 0 ; i < 400000 ; i ++ {
260
+ proto := "TCP"
261
+ if i % 2 == 0 {
262
+ proto = "UDP"
263
+ }
264
+ ev := makeUnicornFlowEvent (proto )
265
+ if proto == "TCP" && ev .BytesToClient > 0 {
262
266
key := fmt .Sprintf ("%s_%s_%d" , ev .SrcIP , ev .DestIP , ev .DestPort )
263
267
createdFlows [key ]++
264
268
}
265
269
d .Dispatch (& ev )
266
270
}
267
271
268
272
for {
269
- if dsub .GetTotalAggs () < len (createdFlows ) {
273
+ if dsub .GetTotalAggs () < ( len (createdFlows ) / 2 ) {
270
274
log .Debug (dsub .GetTotalAggs ())
271
275
time .Sleep (100 * time .Millisecond )
272
276
} else {
@@ -309,3 +313,66 @@ func TestUnicornAggregatorWithDispatch(t *testing.T) {
309
313
}
310
314
}
311
315
}
316
+
317
+ func TestUnicornMixedUDPTCP (t * testing.T ) {
318
+ rand .Seed (time .Now ().UTC ().UnixNano ())
319
+ dsub := & testSubmitter {
320
+ Data : make ([]string , 0 ),
321
+ }
322
+ f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false , true )
323
+ f .Run ()
324
+
325
+ createdFlows := make (map [string ]int )
326
+ for i := 0 ; i < 200000 ; i ++ {
327
+ proto := "TCP"
328
+ if i % 2 == 0 {
329
+ proto = "UDP"
330
+ }
331
+ ev := makeUnicornFlowEvent (proto )
332
+ key := fmt .Sprintf ("%s_%s_%d" , ev .SrcIP , ev .DestIP , ev .DestPort )
333
+ createdFlows [key ]++
334
+ f .Consume (& ev )
335
+ }
336
+
337
+ for {
338
+ if dsub .GetTotalAggs () < len (createdFlows ) {
339
+ log .Debug (dsub .GetTotalAggs ())
340
+ time .Sleep (100 * time .Millisecond )
341
+ } else {
342
+ break
343
+ }
344
+ }
345
+
346
+ consumeWaitChan := make (chan bool )
347
+ f .Stop (consumeWaitChan )
348
+ <- consumeWaitChan
349
+
350
+ if len (dsub .Data ) == 0 {
351
+ t .Fatalf ("collected aggregations are empty" )
352
+ }
353
+
354
+ log .Info (dsub .GetTotalAggs (), len (createdFlows ), len (dsub .Data ))
355
+
356
+ var totallen int
357
+ for _ , v := range dsub .Data {
358
+ totallen += len (v )
359
+ }
360
+ if totallen == 0 {
361
+ t .Fatalf ("length of collected aggregations is zero" )
362
+ }
363
+
364
+ if dsub .GetTotalAggs () != len (createdFlows ) {
365
+ t .Fatalf ("unexpected number of flow aggregates: %d/%d" , dsub .GetTotalAggs (),
366
+ len (createdFlows ))
367
+ }
368
+
369
+ for k , v := range dsub .GetFlowTuples () {
370
+ if _ , ok := createdFlows [k ]; ! ok {
371
+ t .Fatalf ("missing flow aggregate: %s" , k )
372
+ }
373
+ if v ["count" ] != int64 (createdFlows [k ]) {
374
+ t .Fatalf ("unexpected number of flows for %s: %d/%d" ,
375
+ k , v ["count" ], createdFlows [k ])
376
+ }
377
+ }
378
+ }
0 commit comments