-
Notifications
You must be signed in to change notification settings - Fork 4
/
audit_logger_test.go
126 lines (112 loc) · 3.88 KB
/
audit_logger_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
package passhash_test
import (
"testing"
)
import (
"github.com/dhui/passhash"
)
// setupAuditLoggerTestData logs test data to the given AuditLogger
func setupAuditLoggerTestData(userID passhash.UserID, al passhash.AuditLogger) int { // nolint: unparam
numIters := 5
for i := 0; i < numIters; i++ {
al.Log(userID, passhash.AuthnSucceeded, passhash.EmptyIP)
al.Log(userID, passhash.AuthnFailed, passhash.EmptyIP)
al.Log(userID, passhash.UpgradedKdf, passhash.EmptyIP)
}
return numIters * 3
}
func TestDummyAuditLoggerLog(t *testing.T) {
al := passhash.DummyAuditLogger{}
userID := passhash.UserID(0)
al.Log(userID, passhash.AuthnSucceeded, passhash.EmptyIP)
}
func TestDummyAuditLoggerLastN(t *testing.T) {
al := &passhash.DummyAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
if l := len(al.LastN(userID, n)); l != 0 {
t.Errorf("DummyAuditLogger has data! Has %d elements", l)
}
}
func TestDummyAuditLoggerLastNWithTypes(t *testing.T) {
al := &passhash.DummyAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
if l := len(al.LastNWithTypes(userID, n, passhash.AuthnSucceeded)); l != 0 {
t.Errorf("DummyAuditLogger has data! Has %d elements", l)
}
}
func TestMemoryAuditLoggerLog(t *testing.T) {
al := passhash.MemoryAuditLogger{}
userID := passhash.UserID(0)
al.Log(userID, passhash.AuthnSucceeded, passhash.EmptyIP)
if l := len(al.LastN(userID, 10)); l != 1 {
t.Errorf("Unexpected number of logs. Have %d instead of 1", l)
}
}
func TestMemoryAuditLoggerLastNLess(t *testing.T) {
al := &passhash.MemoryAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
lastN := al.LastN(userID, n/2)
if l := len(lastN); l != n/2 {
t.Errorf("Did not retrieve expected number of logs. Have %d logs instead of %d", l, n/2)
}
}
func TestMemoryAuditLoggerLastNEqual(t *testing.T) {
al := &passhash.MemoryAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
lastN := al.LastN(userID, n)
if l := len(lastN); l != n {
t.Errorf("Did not retrieve expected number of logs. Have %d logs instead of %d", l, n)
}
}
func TestMemoryAuditLoggerLastNMore(t *testing.T) {
al := &passhash.MemoryAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
lastN := al.LastN(userID, n*2)
if l := len(lastN); l != n {
t.Errorf("Did not retrieve expected number of logs. Have %d logs instead of %d", l, n)
}
}
func TestMemoryAuditLoggerLastNWithTypesNone(t *testing.T) {
al := &passhash.MemoryAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
lastN := al.LastNWithTypes(userID, n)
if len(lastN) != 0 {
t.Errorf("Got %d log entries when no types were specified", len(lastN))
}
}
func TestMemoryAuditLoggerLastNWithTypesLess(t *testing.T) {
al := &passhash.MemoryAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
lastN := al.LastNWithTypes(userID, (n/3)-3, passhash.AuthnSucceeded)
if l := len(lastN); l != (n/3)-3 {
t.Errorf("Did not retrieve expected number of logs. Have %d logs instead of %d", l, (n/3)-3)
t.Log(lastN)
}
}
func TestMemoryAuditLoggerLastNWithTypesEqual(t *testing.T) {
al := &passhash.MemoryAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
lastN := al.LastNWithTypes(userID, (n / 3), passhash.AuthnSucceeded)
if l := len(lastN); l != n/3 {
t.Errorf("Did not retrieve expected number of logs. Have %d logs instead of %d", l, n/3)
t.Log(lastN)
}
}
func TestMemoryAuditLoggerLastNWithTypesMore(t *testing.T) {
al := &passhash.MemoryAuditLogger{}
userID := passhash.UserID(0)
n := setupAuditLoggerTestData(userID, al)
lastN := al.LastNWithTypes(userID, n, passhash.AuthnSucceeded)
if l := len(lastN); l != n/3 {
t.Errorf("Did not retrieve expected number of logs. Have %d logs instead of %d", l, n/3)
t.Log(lastN)
}
}