|
| 1 | +/* |
| 2 | + * Copyright 2021-2022 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 event |
| 20 | + |
| 21 | +import ( |
| 22 | + "github.com/rabbitstack/fibratus/pkg/util/bitmask" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/rabbitstack/fibratus/pkg/sys/etw" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func TestBitmask(t *testing.T) { |
| 30 | + var tests = []struct { |
| 31 | + typ Type |
| 32 | + expected bool |
| 33 | + }{ |
| 34 | + {TerminateThread, true}, |
| 35 | + {TerminateProcess, true}, |
| 36 | + {CreateThread, true}, |
| 37 | + {CreateFile, false}, |
| 38 | + {WriteFile, false}, |
| 39 | + {LoadImage, false}, |
| 40 | + {MapFileRundown, true}, |
| 41 | + {ProcessRundown, true}, |
| 42 | + } |
| 43 | + |
| 44 | + b := bitmask.New() |
| 45 | + for _, typ := range AllWithState() { |
| 46 | + if typ == WriteFile || typ == LoadImage || typ == CreateFile { |
| 47 | + continue |
| 48 | + } |
| 49 | + b.Set(typ.ID()) |
| 50 | + } |
| 51 | + |
| 52 | + for _, tt := range tests { |
| 53 | + t.Run(tt.typ.String(), func(t *testing.T) { |
| 54 | + assert.Equal(t, tt.expected, b.IsSet(tt.typ.ID())) |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestBitSets(t *testing.T) { |
| 60 | + var tests = []struct { |
| 61 | + evt *Event |
| 62 | + expected bool |
| 63 | + }{ |
| 64 | + {&Event{Type: TerminateThread}, true}, |
| 65 | + {&Event{Type: TerminateProcess}, true}, |
| 66 | + {&Event{Type: CreateThread, Category: Thread}, true}, |
| 67 | + {&Event{Type: CreateFile}, false}, |
| 68 | + {&Event{Type: WriteFile}, false}, |
| 69 | + {&Event{Type: LoadImage}, false}, |
| 70 | + {&Event{Type: MapFileRundown}, true}, |
| 71 | + {&Event{Type: ProcessRundown}, true}, |
| 72 | + } |
| 73 | + |
| 74 | + var bitsets BitSets |
| 75 | + |
| 76 | + bitsets.SetBit(BitmaskBitSet, TerminateThread) |
| 77 | + bitsets.SetBit(TypeBitSet, TerminateProcess) |
| 78 | + bitsets.SetBit(CategoryBitSet, CreateThread) |
| 79 | + bitsets.SetBit(TypeBitSet, MapFileRundown) |
| 80 | + bitsets.SetBit(BitmaskBitSet, ProcessRundown) |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.evt.Type.String(), func(t *testing.T) { |
| 84 | + assert.Equal(t, tt.expected, bitsets.IsBitSet(tt.evt)) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func BenchmarkBitmask(b *testing.B) { |
| 90 | + b.ReportAllocs() |
| 91 | + |
| 92 | + bm := bitmask.New() |
| 93 | + bm.Set(TerminateThread.ID()) |
| 94 | + bm.Set(CreateThread.ID()) |
| 95 | + bm.Set(TerminateProcess.ID()) |
| 96 | + bm.Set(CreateFile.ID()) |
| 97 | + |
| 98 | + evt := &etw.EventRecord{Header: etw.EventHeader{ProviderID: ThreadEventGUID, EventDescriptor: etw.EventDescriptor{Opcode: 2}}} |
| 99 | + |
| 100 | + b.ResetTimer() |
| 101 | + |
| 102 | + for i := 0; i < b.N; i++ { |
| 103 | + if !bm.IsSet(evt.ID()) { |
| 104 | + panic("mask should be present") |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func BenchmarkStdlibMap(b *testing.B) { |
| 110 | + b.ReportAllocs() |
| 111 | + |
| 112 | + evts := make(map[Type]bool) |
| 113 | + evts[TerminateThread] = true |
| 114 | + evts[CreateThread] = true |
| 115 | + evts[TerminateProcess] = true |
| 116 | + evts[CreateFile] = true |
| 117 | + |
| 118 | + evt := etw.EventRecord{Header: etw.EventHeader{ProviderID: ThreadEventGUID, EventDescriptor: etw.EventDescriptor{Opcode: 2}}} |
| 119 | + etype := NewTypeFromEventRecord(&evt) |
| 120 | + |
| 121 | + b.ResetTimer() |
| 122 | + |
| 123 | + for i := 0; i < b.N; i++ { |
| 124 | + if !evts[etype] { |
| 125 | + panic("event should be present") |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments