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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package logger
import (
"bufio"
"bytes"
"context"
"fmt"
"log"
"sync"
"testing"
"time"
qt "github.com/frankban/quicktest"
"tailscale.com/tailcfg"
"tailscale.com/version"
)
func TestFuncWriter(t *testing.T) {
w := FuncWriter(t.Logf)
lg := log.New(w, "prefix: ", 0)
lg.Printf("plumbed through")
}
func TestStdLogger(t *testing.T) {
lg := StdLogger(t.Logf)
lg.Printf("plumbed through")
}
func logTester(want []string, t *testing.T, i *int) Logf {
return func(format string, args ...any) {
got := fmt.Sprintf(format, args...)
if *i >= len(want) {
t.Fatalf("Logging continued past end of expected input: %s", got)
}
if got != want[*i] {
t.Fatalf("\nwanted: %s\n got: %s", want[*i], got)
}
t.Log(got)
*i++
}
}
func TestRateLimiter(t *testing.T) {
want := []string{
"boring string with constant formatting (constant)",
"templated format string no. 0",
"boring string with constant formatting (constant)",
"[RATELIMIT] format(\"boring string with constant formatting %s\")",
"templated format string no. 1",
"[RATELIMIT] format(\"templated format string no. %d\")",
"Make sure this string makes it through the rest (that are blocked) 4",
"4 shouldn't get filtered.",
"hello 1",
"hello 2",
"[RATELIMIT] format(\"hello %v\")",
"[RATELIMIT] format(\"hello %v\") (2 dropped)",
"hello 5",
"hello 6",
"[RATELIMIT] format(\"hello %v\")",
"hello 7",
}
var now time.Time
nowf := func() time.Time { return now }
testsRun := 0
lgtest := logTester(want, t, &testsRun)
lg := RateLimitedFnWithClock(lgtest, 1*time.Minute, 2, 50, nowf)
var prefixed Logf
for i := range 10 {
lg("boring string with constant formatting %s", "(constant)")
lg("templated format string no. %d", i)
if i == 4 {
lg("Make sure this string makes it through the rest (that are blocked) %d", i)
prefixed = WithPrefix(lg, string(rune('0'+i)))
prefixed(" shouldn't get filtered.")
}
}
lg("hello %v", 1)
lg("hello %v", 2) // printed, but rate limit starts
lg("hello %v", 3) // rate limited (not printed)
now = now.Add(1 * time.Minute)
lg("hello %v", 4) // still limited (not printed)
now = now.Add(1 * time.Minute)
lg("hello %v", 5) // restriction lifted; prints drop count + message
lg("hello %v", 6) // printed, but rate limit starts
now = now.Add(2 * time.Minute)
lg("hello %v", 7) // restriction lifted; no drop count needed
if testsRun < len(want) {
t.Fatalf("Tests after %s weren't logged.", want[testsRun])
}
}
func testTimer(d time.Duration) func() time.Time {
timeNow := time.Now()
return func() time.Time {
timeNow = timeNow.Add(d)
return timeNow
}
}
func TestLogOnChange(t *testing.T) {
want := []string{
"1 2 3 4 5 6",
"1 2 3 4 5 6",
"1 2 3 4 5 7",
"1 2 3 4 5",
"1 2 3 4 5 6 7",
}
timeNow := testTimer(1 * time.Second)
testsRun := 0
lgtest := logTester(want, t, &testsRun)
lg := LogOnChange(lgtest, 5*time.Second, timeNow)
for range 10 {
lg("%s", "1 2 3 4 5 6")
}
lg("1 2 3 4 5 7")
lg("1 2 3 4 5")
lg("1 2 3 4 5")
lg("1 2 3 4 5 6 7")
if testsRun < len(want) {
t.Fatalf("'Wanted' lines including and after [%s] weren't logged.", want[testsRun])
}
}
func TestArgWriter(t *testing.T) {
got := new(bytes.Buffer)
fmt.Fprintf(got, "Greeting: %v", ArgWriter(func(bw *bufio.Writer) {
bw.WriteString("Hello, ")
bw.WriteString("world")
bw.WriteByte('!')
}))
const want = "Greeting: Hello, world!"
if got.String() != want {
t.Errorf("got %q; want %q", got, want)
}
}
func TestSynchronization(t *testing.T) {
timeNow := testTimer(1 * time.Second)
tests := []struct {
name string
logf Logf
}{
{"RateLimitedFn", RateLimitedFn(t.Logf, 1*time.Minute, 2, 50)},
{"LogOnChange", LogOnChange(t.Logf, 5*time.Second, timeNow)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var wg sync.WaitGroup
wg.Add(2)
f := func() {
tt.logf("1 2 3 4 5")
wg.Done()
}
go f()
go f()
wg.Wait()
})
}
}
// test that RateLimitedFn is safe for reentrancy without deadlocking
func TestRateLimitedFnReentrancy(t *testing.T) {
rlogf := RateLimitedFn(t.Logf, time.Nanosecond, 10, 10)
rlogf("Hello.")
rlogf("Hello, %v", ArgWriter(func(bw *bufio.Writer) {
bw.WriteString("world")
}))
rlogf("Hello, %v", ArgWriter(func(bw *bufio.Writer) {
bw.WriteString("bye")
rlogf("boom") // this used to deadlock
}))
}
func TestContext(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
// Test that FromContext returns log.Printf when the context has no custom log function.
defer log.SetOutput(log.Writer())
defer log.SetFlags(log.Flags())
var buf bytes.Buffer
log.SetOutput(&buf)
log.SetFlags(0)
logf := FromContext(ctx)
logf("a")
c.Assert(buf.String(), qt.Equals, "a\n")
// Test that FromContext and Ctx work together.
var called bool
markCalled := func(string, ...any) {
called = true
}
ctx = Ctx(ctx, markCalled)
logf = FromContext(ctx)
logf("a")
c.Assert(called, qt.IsTrue)
}
func TestJSON(t *testing.T) {
var buf bytes.Buffer
var logf Logf = func(f string, a ...any) { fmt.Fprintf(&buf, f, a...) }
logf.JSON(1, "foo", &tailcfg.Hostinfo{})
want := "[v\x00JSON]1" + `{"foo":{}}`
if got := buf.String(); got != want {
t.Errorf("mismatch\n got: %q\nwant: %q\n", got, want)
}
}
func TestAsJSON(t *testing.T) {
got := fmt.Sprintf("got %v", AsJSON(struct {
Foo string
Bar int
}{"hi", 123}))
const want = `got {"Foo":"hi","Bar":123}`
if got != want {
t.Errorf("got %#q; want %#q", got, want)
}
got = fmt.Sprintf("got %v", AsJSON(func() {}))
const wantErr = `got %!JSON-ERROR:json: unsupported type: func()`
if got != wantErr {
t.Errorf("for marshal error, got %#q; want %#q", got, wantErr)
}
if version.IsRace() {
// skip the rest of the test in race mode;
// race mode causes more allocs which we don't care about.
return
}
var buf bytes.Buffer
n := int(testing.AllocsPerRun(1000, func() {
buf.Reset()
fmt.Fprintf(&buf, "got %v", AsJSON("hi"))
}))
if n > 2 {
// the JSON AsMarshal itself + boxing
// the asJSONResult into an interface (which needs
// to happen at some point to get to fmt, so might
// as well return an interface from AsJSON))
t.Errorf("allocs = %v; want max 2", n)
}
}
func TestHTTPServerLogFilter(t *testing.T) {
var buf bytes.Buffer
logf := func(format string, args ...any) {
t.Logf("[logf] "+format, args...)
fmt.Fprintf(&buf, format, args...)
}
lf := HTTPServerLogFilter{logf}
quietLogger := log.New(lf, "", 0)
quietLogger.Printf("foo bar")
quietLogger.Printf("http: TLS handshake error from %s:%d: EOF", "1.2.3.4", 9999)
quietLogger.Printf("baz")
const want = "foo bar\nbaz\n"
if s := buf.String(); s != want {
t.Errorf("got buf=%q, want %q", s, want)
}
}
|