-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_test.go
More file actions
163 lines (149 loc) · 5.22 KB
/
Copy pathlog_test.go
File metadata and controls
163 lines (149 loc) · 5.22 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
package main
import (
"bytes"
"log/slog"
"strings"
"testing"
"time"
)
func TestShortUserID(t *testing.T) {
tests := []struct {
input string
want string
}{
{"", "anon"},
{"AbCdEfGhIjKlMnOp", "AbCdEfGh"},
{"short", "short"},
{"exactly8", "exactly8"},
}
for _, tt := range tests {
got := shortUserID(tt.input)
if got != tt.want {
t.Errorf("shortUserID(%q) = %q, want %q", tt.input, got, tt.want)
}
}
}
// withLogCapture sets up a slog logger that writes to a buffer at the given
// level, runs fn, then restores the previous default logger.
func withLogCapture(level slog.Level, fn func()) string {
var buf bytes.Buffer
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: level})
prev := slog.Default()
slog.SetDefault(slog.New(handler))
defer slog.SetDefault(prev)
fn()
return buf.String()
}
func TestLogRequestIsDebugLevel(t *testing.T) {
// At info level, logRequest should produce no output
output := withLogCapture(slog.LevelInfo, func() {
logRequest("GET", "https://example.com/page", 200, 45*time.Millisecond, "192.168.1.5", "AbCdEfGhIjKl")
})
if output != "" {
t.Errorf("logRequest at info level should produce no output, got: %s", output)
}
// At debug level, logRequest should produce output
output = withLogCapture(slog.LevelDebug, func() {
logRequest("GET", "https://example.com/page", 200, 45*time.Millisecond, "192.168.1.5", "AbCdEfGhIjKl")
})
if output == "" {
t.Error("logRequest at debug level should produce output")
}
if !strings.Contains(output, "level=DEBUG") {
t.Errorf("expected level=DEBUG, got: %s", output)
}
if !strings.Contains(output, "ip=192.168.1.5") {
t.Errorf("expected ip=192.168.1.5, got: %s", output)
}
if !strings.Contains(output, "user=AbCdEfGh") {
t.Errorf("expected user=AbCdEfGh (truncated), got: %s", output)
}
}
func TestLogBlockedIsInfoLevel(t *testing.T) {
output := withLogCapture(slog.LevelInfo, func() {
logBlocked("ads.example.com", "https://ads.example.com/banner.js", "||ads.example.com^", "10.0.0.1", "CredIDxyz12345")
})
if !strings.Contains(output, "level=INFO") {
t.Errorf("expected level=INFO, got: %s", output)
}
if !strings.Contains(output, "msg=blocked") {
t.Errorf("expected msg=blocked, got: %s", output)
}
if !strings.Contains(output, "ip=10.0.0.1") {
t.Errorf("expected ip=10.0.0.1, got: %s", output)
}
if !strings.Contains(output, "user=CredIDxy") {
t.Errorf("expected user=CredIDxy (truncated), got: %s", output)
}
}
func TestLogPassthroughIsDebugLevel(t *testing.T) {
// At info level, logPassthrough should produce no output
output := withLogCapture(slog.LevelInfo, func() {
logPassthrough("bank.example.com", "192.168.1.10", "")
})
if output != "" {
t.Errorf("logPassthrough at info level should produce no output, got: %s", output)
}
// At debug level, logPassthrough should produce output
output = withLogCapture(slog.LevelDebug, func() {
logPassthrough("bank.example.com", "192.168.1.10", "")
})
if !strings.Contains(output, "level=DEBUG") {
t.Errorf("expected level=DEBUG, got: %s", output)
}
if !strings.Contains(output, "msg=passthrough") {
t.Errorf("expected msg=passthrough, got: %s", output)
}
if !strings.Contains(output, "user=anon") {
t.Errorf("expected user=anon for empty credential, got: %s", output)
}
}
func TestLogElementHiddenIsDebugLevel(t *testing.T) {
// At info level, logElementHidden should produce no output
output := withLogCapture(slog.LevelInfo, func() {
logElementHidden("example.com", ".ad-banner, #sidebar-ad", "192.168.1.5", "CredABCD1234")
})
if output != "" {
t.Errorf("logElementHidden at info level should produce no output, got: %s", output)
}
// At debug level, logElementHidden should produce output
output = withLogCapture(slog.LevelDebug, func() {
logElementHidden("example.com", ".ad-banner, #sidebar-ad", "192.168.1.5", "CredABCD1234")
})
if !strings.Contains(output, "level=DEBUG") {
t.Errorf("expected level=DEBUG, got: %s", output)
}
if !strings.Contains(output, "msg=element-hidden") {
t.Errorf("expected msg=element-hidden, got: %s", output)
}
if !strings.Contains(output, "host=example.com") {
t.Errorf("expected host=example.com, got: %s", output)
}
if !strings.Contains(output, ".ad-banner") {
t.Errorf("expected rule to contain .ad-banner, got: %s", output)
}
}
func TestLogErrorIsErrorLevel(t *testing.T) {
output := withLogCapture(slog.LevelInfo, func() {
logError("connect/roundtrip", errForTest("dial tcp: timeout"), "192.168.1.5", "SomeCredID")
})
if !strings.Contains(output, "level=ERROR") {
t.Errorf("expected level=ERROR, got: %s", output)
}
if !strings.Contains(output, "ip=192.168.1.5") {
t.Errorf("expected ip=192.168.1.5, got: %s", output)
}
}
func TestLogErrorSuppressedAtWarnLevel(t *testing.T) {
// logError is ERROR level -- should NOT appear when level is set higher
// (there is no level higher than ERROR in slog, so this test just
// verifies it appears at warn level, which is lower than error)
output := withLogCapture(slog.LevelWarn, func() {
logError("test/context", errForTest("some error"), "1.2.3.4", "")
})
if !strings.Contains(output, "level=ERROR") {
t.Errorf("logError should still appear at warn level, got: %s", output)
}
}
type errForTest string
func (e errForTest) Error() string { return string(e) }