summaryrefslogtreecommitdiffhomepage
path: root/cmd/natc/ippool/consensusippool_test.go
blob: 242cdffaf26d3b45c6931e7d85bbceb7217b01ba (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

package ippool

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/netip"
	"testing"
	"time"

	"github.com/hashicorp/raft"
	"go4.org/netipx"
	"tailscale.com/tailcfg"
	"tailscale.com/tsconsensus"
	"tailscale.com/util/must"
)

func makeSetFromPrefix(pfx netip.Prefix) *netipx.IPSet {
	var ipsb netipx.IPSetBuilder
	ipsb.AddPrefix(pfx)
	return must.Get(ipsb.IPSet())
}

type FakeConsensus struct {
	ipp *ConsensusIPPool
}

func (c *FakeConsensus) ExecuteCommand(cmd tsconsensus.Command) (tsconsensus.CommandResult, error) {
	b, err := json.Marshal(cmd)
	if err != nil {
		return tsconsensus.CommandResult{}, err
	}
	result := c.ipp.Apply(&raft.Log{Data: b})
	return result.(tsconsensus.CommandResult), nil
}

func makePool(pfx netip.Prefix) *ConsensusIPPool {
	ipp := NewConsensusIPPool(makeSetFromPrefix(pfx))
	ipp.consensus = &FakeConsensus{ipp: ipp}
	return ipp
}

func TestConsensusIPForDomain(t *testing.T) {
	pfx := netip.MustParsePrefix("100.64.0.0/16")
	ipp := makePool(pfx)
	from := tailcfg.NodeID(1)

	a, err := ipp.IPForDomain(from, "example.com")
	if err != nil {
		t.Fatal(err)
	}
	if !pfx.Contains(a) {
		t.Fatalf("expected %v to be in the prefix %v", a, pfx)
	}

	b, err := ipp.IPForDomain(from, "a.example.com")
	if err != nil {
		t.Fatal(err)
	}
	if !pfx.Contains(b) {
		t.Fatalf("expected %v to be in the prefix %v", b, pfx)
	}
	if b == a {
		t.Fatalf("same address issued twice %v, %v", a, b)
	}

	c, err := ipp.IPForDomain(from, "example.com")
	if err != nil {
		t.Fatal(err)
	}
	if c != a {
		t.Fatalf("expected %v to be remembered as the addr for example.com, but got %v", a, c)
	}
}

func TestConsensusPoolExhaustion(t *testing.T) {
	ipp := makePool(netip.MustParsePrefix("100.64.0.0/31"))
	from := tailcfg.NodeID(1)

	subdomains := []string{"a", "b", "c"}
	for i, sd := range subdomains {
		_, err := ipp.IPForDomain(from, fmt.Sprintf("%s.example.com", sd))
		if i < 2 && err != nil {
			t.Fatal(err)
		}
		expected := "ip pool exhausted"
		if i == 2 && err.Error() != expected {
			t.Fatalf("expected error to be '%s', got '%s'", expected, err.Error())
		}
	}
}

func TestConsensusPoolExpiry(t *testing.T) {
	ipp := makePool(netip.MustParsePrefix("100.64.0.0/31"))
	firstIP := netip.MustParseAddr("100.64.0.0")
	secondIP := netip.MustParseAddr("100.64.0.1")
	timeOfUse := time.Now()
	beforeTimeOfUse := timeOfUse.Add(-1 * time.Hour)
	afterTimeOfUse := timeOfUse.Add(1 * time.Hour)
	from := tailcfg.NodeID(1)

	// the pool is unused, we get an address, and it's marked as being used at timeOfUse
	aAddr, err := ipp.applyCheckoutAddr(from, "a.example.com", time.Time{}, timeOfUse)
	if err != nil {
		t.Fatal(err)
	}
	if aAddr.Compare(firstIP) != 0 {
		t.Fatalf("expected %s, got %s", firstIP, aAddr)
	}
	ww, ok := ipp.domainLookup(from, firstIP)
	if !ok {
		t.Fatal("expected wherewhen to be found")
	}
	if ww.Domain != "a.example.com" {
		t.Fatalf("expected aAddr to look up to a.example.com, got: %s", ww.Domain)
	}

	// the time before which we will reuse addresses is prior to timeOfUse, so no reuse
	bAddr, err := ipp.applyCheckoutAddr(from, "b.example.com", beforeTimeOfUse, timeOfUse)
	if err != nil {
		t.Fatal(err)
	}
	if bAddr.Compare(secondIP) != 0 {
		t.Fatalf("expected %s, got %s", secondIP, bAddr)
	}

	// the time before which we will reuse addresses is after timeOfUse, so reuse addresses that were marked as used at timeOfUse.
	cAddr, err := ipp.applyCheckoutAddr(from, "c.example.com", afterTimeOfUse, timeOfUse)
	if err != nil {
		t.Fatal(err)
	}
	if cAddr.Compare(firstIP) != 0 {
		t.Fatalf("expected %s, got %s", firstIP, cAddr)
	}
	ww, ok = ipp.domainLookup(from, firstIP)
	if !ok {
		t.Fatal("expected wherewhen to be found")
	}
	if ww.Domain != "c.example.com" {
		t.Fatalf("expected firstIP to look up to c.example.com, got: %s", ww.Domain)
	}

	// the addr remains associated with c.example.com
	cAddrAgain, err := ipp.applyCheckoutAddr(from, "c.example.com", afterTimeOfUse, timeOfUse)
	if err != nil {
		t.Fatal(err)
	}
	if cAddrAgain.Compare(cAddr) != 0 {
		t.Fatalf("expected cAddrAgain to be cAddr, but they are different. cAddrAgain=%s cAddr=%s", cAddrAgain, cAddr)
	}
	ww, ok = ipp.domainLookup(from, firstIP)
	if !ok {
		t.Fatal("expected wherewhen to be found")
	}
	if ww.Domain != "c.example.com" {
		t.Fatalf("expected firstIP to look up to c.example.com, got: %s", ww.Domain)
	}
}

func TestConsensusPoolApplyMarkLastUsed(t *testing.T) {
	ipp := makePool(netip.MustParsePrefix("100.64.0.0/31"))
	firstIP := netip.MustParseAddr("100.64.0.0")
	time1 := time.Now()
	time2 := time1.Add(1 * time.Hour)
	from := tailcfg.NodeID(1)
	domain := "example.com"

	aAddr, err := ipp.applyCheckoutAddr(from, domain, time.Time{}, time1)
	if err != nil {
		t.Fatal(err)
	}
	if aAddr.Compare(firstIP) != 0 {
		t.Fatalf("expected %s, got %s", firstIP, aAddr)
	}
	// example.com LastUsed is now time1
	ww, ok := ipp.domainLookup(from, firstIP)
	if !ok {
		t.Fatal("expected wherewhen to be found")
	}
	if ww.LastUsed != time1 {
		t.Fatalf("expected %s, got %s", time1, ww.LastUsed)
	}
	if ww.Domain != domain {
		t.Fatalf("expected %s, got %s", domain, ww.Domain)
	}

	err = ipp.applyMarkLastUsed(from, firstIP, domain, time2)
	if err != nil {
		t.Fatal(err)
	}

	// example.com LastUsed is now time2
	ww, ok = ipp.domainLookup(from, firstIP)
	if !ok {
		t.Fatal("expected wherewhen to be found")
	}
	if ww.LastUsed != time2 {
		t.Fatalf("expected %s, got %s", time2, ww.LastUsed)
	}
	if ww.Domain != domain {
		t.Fatalf("expected %s, got %s", domain, ww.Domain)
	}
}

func TestConsensusDomainForIP(t *testing.T) {
	ipp := makePool(netip.MustParsePrefix("100.64.0.0/16"))
	from := tailcfg.NodeID(1)
	domain := "example.com"
	now := time.Now()

	d, ok := ipp.DomainForIP(from, netip.MustParseAddr("100.64.0.1"), now)
	if d != "" {
		t.Fatalf("expected an empty string if the addr is not found but got %s", d)
	}
	if ok {
		t.Fatalf("expected domain to not be found for IP, as it has never been looked up")
	}
	a, err := ipp.IPForDomain(from, domain)
	if err != nil {
		t.Fatal(err)
	}
	d2, ok := ipp.DomainForIP(from, a, now)
	if d2 != domain {
		t.Fatalf("expected %s but got %s", domain, d2)
	}
	if !ok {
		t.Fatalf("expected domain to be found for IP that was handed out for it")
	}
}

func TestConsensusReadDomainForIP(t *testing.T) {
	ipp := makePool(netip.MustParsePrefix("100.64.0.0/16"))
	from := tailcfg.NodeID(1)
	domain := "example.com"

	d, err := ipp.readDomainForIP(from, netip.MustParseAddr("100.64.0.1"))
	if err != nil {
		t.Fatal(err)
	}
	if d != "" {
		t.Fatalf("expected an empty string if the addr is not found but got %s", d)
	}
	a, err := ipp.IPForDomain(from, domain)
	if err != nil {
		t.Fatal(err)
	}
	d2, err := ipp.readDomainForIP(from, a)
	if err != nil {
		t.Fatal(err)
	}
	if d2 != domain {
		t.Fatalf("expected %s but got %s", domain, d2)
	}
}

func TestConsensusSnapshot(t *testing.T) {
	pfx := netip.MustParsePrefix("100.64.0.0/16")
	ipp := makePool(pfx)
	domain := "example.com"
	expectedAddr := netip.MustParseAddr("100.64.0.0")
	expectedFrom := expectedAddr
	expectedTo := netip.MustParseAddr("100.64.255.255")
	from := tailcfg.NodeID(1)

	// pool allocates first addr for from
	if _, err := ipp.IPForDomain(from, domain); err != nil {
		t.Fatal(err)
	}
	// take a snapshot
	fsmSnap, err := ipp.Snapshot()
	if err != nil {
		t.Fatal(err)
	}
	snap := fsmSnap.(fsmSnapshot)

	// verify snapshot state matches the state we know ipp will have
	// ipset matches ipp.IPSet
	if len(snap.IPSet.Ranges) != 1 {
		t.Fatalf("expected 1, got %d", len(snap.IPSet.Ranges))
	}
	if snap.IPSet.Ranges[0].From != expectedFrom {
		t.Fatalf("want %s, got %s", expectedFrom, snap.IPSet.Ranges[0].From)
	}
	if snap.IPSet.Ranges[0].To != expectedTo {
		t.Fatalf("want %s, got %s", expectedTo, snap.IPSet.Ranges[0].To)
	}

	// perPeerMap has one entry, for from
	if len(snap.PerPeerMap) != 1 {
		t.Fatalf("expected 1, got %d", len(snap.PerPeerMap))
	}
	ps := snap.PerPeerMap[from]

	// the one peer state has allocated one address, the first in the prefix
	if len(ps.DomainToAddr) != 1 {
		t.Fatalf("expected 1, got %d", len(ps.DomainToAddr))
	}
	addr := ps.DomainToAddr[domain]
	if addr != expectedAddr {
		t.Fatalf("want %s, got %s", expectedAddr.String(), addr.String())
	}
	if len(ps.AddrToDomain) != 1 {
		t.Fatalf("expected 1, got %d", len(ps.AddrToDomain))
	}
	ww := ps.AddrToDomain[addr]
	if ww.Domain != domain {
		t.Fatalf("want %s, got %s", domain, ww.Domain)
	}
}

func TestConsensusRestore(t *testing.T) {
	pfx := netip.MustParsePrefix("100.64.0.0/16")
	ipp := makePool(pfx)
	domain := "example.com"
	expectedAddr := netip.MustParseAddr("100.64.0.0")
	from := tailcfg.NodeID(1)

	if _, err := ipp.IPForDomain(from, domain); err != nil {
		t.Fatal(err)
	}
	// take the snapshot after only 1 addr allocated
	fsmSnap, err := ipp.Snapshot()
	if err != nil {
		t.Fatal(err)
	}
	snap := fsmSnap.(fsmSnapshot)

	if _, err := ipp.IPForDomain(from, "b.example.com"); err != nil {
		t.Fatal(err)
	}
	if _, err := ipp.IPForDomain(from, "c.example.com"); err != nil {
		t.Fatal(err)
	}
	if _, err := ipp.IPForDomain(from, "d.example.com"); err != nil {
		t.Fatal(err)
	}
	// ipp now has 4 entries in domainToAddr
	ps, _ := ipp.perPeerMap.Load(from)
	if len(ps.domainToAddr) != 4 {
		t.Fatalf("want 4, got %d", len(ps.domainToAddr))
	}

	// restore the snapshot
	bs, err := json.Marshal(snap)
	if err != nil {
		t.Fatal(err)
	}
	err = ipp.Restore(io.NopCloser(bytes.NewBuffer(bs)))
	if err != nil {
		t.Fatal(err)
	}

	// everything should be as it was when the snapshot was taken
	if ipp.perPeerMap.Len() != 1 {
		t.Fatalf("want 1, got %d", ipp.perPeerMap.Len())
	}
	psAfter, _ := ipp.perPeerMap.Load(from)
	if len(psAfter.domainToAddr) != 1 {
		t.Fatalf("want 1, got %d", len(psAfter.domainToAddr))
	}
	if psAfter.domainToAddr[domain] != expectedAddr {
		t.Fatalf("want %s, got %s", expectedAddr, psAfter.domainToAddr[domain])
	}
	ww, _ := psAfter.addrToDomain.Load(expectedAddr)
	if ww.Domain != domain {
		t.Fatalf("want %s, got %s", domain, ww.Domain)
	}
}

func TestConsensusIsCloseToExpiry(t *testing.T) {
	a := time.Now()
	b := a.Add(5 * time.Second)
	if !isCloseToExpiry(a, b, 8*time.Second) {
		t.Fatal("times are not within half the lifetime, expected true")
	}
	if isCloseToExpiry(a, b, 12*time.Second) {
		t.Fatal("times are within half the lifetime, expected false")
	}
}