mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.2 KiB
Go
108 lines
2.2 KiB
Go
![]()
4 years ago
|
// 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.
|
||
|
|
||
![]()
3 years ago
|
package persist
|
||
![]()
4 years ago
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
|
||
![]()
4 years ago
|
"tailscale.com/types/wgkey"
|
||
![]()
4 years ago
|
)
|
||
|
|
||
![]()
3 years ago
|
func fieldsOf(t reflect.Type) (fields []string) {
|
||
|
for i := 0; i < t.NumField(); i++ {
|
||
|
if name := t.Field(i).Name; name != "_" {
|
||
|
fields = append(fields, name)
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
![]()
4 years ago
|
func TestPersistEqual(t *testing.T) {
|
||
![]()
4 years ago
|
persistHandles := []string{"LegacyFrontendPrivateMachineKey", "PrivateNodeKey", "OldPrivateNodeKey", "Provider", "LoginName"}
|
||
![]()
4 years ago
|
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)
|
||
|
}
|
||
|
|
||
![]()
4 years ago
|
newPrivate := func() wgkey.Private {
|
||
|
k, err := wgkey.NewPrivate()
|
||
![]()
4 years ago
|
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},
|
||
|
|
||
|
{
|
||
![]()
4 years ago
|
&Persist{LegacyFrontendPrivateMachineKey: k1},
|
||
|
&Persist{LegacyFrontendPrivateMachineKey: newPrivate()},
|
||
![]()
4 years ago
|
false,
|
||
|
},
|
||
|
{
|
||
![]()
4 years ago
|
&Persist{LegacyFrontendPrivateMachineKey: k1},
|
||
|
&Persist{LegacyFrontendPrivateMachineKey: k1},
|
||
![]()
4 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|