Skip to content

Commit af8987b

Browse files
committed
提升测试覆盖率
1 parent 377a137 commit af8987b

File tree

1 file changed

+88
-46
lines changed

1 file changed

+88
-46
lines changed

exnet/ip_test.go

+88-46
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,31 @@ import (
2323

2424
func TestHasLocalIPddr(t *testing.T) {
2525
for ipString, expected := range map[string]bool{
26-
"127.0.0.1": true,
27-
"::1": true,
28-
"182.56.9.18": false,
29-
"192.168.9.18": true,
30-
"10.168.9.18": true,
31-
"11.168.9.18": false,
32-
"172.16.9.18": true,
33-
"172.17.9.18": true,
34-
"172.18.9.18": true,
35-
"172.19.9.18": true,
36-
"172.20.9.18": true,
37-
"172.21.9.18": true,
38-
"172.22.9.18": true,
39-
"172.23.9.18": true,
40-
"172.24.9.18": true,
41-
"172.25.9.18": true,
42-
"172.26.9.18": true,
43-
"172.27.9.18": true,
44-
"172.28.9.18": true,
45-
"172.29.9.18": true,
46-
"172.30.9.18": true,
47-
"172.31.9.18": true,
48-
"172.32.9.18": false,
26+
"": false,
27+
"invalid ip address": false,
28+
"127.0.0.1": true,
29+
"::1": true,
30+
"182.56.9.18": false,
31+
"192.168.9.18": true,
32+
"10.168.9.18": true,
33+
"11.168.9.18": false,
34+
"172.16.9.18": true,
35+
"172.17.9.18": true,
36+
"172.18.9.18": true,
37+
"172.19.9.18": true,
38+
"172.20.9.18": true,
39+
"172.21.9.18": true,
40+
"172.22.9.18": true,
41+
"172.23.9.18": true,
42+
"172.24.9.18": true,
43+
"172.25.9.18": true,
44+
"172.26.9.18": true,
45+
"172.27.9.18": true,
46+
"172.28.9.18": true,
47+
"172.29.9.18": true,
48+
"172.30.9.18": true,
49+
"172.31.9.18": true,
50+
"172.32.9.18": false,
4951
} {
5052
if HasLocalIPddr(ipString) != expected {
5153
t.Errorf("ip %s", ipString)
@@ -55,29 +57,31 @@ func TestHasLocalIPddr(t *testing.T) {
5557

5658
func TestHasLocalIP(t *testing.T) {
5759
for ipString, expected := range map[string]bool{
58-
"127.0.0.1": true,
59-
"::1": true,
60-
"182.56.9.18": false,
61-
"192.168.9.18": true,
62-
"10.168.9.18": true,
63-
"11.168.9.18": false,
64-
"172.16.9.18": true,
65-
"172.17.9.18": true,
66-
"172.18.9.18": true,
67-
"172.19.9.18": true,
68-
"172.20.9.18": true,
69-
"172.21.9.18": true,
70-
"172.22.9.18": true,
71-
"172.23.9.18": true,
72-
"172.24.9.18": true,
73-
"172.25.9.18": true,
74-
"172.26.9.18": true,
75-
"172.27.9.18": true,
76-
"172.28.9.18": true,
77-
"172.29.9.18": true,
78-
"172.30.9.18": true,
79-
"172.31.9.18": true,
80-
"172.32.9.18": false,
60+
"": false,
61+
"invalid ip address": false,
62+
"127.0.0.1": true,
63+
"::1": true,
64+
"182.56.9.18": false,
65+
"192.168.9.18": true,
66+
"10.168.9.18": true,
67+
"11.168.9.18": false,
68+
"172.16.9.18": true,
69+
"172.17.9.18": true,
70+
"172.18.9.18": true,
71+
"172.19.9.18": true,
72+
"172.20.9.18": true,
73+
"172.21.9.18": true,
74+
"172.22.9.18": true,
75+
"172.23.9.18": true,
76+
"172.24.9.18": true,
77+
"172.25.9.18": true,
78+
"172.26.9.18": true,
79+
"172.27.9.18": true,
80+
"172.28.9.18": true,
81+
"172.29.9.18": true,
82+
"172.30.9.18": true,
83+
"172.31.9.18": true,
84+
"172.32.9.18": false,
8185
} {
8286
if HasLocalIP(net.ParseIP(ipString)) != expected {
8387
t.Errorf("ip %s", ipString)
@@ -200,6 +204,17 @@ func TestIPString2Long(t *testing.T) {
200204
t.Errorf("ip:%s long:%d != expected:%d", v.ip, v.long, expected)
201205
}
202206
}
207+
208+
for _, ip := range []string{
209+
"",
210+
"invalid ip address",
211+
"::1",
212+
} {
213+
_, err := IPString2Long(ip)
214+
if err == nil {
215+
t.Errorf("ip:%s invalid IP passes", ip)
216+
}
217+
}
203218
}
204219

205220
func TestLong2IPString(t *testing.T) {
@@ -221,6 +236,14 @@ func TestLong2IPString(t *testing.T) {
221236
t.Errorf(" long:%d ip:%s != expected:%s", v.long, v.ip, expected)
222237
}
223238
}
239+
240+
// 在64位机器上运行,否者输入值将超过限制
241+
if 32<<(^uint(0)>>63) == 64 {
242+
_, err := Long2IPString(1<<64 - 1)
243+
if err == nil {
244+
t.Errorf("long:%d out of range", uint64(1<<64-1))
245+
}
246+
}
224247
}
225248

226249
func TestIP2Long(t *testing.T) {
@@ -242,6 +265,17 @@ func TestIP2Long(t *testing.T) {
242265
t.Errorf("ip:%s long:%d != expected:%d", v.ip, v.long, expected)
243266
}
244267
}
268+
269+
for _, ip := range []string{
270+
"",
271+
"invalid ip address",
272+
"::1",
273+
} {
274+
_, err := IP2Long(net.ParseIP(ip))
275+
if err == nil {
276+
t.Errorf("ip:%s invalid IP passes", ip)
277+
}
278+
}
245279
}
246280

247281
func TestLong2IP(t *testing.T) {
@@ -263,4 +297,12 @@ func TestLong2IP(t *testing.T) {
263297
t.Errorf(" long:%d ip:%s != expected:%s", v.long, v.ip, expected.String())
264298
}
265299
}
300+
301+
// 在64位机器上运行,否者输入值将超过限制
302+
if 32<<(^uint(0)>>63) == 64 {
303+
_, err := Long2IP(1<<64 - 1)
304+
if err == nil {
305+
t.Errorf("long:%d out of range", uint64(1<<64-1))
306+
}
307+
}
266308
}

0 commit comments

Comments
 (0)