summaryrefslogtreecommitdiffhomepage
path: root/wgengine/netstack/netstack_test.go
blob: 9b4792190dc3d2304ddfb6909dee7714ab4d49a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2021 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 netstack

import (
	"reflect"
	"testing"

	"inet.af/netaddr"
	"tailscale.com/tailcfg"
	"tailscale.com/types/netmap"
)

func TestDNSMapFromNetworkMap(t *testing.T) {
	pfx := netaddr.MustParseIPPrefix
	ip := netaddr.MustParseIP
	tests := []struct {
		name string
		nm   *netmap.NetworkMap
		want DNSMap
	}{
		{
			name: "self",
			nm: &netmap.NetworkMap{
				Name: "foo.tailnet",
				Addresses: []netaddr.IPPrefix{
					pfx("100.102.103.104/32"),
					pfx("100::123/128"),
				},
			},
			want: DNSMap{
				"foo":         ip("100.102.103.104"),
				"foo.tailnet": ip("100.102.103.104"),
			},
		},
		{
			name: "self_and_peers",
			nm: &netmap.NetworkMap{
				Name: "foo.tailnet",
				Addresses: []netaddr.IPPrefix{
					pfx("100.102.103.104/32"),
					pfx("100::123/128"),
				},
				Peers: []*tailcfg.Node{
					{
						Name: "a.tailnet",
						Addresses: []netaddr.IPPrefix{
							pfx("100.0.0.201/32"),
							pfx("100::201/128"),
						},
					},
					{
						Name: "b.tailnet",
						Addresses: []netaddr.IPPrefix{
							pfx("100::202/128"),
						},
					},
				},
			},
			want: DNSMap{
				"foo":         ip("100.102.103.104"),
				"foo.tailnet": ip("100.102.103.104"),
				"a":           ip("100.0.0.201"),
				"a.tailnet":   ip("100.0.0.201"),
				"b":           ip("100::202"),
				"b.tailnet":   ip("100::202"),
			},
		},
		{
			name: "self_has_v6_only",
			nm: &netmap.NetworkMap{
				Name: "foo.tailnet",
				Addresses: []netaddr.IPPrefix{
					pfx("100::123/128"),
				},
				Peers: []*tailcfg.Node{
					{
						Name: "a.tailnet",
						Addresses: []netaddr.IPPrefix{
							pfx("100.0.0.201/32"),
							pfx("100::201/128"),
						},
					},
					{
						Name: "b.tailnet",
						Addresses: []netaddr.IPPrefix{
							pfx("100::202/128"),
						},
					},
				},
			},
			want: DNSMap{
				"foo":         ip("100::123"),
				"foo.tailnet": ip("100::123"),
				"a":           ip("100::201"),
				"a.tailnet":   ip("100::201"),
				"b":           ip("100::202"),
				"b.tailnet":   ip("100::202"),
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := DNSMapFromNetworkMap(tt.nm)
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("mismatch:\n got %v\nwant %v\n", got, tt.want)
			}
		})
	}
}