tailcfg: add Hostinfo.BasicallyEqual

Change-Id: I4fd82dea9bb3618f3a93d8b2d2486f3eabbbd915
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2021-12-22 12:51:39 -08:00
parent 0aa4c6f147
commit 850a603caa
2 changed files with 89 additions and 1 deletions

View File

@ -571,12 +571,31 @@ func (h *Hostinfo) Equal(h2 *Hostinfo) bool {
if h == nil && h2 == nil {
return true
}
if (h == nil) != (h2 == nil) {
if h == nil || h2 == nil {
return false
}
return reflect.DeepEqual(h, h2)
}
// BasicallyEqual reports whether h and h2 are equal other than the
// NetInfo DERP latency timing. (see NetInfo.BasicallyEqual).
func (h *Hostinfo) BasicallyEqual(h2 *Hostinfo) bool {
if h == nil && h2 == nil {
return true
}
if h == nil || h2 == nil {
return false
}
a := *h
b := *h2
if !a.NetInfo.BasicallyEqual(b.NetInfo) {
return false
}
a.NetInfo = nil
b.NetInfo = nil
return a.Equal(&b)
}
// SignatureType specifies a scheme for signing RegisterRequest messages. It
// specifies the crypto algorithms to use, the contents of what is signed, and
// any other relevant details. Historically, requests were unsigned so the zero

View File

@ -190,6 +190,75 @@ func TestHostinfoEqual(t *testing.T) {
}
}
func TestHostinfoBasicallyEqual(t *testing.T) {
tests := []struct {
a, b *Hostinfo
want bool
}{
{
want: true,
},
{
a: new(Hostinfo),
b: new(Hostinfo),
want: true,
},
{
a: &Hostinfo{},
b: &Hostinfo{
NetInfo: &NetInfo{},
},
want: false, // one's nil, the other's not
},
{
a: &Hostinfo{
NetInfo: &NetInfo{},
},
b: &Hostinfo{
NetInfo: &NetInfo{},
},
want: true,
},
{
a: &Hostinfo{
NetInfo: &NetInfo{},
},
b: &Hostinfo{
NetInfo: &NetInfo{
DERPLatency: map[string]float64{ // ignored
"1": 1.0,
"2": 2.0,
},
},
},
want: true,
},
{
a: &Hostinfo{
NetInfo: &NetInfo{
PreferredDERP: 1,
},
},
b: &Hostinfo{
NetInfo: &NetInfo{
PreferredDERP: 2, // differs
DERPLatency: map[string]float64{ // ignored
"1": 1.0,
"2": 2.0,
},
},
},
want: false,
},
}
for i, tt := range tests {
got := tt.a.BasicallyEqual(tt.b)
if got != tt.want {
t.Errorf("%d. BasicallyEqual = %v; want %v", i, got, tt.want)
}
}
}
func TestNodeEqual(t *testing.T) {
nodeHandles := []string{
"ID", "StableID", "Name", "User", "Sharer",