forked from mdlayher/ndp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_setup_test.go
84 lines (67 loc) · 1.63 KB
/
conn_setup_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
80
81
82
83
84
package ndp
import (
"errors"
"net"
"net/netip"
"os"
"testing"
)
func testICMPConn(t *testing.T) (*Conn, *Conn, netip.Addr) {
t.Helper()
ifi := testInterface(t)
// Create two ICMPv6 connections that will communicate with each other.
c1, addr := icmpConn(t, ifi)
c2, _ := icmpConn(t, ifi)
t.Cleanup(func() {
_ = c1.Close()
_ = c2.Close()
})
return c1, c2, addr
}
func icmpConn(t *testing.T, ifi *net.Interface) (*Conn, netip.Addr) {
t.Helper()
// Wire up a standard ICMPv6 NDP connection.
c, addr, err := Listen(ifi, LinkLocal)
if err != nil {
if !errors.Is(err, os.ErrPermission) {
t.Fatalf("failed to dial NDP: %v", err)
}
t.Skipf("skipping, permission denied, cannot test ICMPv6 NDP: %v", err)
}
c.icmpTest = true
return c, addr
}
func testInterface(t *testing.T) *net.Interface {
t.Helper()
ifis, err := net.Interfaces()
if err != nil {
t.Fatalf("failed to get interfaces: %v", err)
}
for _, ifi := range ifis {
// Is the interface up and not a loopback?
if ifi.Flags&net.FlagUp != 1 || ifi.Flags&net.FlagLoopback != 0 {
continue
}
// Does the interface have an IPv6 address assigned?
addrs, err := ifi.Addrs()
if err != nil {
t.Fatalf("failed to get interface %q addresses: %v", ifi.Name, err)
}
for _, a := range addrs {
ipNet, ok := a.(*net.IPNet)
if !ok {
continue
}
ip, ok := netip.AddrFromSlice(ipNet.IP)
if !ok {
t.Fatalf("failed to parse IPv6 address: %v", ipNet.IP)
}
// Is this address an IPv6 address?
if ip.Is6() && !ip.Is4In6() {
return &ifi
}
}
}
t.Skip("skipping, could not find a usable IPv6-enabled interface")
return nil
}