2020-02-16 06:23:58 +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 tailcfg
|
|
|
|
|
|
|
|
import (
|
2020-06-19 02:32:55 +00:00
|
|
|
"encoding"
|
2021-04-01 21:44:40 +00:00
|
|
|
"encoding/json"
|
2020-02-16 06:23:58 +00:00
|
|
|
"reflect"
|
2020-06-19 02:32:55 +00:00
|
|
|
"strings"
|
2020-02-16 06:23:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-12-24 20:33:55 +00:00
|
|
|
"inet.af/netaddr"
|
2021-10-27 23:21:44 +00:00
|
|
|
"tailscale.com/tstest"
|
2021-09-03 20:17:46 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-05-24 21:54:47 +00:00
|
|
|
"tailscale.com/version"
|
2020-02-16 06:23:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func fieldsOf(t reflect.Type) (fields []string) {
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
fields = append(fields, t.Field(i).Name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-18 03:33:01 +00:00
|
|
|
func TestHostinfoEqual(t *testing.T) {
|
2020-02-25 22:05:17 +00:00
|
|
|
hiHandles := []string{
|
2020-12-01 02:05:51 +00:00
|
|
|
"IPNVersion", "FrontendLogID", "BackendLogID",
|
2021-02-15 20:58:56 +00:00
|
|
|
"OS", "OSVersion", "Package", "DeviceModel", "Hostname",
|
2020-12-01 02:05:51 +00:00
|
|
|
"ShieldsUp", "ShareeNode",
|
|
|
|
"GoArch",
|
|
|
|
"RoutableIPs", "RequestTags",
|
|
|
|
"Services", "NetInfo",
|
2020-02-25 22:05:17 +00:00
|
|
|
}
|
2020-02-18 03:33:01 +00:00
|
|
|
if have := fieldsOf(reflect.TypeOf(Hostinfo{})); !reflect.DeepEqual(have, hiHandles) {
|
|
|
|
t.Errorf("Hostinfo.Equal check might be out of sync\nfields: %q\nhandled: %q\n",
|
|
|
|
have, hiHandles)
|
|
|
|
}
|
|
|
|
|
2020-12-24 20:33:55 +00:00
|
|
|
nets := func(strs ...string) (ns []netaddr.IPPrefix) {
|
2020-02-18 03:33:01 +00:00
|
|
|
for _, s := range strs {
|
2020-12-24 20:33:55 +00:00
|
|
|
n, err := netaddr.ParseIPPrefix(s)
|
2020-02-18 03:33:01 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-17 03:27:00 +00:00
|
|
|
ns = append(ns, n)
|
2020-02-18 03:33:01 +00:00
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
a, b *Hostinfo
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{},
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
&Hostinfo{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{},
|
|
|
|
&Hostinfo{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Hostinfo{IPNVersion: "1"},
|
|
|
|
&Hostinfo{IPNVersion: "2"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{IPNVersion: "2"},
|
|
|
|
&Hostinfo{IPNVersion: "2"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Hostinfo{FrontendLogID: "1"},
|
|
|
|
&Hostinfo{FrontendLogID: "2"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{FrontendLogID: "2"},
|
|
|
|
&Hostinfo{FrontendLogID: "2"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Hostinfo{BackendLogID: "1"},
|
|
|
|
&Hostinfo{BackendLogID: "2"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{BackendLogID: "2"},
|
|
|
|
&Hostinfo{BackendLogID: "2"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Hostinfo{OS: "windows"},
|
|
|
|
&Hostinfo{OS: "linux"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{OS: "windows"},
|
|
|
|
&Hostinfo{OS: "windows"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Hostinfo{Hostname: "vega"},
|
|
|
|
&Hostinfo{Hostname: "iris"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{Hostname: "vega"},
|
|
|
|
&Hostinfo{Hostname: "vega"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
&Hostinfo{RoutableIPs: nil},
|
|
|
|
&Hostinfo{RoutableIPs: nets("10.0.0.0/16")},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{RoutableIPs: nets("10.1.0.0/16", "192.168.1.0/24")},
|
|
|
|
&Hostinfo{RoutableIPs: nets("10.2.0.0/16", "192.168.2.0/24")},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{RoutableIPs: nets("10.1.0.0/16", "192.168.1.0/24")},
|
|
|
|
&Hostinfo{RoutableIPs: nets("10.1.0.0/16", "192.168.2.0/24")},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{RoutableIPs: nets("10.1.0.0/16", "192.168.1.0/24")},
|
|
|
|
&Hostinfo{RoutableIPs: nets("10.1.0.0/16", "192.168.1.0/24")},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2020-05-01 05:01:27 +00:00
|
|
|
{
|
|
|
|
&Hostinfo{RequestTags: []string{"abc", "def"}},
|
|
|
|
&Hostinfo{RequestTags: []string{"abc", "def"}},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{RequestTags: []string{"abc", "def"}},
|
|
|
|
&Hostinfo{RequestTags: []string{"abc", "123"}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Hostinfo{RequestTags: []string{}},
|
|
|
|
&Hostinfo{RequestTags: []string{"abc"}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2020-02-18 03:33:01 +00:00
|
|
|
{
|
2021-04-01 16:54:54 +00:00
|
|
|
&Hostinfo{Services: []Service{{Proto: TCP, Port: 1234, Description: "foo"}}},
|
|
|
|
&Hostinfo{Services: []Service{{Proto: UDP, Port: 2345, Description: "bar"}}},
|
2020-02-18 03:33:01 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-04-01 16:54:54 +00:00
|
|
|
&Hostinfo{Services: []Service{{Proto: TCP, Port: 1234, Description: "foo"}}},
|
|
|
|
&Hostinfo{Services: []Service{{Proto: TCP, Port: 1234, Description: "foo"}}},
|
2020-02-18 03:33:01 +00:00
|
|
|
true,
|
|
|
|
},
|
2020-12-01 02:05:51 +00:00
|
|
|
{
|
|
|
|
&Hostinfo{ShareeNode: true},
|
|
|
|
&Hostinfo{},
|
|
|
|
false,
|
|
|
|
},
|
2020-02-18 03:33:01 +00:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
got := tt.a.Equal(tt.b)
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%d. Equal = %v; want %v", i, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 20:51:39 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 06:23:58 +00:00
|
|
|
func TestNodeEqual(t *testing.T) {
|
2021-01-05 21:52:33 +00:00
|
|
|
nodeHandles := []string{
|
2021-01-27 16:50:31 +00:00
|
|
|
"ID", "StableID", "Name", "User", "Sharer",
|
2021-01-05 21:52:33 +00:00
|
|
|
"Key", "KeyExpiry", "Machine", "DiscoKey",
|
|
|
|
"Addresses", "AllowedIPs", "Endpoints", "DERP", "Hostinfo",
|
2021-10-20 21:26:31 +00:00
|
|
|
"Created", "Tags", "PrimaryRoutes",
|
2021-07-27 15:01:29 +00:00
|
|
|
"LastSeen", "Online", "KeepAlive", "MachineAuthorized",
|
2021-04-01 21:03:34 +00:00
|
|
|
"Capabilities",
|
2021-01-27 16:50:31 +00:00
|
|
|
"ComputedName", "computedHostIfDifferent", "ComputedNameWithHost",
|
2021-01-05 21:52:33 +00:00
|
|
|
}
|
2020-02-16 06:23:58 +00:00
|
|
|
if have := fieldsOf(reflect.TypeOf(Node{})); !reflect.DeepEqual(have, nodeHandles) {
|
|
|
|
t.Errorf("Node.Equal check might be out of sync\nfields: %q\nhandled: %q\n",
|
|
|
|
have, nodeHandles)
|
|
|
|
}
|
|
|
|
|
2021-10-28 17:39:56 +00:00
|
|
|
n1 := key.NewNode().Public()
|
2021-09-03 20:17:46 +00:00
|
|
|
m1 := key.NewMachine().Public()
|
2020-02-16 06:23:58 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
a, b *Node
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&Node{},
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
&Node{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{},
|
|
|
|
&Node{},
|
|
|
|
true,
|
2021-01-21 02:34:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{},
|
|
|
|
&Node{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{ID: 1},
|
|
|
|
&Node{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{ID: 1},
|
|
|
|
&Node{ID: 1},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{StableID: "node-abcd"},
|
|
|
|
&Node{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{StableID: "node-abcd"},
|
|
|
|
&Node{StableID: "node-abcd"},
|
|
|
|
true,
|
2020-02-16 06:23:58 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{User: 0},
|
|
|
|
&Node{User: 1},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{User: 1},
|
|
|
|
&Node{User: 1},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2021-11-02 03:55:52 +00:00
|
|
|
&Node{Key: n1},
|
|
|
|
&Node{Key: key.NewNode().Public()},
|
2020-02-16 06:23:58 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-11-02 03:55:52 +00:00
|
|
|
&Node{Key: n1},
|
|
|
|
&Node{Key: n1},
|
2020-02-16 06:23:58 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{KeyExpiry: now},
|
|
|
|
&Node{KeyExpiry: now.Add(60 * time.Second)},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{KeyExpiry: now},
|
|
|
|
&Node{KeyExpiry: now},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2021-09-03 20:17:46 +00:00
|
|
|
&Node{Machine: m1},
|
|
|
|
&Node{Machine: key.NewMachine().Public()},
|
2020-02-16 06:23:58 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-03 20:17:46 +00:00
|
|
|
&Node{Machine: m1},
|
|
|
|
&Node{Machine: m1},
|
2020-02-16 06:23:58 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2020-12-24 20:33:55 +00:00
|
|
|
&Node{Addresses: []netaddr.IPPrefix{}},
|
2020-02-16 06:23:58 +00:00
|
|
|
&Node{Addresses: nil},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2020-12-24 20:33:55 +00:00
|
|
|
&Node{Addresses: []netaddr.IPPrefix{}},
|
|
|
|
&Node{Addresses: []netaddr.IPPrefix{}},
|
2020-02-16 06:23:58 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2020-12-24 20:33:55 +00:00
|
|
|
&Node{AllowedIPs: []netaddr.IPPrefix{}},
|
2020-02-16 06:23:58 +00:00
|
|
|
&Node{AllowedIPs: nil},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2020-12-24 20:33:55 +00:00
|
|
|
&Node{Addresses: []netaddr.IPPrefix{}},
|
|
|
|
&Node{Addresses: []netaddr.IPPrefix{}},
|
2020-02-16 06:23:58 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Endpoints: []string{}},
|
|
|
|
&Node{Endpoints: nil},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Endpoints: []string{}},
|
|
|
|
&Node{Endpoints: []string{}},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Hostinfo: Hostinfo{Hostname: "alice"}},
|
|
|
|
&Node{Hostinfo: Hostinfo{Hostname: "bob"}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Hostinfo: Hostinfo{}},
|
|
|
|
&Node{Hostinfo: Hostinfo{}},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Created: now},
|
|
|
|
&Node{Created: now.Add(60 * time.Second)},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Created: now},
|
|
|
|
&Node{Created: now},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{LastSeen: &now},
|
|
|
|
&Node{LastSeen: nil},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{LastSeen: &now},
|
|
|
|
&Node{LastSeen: &now},
|
|
|
|
true,
|
|
|
|
},
|
2020-08-11 02:45:20 +00:00
|
|
|
{
|
|
|
|
&Node{DERP: "foo"},
|
|
|
|
&Node{DERP: "bar"},
|
|
|
|
false,
|
|
|
|
},
|
2021-10-20 21:26:31 +00:00
|
|
|
{
|
|
|
|
&Node{Tags: []string{"tag:foo"}},
|
|
|
|
&Node{Tags: []string{"tag:foo"}},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Tags: []string{"tag:foo", "tag:bar"}},
|
|
|
|
&Node{Tags: []string{"tag:bar"}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Tags: []string{"tag:foo"}},
|
|
|
|
&Node{Tags: []string{"tag:bar"}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Node{Tags: []string{"tag:foo"}},
|
|
|
|
&Node{},
|
|
|
|
false,
|
|
|
|
},
|
2020-02-16 06:23:58 +00:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
got := tt.a.Equal(tt.b)
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%d. Equal = %v; want %v", i, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 06:21:56 +00:00
|
|
|
|
|
|
|
func TestNetInfoFields(t *testing.T) {
|
|
|
|
handled := []string{
|
|
|
|
"MappingVariesByDestIP",
|
|
|
|
"HairPinning",
|
|
|
|
"WorkingIPv6",
|
|
|
|
"WorkingUDP",
|
2021-03-09 23:09:10 +00:00
|
|
|
"HavePortMap",
|
2020-07-06 20:51:17 +00:00
|
|
|
"UPnP",
|
|
|
|
"PMP",
|
|
|
|
"PCP",
|
2020-03-04 06:21:56 +00:00
|
|
|
"PreferredDERP",
|
|
|
|
"LinkType",
|
|
|
|
"DERPLatency",
|
|
|
|
}
|
|
|
|
if have := fieldsOf(reflect.TypeOf(NetInfo{})); !reflect.DeepEqual(have, handled) {
|
|
|
|
t.Errorf("NetInfo.Clone/BasicallyEqually check might be out of sync\nfields: %q\nhandled: %q\n",
|
|
|
|
have, handled)
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 02:32:55 +00:00
|
|
|
|
|
|
|
type keyIn interface {
|
|
|
|
String() string
|
|
|
|
MarshalText() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testKey(t *testing.T, prefix string, in keyIn, out encoding.TextUnmarshaler) {
|
|
|
|
got, err := in.MarshalText()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := out.UnmarshalText(got); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if s := in.String(); string(got) != s {
|
|
|
|
t.Errorf("MarshalText = %q != String %q", got, s)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(string(got), prefix) {
|
|
|
|
t.Errorf("%q didn't start with prefix %q", got, prefix)
|
|
|
|
}
|
|
|
|
if reflect.ValueOf(out).Elem().Interface() != in {
|
|
|
|
t.Errorf("mismatch after unmarshal")
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 17:40:34 +00:00
|
|
|
|
|
|
|
func TestCloneUser(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
u *User
|
|
|
|
}{
|
|
|
|
{"nil_logins", &User{}},
|
|
|
|
{"zero_logins", &User{Logins: make([]LoginID, 0)}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
u2 := tt.u.Clone()
|
|
|
|
if !reflect.DeepEqual(tt.u, u2) {
|
|
|
|
t.Errorf("not equal")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloneNode(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
v *Node
|
|
|
|
}{
|
|
|
|
{"nil_fields", &Node{}},
|
|
|
|
{"zero_fields", &Node{
|
2020-12-24 20:33:55 +00:00
|
|
|
Addresses: make([]netaddr.IPPrefix, 0),
|
|
|
|
AllowedIPs: make([]netaddr.IPPrefix, 0),
|
2020-07-27 17:40:34 +00:00
|
|
|
Endpoints: make([]string, 0),
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
v2 := tt.v.Clone()
|
|
|
|
if !reflect.DeepEqual(tt.v, v2) {
|
|
|
|
t.Errorf("not equal")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 21:44:40 +00:00
|
|
|
|
|
|
|
func TestUserProfileJSONMarshalForMac(t *testing.T) {
|
|
|
|
// Old macOS clients had a bug where they required
|
|
|
|
// UserProfile.Roles to be non-null. Lock that in
|
|
|
|
// 1.0.x/1.2.x clients are gone in the wild.
|
|
|
|
// See mac commit 0242c08a2ca496958027db1208f44251bff8488b (Sep 30).
|
|
|
|
// It was fixed in at least 1.4.x, and perhaps 1.2.x.
|
|
|
|
j, err := json.Marshal(UserProfile{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
const wantSub = `"Roles":[]`
|
|
|
|
if !strings.Contains(string(j), wantSub) {
|
|
|
|
t.Fatalf("didn't contain %#q; got: %s", wantSub, j)
|
|
|
|
}
|
|
|
|
|
|
|
|
// And back:
|
|
|
|
var up UserProfile
|
|
|
|
if err := json.Unmarshal(j, &up); err != nil {
|
|
|
|
t.Fatalf("Unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
}
|
tailcfg: add Endpoint, EndpointType, MapRequest.EndpointType
Track endpoints internally with a new tailcfg.Endpoint type that
includes a typed netaddr.IPPort (instead of just a string) and
includes a type for how that endpoint was discovered (STUN, local,
etc).
Use []tailcfg.Endpoint instead of []string internally.
At the last second, send it to the control server as the existing
[]string for endpoints, but also include a new parallel
MapRequest.EndpointType []tailcfg.EndpointType, so the control server
can start filtering out less-important endpoint changes from
new-enough clients. Notably, STUN-discovered endpoints can be filtered
out from 1.6+ clients, as they can discover them amongst each other
via CallMeMaybe disco exchanges started over DERP. And STUN endpoints
change a lot, causing a lot of MapResposne updates. But portmapped
endpoints are worth keeping for now, as they they work right away
without requiring the firewall traversal extra RTT dance.
End result will be less control->client bandwidth. (despite negligible
increase in client->control bandwidth)
Updates tailscale/corp#1543
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-04-12 20:24:29 +00:00
|
|
|
|
|
|
|
func TestEndpointTypeMarshal(t *testing.T) {
|
|
|
|
eps := []EndpointType{
|
|
|
|
EndpointUnknownType,
|
|
|
|
EndpointLocal,
|
|
|
|
EndpointSTUN,
|
|
|
|
EndpointPortmapped,
|
|
|
|
EndpointSTUN4LocalPort,
|
|
|
|
}
|
|
|
|
got, err := json.Marshal(eps)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
const want = `[0,1,2,3,4]`
|
|
|
|
if string(got) != want {
|
|
|
|
t.Errorf("got %s; want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 00:01:44 +00:00
|
|
|
|
|
|
|
var sinkBytes []byte
|
|
|
|
|
|
|
|
func BenchmarkKeyMarshalText(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
var k [32]byte
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
sinkBytes = keyMarshalText("prefix", k)
|
|
|
|
}
|
|
|
|
}
|
2021-05-24 21:54:47 +00:00
|
|
|
|
|
|
|
func TestAppendKeyAllocs(t *testing.T) {
|
|
|
|
if version.IsRace() {
|
|
|
|
t.Skip("skipping in race detector") // append(b, make([]byte, N)...) not optimized in compiler with race
|
|
|
|
}
|
|
|
|
var k [32]byte
|
2021-10-27 23:21:44 +00:00
|
|
|
err := tstest.MinAllocsPerRun(t, 1, func() {
|
2021-05-24 21:54:47 +00:00
|
|
|
sinkBytes = keyMarshalText("prefix", k)
|
2021-10-27 23:21:44 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2021-05-24 21:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:00:04 +00:00
|
|
|
func TestRegisterRequestNilClone(t *testing.T) {
|
|
|
|
var nilReq *RegisterRequest
|
|
|
|
got := nilReq.Clone()
|
|
|
|
if got != nil {
|
|
|
|
t.Errorf("got = %v; want nil", got)
|
|
|
|
}
|
|
|
|
}
|