2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-08-25 18:42:54 +00:00
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
package ipnlocal
|
2020-08-25 18:42:54 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-04 15:36:07 +00:00
|
|
|
"reflect"
|
2020-08-25 18:42:54 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/ipn/ipnstate"
|
2022-02-28 21:08:45 +00:00
|
|
|
"tailscale.com/ipn/store/mem"
|
2020-08-25 18:42:54 +00:00
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/tstest"
|
|
|
|
"tailscale.com/types/key"
|
2021-05-05 03:36:10 +00:00
|
|
|
"tailscale.com/types/logger"
|
2023-03-01 03:00:00 +00:00
|
|
|
"tailscale.com/types/logid"
|
2021-02-05 23:23:01 +00:00
|
|
|
"tailscale.com/types/persist"
|
2020-08-25 18:42:54 +00:00
|
|
|
"tailscale.com/wgengine"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestLocalLogLines tests to make sure that the log lines required for log parsing are
|
|
|
|
// being logged by the expected functions. Update these tests if moving log lines between
|
|
|
|
// functions.
|
|
|
|
func TestLocalLogLines(t *testing.T) {
|
2020-09-04 15:09:56 +00:00
|
|
|
logListen := tstest.NewLogLineTracker(t.Logf, []string{
|
2020-08-25 18:42:54 +00:00
|
|
|
"SetPrefs: %v",
|
2020-12-21 18:58:06 +00:00
|
|
|
"[v1] peer keys: %s",
|
|
|
|
"[v1] v%v peers: %v",
|
2020-08-25 18:42:54 +00:00
|
|
|
})
|
2021-01-09 00:50:03 +00:00
|
|
|
defer logListen.Close()
|
2020-08-25 18:42:54 +00:00
|
|
|
|
2021-05-05 03:36:10 +00:00
|
|
|
// Put a rate-limiter with a burst of 0 between the components below.
|
|
|
|
// This instructs the rate-limiter to eliminate all logging that
|
|
|
|
// isn't explicitly exempt from rate-limiting.
|
|
|
|
// This lets the logListen tracker verify that the rate-limiter allows these key lines.
|
|
|
|
logf := logger.RateLimitedFnWithClock(logListen.Logf, 5*time.Second, 0, 10, time.Now)
|
|
|
|
|
2023-03-01 03:00:00 +00:00
|
|
|
logid := func(hex byte) logid.PublicID {
|
|
|
|
var ret logid.PublicID
|
2020-08-25 18:42:54 +00:00
|
|
|
for i := 0; i < len(ret); i++ {
|
|
|
|
ret[i] = hex
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
idA := logid(0xaa)
|
|
|
|
|
|
|
|
// set up a LocalBackend, super bare bones. No functional data.
|
2022-02-28 21:08:45 +00:00
|
|
|
store := new(mem.Store)
|
2021-05-05 03:36:10 +00:00
|
|
|
e, err := wgengine.NewFakeUserspaceEngine(logf, 0)
|
2020-08-25 18:42:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-15 18:48:49 +00:00
|
|
|
t.Cleanup(e.Close)
|
2020-08-25 18:42:54 +00:00
|
|
|
|
2023-03-23 17:49:56 +00:00
|
|
|
lb, err := NewLocalBackend(logf, idA, store, nil, e, 0)
|
2020-08-25 18:42:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-09-04 15:36:07 +00:00
|
|
|
defer lb.Shutdown()
|
2020-08-25 18:42:54 +00:00
|
|
|
|
|
|
|
lb.hostinfo = &tailcfg.Hostinfo{}
|
|
|
|
// hacky manual override of the usual log-on-change behaviour of keylogf
|
|
|
|
lb.keyLogf = logListen.Logf
|
|
|
|
|
2020-09-04 15:36:07 +00:00
|
|
|
testWantRemain := func(wantRemain ...string) func(t *testing.T) {
|
2020-08-25 18:42:54 +00:00
|
|
|
return func(t *testing.T) {
|
2020-09-04 15:36:07 +00:00
|
|
|
if remain := logListen.Check(); !reflect.DeepEqual(remain, wantRemain) {
|
2021-05-05 03:36:10 +00:00
|
|
|
t.Helper()
|
2020-09-04 15:36:07 +00:00
|
|
|
t.Errorf("remain %q, want %q", remain, wantRemain)
|
2020-08-25 18:42:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log prefs line
|
2021-02-05 23:23:01 +00:00
|
|
|
persist := &persist.Persist{}
|
2021-02-04 21:12:42 +00:00
|
|
|
prefs := ipn.NewPrefs()
|
2020-08-25 18:42:54 +00:00
|
|
|
prefs.Persist = persist
|
|
|
|
lb.SetPrefs(prefs)
|
|
|
|
|
2020-12-21 18:58:06 +00:00
|
|
|
t.Run("after_prefs", testWantRemain("[v1] peer keys: %s", "[v1] v%v peers: %v"))
|
2020-08-25 18:42:54 +00:00
|
|
|
|
|
|
|
// log peers, peer keys
|
2021-05-05 03:36:10 +00:00
|
|
|
lb.mu.Lock()
|
|
|
|
lb.parseWgStatusLocked(&wgengine.Status{
|
2021-02-04 21:12:42 +00:00
|
|
|
Peers: []ipnstate.PeerStatusLite{{
|
2020-08-25 18:42:54 +00:00
|
|
|
TxBytes: 10,
|
|
|
|
RxBytes: 10,
|
|
|
|
LastHandshake: time.Now(),
|
2021-11-02 01:38:24 +00:00
|
|
|
NodeKey: key.NewNode().Public(),
|
2020-08-25 18:42:54 +00:00
|
|
|
}},
|
2021-05-05 03:36:10 +00:00
|
|
|
})
|
2020-10-29 22:26:10 +00:00
|
|
|
lb.mu.Unlock()
|
2020-08-25 18:42:54 +00:00
|
|
|
|
2020-09-04 15:36:07 +00:00
|
|
|
t.Run("after_peers", testWantRemain())
|
2021-05-05 03:36:10 +00:00
|
|
|
|
|
|
|
// Log it again with different stats to ensure it's not dup-suppressed.
|
|
|
|
logListen.Reset()
|
|
|
|
lb.mu.Lock()
|
|
|
|
lb.parseWgStatusLocked(&wgengine.Status{
|
|
|
|
Peers: []ipnstate.PeerStatusLite{{
|
|
|
|
TxBytes: 11,
|
|
|
|
RxBytes: 12,
|
|
|
|
LastHandshake: time.Now(),
|
2021-11-02 01:38:24 +00:00
|
|
|
NodeKey: key.NewNode().Public(),
|
2021-05-05 03:36:10 +00:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
lb.mu.Unlock()
|
|
|
|
t.Run("after_second_peer_status", testWantRemain("SetPrefs: %v"))
|
2020-08-25 18:42:54 +00:00
|
|
|
}
|