mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-01 22:15:51 +00:00
59026a291d
Prior to wireguard-go using printf-style logging, all wireguard-go logging occurred using format string "%s". We fixed that but continued to use %s when we rewrote peer identifiers into Tailscale style. This commit removes that %sl, which makes rate limiting work correctly. As a happy side-benefit, it should generate less garbage. Instead of replacing all wireguard-go peer identifiers that might occur anywhere in a fully formatted log string, assume that they only come from args. Check all args for things that look like *device.Peers and replace them with appropriately reformatted strings. There is a variety of ways that this could go wrong (unusual format verbs or modifiers, peer identifiers occurring as part of a larger printed object, future API changes), but none of them occur now, are likely to be added, or would be hard to work around if they did. Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package wglog_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"tailscale.com/types/wgkey"
|
|
"tailscale.com/wgengine/wgcfg"
|
|
"tailscale.com/wgengine/wglog"
|
|
)
|
|
|
|
func TestLogger(t *testing.T) {
|
|
tests := []struct {
|
|
format string
|
|
args []interface{}
|
|
want string
|
|
omit bool
|
|
}{
|
|
{"hi", nil, "hi", false},
|
|
{"Routine: starting", nil, "", true},
|
|
{"%v says it misses you", []interface{}{stringer("peer(IMTB…r7lM)")}, "[IMTBr] says it misses you", false},
|
|
}
|
|
|
|
type log struct {
|
|
format string
|
|
args []interface{}
|
|
}
|
|
|
|
c := make(chan log, 1)
|
|
logf := func(format string, args ...interface{}) {
|
|
select {
|
|
case c <- log{format, args}:
|
|
default:
|
|
t.Errorf("wrote %q, but shouldn't have", fmt.Sprintf(format, args...))
|
|
}
|
|
}
|
|
|
|
x := wglog.NewLogger(logf)
|
|
key, err := wgkey.ParseHex("20c4c1ae54e1fd37cab6e9a532ca20646aff496796cc41d4519560e5e82bee53")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
x.SetPeers([]wgcfg.Peer{{PublicKey: key}})
|
|
|
|
for _, tt := range tests {
|
|
if tt.omit {
|
|
// Write a message ourselves into the channel.
|
|
// Then if logf also attempts to write into the channel, it'll fail.
|
|
c <- log{}
|
|
}
|
|
x.DeviceLogger.Errorf(tt.format, tt.args...)
|
|
gotLog := <-c
|
|
if tt.omit {
|
|
continue
|
|
}
|
|
if got := fmt.Sprintf(gotLog.format, gotLog.args...); got != tt.want {
|
|
t.Errorf("Printf(%q, %v) = %q want %q", tt.format, tt.args, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func stringer(s string) stringerString {
|
|
return stringerString(s)
|
|
}
|
|
|
|
type stringerString string
|
|
|
|
func (s stringerString) String() string { return string(s) }
|