This repository was archived by the owner on May 19, 2022. It is now read-only.
forked from carbonblack/cb-event-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent_processing_test.go
197 lines (159 loc) · 4.52 KB
/
event_processing_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
package main
import (
"bytes"
"encoding/json"
"github.com/streadway/amqp"
"io/ioutil"
"os"
"path"
"testing"
)
func processJson(routingKey string, indata []byte) ([]map[string]interface{}, error) {
var msg map[string]interface{}
decoder := json.NewDecoder(bytes.NewReader(indata))
// Ensure that we decode numbers in the JSON as integers and *not* float64s
decoder.UseNumber()
if err := decoder.Decode(&msg); err != nil {
return nil, err
}
msgs, err := ProcessJSONMessage(msg, routingKey)
if err != nil {
return nil, err
} else {
return msgs, nil
}
}
func marshalJson(msgs []map[string]interface{}) (string, error) {
var ret string
for _, msg := range msgs {
msg["cb_server"] = "cbserver"
marshaled, err := json.Marshal(msg)
if err != nil {
return "", err
}
ret += string(marshaled) + "\n"
}
return ret, nil
}
func processProtobuf(routingKey string, indata []byte) ([]map[string]interface{}, error) {
emptyHeaders := new(amqp.Table)
msg, err := ProcessProtobufMessage(routingKey, indata, *emptyHeaders)
if err != nil {
return nil, err
} else {
msgs := make([]map[string]interface{}, 0, 1)
msgs = append(msgs, msg)
return msgs, nil
}
}
func BenchmarkProtobufEventProcessing(b *testing.B) {
fn := path.Join("./tests/raw_data/protobuf/ingress.event.process/0.protobuf")
fp, _ := os.Open(fn)
d, _ := ioutil.ReadAll(fp)
for i := 0; i < b.N; i++ {
processProtobuf("ingress.event.process", d)
}
}
func BenchmarkJsonEventProcessing(b *testing.B) {
fn := path.Join("./tests/raw_data/json/watchlist.hit.process/0.json")
fp, _ := os.Open(fn)
d, _ := ioutil.ReadAll(fp)
for i := 0; i < b.N; i++ {
processJson("watchlist.hit.process", d)
}
}
func BenchmarkZipBundleProcessing(b *testing.B) {
fn := path.Join("./tests/stress_rabbit/zipbundles/1")
fp, _ := os.Open(fn)
d, _ := ioutil.ReadAll(fp)
fakeHeaders := amqp.Table{}
for i := 0; i < b.N; i++ {
ProcessRawZipBundle("", d, fakeHeaders)
}
}
type outputMessageFunc func([]map[string]interface{}) (string, error)
func TestEventProcessing(t *testing.T) {
t.Log("Generating JSON output to go_output...")
processTestEvents(t, "go_output", marshalJson)
}
func processTestEvents(t *testing.T, outputDir string, outputFunc outputMessageFunc) {
formats := [...]struct {
formatType string
process func(string, []byte) ([]map[string]interface{}, error)
}{{"json", processJson}, {"protobuf", processProtobuf}}
config.CbServerURL = "https://cbtests/"
config.EventMap = make(map[string]bool)
for _, format := range formats {
pathname := path.Join("./tests/raw_data", format.formatType)
fp, err := os.Open(pathname)
if err != nil {
t.Logf("Could not open %s", pathname)
t.FailNow()
}
infos, err := fp.Readdir(0)
if err != nil {
t.Logf("Could not enumerate directory %s", pathname)
t.FailNow()
}
fp.Close()
for _, info := range infos {
if !info.IsDir() {
continue
}
routingKey := info.Name()
os.MkdirAll(path.Join("./tests", outputDir, format.formatType, routingKey), 0755)
// add this routing key into the filtering map
config.EventMap[routingKey] = true
// process all files inside this directory
routingDir := path.Join(pathname, info.Name())
fp, err := os.Open(routingDir)
if err != nil {
t.Logf("Could not open directory %s", routingDir)
t.FailNow()
}
files, err := fp.Readdir(0)
if err != nil {
t.Errorf("Could not enumerate directory %s; continuing", routingDir)
continue
}
fp.Close()
for _, fn := range files {
if fn.IsDir() {
continue
}
fp, err := os.Open(path.Join(routingDir, fn.Name()))
if err != nil {
t.Errorf("Could not open %s for reading", path.Join(routingDir, fn.Name()))
continue
}
b, err := ioutil.ReadAll(fp)
if err != nil {
t.Errorf("Could not read %s", path.Join(routingDir, fn.Name()))
continue
}
fp.Close()
msgs, err := format.process(routingKey, b)
if err != nil {
t.Errorf("Error processing %s: %s", path.Join(routingDir, fn.Name()), err)
continue
}
if len(msgs[0]) == 0 {
t.Errorf("got zero messages out of: %s/%s", routingDir, fn.Name())
continue
}
out, err := outputFunc(msgs)
if err != nil {
t.Errorf("Error serializing %s: %s", path.Join(routingDir, fn.Name()), err)
continue
}
outfp, err := os.Create(path.Join("./tests", outputDir, format.formatType, routingKey, fn.Name()))
if err != nil {
t.Errorf("Error creating file: %s", err)
continue
}
outfp.Write([]byte(out))
outfp.Close()
}
}
}
}