|
| 1 | +/* |
| 2 | + * Copyright 2021-present by Nedim Sabic Sabic |
| 3 | + * https://www.fibratus.io |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package ql |
| 20 | + |
| 21 | +import ( |
| 22 | + "github.com/rabbitstack/fibratus/pkg/event" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestSequenceExprIsEvaluable(t *testing.T) { |
| 29 | + var tests = []struct { |
| 30 | + expr string |
| 31 | + evt *event.Event |
| 32 | + isEval bool |
| 33 | + assertions func(t *testing.T, sexpr *SequenceExpr) |
| 34 | + }{ |
| 35 | + {"evt.name = 'CreateProcess'", &event.Event{Type: event.CreateProcess, Category: event.Process}, true, |
| 36 | + func(t *testing.T, sexpr *SequenceExpr) { |
| 37 | + assert.True(t, sexpr.bitsets.IsTypesInitialized()) |
| 38 | + assert.False(t, sexpr.bitsets.IsBitmaskInitialized()) |
| 39 | + assert.False(t, sexpr.bitsets.IsCategoryInitialized()) |
| 40 | + }, |
| 41 | + }, |
| 42 | + {"evt.name = 'CreateProcess'", &event.Event{Type: event.TerminateProcess, Category: event.Process}, false, nil}, |
| 43 | + {"evt.name = 'CreateProcess' or evt.name = 'TerminateThread'", &event.Event{Type: event.TerminateProcess, Category: event.Process}, false, nil}, |
| 44 | + {"evt.name = 'CreateProcess' or evt.category = 'object'", &event.Event{Type: event.TerminateProcess, Category: event.Process}, false, nil}, |
| 45 | + {"evt.name = 'CreateProcess' or evt.name = 'OpenProcess'", &event.Event{Type: event.OpenProcess, Category: event.Process}, true, |
| 46 | + func(t *testing.T, sexpr *SequenceExpr) { |
| 47 | + assert.True(t, sexpr.bitsets.IsTypesInitialized()) |
| 48 | + assert.False(t, sexpr.bitsets.IsBitmaskInitialized()) |
| 49 | + assert.False(t, sexpr.bitsets.IsCategoryInitialized()) |
| 50 | + }, |
| 51 | + }, |
| 52 | + {"evt.name = 'CreateProcess' or evt.name = 'CreateThread'", &event.Event{Type: event.CreateThread, Category: event.Thread}, true, |
| 53 | + func(t *testing.T, sexpr *SequenceExpr) { |
| 54 | + assert.False(t, sexpr.bitsets.IsTypesInitialized()) |
| 55 | + assert.True(t, sexpr.bitsets.IsBitmaskInitialized()) |
| 56 | + assert.False(t, sexpr.bitsets.IsCategoryInitialized()) |
| 57 | + }, |
| 58 | + }, |
| 59 | + {"evt.name = 'CreateProcess' or evt.category = 'registry'", &event.Event{Type: event.RegSetValue, Category: event.Registry}, true, |
| 60 | + func(t *testing.T, sexpr *SequenceExpr) { |
| 61 | + assert.True(t, sexpr.bitsets.IsTypesInitialized()) |
| 62 | + assert.False(t, sexpr.bitsets.IsBitmaskInitialized()) |
| 63 | + assert.True(t, sexpr.bitsets.IsCategoryInitialized()) |
| 64 | + }, |
| 65 | + }, |
| 66 | + {"evt.name = 'CreateProcess' or evt.name = 'OpenProcess' or evt.category = 'registry'", &event.Event{Type: event.OpenProcess, Category: event.Process}, true, |
| 67 | + func(t *testing.T, sexpr *SequenceExpr) { |
| 68 | + assert.True(t, sexpr.bitsets.IsTypesInitialized()) |
| 69 | + assert.False(t, sexpr.bitsets.IsBitmaskInitialized()) |
| 70 | + assert.True(t, sexpr.bitsets.IsCategoryInitialized()) |
| 71 | + }, |
| 72 | + }, |
| 73 | + {"evt.name = 'CreateProcess' or evt.name = 'SetThreadContext' or evt.category = 'registry'", &event.Event{Type: event.CreateProcess, Category: event.Process}, true, |
| 74 | + func(t *testing.T, sexpr *SequenceExpr) { |
| 75 | + assert.False(t, sexpr.bitsets.IsTypesInitialized()) |
| 76 | + assert.True(t, sexpr.bitsets.IsBitmaskInitialized()) |
| 77 | + assert.True(t, sexpr.bitsets.IsCategoryInitialized()) |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.expr, func(t *testing.T) { |
| 84 | + p := NewParser(tt.expr) |
| 85 | + expr, err := p.ParseExpr() |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + sexpr := &SequenceExpr{Expr: expr} |
| 89 | + sexpr.init() |
| 90 | + sexpr.walk() |
| 91 | + |
| 92 | + assert.Equal(t, tt.isEval, sexpr.IsEvaluable(tt.evt)) |
| 93 | + if tt.assertions != nil { |
| 94 | + tt.assertions(t, sexpr) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments