mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-01 17:49:02 +00:00
ipn: implement Prefs.Equals efficiently.
Signed-off-by: David Anderson <dave@natulte.net>
This commit is contained in:
@@ -39,6 +39,21 @@ type Persist struct {
|
||||
LoginName string
|
||||
}
|
||||
|
||||
func (p *Persist) Equals(p2 *Persist) bool {
|
||||
if p == nil && p2 == nil {
|
||||
return true
|
||||
}
|
||||
if p == nil || p2 == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return p.PrivateMachineKey.Equal(p2.PrivateMachineKey) &&
|
||||
p.PrivateNodeKey.Equal(p2.PrivateNodeKey) &&
|
||||
p.OldPrivateNodeKey.Equal(p2.OldPrivateNodeKey) &&
|
||||
p.Provider == p2.Provider &&
|
||||
p.LoginName == p2.LoginName
|
||||
}
|
||||
|
||||
func (p *Persist) Pretty() string {
|
||||
var mk, ok, nk wgcfg.Key
|
||||
if !p.PrivateMachineKey.IsZero() {
|
||||
|
||||
98
control/controlclient/persist_test.go
Normal file
98
control/controlclient/persist_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// 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 controlclient
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/tailscale/wireguard-go/wgcfg"
|
||||
)
|
||||
|
||||
func TestPersistEqual(t *testing.T) {
|
||||
persistHandles := []string{"PrivateMachineKey", "PrivateNodeKey", "OldPrivateNodeKey", "Provider", "LoginName"}
|
||||
if have := fieldsOf(reflect.TypeOf(Persist{})); !reflect.DeepEqual(have, persistHandles) {
|
||||
t.Errorf("Persist.Equal check might be out of sync\nfields: %q\nhandled: %q\n",
|
||||
have, persistHandles)
|
||||
}
|
||||
|
||||
newPrivate := func() wgcfg.PrivateKey {
|
||||
k, err := wgcfg.NewPrivateKey()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return k
|
||||
}
|
||||
k1 := newPrivate()
|
||||
tests := []struct {
|
||||
a, b *Persist
|
||||
want bool
|
||||
}{
|
||||
{nil, nil, true},
|
||||
{nil, &Persist{}, false},
|
||||
{&Persist{}, nil, false},
|
||||
{&Persist{}, &Persist{}, true},
|
||||
|
||||
{
|
||||
&Persist{PrivateMachineKey: k1},
|
||||
&Persist{PrivateMachineKey: newPrivate()},
|
||||
false,
|
||||
},
|
||||
{
|
||||
&Persist{PrivateMachineKey: k1},
|
||||
&Persist{PrivateMachineKey: k1},
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
&Persist{PrivateNodeKey: k1},
|
||||
&Persist{PrivateNodeKey: newPrivate()},
|
||||
false,
|
||||
},
|
||||
{
|
||||
&Persist{PrivateNodeKey: k1},
|
||||
&Persist{PrivateNodeKey: k1},
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
&Persist{OldPrivateNodeKey: k1},
|
||||
&Persist{OldPrivateNodeKey: newPrivate()},
|
||||
false,
|
||||
},
|
||||
{
|
||||
&Persist{OldPrivateNodeKey: k1},
|
||||
&Persist{OldPrivateNodeKey: k1},
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
&Persist{Provider: "google"},
|
||||
&Persist{Provider: "o365"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
&Persist{Provider: "google"},
|
||||
&Persist{Provider: "google"},
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
&Persist{LoginName: "foo@tailscale.com"},
|
||||
&Persist{LoginName: "bar@tailscale.com"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
&Persist{LoginName: "foo@tailscale.com"},
|
||||
&Persist{LoginName: "foo@tailscale.com"},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for i, test := range tests {
|
||||
if got := test.a.Equals(test.b); got != test.want {
|
||||
t.Errorf("%d. Equals = %v; want %v", i, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user