-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparse_test.go
134 lines (128 loc) · 2.69 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
// Copyright 2015 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ipaddr_test
import (
"reflect"
"testing"
"github.com/mikioh/ipaddr"
)
func TestParse(t *testing.T) {
for i, tt := range []struct {
in string
pos *ipaddr.Position
ps []ipaddr.Prefix
}{
// IPv4 addresses, prefixes
{
"192.168.0.1",
toPosition("192.168.0.1", "192.168.0.1/32"),
toPrefixes([]string{
"192.168.0.1/32",
}),
},
{
"192.168.0.1/32",
toPosition("192.168.0.1", "192.168.0.1/32"),
toPrefixes([]string{
"192.168.0.1/32",
}),
},
{
"192.168.0.1/24",
toPosition("192.168.0.1", "192.168.0.0/24"),
toPrefixes([]string{
"192.168.0.0/24",
}),
},
{
"192.168.0.2,192.168.0.2/24",
toPosition("192.168.0.0", "192.168.0.0/24"),
toPrefixes([]string{
"192.168.0.0/24",
"192.168.0.2/32",
}),
},
{
"192.168.0.2/24,192.168.0.2",
toPosition("192.168.0.0", "192.168.0.0/24"),
toPrefixes([]string{
"192.168.0.0/24",
"192.168.0.2/32",
}),
},
// IPv6 addresses, prefixes
{
"2001:db8::1",
toPosition("2001:db8::1", "2001:db8::1/128"),
toPrefixes([]string{
"2001:db8::1/128",
}),
},
{
"2001:db8::1/128",
toPosition("2001:db8::1", "2001:db8::1/128"),
toPrefixes([]string{
"2001:db8::1/128",
}),
},
{
"2001:db8::1/64",
toPosition("2001:db8::1", "2001:db8::/64"),
toPrefixes([]string{
"2001:db8::/64",
}),
},
{
"2001:db8::2,2001:db8::2/64",
toPosition("2001:db8::", "2001:db8::/64"),
toPrefixes([]string{
"2001:db8::/64",
"2001:db8::2/128",
}),
},
{
"2001:db8::2/64,2001:db8::2",
toPosition("2001:db8::", "2001:db8::/64"),
toPrefixes([]string{
"2001:db8::/64",
"2001:db8::2/128",
}),
},
// Mixed addresses, prefixes
{
"192.168.0.3,192.168.0.3/24,172.16.0.0/16,2001:db8::3,2001:db8::3/64",
toPosition("172.16.0.0", "172.16.0.0/16"),
toPrefixes([]string{
"172.16.0.0/16",
"192.168.0.0/24",
"192.168.0.3/32",
"2001:db8::/64",
"2001:db8::3/128",
}),
},
{
"2001:db8::3,2001:db8::3/64,192.168.0.3,192.168.0.3/24,172.16.0.0/16",
toPosition("172.16.0.0", "172.16.0.0/16"),
toPrefixes([]string{
"172.16.0.0/16",
"192.168.0.0/24",
"192.168.0.3/32",
"2001:db8::/64",
"2001:db8::3/128",
}),
},
} {
out, err := ipaddr.Parse(tt.in)
if err != nil {
t.Errorf("#%d: %v", i, err)
continue
}
if !reflect.DeepEqual(out.Pos(), tt.pos) {
t.Errorf("#%d: got %v; want %v", i, out.Pos(), tt.pos)
}
if !reflect.DeepEqual(out.List(), tt.ps) {
t.Errorf("#%d: got %v; want %v", i, out.List(), tt.ps)
}
}
}