-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaturity_test.go
More file actions
328 lines (297 loc) · 11.4 KB
/
Copy pathmaturity_test.go
File metadata and controls
328 lines (297 loc) · 11.4 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package eql
import (
"strings"
"testing"
)
// ---------------------------------------------------------------------------
// Recursive subquery model
// ---------------------------------------------------------------------------
func TestSequenceStepSubquery(t *testing.T) {
q := `sequence by host.id with maxspan=5m
[process where process.name : "cmd.exe" and process.parent.name : "explorer.exe"]
[network where destination.port == 443]
until [process where event.type == "end"]`
res := ExtractConditions(q)
requireNoErrors(t, res)
if res.Sequence == nil || len(res.Sequence.Steps) != 2 {
t.Fatalf("sequence = %+v", res.Sequence)
}
// Step 0 subquery is self-contained: its own conditions, step-relative.
sub0 := res.Sequence.Steps[0].Subquery
if sub0 == nil {
t.Fatal("step 0 has no subquery")
}
if len(sub0.Conditions) != 2 {
t.Fatalf("step 0 subquery conditions = %+v", sub0.Conditions)
}
for _, c := range sub0.Conditions {
if c.SequenceStep != 0 {
t.Errorf("subquery condition should be step-relative (0), got %d", c.SequenceStep)
}
if c.EventCategory != "process" {
t.Errorf("subquery category = %q", c.EventCategory)
}
}
if len(sub0.EventCategories) != 1 || sub0.EventCategories[0] != "process" {
t.Errorf("subquery categories = %v", sub0.EventCategories)
}
sub1 := res.Sequence.Steps[1].Subquery
if sub1 == nil || len(sub1.Conditions) != 1 || sub1.Conditions[0].Field != "destination.port" {
t.Errorf("step 1 subquery = %+v", sub1)
}
// Until clause also carries a subquery.
if res.Sequence.Until == nil || res.Sequence.Until.Subquery == nil {
t.Fatal("until has no subquery")
}
if res.Sequence.Until.Subquery.Conditions[0].Field != "event.type" {
t.Errorf("until subquery = %+v", res.Sequence.Until.Subquery.Conditions)
}
}
func TestSubqueryIndependentOfFlatView(t *testing.T) {
// The flat top-level conditions carry sequence-step context; the per-step
// subqueries are independent 0-based extractions of the same filters.
q := `sequence [process where a == 1] [network where b == 2]`
res := ExtractConditions(q)
requireNoErrors(t, res)
// Flat view: step context preserved.
if res.Conditions[0].SequenceStep != 0 || res.Conditions[1].SequenceStep != 1 {
t.Errorf("flat steps = %d/%d", res.Conditions[0].SequenceStep, res.Conditions[1].SequenceStep)
}
// Structured view: each subquery is self-contained.
if res.Sequence.Steps[1].Subquery.Conditions[0].SequenceStep != 0 {
t.Error("subquery must be step-relative")
}
}
// ---------------------------------------------------------------------------
// Co-fields in comparisons
// ---------------------------------------------------------------------------
func TestCoFieldsArithmetic(t *testing.T) {
res := ExtractConditions(`network where source.bytes + destination.bytes > 1000000`)
requireNoErrors(t, res)
c := res.Conditions[0]
if c.Field != "source.bytes" {
t.Fatalf("primary field = %q", c.Field)
}
if len(c.CoFields) != 1 || c.CoFields[0] != "destination.bytes" {
t.Errorf("CoFields = %v, want [destination.bytes]", c.CoFields)
}
}
func TestCoFieldsMultiple(t *testing.T) {
res := ExtractConditions(`process where (a.x + b.y + c.z) % 3 == 0`)
requireNoErrors(t, res)
c := res.Conditions[0]
if c.Field != "a.x" {
t.Fatalf("primary = %q", c.Field)
}
if strings.Join(c.CoFields, ",") != "b.y,c.z" {
t.Errorf("CoFields = %v", c.CoFields)
}
}
func TestNoCoFieldsForSimpleComparison(t *testing.T) {
res := ExtractConditions(`process where process.name == "cmd.exe"`)
requireNoErrors(t, res)
if len(res.Conditions[0].CoFields) != 0 {
t.Errorf("CoFields = %v, want none", res.Conditions[0].CoFields)
}
}
func TestCoFieldsExcludeValueField(t *testing.T) {
// Field-to-field comparison: the value field is not also a co-field.
res := ExtractConditions(`process where process.name == process.parent.name`)
requireNoErrors(t, res)
c := res.Conditions[0]
if !c.ValueIsField || len(c.CoFields) != 0 {
t.Errorf("got ValueIsField=%v CoFields=%v", c.ValueIsField, c.CoFields)
}
}
// ---------------------------------------------------------------------------
// Field provenance
// ---------------------------------------------------------------------------
func TestClassifyFieldProvenance(t *testing.T) {
q := `sequence by host.id
[process where process.name : "a.exe"]
[network where destination.port == 445]
until [process where user.name : "admin"]
| unique event.category`
res := ExtractConditions(q)
requireNoErrors(t, res)
tests := []struct {
field string
want FieldProvenance
}{
{"host.id", ProvenanceJoinKey}, // by-clause key
{"process.name", ProvenanceMain}, // first step = primary stream
{"destination.port", ProvenanceJoined}, // later step = correlated event
{"user.name", ProvenanceJoined}, // until clause = correlated event
{"event.category", ProvenanceMain}, // referenced in a pipe = main stream
{"nonexistent.field", ProvenanceUnknown},
}
for _, tt := range tests {
got := ClassifyFieldProvenance(res, tt.field)
if got != tt.want {
t.Errorf("provenance(%q) = %q, want %q", tt.field, got, tt.want)
}
}
}
func TestClassifyFieldProvenanceAmbiguous(t *testing.T) {
// A field used as both a join key and a filter is ambiguous.
q := `sequence by process.name [process where process.name : "a.exe"] [network where true]`
res := ExtractConditions(q)
if got := ClassifyFieldProvenance(res, "process.name"); got != ProvenanceAmbiguous {
t.Errorf("provenance = %q, want ambiguous", got)
}
}
func TestClassifyFieldProvenanceNilResult(t *testing.T) {
if got := ClassifyFieldProvenance(nil, "x"); got != ProvenanceUnknown {
t.Errorf("provenance(nil) = %q", got)
}
res := ExtractConditions(`process where a == 1`)
if got := ClassifyFieldProvenance(res, ""); got != ProvenanceUnknown {
t.Errorf("provenance(empty field) = %q", got)
}
}
func TestProvenanceCoField(t *testing.T) {
// A co-field of a comparison counts as main provenance.
res := ExtractConditions(`network where source.bytes + destination.bytes > 100`)
if got := ClassifyFieldProvenance(res, "destination.bytes"); got != ProvenanceMain {
t.Errorf("co-field provenance = %q, want main", got)
}
}
// ---------------------------------------------------------------------------
// DeduplicateConditions
// ---------------------------------------------------------------------------
func TestDeduplicateConditions(t *testing.T) {
conds := []Condition{
{Field: "process.name", Operator: "==", Value: "cmd.exe"},
{Field: "process.name", Operator: "==", Value: "cmd.exe"}, // exact dup
{Field: "process.name", Operator: "==", Value: "cmd.exe", Negated: true},
{Field: "process.name", Operator: "==", Value: "powershell.exe"},
{Field: "process.name", Operator: "==", Value: "cmd.exe", SequenceStep: 1}, // different step
}
got := DeduplicateConditions(conds)
if len(got) != 4 {
t.Fatalf("dedup produced %d conditions, want 4: %+v", len(got), got)
}
// First occurrence order preserved.
if got[0].Value != "cmd.exe" || got[0].Negated {
t.Errorf("first = %+v", got[0])
}
}
func TestDeduplicateConditionsEmpty(t *testing.T) {
if got := DeduplicateConditions(nil); len(got) != 0 {
t.Errorf("dedup(nil) = %+v", got)
}
}
func TestDeduplicateDistinctContexts(t *testing.T) {
// Same field/op/value but different pipe stage / until / category must not collapse.
conds := []Condition{
{Field: "f", Operator: "==", Value: "v", EventCategory: "process"},
{Field: "f", Operator: "==", Value: "v", EventCategory: "network"},
{Field: "f", Operator: "==", Value: "v", PipeStage: 1},
{Field: "f", Operator: "==", Value: "v", FromUntil: true},
}
if got := DeduplicateConditions(conds); len(got) != 4 {
t.Errorf("dedup collapsed distinct contexts: %d/4", len(got))
}
}
func TestFieldInTextWholeToken(t *testing.T) {
// Pipe-argument field matching must be whole-token, not substring.
res := ExtractConditions(`process where a == 1 | unique process.name`)
if got := ClassifyFieldProvenance(res, "process.na"); got != ProvenanceUnknown {
t.Errorf("substring should not match: got %q", got)
}
if got := ClassifyFieldProvenance(res, "process.name"); got != ProvenanceMain {
t.Errorf("whole token = %q, want main", got)
}
}
// ---------------------------------------------------------------------------
// Helper family parity (IsStatisticalQuery, HasComplexWhereConditions, ...)
// ---------------------------------------------------------------------------
func TestIsStatisticalQuery(t *testing.T) {
tests := []struct {
query string
want bool
}{
{`process where process.name : "cmd.exe"`, false},
{`process where true | count`, true},
{`process where true | unique process.name`, true},
{`process where true | unique_count user.name`, true},
{`process where true | head 10`, false},
{`sequence by x [a where true] [b where true]`, true},
{`sample by x [a where true] [b where true]`, true},
{`join by x [a where true] [b where true]`, false}, // join is not statistical
}
for _, tt := range tests {
res := ExtractConditions(tt.query)
if got := IsStatisticalQuery(res); got != tt.want {
t.Errorf("IsStatisticalQuery(%q) = %v, want %v", tt.query, got, tt.want)
}
}
if IsStatisticalQuery(nil) {
t.Error("IsStatisticalQuery(nil) should be false")
}
}
func TestHasComplexWhereConditions(t *testing.T) {
tests := []struct {
query string
want bool
}{
{`process where process.pid == 4`, false},
{`process where process.name : "cmd.exe"`, true},
{`process where process.name like "cmd*"`, true},
{`process where process.command_line regex "x.*"`, true},
{`network where cidrMatch(source.ip, "10.0.0.0/8")`, true},
{`process where process.name in ("a", "b")`, true},
{`process where endsWith(process.name, ".exe")`, true},
{`process where a == 1 and b != 2`, false},
}
for _, tt := range tests {
res := ExtractConditions(tt.query)
if got := HasComplexWhereConditions(res); got != tt.want {
t.Errorf("HasComplexWhereConditions(%q) = %v, want %v", tt.query, got, tt.want)
}
}
if HasComplexWhereConditions(nil) {
t.Error("HasComplexWhereConditions(nil) should be false")
}
}
func TestHasUnmappedComputedFields(t *testing.T) {
// EQL has no computed fields; always false (API parity stub).
if HasUnmappedComputedFields(ExtractConditions(`process where a == 1`)) {
t.Error("EQL has no computed fields")
}
if HasUnmappedComputedFields(nil) {
t.Error("nil should be false")
}
}
func TestGetEventTypeFromConditions(t *testing.T) {
tests := []struct {
query string
want string
}{
{`process where event.code == "4688"`, "windows_4688"},
{`process where winlog.event_id == "4624"`, "windows_4624"},
{`process where event.code : "1" and event.provider : "Microsoft-Windows-Sysmon"`, "sysmon_1"},
{`process where event.code in ("4688", "4689")`, "windows_4688"},
{`process where process.name : "cmd.exe"`, ""},
}
for _, tt := range tests {
res := ExtractConditions(tt.query)
if got := GetEventTypeFromConditions(res); got != tt.want {
t.Errorf("GetEventTypeFromConditions(%q) = %q, want %q", tt.query, got, tt.want)
}
}
if GetEventTypeFromConditions(nil) != "" {
t.Error("nil should be empty")
}
}
func TestGroupByFieldsAlias(t *testing.T) {
res := ExtractConditions(`sequence by host.id, user.name [a where true] [b where true]`)
gb := res.GroupByFields()
if len(gb) != 2 || gb[0] != "host.id" || gb[1] != "user.name" {
t.Errorf("GroupByFields = %v", gb)
}
var nilResult *ParseResult
if nilResult.GroupByFields() != nil {
t.Error("nil GroupByFields should be nil")
}
}