-
Notifications
You must be signed in to change notification settings - Fork 80
/
parse_test.go
155 lines (140 loc) · 4.1 KB
/
parse_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"net"
"testing"
)
const maxCount = 1000000
func Test_expandCIDR(t *testing.T) {
type args struct {
CIDR string
}
tests := []struct {
name string
args args
wantErr bool
}{
{"IPv4_32", args{CIDR: `192.168.1.1/32`}, false},
{"IPv4_30", args{CIDR: `192.168.1.13/30`}, false},
{"IPv4_16", args{CIDR: `192.168.1.17/16`}, false},
{"IPv4_12", args{CIDR: `192.15.1.17/12`}, false},
{"IPv4_0", args{CIDR: `192.15.1.17/1`}, false},
{"IPv6_128", args{CIDR: `ff:2:04::/128`}, false},
{"IPv6_115", args{CIDR: `0:f:2::14/115`}, false},
{"IPv6_64", args{CIDR: `0:f:2:4::/64`}, false},
{"too wide mask", args{CIDR: `0:f:2:4::/63`}, true},
{"too wide mask", args{CIDR: `0:f:2:4::/0`}, true},
{"invalid CIDR", args{CIDR: `0:f:2:4:/63`}, true},
{"invalid CIDR", args{CIDR: `[0:f:2:4::]/63`}, true},
{"invalid CIDR", args{CIDR: `127.0.0.1/63`}, true},
{"invalid CIDR", args{CIDR: `127..1/0`}, true},
{"not CIDR", args{CIDR: `::1`}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := expandCIDR(tt.args.CIDR)
if err != nil {
if !tt.wantErr {
t.Errorf("expandCIDR() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
// test number of IPs corresponds to mask CIDR size
// and all of those belong to the CIDR
var (
count int
ipnet *net.IPNet
)
for ip := range got {
count++
if count < 3 {
t.Log(ip)
}
_, ipnet, _ = net.ParseCIDR(tt.args.CIDR)
if !ipnet.Contains(net.ParseIP(ip)) {
t.Errorf("%s doesn't belong to CIDR", ip)
return
}
if count == maxCount {
break
}
}
// skip count check if exceeded test limit
if count == maxCount {
return
}
// check number of IPs is correct
mBits, mSize := ipnet.Mask.Size()
IPCount := 1 << (mSize - mBits)
if IPCount != count {
t.Errorf("Number of IPs is not right")
}
t.Logf("range fully tested")
})
}
}
func Test_splitHostPort(t *testing.T) {
type args struct {
addr string
}
tests := []struct {
name string
args args
wantHost string
wantPort string
}{
{`Initial input`, args{addr: ``}, ``, ``},
{`Portless IPv4`, args{addr: `1.1.1.1`}, `1.1.1.1`, ``},
{`Portfull IPv4`, args{addr: `1.1.1.1:443`}, `1.1.1.1`, `443`},
{`Portless IPv4 CIDR`, args{addr: `1.1.1.1/32`}, `1.1.1.1/32`, ``},
{`Portfull IPv4 CIDR`, args{addr: `1.1.1.1/32:443`}, `1.1.1.1/32`, `443`},
{`Portless IPv6`, args{addr: `::1`}, `::1`, ``},
{`Ambiguous port IPv6`, args{addr: `::1:443`}, `::1:443`, ``},
{`Bracket IPv6 with port`, args{addr: `[::1]:443`}, `::1`, `443`},
{`Wrong bracket port IPv6`, args{addr: `::1]:443`}, `::1]`, `443`},
{`Unambiguous port IPv6`, args{addr: `::1:44300`}, `::1`, `44300`},
{`Unambiguous port IPv6`, args{addr: `::1:44300`}, `::1`, `44300`},
{`Unambiguous port IPv6`, args{addr: `1:1:1:1:1:1:1:1:80`}, `1:1:1:1:1:1:1:1`, `80`},
{`ambiguous port IPv6`, args{addr: `1:1:1:1:1:1:1:80`}, `1:1:1:1:1:1:1:80`, ``},
{`Portless IPv6 CIDR`, args{addr: `::1/64`}, `::1/64`, ``},
{`Portfull IPv6 CIDR`, args{addr: `::1/64:443`}, `::1/64`, `443`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHost, gotPort := splitHostPort(tt.args.addr)
if gotHost != tt.wantHost {
t.Errorf("splitHostPort() gotHost = %v, want %v", gotHost, tt.wantHost)
}
if gotPort != tt.wantPort {
t.Errorf("splitHostPort() gotPort = %v, want %v", gotPort, tt.wantPort)
}
})
}
}
func Test_isDomainName(t *testing.T) {
cases := []struct {
host string
expected bool
}{
// -- valid
{"test.com.ru", true},
{"test-1.com", true},
{"1.1.1.com", true},
{"test.com.", true}, // yes trailing dot is allowed by RFC
// -- invalid
{"127.0.0.1", false},
{"test", false}, // single level
{"test.", false}, // single level
{"test_test.com", false},
{".test", false},
{"*.test.com", false},
{"test-.com", false},
{"te!st.com", false},
{"!.dot.com", false},
}
for _, c := range cases {
actual := isDomainName(c.host)
if actual != c.expected {
t.Errorf("isDomainName(%s) expected to be %v", c.host, c.expected)
}
}
}