-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity_test.go
More file actions
169 lines (140 loc) · 4.16 KB
/
security_test.go
File metadata and controls
169 lines (140 loc) · 4.16 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
package emit
import (
"bytes"
"strings"
"testing"
)
// TestSensitiveFieldMasking tests that sensitive fields are properly masked
func TestSensitiveFieldMasking(t *testing.T) {
var buf bytes.Buffer
testLogger := &Logger{
level: DEBUG,
writer: &buf,
format: JSON_FORMAT,
sensitiveMode: MASK_SENSITIVE,
piiMode: SHOW_PII,
sensitiveFields: defaultSensitiveFields,
piiFields: defaultPIIFields,
maskString: "***MASKED***",
piiMaskString: "***PII***",
}
originalLogger := defaultLogger
defaultLogger = testLogger
defer func() { defaultLogger = originalLogger }()
// Test with a map containing sensitive fields
Info.Field("Login attempt",
NewFields().
String("password", "secret123").
String("api_key", "sk-1234567890").
String("username", "testuser"))
output := buf.String()
// Password and API key should be masked
if strings.Contains(output, "secret123") {
t.Error("Password should be masked")
}
if strings.Contains(output, "sk-1234567890") {
t.Error("API key should be masked")
}
// Username should be visible
if !strings.Contains(output, "testuser") {
t.Error("Username should be visible")
}
}
// TestPIIFieldMasking tests that PII fields are properly masked
func TestPIIFieldMasking(t *testing.T) {
var buf bytes.Buffer
testLogger := &Logger{
level: DEBUG,
writer: &buf,
format: JSON_FORMAT,
sensitiveMode: SHOW_SENSITIVE,
piiMode: MASK_PII,
sensitiveFields: defaultSensitiveFields,
piiFields: defaultPIIFields,
maskString: "***MASKED***",
piiMaskString: "***PII***",
}
originalLogger := defaultLogger
defaultLogger = testLogger
defer func() { defaultLogger = originalLogger }()
Info.Field("User registration",
NewFields().
String("email", "user@example.com").
String("phone", "555-123-4567").
String("status", "active"))
output := buf.String()
// Email and phone should be masked as PII
if strings.Contains(output, "user@example.com") {
t.Error("Email should be masked as PII")
}
if strings.Contains(output, "555-123-4567") {
t.Error("Phone should be masked as PII")
}
// Status should be visible
if !strings.Contains(output, "active") {
t.Error("Status should be visible")
}
}
// TestBothMaskingModes tests sensitive and PII masking together
func TestBothMaskingModes(t *testing.T) {
var buf bytes.Buffer
testLogger := &Logger{
level: DEBUG,
writer: &buf,
format: JSON_FORMAT,
sensitiveMode: MASK_SENSITIVE,
piiMode: MASK_PII,
sensitiveFields: defaultSensitiveFields,
piiFields: defaultPIIFields,
maskString: "***MASKED***",
piiMaskString: "***PII***",
}
originalLogger := defaultLogger
defaultLogger = testLogger
defer func() { defaultLogger = originalLogger }()
Info.Field("Combined test",
NewFields().
String("password", "secret").
String("email", "test@test.com").
String("message", "Hello"))
output := buf.String()
// Both should be masked
if strings.Contains(output, "secret") {
t.Error("Password should be masked")
}
if strings.Contains(output, "test@test.com") {
t.Error("Email should be masked")
}
// Message should be visible
if !strings.Contains(output, "Hello") {
t.Error("Message should be visible")
}
}
// TestNoMasking tests that all data is visible when masking is disabled
func TestNoMasking(t *testing.T) {
var buf bytes.Buffer
testLogger := &Logger{
level: DEBUG,
writer: &buf,
format: JSON_FORMAT,
sensitiveMode: SHOW_SENSITIVE,
piiMode: SHOW_PII,
sensitiveFields: defaultSensitiveFields,
piiFields: defaultPIIFields,
}
originalLogger := defaultLogger
defaultLogger = testLogger
defer func() { defaultLogger = originalLogger }()
Info.Field("No masking test",
NewFields().
String("password", "visible_password").
String("email", "visible@email.com"))
output := buf.String()
// Both should be visible
if !strings.Contains(output, "visible_password") {
t.Error("Password should be visible when masking is off")
}
if !strings.Contains(output, "visible@email.com") {
t.Error("Email should be visible when masking is off")
}
}