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
|
package processing
// DCSO FEVER
// Copyright (c) 2020, DCSO GmbH
import (
"encoding/json"
"fmt"
"math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/DCSO/fever/types"
)
const (
numOfProfiledFlowItems = 10000
)
func makeFlowProfilerEvent() types.Entry {
e := types.Entry{
SrcIP: fmt.Sprintf("10.0.0.%d", rand.Intn(250)),
SrcPort: []int64{1, 2, 3, 4, 5}[rand.Intn(5)],
DestIP: fmt.Sprintf("10.0.0.%d", rand.Intn(250)),
DestPort: []int64{11, 12, 13, 14, 15}[rand.Intn(5)],
Timestamp: time.Now().Format(types.SuricataTimestampFormat),
EventType: "flow",
Proto: "TCP",
AppProto: []string{"foo", "bar", "baz"}[rand.Intn(3)],
BytesToClient: int64(rand.Intn(10000)),
BytesToServer: int64(rand.Intn(10000)),
PktsToClient: int64(rand.Intn(100)),
PktsToServer: int64(rand.Intn(100)),
}
jsonBytes, _ := json.Marshal(e)
e.JSONLine = string(jsonBytes)
return e
}
type flowProfilerTestSubmitter struct {
sync.Mutex
Values [][]byte
}
func (fpts *flowProfilerTestSubmitter) SubmitWithHeaders(rawData []byte, key string, contentType string, myHeaders map[string]string) {
fpts.Lock()
defer fpts.Unlock()
fpts.Values = append(fpts.Values, rawData)
}
func (fpts *flowProfilerTestSubmitter) Submit(rawData []byte, key string, contentType string) {
fpts.Lock()
defer fpts.Unlock()
fpts.Values = append(fpts.Values, rawData)
}
func (fpts *flowProfilerTestSubmitter) UseCompression() {
// pass
}
func (fpts *flowProfilerTestSubmitter) Finish() {
// pass
}
// TestFlowProfiler checks whether flow profiles are generated correctly.
// To do this, it consumes a set of example events with randomized event types
// and sizes, generates a reference set of statistics and then compares it to
// the values submitted to a test submitter which simply stores these values.
func TestFlowProfiler(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
myMap := make(map[string]ProtoProfile)
seenProfile := make(map[string]ProtoProfile)
feedWaitChan := make(chan bool)
s := &flowProfilerTestSubmitter{
Values: make([][]byte, 0),
}
f, err := MakeFlowProfiler(1*time.Second, s)
if err != nil {
t.Fatal(err)
}
f.Run()
for i := 0; i < numOfProfiledFlowItems; i++ {
ev := makeFlowProfilerEvent()
myProfile := myMap[ev.AppProto]
myProfile.BytesToClt += uint64(ev.BytesToClient)
myProfile.BytesToSrv += uint64(ev.BytesToServer)
myProfile.PacketsToClt += uint64(ev.PktsToClient)
myProfile.PacketsToSrv += uint64(ev.PktsToServer)
myMap[ev.AppProto] = myProfile
f.Consume(&ev)
}
go func() {
r := regexp.MustCompile(`proto=(?P<Proto>[^ ]+) flowbytestoclient=(?P<fbtc>[0-9]+),flowbytestoserver=(?P<fbts>[0-9]+),flowpktstoclient=(?P<fptc>[0-9]+),flowpktstoserver=(?P<fpts>[0-9]+)`)
for {
s.Lock()
found := 0
for _, v := range s.Values {
for _, proto := range []string{"foo", "bar", "baz"} {
if strings.Contains(string(v), fmt.Sprintf("proto=%s flowbytestoclient=0,flowbytestoserver=0,flowpktstoclient=0,flowpktstoserver=0", proto)) {
found++
}
}
}
s.Unlock()
if found == 3 {
break
}
time.Sleep(100 * time.Millisecond)
}
s.Lock()
for _, v := range s.Values {
sm := r.FindStringSubmatch(string(v))
if sm == nil {
continue
}
p := seenProfile[sm[1]]
intV, err := strconv.ParseUint(sm[2], 10, 64)
if err == nil {
p.BytesToClt += intV
}
intV, err = strconv.ParseUint(sm[3], 10, 64)
if err == nil {
p.BytesToSrv += intV
}
intV, err = strconv.ParseUint(sm[4], 10, 64)
if err == nil {
p.PacketsToClt += intV
}
intV, err = strconv.ParseUint(sm[5], 10, 64)
if err == nil {
p.PacketsToSrv += intV
}
seenProfile[sm[1]] = p
}
s.Unlock()
close(feedWaitChan)
}()
<-feedWaitChan
consumeWaitChan := make(chan bool)
f.Stop(consumeWaitChan)
<-consumeWaitChan
if !reflect.DeepEqual(myMap, seenProfile) {
t.Fatal("different result for test")
}
}
|