This repository was archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathip_test.go
More file actions
127 lines (115 loc) · 2.85 KB
/
ip_test.go
File metadata and controls
127 lines (115 loc) · 2.85 KB
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
package ip
import (
"net"
"strings"
"testing"
)
type parseCase struct {
x string
ipv6, net bool
zone string
}
type stringCase struct {
x string
}
var mustParse = []parseCase{
{"4.5.6.7", false, false, ""},
{"1.2.3.4%lo0", false, false, "lo0"},
{"192.168.0.0/16", false, true, ""},
{"192.168.0.0/16%eth0", false, true, "eth0"},
{"::1", true, false, ""},
{"::1%eth0", true, false, "eth0"},
{"2001:DB8::/48", true, true, ""},
{"2001:DB8::/48%eth0", true, true, "eth0"},
}
var stringCases = []stringCase{
{"4.5.6.7"},
{"1.2.3.4%lo0"},
{"192.168.0.0/16"},
{"192.168.0.0/16%eth0"},
{"::1"},
{"::1%eth0"},
{"2001:DB8::/48"},
{"2001:DB8::/48%eth0"},
}
var mustNotParse = []string{
"",
"a1.2.3.4%lo0",
"%192.168.0.0/16",
"192.168.0",
"192.168.0.0.0/16%eth0",
"%::1%",
"::1z%eth0",
"2001:DB8::/48%%",
"2001:DB8::/48%eth0%",
}
func TestParse(t *testing.T) {
for _, validCase := range mustParse {
ipn, err := Parse(validCase.x)
if err != nil {
t.Errorf("Parse(\"%s\") failed (should parse)", validCase.x)
}
if validCase.ipv6 {
if !ipn.IsIPv6() {
t.Errorf("Parse(\"%s\") failed (should be ipv6)", validCase.x)
}
} else {
if !ipn.IsIPv4() {
t.Errorf("Parse(\"%s\") failed (should be ipv4)", validCase.x)
}
}
if validCase.net {
if !ipn.IsNetwork() {
t.Errorf("Parse(\"%s\") failed (should be network addr)", validCase.x)
}
} else {
if ipn.IsNetwork() {
t.Errorf("Parse(\"%s\") failed (should not be network addr)", validCase.x)
}
}
if !ipn.EqualZone(validCase.zone) {
t.Errorf("Parse(\"%s\") failed (bad zone %s != %s) %s", validCase.x, ipn.Zone, validCase.zone)
}
}
for _, invalid := range mustNotParse {
if _, err := Parse(invalid); err == nil {
t.Errorf("Parse(\"%s\") failed (should not parse)", invalid)
}
}
}
func TestContains(t *testing.T) {
x, _ := Parse("80.0.0.0/8")
if !x.Contains(net.ParseIP("80.1.2.3")) {
t.Errorf("Contains() fails")
}
y, _ := Parse("80.1.2.3")
if !y.Contains(net.ParseIP("80.1.2.3")) {
t.Errorf("Contains() fails 2")
}
}
func TestInterfaces(t *testing.T) {
x, _ := Parse("80.0.0.0/8")
ifaces, err := x.Interfaces()
if err != nil {
t.Errorf("Interfaces() failed, err=", err)
}
for _, iface := range ifaces {
t.Logf("Interface: iface=", iface)
}
}
func TestNetwork(t *testing.T) {
x, _ := Parse("80.0.0.0/8")
if x.Network() != "ip+net+zone" {
t.Errorf("Network() failed")
}
}
func TestString(t *testing.T) {
for _, stringCase := range stringCases {
j, _ := Parse(stringCase.x)
if s := j.String(); strings.ToUpper(s) != strings.ToUpper(stringCase.x) {
t.Errorf("String() failed (%s != %s)", s, stringCase.x)
} else {
t.Logf("String() success (%s == %s)", s, stringCase.x)
}
}
}