forked from slackhq/go-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
147 lines (127 loc) · 3.91 KB
/
parser_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
package main
import (
"github.com/stretchr/testify/assert"
"syscall"
"testing"
"time"
)
func TestAuditConstants(t *testing.T) {
assert.Equal(t, 7, HEADER_MIN_LENGTH)
assert.Equal(t, 6, HEADER_START_POS)
assert.Equal(t, time.Second*2, COMPLETE_AFTER)
assert.Equal(t, []byte{")"[0]}, headerEndChar)
}
func TestNewAuditMessage(t *testing.T) {
msg := &syscall.NetlinkMessage{
Header: syscall.NlMsghdr{
Len: uint32(44),
Type: uint16(1309),
Flags: uint16(0),
Seq: uint32(0),
Pid: uint32(0),
},
Data: []byte("audit(10000001:99): hi there"),
}
am := NewAuditMessage(msg)
assert.Equal(t, uint16(1309), am.Type)
assert.Equal(t, 99, am.Seq)
assert.Equal(t, "10000001", am.AuditTime)
assert.Equal(t, "hi there", am.Data)
}
func TestAuditMessageGroup_AddMessage(t *testing.T) {
uidMap = make(map[string]string, 0)
uidMap["0"] = "hi"
uidMap["1"] = "nope"
amg := &AuditMessageGroup{
Seq: 1,
AuditTime: "ok",
CompleteAfter: time.Now().Add(COMPLETE_AFTER),
UidMap: make(map[string]string, 2),
}
m := &AuditMessage{
Data: "uid=0 things notuid=nopethisisnot",
}
amg.AddMessage(m)
assert.Equal(t, 1, len(amg.Msgs), "Expected 1 message")
assert.Equal(t, m, amg.Msgs[0], "First message was wrong")
assert.Equal(t, 1, len(amg.UidMap), "Incorrect uid mapping count")
assert.Equal(t, "hi", amg.UidMap["0"])
// Make sure we don't parse uids for message types that don't have them
m = &AuditMessage{
Type: uint16(1309),
Data: "uid=1",
}
amg.AddMessage(m)
assert.Equal(t, 2, len(amg.Msgs), "Expected 2 messages")
assert.Equal(t, m, amg.Msgs[1], "2nd message was wrong")
assert.Equal(t, 1, len(amg.UidMap), "Incorrect uid mapping count")
m = &AuditMessage{
Type: uint16(1307),
Data: "uid=1",
}
amg.AddMessage(m)
assert.Equal(t, 3, len(amg.Msgs), "Expected 2 messages")
assert.Equal(t, m, amg.Msgs[2], "3rd message was wrong")
assert.Equal(t, 1, len(amg.UidMap), "Incorrect uid mapping count")
}
func TestNewAuditMessageGroup(t *testing.T) {
uidMap = make(map[string]string, 0)
m := &AuditMessage{
Type: uint16(1300),
Seq: 1019,
AuditTime: "9919",
Data: "Stuff is here",
}
amg := NewAuditMessageGroup(m)
assert.Equal(t, 1019, amg.Seq)
assert.Equal(t, "9919", amg.AuditTime)
assert.True(t, amg.CompleteAfter.After(time.Now()), "Complete after time should be greater than right now")
assert.Equal(t, 6, cap(amg.Msgs), "Msgs capacity should be 6")
assert.Equal(t, 1, len(amg.Msgs), "Msgs should only have 1 message")
assert.Equal(t, 0, len(amg.UidMap), "No uids in the original message")
assert.Equal(t, m, amg.Msgs[0], "First message should be the original")
}
func Test_getUsername(t *testing.T) {
uidMap = make(map[string]string, 0)
assert.Equal(t, "root", getUsername("0"), "0 should be root you animal")
assert.Equal(t, "UNKNOWN_USER", getUsername("-1"), "Expected UNKNOWN_USER")
val, ok := uidMap["0"]
if !ok {
t.Fatal("Expected the uid mapping to be cached")
}
assert.Equal(t, "root", val)
val, ok = uidMap["-1"]
if !ok {
t.Fatal("Expected the uid mapping to be cached")
}
assert.Equal(t, "UNKNOWN_USER", val)
}
func TestAuditMessageGroup_mapUids(t *testing.T) {
uidMap = make(map[string]string, 0)
uidMap["0"] = "hi"
uidMap["1"] = "there"
uidMap["2"] = "fun"
uidMap["3"] = "test"
uidMap["99999"] = "derp"
amg := &AuditMessageGroup{
Seq: 1,
AuditTime: "ok",
CompleteAfter: time.Now().Add(COMPLETE_AFTER),
UidMap: make(map[string]string, 2),
}
m := &AuditMessage{
Data: "uid=0 1uid=1 2uid=2 3uid=3 not here 4uid=99999",
}
amg.mapUids(m)
assert.Equal(t, 5, len(amg.UidMap), "Uid map is too big")
assert.Equal(t, "hi", amg.UidMap["0"])
assert.Equal(t, "there", amg.UidMap["1"])
assert.Equal(t, "fun", amg.UidMap["2"])
assert.Equal(t, "test", amg.UidMap["3"])
assert.Equal(t, "derp", amg.UidMap["99999"])
}
func Benchmark_getUsername(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = getUsername("0")
}
}