-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipvx_test.go
121 lines (109 loc) · 3.3 KB
/
ipvx_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
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
package ipvx_test
import (
"fmt"
"net"
"testing"
"github.com/dnesting/ipvx"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
func TestIs4(t *testing.T) {
// This test goes a bit above and beyond. Rather than creating test *net.IPAddr and similar
// instances, we actually double-check that all of our assumptions about how the network stack
// resolves addresses are true.
type testcase struct {
addr string
network string
is4 bool
}
cases := []testcase{
// This test only parses addresses, and so while these test cases are useful to check for
// real-world behavior, we don't attempt to actually bounce them off of a sockets
// implementation to test them. And even if we did, some of these would be system-dependent.
// {"", "ip", false},
// {"", "ip4", true},
// {"", "ip6", true},
// {":123", "udp", false},
// {":", "udp4", true},
// {":", "udp6", true},
// {":123", "tcp", false},
// {":", "tcp4", true},
// {":", "tcp6", true},
// These should all be IPv4
{"0.0.0.0", "ip", true},
{"0.0.0.0", "ip4", true},
{"0.0.0.0/0", "ip+net", true},
{"0.0.0.0:0", "udp", true},
{"0.0.0.0:0", "udp4", true},
{"0.0.0.0:0", "tcp", true},
{"0.0.0.0:0", "tcp4", true},
{"127.0.0.1", "ip", true},
{"127.0.0.1", "ip4", true},
{"127.0.0.1/8", "ip+net", true},
{"127.0.0.1:0", "udp", true},
{"127.0.0.1:0", "udp4", true},
{"127.0.0.1:0", "tcp", true},
{"127.0.0.1:0", "tcp4", true},
{"::ffff:127.0.0.1", "ip", true},
{"::ffff:127.0.0.1", "ip4", true},
{"::ffff:127.0.0.1/102", "ip+net", true}, // NB: IPv6 length; a /8 would yield ::/8 which is considered IPv6.
{"[::ffff:127.0.0.1]:123", "udp", true},
{"[::ffff:127.0.0.1]:123", "udp4", true},
{"[::ffff:127.0.0.1]:123", "tcp", true},
{"[::ffff:127.0.0.1]:123", "tcp4", true},
// These should all be IPv6
{"::", "ip", false},
{"::", "ip6", false},
{"::%en0", "ip6", false},
{"::/64", "ip+net", false},
{"[::]:0", "udp", false},
{"[::]:0", "udp6", false},
{"[::]:0", "tcp", false},
{"[::]:0", "tcp6", false},
{"::1", "ip", false},
{"::1", "ip6", false},
{"::1%en0", "ip6", false},
{"::1/8", "ip+net", false},
{"[::1]:0", "udp", false},
{"[::1]:0", "udp6", false},
{"[::1]:0", "tcp", false},
{"[::1]:0", "tcp6", false},
}
for _, c := range cases {
var addr net.Addr
var err error
switch c.network {
case "ip", "ip4", "ip6":
addr, err = net.ResolveIPAddr(c.network, c.addr)
case "udp", "udp4", "udp6":
addr, err = net.ResolveUDPAddr(c.network, c.addr)
case "tcp", "tcp4", "tcp6":
addr, err = net.ResolveTCPAddr(c.network, c.addr)
case "ip+net":
_, addr, err = net.ParseCIDR(c.addr)
default:
panic(fmt.Sprintf("unexpected network type %q in test", c.network))
}
if err != nil {
t.Errorf("case %q %q: got unexpected error %s", c.network, c.addr, err)
continue
}
is4 := ipvx.Is4(addr)
if is4 != c.is4 {
t.Errorf("case %q %q: expected is4=%v, got %v (ip=%v)", c.network, c.addr, c.is4, is4, ipvx.GetIP(addr))
}
}
}
func Example() {
var conn net.Conn
// Where you might have done something like this in the past:
if ipaddr, ok := conn.LocalAddr().(*net.IPAddr); ok {
if ipaddr.IP.To4() == nil {
ipv6.NewConn(conn).SetHopLimit(2)
} else {
ipv4.NewConn(conn).SetTTL(2)
}
}
// Now you can just do:
ipvx.NewConn(conn).SetHopLimit(2)
}