@@ -15,15 +15,15 @@ import (
1515 log "github.com/sirupsen/logrus"
1616)
1717
18- func makeUnicornFlowEvent () types.Entry {
18+ func makeUnicornFlowEvent (proto string ) types.Entry {
1919 e := types.Entry {
2020 SrcIP : fmt .Sprintf ("10.%d.%d.%d" , rand .Intn (250 ), rand .Intn (250 ), rand .Intn (250 )),
2121 SrcPort : []int64 {1 , 2 , 3 , 4 , 5 }[rand .Intn (5 )],
2222 DestIP : fmt .Sprintf ("10.0.0.%d" , rand .Intn (250 )),
2323 DestPort : []int64 {11 , 12 , 13 , 14 , 15 }[rand .Intn (5 )],
2424 Timestamp : time .Now ().Format (types .SuricataTimestampFormat ),
2525 EventType : "flow" ,
26- Proto : "TCP" ,
26+ Proto : proto ,
2727 BytesToClient : int64 (rand .Intn (10000 )),
2828 BytesToServer : int64 (rand .Intn (10000 )),
2929 PktsToClient : int64 (rand .Intn (100 )),
@@ -101,7 +101,7 @@ func TestUnicornAggregatorNoSubmission(t *testing.T) {
101101 dsub := & testSubmitter {
102102 Data : make ([]string , 0 ),
103103 }
104- f := MakeUnicornAggregator (dsub , 100 * time .Millisecond , false )
104+ f := MakeUnicornAggregator (dsub , 100 * time .Millisecond , false , false )
105105 f .Run ()
106106
107107 time .Sleep (1 * time .Second )
@@ -128,12 +128,12 @@ func TestUnicornAggregator(t *testing.T) {
128128 dsub := & testSubmitter {
129129 Data : make ([]string , 0 ),
130130 }
131- f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false )
131+ f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false , false )
132132 f .Run ()
133133
134134 createdFlows := make (map [string ]int )
135135 for i := 0 ; i < 200000 ; i ++ {
136- ev := makeUnicornFlowEvent ()
136+ ev := makeUnicornFlowEvent ("TCP" )
137137 if ev .BytesToClient > 0 {
138138 key := fmt .Sprintf ("%s_%s_%d" , ev .SrcIP , ev .DestIP , ev .DestPort )
139139 createdFlows [key ]++
@@ -189,7 +189,7 @@ func TestUnicornAggregatorWithTestdata(t *testing.T) {
189189 dsub := & testSubmitter {
190190 Data : make ([]string , 0 ),
191191 }
192- f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false )
192+ f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false , false )
193193 f .EnableTestFlow ("1.2.3.4" , "5.6.7.8" , 33333 )
194194 f .Run ()
195195
@@ -239,7 +239,7 @@ func TestUnicornAggregatorWithDispatch(t *testing.T) {
239239 dsub := & testSubmitter {
240240 Data : make ([]string , 0 ),
241241 }
242- f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false )
242+ f := MakeUnicornAggregator (dsub , 500 * time .Millisecond , false , false )
243243 feedWaitChan := make (chan bool )
244244 outChan := make (chan types.Entry )
245245
@@ -256,17 +256,21 @@ func TestUnicornAggregatorWithDispatch(t *testing.T) {
256256 f .Run ()
257257
258258 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 {
262266 key := fmt .Sprintf ("%s_%s_%d" , ev .SrcIP , ev .DestIP , ev .DestPort )
263267 createdFlows [key ]++
264268 }
265269 d .Dispatch (& ev )
266270 }
267271
268272 for {
269- if dsub .GetTotalAggs () < len (createdFlows ) {
273+ if dsub .GetTotalAggs () < ( len (createdFlows ) / 2 ) {
270274 log .Debug (dsub .GetTotalAggs ())
271275 time .Sleep (100 * time .Millisecond )
272276 } else {
@@ -309,3 +313,66 @@ func TestUnicornAggregatorWithDispatch(t *testing.T) {
309313 }
310314 }
311315}
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