File: flow_aggregator_test.go

package info (click to toggle)
fever 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 920 kB
  • sloc: sh: 41; makefile: 18
file content (250 lines) | stat: -rw-r--r-- 6,429 bytes parent folder | download | duplicates (6)
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
package processing

// DCSO FEVER
// Copyright (c) 2017, 2019, DCSO GmbH

import (
	"encoding/json"
	"fmt"
	"math/rand"
	"sync"
	"testing"
	"time"

	"github.com/DCSO/fever/types"
)

const (
	numOfTestFlowItems = 200000
)

func makeFlowEvent() 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",
		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
}

func TestFlowAggregator(t *testing.T) {
	rand.Seed(time.Now().UTC().UnixNano())
	outChan := make(chan types.Entry)
	feedWaitChan := make(chan bool)
	closeChan := make(chan bool)

	f := MakeFlowAggregator(1*time.Second, outChan)
	var procFlowsLock sync.Mutex
	var processedFlows int
	var eTotalPktsToClient int64
	var eTotalPktsToServer int64
	var eTotalBytesToClient int64
	var eTotalBytesToServer int64
	var rTotalPktsToClient int64
	var rTotalPktsToServer int64
	var rTotalBytesToClient int64
	var rTotalBytesToServer int64

	go func(pc *int64, ps *int64, bc *int64, bs *int64) {
		for {
			select {
			case e := <-outChan:
				var out struct {
					SrcPort []int `json:"src_port"`
					Flow    struct {
						BytesToServer int64 `json:"bytes_toserver"`
						BytesToClient int64 `json:"bytes_toclient"`
						PktsToServer  int64 `json:"pkts_toserver"`
						PktsToClient  int64 `json:"pkts_toclient"`
					} `json:"flow"`
				}
				err := json.Unmarshal([]byte(e.JSONLine), &out)
				if err != nil {
					t.Fail()
				}

				// we count the source ports to determine the number of
				// aggregated flows
				procFlowsLock.Lock()
				processedFlows += len(out.SrcPort)
				procFlowsLock.Unlock()

				*bc += out.Flow.BytesToClient
				*bs += out.Flow.BytesToServer
				*pc += out.Flow.PktsToClient
				*ps += out.Flow.PktsToServer
			case <-closeChan:
				close(feedWaitChan)
				return
			}
		}
	}(&rTotalPktsToClient, &rTotalPktsToServer, &rTotalBytesToClient,
		&rTotalBytesToServer)

	f.Run()

	for i := 0; i < numOfTestFlowItems; i++ {
		ev := makeFlowEvent()
		eTotalBytesToClient += ev.BytesToClient
		eTotalBytesToServer += ev.BytesToServer
		eTotalPktsToClient += ev.PktsToClient
		eTotalPktsToServer += ev.PktsToServer
		f.Consume(&ev)
	}

	go func() {
		for {
			procFlowsLock.Lock()
			if processedFlows == numOfTestFlowItems {
				procFlowsLock.Unlock()
				break
			}
			procFlowsLock.Unlock()
			time.Sleep(100 * time.Millisecond)
		}
		close(closeChan)
	}()

	<-feedWaitChan

	consumeWaitChan := make(chan bool)
	f.Stop(consumeWaitChan)
	<-consumeWaitChan

	if eTotalBytesToClient != rTotalBytesToClient {
		t.Fatalf("total bytes to client differ: %d/%d", eTotalBytesToClient,
			rTotalBytesToClient)
	}
	if eTotalBytesToServer != rTotalBytesToServer {
		t.Fatalf("total bytes to server differ: %d/%d", eTotalBytesToServer,
			rTotalBytesToServer)
	}
	if eTotalPktsToClient != rTotalPktsToClient {
		t.Fatalf("total pkts to client differ: %d/%d", eTotalPktsToClient,
			rTotalPktsToClient)
	}
	if eTotalPktsToServer != rTotalPktsToServer {
		t.Fatalf("total pkts to server differ: %d/%d", eTotalPktsToServer,
			rTotalPktsToServer)
	}
}

func TestFlowAggregatorWithDispatch(t *testing.T) {
	rand.Seed(time.Now().UTC().UnixNano())
	outChan := make(chan types.Entry)
	dbChan := make(chan types.Entry, numOfTestFlowItems)
	feedWaitChan := make(chan bool)
	closeChan := make(chan bool)

	f := MakeFlowAggregator(1*time.Second, outChan)

	var procFlowsLock sync.Mutex
	var processedFlows int
	var eTotalPktsToClient int64
	var eTotalPktsToServer int64
	var eTotalBytesToClient int64
	var eTotalBytesToServer int64
	var rTotalPktsToClient int64
	var rTotalPktsToServer int64
	var rTotalBytesToClient int64
	var rTotalBytesToServer int64

	go func(pc *int64, ps *int64, bc *int64, bs *int64) {
		for {
			select {
			case e := <-outChan:
				var out struct {
					SrcPort []int `json:"src_port"`
					Flow    struct {
						BytesToServer int64 `json:"bytes_toserver"`
						BytesToClient int64 `json:"bytes_toclient"`
						PktsToServer  int64 `json:"pkts_toserver"`
						PktsToClient  int64 `json:"pkts_toclient"`
					} `json:"flow"`
				}
				err := json.Unmarshal([]byte(e.JSONLine), &out)
				if err != nil {
					t.Fail()
				}

				// we count the source ports to determine the number of
				// aggregated flows
				procFlowsLock.Lock()
				processedFlows += len(out.SrcPort)
				procFlowsLock.Unlock()

				*bc += out.Flow.BytesToClient
				*bs += out.Flow.BytesToServer
				*pc += out.Flow.PktsToClient
				*ps += out.Flow.PktsToServer
			case <-closeChan:
				close(feedWaitChan)
				return
			}
		}
	}(&rTotalPktsToClient, &rTotalPktsToServer, &rTotalBytesToClient,
		&rTotalBytesToServer)

	d := MakeHandlerDispatcher(dbChan)
	d.RegisterHandler(f)
	f.Run()

	for i := 0; i < numOfTestFlowItems; i++ {
		ev := makeFlowEvent()
		eTotalBytesToClient += ev.BytesToClient
		eTotalBytesToServer += ev.BytesToServer
		eTotalPktsToClient += ev.PktsToClient
		eTotalPktsToServer += ev.PktsToServer
		d.Dispatch(&ev)
	}

	go func() {
		for {
			procFlowsLock.Lock()
			if processedFlows == numOfTestFlowItems {
				procFlowsLock.Unlock()
				break
			}
			procFlowsLock.Unlock()
			time.Sleep(100 * time.Millisecond)
		}
		close(closeChan)
	}()

	<-feedWaitChan
	consumeWaitChan := make(chan bool)
	f.Stop(consumeWaitChan)
	<-consumeWaitChan

	if len(dbChan) != numOfTestFlowItems {
		t.Fatalf("not all input events forwarded: %d", len(dbChan))
	}
	close(dbChan)

	if eTotalBytesToClient != rTotalBytesToClient {
		t.Fatalf("total bytes to client differ: %d/%d", eTotalBytesToClient,
			rTotalBytesToClient)
	}
	if eTotalBytesToServer != rTotalBytesToServer {
		t.Fatalf("total bytes to server differ: %d/%d", eTotalBytesToServer,
			rTotalBytesToServer)
	}
	if eTotalPktsToClient != rTotalPktsToClient {
		t.Fatalf("total pkts to client differ: %d/%d", eTotalPktsToClient,
			rTotalPktsToClient)
	}
	if eTotalPktsToServer != rTotalPktsToServer {
		t.Fatalf("total pkts to server differ: %d/%d", eTotalPktsToServer,
			rTotalPktsToServer)
	}
}