-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip_test.go
executable file
·129 lines (112 loc) · 2.19 KB
/
ip_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
// Package net contains helper function for handling
// e.g. ip addresses or domain names
package net
import (
"testing"
)
func TestIsIPv4(t *testing.T) {
var testIPs = []struct {
ip string
expected bool
}{
{"1.2.3.4", true},
{"1.2.3.4/32", true},
{"256.0.0.1", false},
{"1.2.3.4.5", false},
{"0.175", false},
{".12", false},
}
for _, e := range testIPs {
r := IsIPAddr(e.ip)
if r != e.expected {
t.Errorf("%s", e.ip)
}
}
}
func TestIsIPv6(t *testing.T) {
var testIPs = []struct {
ip string
expected bool
}{
{"1.2.3.4", true},
{"256.0.0.1", false},
{"1.2.3.4.5", false},
{"2001:eab::1", true},
{"2001:eab::1/128", true},
}
for _, e := range testIPs {
r := IsIPAddr(e.ip)
if r != e.expected {
t.Errorf("%s", e.ip)
}
}
}
func TestIsNetwork(t *testing.T) {
var networkTests = []struct {
network string
expected bool
}{
{"1.2.3.0/24", true},
{"2001:db8::/32", true},
{"2001:db8::1/32", false},
{"1.2.3.4", false},
{"1.2.3.4/24", false},
}
for _, e := range networkTests {
if IsNetwork(e.network) != e.expected {
t.Errorf("%s", e.network)
}
}
}
func TestIsIPRange(t *testing.T) {
var iprangeTests = []struct {
iprange string
expected bool
}{
{"1.2.3.1-1.2.3.255", true},
{"1.2.3.1-1.2.3.255", true},
{"2001:db8::1-2001:db8::ffff", true},
{"1.2.3.4", false},
{"1.2.3.4-100", false},
}
for _, e := range iprangeTests {
if IsIPRange(e.iprange) != e.expected {
t.Errorf("%s", e.iprange)
}
}
}
func TestReverseIP(t *testing.T) {
// google-public-dns-b.google.com
r, err := ReverseIPAddr("8.8.4.4")
if err != nil {
t.Error(err)
} else {
t.Logf("%s", r)
}
r, err = ReverseIPAddr("2001:4860:4860::8844")
if err != nil {
t.Error(err)
} else {
t.Logf("%s", r)
}
}
/*
func TestIsURL(t *testing.T) { }
*/
func TestIntToIP(t *testing.T) {
s := "16777216"
ip := IntToIP(s)
if ip.String() != "1.0.0.0" {
t.Errorf("%s - %s", s, ip.String())
}
s = "281470698520576"
ip = IntToIP(s)
if ip.String() != "1.0.0.0" {
t.Errorf("%s - %s", s, ip.String())
}
s = "340277174624079928635746358406137511936"
ip = IntToIP(s)
if ip.String() != "ffff::ffff:100:0" {
t.Errorf("%s - %s", s, ip.String())
}
}