-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_internal_test.go
68 lines (61 loc) · 1.19 KB
/
client_internal_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
package wgdynamic
import (
"net"
"testing"
)
func Test_newClient(t *testing.T) {
const iface = "eth0"
tests := []struct {
name string
addrs []net.Addr
ok bool
}{
{
name: "no addresses",
},
{
name: "no suitable addresses",
addrs: []net.Addr{
// This is nonsensical, but it verifies that a failed type
// assertion won't crash the program.
&net.TCPAddr{},
// Link-local IPv4 address.
mustIPNet("169.254.0.1/32"),
// Globally routable IPv6 address.
mustIPNet("2001:db8::1/128"),
},
},
{
name: "OK",
addrs: []net.Addr{
// Link-local IPv4 address.
mustIPNet("169.254.0.1/32"),
// Link-local IPv6 address.
mustIPNet("fe80::1/128"),
},
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := newClient(iface, tt.addrs)
if err != nil {
if tt.ok {
t.Fatalf("failed to create client: %v", err)
}
t.Logf("OK error: %v", err)
return
}
if !tt.ok {
t.Fatal("expected an error, but none occurred")
}
})
}
}
func mustIPNet(s string) *net.IPNet {
_, ipn, err := net.ParseCIDR(s)
if err != nil {
panicf("failed to parse CIDR: %v", err)
}
return ipn
}