2020-08-25 18:42:54 +00:00
|
|
|
// 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 ipn
|
|
|
|
|
|
|
|
import (
|
2020-09-04 15:36:07 +00:00
|
|
|
"reflect"
|
2020-08-25 18:42:54 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/control/controlclient"
|
|
|
|
"tailscale.com/logtail"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/tstest"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
"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
|
|
|
})
|
|
|
|
|
|
|
|
logid := func(hex byte) logtail.PublicID {
|
|
|
|
var ret logtail.PublicID
|
|
|
|
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.
|
|
|
|
store := &MemoryStore{
|
|
|
|
cache: make(map[StateKey][]byte),
|
|
|
|
}
|
|
|
|
e, err := wgengine.NewFakeUserspaceEngine(logListen.Logf, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lb, err := NewLocalBackend(logListen.Logf, idA.String(), store, e)
|
|
|
|
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
|
|
|
|
|
|
|
// custom adjustments for required non-nil fields
|
|
|
|
lb.prefs = NewPrefs()
|
|
|
|
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) {
|
|
|
|
t.Errorf("remain %q, want %q", remain, wantRemain)
|
2020-08-25 18:42:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log prefs line
|
|
|
|
persist := &controlclient.Persist{}
|
|
|
|
prefs := NewPrefs()
|
|
|
|
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
|
|
|
|
status := &wgengine.Status{
|
|
|
|
Peers: []wgengine.PeerStatus{wgengine.PeerStatus{
|
|
|
|
TxBytes: 10,
|
|
|
|
RxBytes: 10,
|
|
|
|
LastHandshake: time.Now(),
|
|
|
|
NodeKey: tailcfg.NodeKey(key.NewPrivate()),
|
|
|
|
}},
|
|
|
|
LocalAddrs: []string{"idk an address"},
|
|
|
|
}
|
2020-10-29 22:26:10 +00:00
|
|
|
lb.mu.Lock()
|
|
|
|
lb.parseWgStatusLocked(status)
|
|
|
|
lb.mu.Unlock()
|
2020-08-25 18:42:54 +00:00
|
|
|
|
2020-09-04 15:36:07 +00:00
|
|
|
t.Run("after_peers", testWantRemain())
|
2020-08-25 18:42:54 +00:00
|
|
|
}
|