-
Notifications
You must be signed in to change notification settings - Fork 20
/
tailscale_test.go
79 lines (61 loc) · 1.56 KB
/
tailscale_test.go
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
package main
import (
"testing"
"time"
"github.com/tailscale/libtailscale/tsnetctest"
)
func TestConn(t *testing.T) {
tsnetctest.RunTestConn(t)
// RunTestConn cleans up after itself, so there shouldn't be
// anything left in the global maps.
servers.mu.Lock()
rem := len(servers.m)
servers.mu.Unlock()
if rem > 0 {
t.Fatalf("want no remaining tsnet objects, got %d", rem)
}
var remConns, remLns int
for i := 0; i < 50; i++ {
conns.mu.Lock()
remConns = len(conns.m)
conns.mu.Unlock()
listeners.mu.Lock()
remLns = len(listeners.m)
listeners.mu.Unlock()
if remConns == 0 && remLns == 0 {
break
}
// We are waiting for cleanup goroutines to finish.
//
// libtailscale closes one side of a socketpair and
// then Go responds to the other side being unreadable
// by closing the connections and listeners.
//
// This is inherently asynchronous.
// Without ditching the standard close(2) and having our
// own close functions.
//
// So we spin for a while
time.Sleep(100 * time.Millisecond)
}
if remConns > 0 {
t.Errorf("want no remaining tsnet_conn objects, got %d", remConns)
}
if remLns > 0 {
t.Errorf("want no remaining tsnet_listener objects, got %d", remLns)
}
}
func TestExtractIP(t *testing.T) {
ipv4 := "1.23.33.4:12343"
ipv6 := "[1::2234::34fc::44]:56576"
got4 := extractIP(ipv4)
got6 := extractIP(ipv6)
want4 := "1.23.33.4"
want6 := "[1::2234::34fc::44]"
if got4 != want4 {
t.Errorf("ipv4 port stripping failed")
}
if got6 != want6 {
t.Errorf("ipv6 port stripping failed %s != %s", got6, want6)
}
}