-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_test.go
More file actions
345 lines (328 loc) · 9.34 KB
/
filter_test.go
File metadata and controls
345 lines (328 loc) · 9.34 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2026 AxonOps Limited.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package audit_test
import (
"fmt"
"testing"
"github.com/axonops/audit"
"github.com/axonops/audit/internal/testhelper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEventRoute_IsEmpty(t *testing.T) {
assert.True(t, (&audit.EventRoute{}).IsEmpty())
assert.False(t, (&audit.EventRoute{IncludeCategories: []string{"write"}}).IsEmpty())
assert.False(t, (&audit.EventRoute{ExcludeEventTypes: []string{"auth_failure"}}).IsEmpty())
}
func TestValidateEventRoute(t *testing.T) {
tax := testhelper.TestTaxonomy()
tests := []struct { //nolint:govet // test struct field order matches readability
name string
wantErr string
route audit.EventRoute
}{
{
name: "empty route",
route: audit.EventRoute{},
},
{
name: "valid include categories",
route: audit.EventRoute{IncludeCategories: []string{"write", "security"}},
},
{
name: "valid include event types",
route: audit.EventRoute{IncludeEventTypes: []string{"auth_failure"}},
},
{
name: "valid exclude categories",
route: audit.EventRoute{ExcludeCategories: []string{"read"}},
},
{
name: "valid exclude event types",
route: audit.EventRoute{ExcludeEventTypes: []string{"config_get"}},
},
{
name: "valid include categories and event types",
route: audit.EventRoute{
IncludeCategories: []string{"write"},
IncludeEventTypes: []string{"auth_failure"},
},
},
{
name: "valid exclude categories and event types",
route: audit.EventRoute{
ExcludeCategories: []string{"read"},
ExcludeEventTypes: []string{"user_delete"},
},
},
{
name: "mixed include and exclude categories",
route: audit.EventRoute{
IncludeCategories: []string{"write"},
ExcludeCategories: []string{"read"},
},
wantErr: "either include or exclude, not both",
},
{
name: "mixed include categories and exclude event types",
route: audit.EventRoute{
IncludeCategories: []string{"write"},
ExcludeEventTypes: []string{"auth_failure"},
},
wantErr: "either include or exclude, not both",
},
{
name: "mixed include event types and exclude categories",
route: audit.EventRoute{
IncludeEventTypes: []string{"user_create"},
ExcludeCategories: []string{"read"},
},
wantErr: "either include or exclude, not both",
},
{
name: "unknown include category",
route: audit.EventRoute{IncludeCategories: []string{"nonexistent"}},
wantErr: "unknown taxonomy entries",
},
{
name: "unknown exclude category",
route: audit.EventRoute{ExcludeCategories: []string{"bogus"}},
wantErr: "unknown taxonomy entries",
},
{
name: "unknown include event type",
route: audit.EventRoute{IncludeEventTypes: []string{"fake_event"}},
wantErr: "unknown taxonomy entries",
},
{
name: "unknown exclude event type",
route: audit.EventRoute{ExcludeEventTypes: []string{"fake_event"}},
wantErr: "unknown taxonomy entries",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := audit.ValidateEventRoute(&tt.route, tax)
if tt.wantErr == "" {
require.NoError(t, err)
} else {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
}
})
}
}
func TestMatchesRoute(t *testing.T) {
tests := []struct { //nolint:govet // test struct
name string
eventType string
category string
route audit.EventRoute
want bool
}{
// Empty route — matches everything.
{
name: "empty route matches all",
route: audit.EventRoute{},
eventType: "user_create",
category: "write",
want: true,
},
// Include mode — categories.
{
name: "include category match",
route: audit.EventRoute{IncludeCategories: []string{"security"}},
eventType: "auth_failure",
category: "security",
want: true,
},
{
name: "include category no match",
route: audit.EventRoute{IncludeCategories: []string{"security"}},
eventType: "user_create",
category: "write",
want: false,
},
// Include mode — event types.
{
name: "include event type match",
route: audit.EventRoute{IncludeEventTypes: []string{"auth_failure"}},
eventType: "auth_failure",
category: "security",
want: true,
},
{
name: "include event type no match",
route: audit.EventRoute{IncludeEventTypes: []string{"auth_failure"}},
eventType: "permission_denied",
category: "security",
want: false,
},
// Include mode — union of categories + event types.
{
name: "include union category match",
route: audit.EventRoute{
IncludeCategories: []string{"write"},
IncludeEventTypes: []string{"auth_failure"},
},
eventType: "user_create",
category: "write",
want: true,
},
{
name: "include union event type match",
route: audit.EventRoute{
IncludeCategories: []string{"write"},
IncludeEventTypes: []string{"auth_failure"},
},
eventType: "auth_failure",
category: "security",
want: true,
},
{
name: "include union no match",
route: audit.EventRoute{
IncludeCategories: []string{"write"},
IncludeEventTypes: []string{"auth_failure"},
},
eventType: "config_get",
category: "read",
want: false,
},
// Exclude mode — categories.
{
name: "exclude category match skips",
route: audit.EventRoute{ExcludeCategories: []string{"read"}},
eventType: "user_get",
category: "read",
want: false,
},
{
name: "exclude category no match delivers",
route: audit.EventRoute{ExcludeCategories: []string{"read"}},
eventType: "user_create",
category: "write",
want: true,
},
// Exclude mode — event types.
{
name: "exclude event type match skips",
route: audit.EventRoute{ExcludeEventTypes: []string{"config_get"}},
eventType: "config_get",
category: "read",
want: false,
},
{
name: "exclude event type no match delivers",
route: audit.EventRoute{ExcludeEventTypes: []string{"config_get"}},
eventType: "user_get",
category: "read",
want: true,
},
// Exclude mode — union of categories + event types.
{
name: "exclude union category match skips",
route: audit.EventRoute{
ExcludeCategories: []string{"read"},
ExcludeEventTypes: []string{"user_delete"},
},
eventType: "config_get",
category: "read",
want: false,
},
{
name: "exclude union event type match skips",
route: audit.EventRoute{
ExcludeCategories: []string{"read"},
ExcludeEventTypes: []string{"user_delete"},
},
eventType: "user_delete",
category: "write",
want: false,
},
{
name: "exclude union no match delivers",
route: audit.EventRoute{
ExcludeCategories: []string{"read"},
ExcludeEventTypes: []string{"user_delete"},
},
eventType: "user_create",
category: "write",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := audit.MatchesRoute(&tt.route, tt.eventType, tt.category, 5)
assert.Equal(t, tt.want, got)
})
}
}
// ---------------------------------------------------------------------------
// Route matching benchmarks
// ---------------------------------------------------------------------------
func BenchmarkMatchesRoute(b *testing.B) {
b.Run("empty_route", func(b *testing.B) {
route := audit.EventRoute{}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
audit.MatchesRoute(&route, "user_create", "write", 5)
}
})
b.Run("include_categories", func(b *testing.B) {
route := audit.EventRoute{
IncludeCategories: []string{"write", "security", "admin", "read"},
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
audit.MatchesRoute(&route, "user_create", "write", 5)
}
})
b.Run("exclude_categories", func(b *testing.B) {
route := audit.EventRoute{
ExcludeCategories: []string{"debug", "trace", "internal"},
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
audit.MatchesRoute(&route, "user_create", "write", 5)
}
})
b.Run("include_event_types", func(b *testing.B) {
route := audit.EventRoute{
IncludeEventTypes: []string{"user_create", "user_delete", "schema_register", "auth_failure"},
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
audit.MatchesRoute(&route, "user_create", "write", 5)
}
})
b.Run("include_20_categories", func(b *testing.B) {
cats := make([]string, 20)
for i := range cats {
cats[i] = fmt.Sprintf("category_%02d", i)
}
cats[15] = "write" // match is near the end
route := audit.EventRoute{IncludeCategories: cats}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
audit.MatchesRoute(&route, "user_create", "write", 5)
}
})
}
// BenchmarkMatchesRoute_Severity is defined in severity_routing_test.go.