forked from yanivagman/tracee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_test.go
254 lines (231 loc) · 6.78 KB
/
input_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
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
251
252
253
254
package main
import (
"encoding/gob"
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
"testing"
"github.com/aquasecurity/tracee/types/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseTraceeInputOptions(t *testing.T) {
testCases := []struct {
testName string
optionStringSlice []string
expectedResultOptions *traceeInputOptions
expectedError error
}{
{
testName: "no options specified",
optionStringSlice: []string{},
expectedResultOptions: nil,
expectedError: errors.New("no tracee input options specified"),
},
{
testName: "non-existent file specified",
optionStringSlice: []string{"file:/iabxfdoabs22do2b"},
expectedResultOptions: nil,
expectedError: errors.New("invalid Tracee input file: /iabxfdoabs22do2b"),
},
{
testName: "non-existent file specified",
optionStringSlice: []string{"file:/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"},
expectedResultOptions: nil,
expectedError: errors.New("invalid Tracee input file: /AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
},
{
testName: "non-existent file specified",
optionStringSlice: []string{"file:"},
expectedResultOptions: nil,
expectedError: errors.New("empty key or value passed: key: >file< value: ><"),
},
{
testName: "invalid file format specified",
optionStringSlice: []string{"format:xml"},
expectedResultOptions: nil,
expectedError: errors.New("invalid tracee input format specified: XML"),
},
{
testName: "invalid input option specified",
optionStringSlice: []string{"shmoo:hallo"},
expectedResultOptions: nil,
expectedError: errors.New("invalid input-tracee option key: shmoo"),
},
{
testName: "invalid input option specified",
optionStringSlice: []string{":"},
expectedResultOptions: nil,
expectedError: errors.New("empty key or value passed: key: >< value: ><"),
},
{
testName: "invalid input option specified",
optionStringSlice: []string{"A"},
expectedResultOptions: nil,
expectedError: errors.New("invalid input-tracee option: A"),
},
{
testName: "invalid input option specified",
optionStringSlice: []string{"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"},
expectedResultOptions: nil,
expectedError: errors.New("invalid input-tracee option: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
},
{
testName: "invalid input option specified",
optionStringSlice: []string{"3O$B@4420**@!;;;go.fmt@!3h;^!#!@841083n1"},
expectedResultOptions: nil,
expectedError: errors.New("invalid input-tracee option: 3O$B@4420**@!;;;go.fmt@!3h;^!#!@841083n1"),
},
}
for _, testcase := range testCases {
t.Run(testcase.testName, func(t *testing.T) {
opt, err := parseTraceeInputOptions(testcase.optionStringSlice)
assert.Equal(t, testcase.expectedError, err)
assert.Equal(t, testcase.expectedResultOptions, opt)
})
}
}
func TestSetupTraceeJSONInputSource(t *testing.T) {
testCases := []struct {
testName string
events []trace.Event
expectedError error
}{
{
testName: "one event",
events: []trace.Event{
{
EventName: "Yankees are the best team in baseball",
},
},
expectedError: nil,
},
{
testName: "two events",
events: []trace.Event{
{
EventName: "Yankees are the best team in baseball",
},
{
EventName: "I hate the Red Sox",
},
},
expectedError: nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
// Setup temp file that tracee-rules reads from
f, err := ioutil.TempFile("", "TestSetupTraceeJSONInputSource-")
if err != nil {
t.Error(err)
}
defer func() {
_ = f.Close()
_ = os.RemoveAll(f.Name())
}()
allEventBytes := []byte{}
for _, ev := range testCase.events {
b, err := json.Marshal(ev)
if err != nil {
t.Error(err)
}
b = append(b, '\n')
allEventBytes = append(allEventBytes, b...)
}
err = ioutil.WriteFile(f.Name(), []byte(allEventBytes), 644)
if err != nil {
t.Error(err)
}
// Set up reading from the file
opts := &traceeInputOptions{inputFile: f, inputFormat: jsonInputFormat}
eventsChan, err := setupTraceeJSONInputSource(opts)
assert.Equal(t, testCase.expectedError, err)
readEvents := []trace.Event{}
for e := range eventsChan {
traceeEvt, ok := e.Payload.(trace.Event)
require.True(t, ok)
readEvents = append(readEvents, traceeEvt)
}
assert.Equal(t, testCase.events, readEvents)
})
}
}
func TestSetupTraceeGobInputSource(t *testing.T) {
testCases := []struct {
testName string
events []trace.Event
expectedError error
}{
{
testName: "one event",
events: []trace.Event{
{
EventName: "Yankees are the best team in baseball",
},
},
expectedError: nil,
},
{
testName: "two events",
events: []trace.Event{
{
EventName: "Yankees are the best team in baseball",
},
{
EventName: "I hate the Red Sox so much",
},
},
expectedError: nil,
},
{
testName: "three events",
events: []trace.Event{
{
EventName: "Yankees are the best team in baseball",
},
{
EventName: "I hate the Red Sox so much",
},
{
EventName: "Aaron Judge is my idol",
},
},
expectedError: nil,
},
}
for _, testCase := range testCases {
t.Run(testCase.testName, func(t *testing.T) {
// Setup temp file that tracee-rules reads from
f, err := ioutil.TempFile("", "TestSetupTraceeGobInputSource-")
if err != nil {
t.Error(err)
}
defer func() {
_ = f.Close()
_ = os.RemoveAll(f.Name())
}()
encoder := gob.NewEncoder(f)
for _, ev := range testCase.events {
err = encoder.Encode(ev)
if err != nil {
t.Error(err)
}
}
f.Seek(0, io.SeekStart)
// Set up reading from the file
opts := &traceeInputOptions{inputFile: f, inputFormat: gobInputFormat}
eventsChan, err := setupTraceeGobInputSource(opts)
assert.Equal(t, testCase.expectedError, err)
readEvents := []trace.Event{}
for e := range eventsChan {
traceeEvt, ok := e.Payload.(trace.Event)
require.True(t, ok)
readEvents = append(readEvents, traceeEvt)
}
assert.Equal(t, testCase.events, readEvents)
})
}
}