summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@tailscale.com>2021-12-22 12:51:39 -0800
committerBrad Fitzpatrick <bradfitz@tailscale.com>2021-12-22 12:56:27 -0800
commit850a603caa63bde8ea75406c9b665ae527918798 (patch)
tree19591ebd108b401b9b4c350db7399d2a6e999ddc
parent0aa4c6f147e7db9fc7c021a11c708f4ec6c604f6 (diff)
downloadtailscale-bradfitz/hostinfo_basically_equal.tar.xz
tailscale-bradfitz/hostinfo_basically_equal.zip
tailcfg: add Hostinfo.BasicallyEqualbradfitz/hostinfo_basically_equal
Change-Id: I4fd82dea9bb3618f3a93d8b2d2486f3eabbbd915 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
-rw-r--r--tailcfg/tailcfg.go21
-rw-r--r--tailcfg/tailcfg_test.go69
2 files changed, 89 insertions, 1 deletions
diff --git a/tailcfg/tailcfg.go b/tailcfg/tailcfg.go
index 2404b6bb8..e2f5812f6 100644
--- a/tailcfg/tailcfg.go
+++ b/tailcfg/tailcfg.go
@@ -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
diff --git a/tailcfg/tailcfg_test.go b/tailcfg/tailcfg_test.go
index 071a88a90..109aeb965 100644
--- a/tailcfg/tailcfg_test.go
+++ b/tailcfg/tailcfg_test.go
@@ -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",