@@ -23,29 +23,31 @@ import (
23
23
24
24
func TestHasLocalIPddr (t * testing.T ) {
25
25
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 ,
49
51
} {
50
52
if HasLocalIPddr (ipString ) != expected {
51
53
t .Errorf ("ip %s" , ipString )
@@ -55,29 +57,31 @@ func TestHasLocalIPddr(t *testing.T) {
55
57
56
58
func TestHasLocalIP (t * testing.T ) {
57
59
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 ,
81
85
} {
82
86
if HasLocalIP (net .ParseIP (ipString )) != expected {
83
87
t .Errorf ("ip %s" , ipString )
@@ -200,6 +204,17 @@ func TestIPString2Long(t *testing.T) {
200
204
t .Errorf ("ip:%s long:%d != expected:%d" , v .ip , v .long , expected )
201
205
}
202
206
}
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
+ }
203
218
}
204
219
205
220
func TestLong2IPString (t * testing.T ) {
@@ -221,6 +236,14 @@ func TestLong2IPString(t *testing.T) {
221
236
t .Errorf (" long:%d ip:%s != expected:%s" , v .long , v .ip , expected )
222
237
}
223
238
}
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
+ }
224
247
}
225
248
226
249
func TestIP2Long (t * testing.T ) {
@@ -242,6 +265,17 @@ func TestIP2Long(t *testing.T) {
242
265
t .Errorf ("ip:%s long:%d != expected:%d" , v .ip , v .long , expected )
243
266
}
244
267
}
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
+ }
245
279
}
246
280
247
281
func TestLong2IP (t * testing.T ) {
@@ -263,4 +297,12 @@ func TestLong2IP(t *testing.T) {
263
297
t .Errorf (" long:%d ip:%s != expected:%s" , v .long , v .ip , expected .String ())
264
298
}
265
299
}
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
+ }
266
308
}
0 commit comments