Skip to content

Commit 85dd5c8

Browse files
author
rpothier
authored
Add IPNetSlice and unit tests (#170)
1 parent 6971c29 commit 85dd5c8

File tree

2 files changed

+386
-0
lines changed

2 files changed

+386
-0
lines changed

ipnet_slice.go

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package pflag
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net"
7+
"strings"
8+
)
9+
10+
// -- ipNetSlice Value
11+
type ipNetSliceValue struct {
12+
value *[]net.IPNet
13+
changed bool
14+
}
15+
16+
func newIPNetSliceValue(val []net.IPNet, p *[]net.IPNet) *ipNetSliceValue {
17+
ipnsv := new(ipNetSliceValue)
18+
ipnsv.value = p
19+
*ipnsv.value = val
20+
return ipnsv
21+
}
22+
23+
// Set converts, and assigns, the comma-separated IPNet argument string representation as the []net.IPNet value of this flag.
24+
// If Set is called on a flag that already has a []net.IPNet assigned, the newly converted values will be appended.
25+
func (s *ipNetSliceValue) Set(val string) error {
26+
27+
// remove all quote characters
28+
rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")
29+
30+
// read flag arguments with CSV parser
31+
ipNetStrSlice, err := readAsCSV(rmQuote.Replace(val))
32+
if err != nil && err != io.EOF {
33+
return err
34+
}
35+
36+
// parse ip values into slice
37+
out := make([]net.IPNet, 0, len(ipNetStrSlice))
38+
for _, ipNetStr := range ipNetStrSlice {
39+
_, n, err := net.ParseCIDR(strings.TrimSpace(ipNetStr))
40+
if err != nil {
41+
return fmt.Errorf("invalid string being converted to CIDR: %s", ipNetStr)
42+
}
43+
out = append(out, *n)
44+
}
45+
46+
if !s.changed {
47+
*s.value = out
48+
} else {
49+
*s.value = append(*s.value, out...)
50+
}
51+
52+
s.changed = true
53+
54+
return nil
55+
}
56+
57+
// Type returns a string that uniquely represents this flag's type.
58+
func (s *ipNetSliceValue) Type() string {
59+
return "ipNetSlice"
60+
}
61+
62+
// String defines a "native" format for this net.IPNet slice flag value.
63+
func (s *ipNetSliceValue) String() string {
64+
65+
ipNetStrSlice := make([]string, len(*s.value))
66+
for i, n := range *s.value {
67+
ipNetStrSlice[i] = n.String()
68+
}
69+
70+
out, _ := writeAsCSV(ipNetStrSlice)
71+
return "[" + out + "]"
72+
}
73+
74+
func ipNetSliceConv(val string) (interface{}, error) {
75+
val = strings.Trim(val, "[]")
76+
// Emtpy string would cause a slice with one (empty) entry
77+
if len(val) == 0 {
78+
return []net.IPNet{}, nil
79+
}
80+
ss := strings.Split(val, ",")
81+
out := make([]net.IPNet, len(ss))
82+
for i, sval := range ss {
83+
_, n, err := net.ParseCIDR(strings.TrimSpace(sval))
84+
if err != nil {
85+
return nil, fmt.Errorf("invalid string being converted to CIDR: %s", sval)
86+
}
87+
out[i] = *n
88+
}
89+
return out, nil
90+
}
91+
92+
// GetIPNetSlice returns the []net.IPNet value of a flag with the given name
93+
func (f *FlagSet) GetIPNetSlice(name string) ([]net.IPNet, error) {
94+
val, err := f.getFlagType(name, "ipNetSlice", ipNetSliceConv)
95+
if err != nil {
96+
return []net.IPNet{}, err
97+
}
98+
return val.([]net.IPNet), nil
99+
}
100+
101+
// IPNetSliceVar defines a ipNetSlice flag with specified name, default value, and usage string.
102+
// The argument p points to a []net.IPNet variable in which to store the value of the flag.
103+
func (f *FlagSet) IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string) {
104+
f.VarP(newIPNetSliceValue(value, p), name, "", usage)
105+
}
106+
107+
// IPNetSliceVarP is like IPNetSliceVar, but accepts a shorthand letter that can be used after a single dash.
108+
func (f *FlagSet) IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string) {
109+
f.VarP(newIPNetSliceValue(value, p), name, shorthand, usage)
110+
}
111+
112+
// IPNetSliceVar defines a []net.IPNet flag with specified name, default value, and usage string.
113+
// The argument p points to a []net.IPNet variable in which to store the value of the flag.
114+
func IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string) {
115+
CommandLine.VarP(newIPNetSliceValue(value, p), name, "", usage)
116+
}
117+
118+
// IPNetSliceVarP is like IPNetSliceVar, but accepts a shorthand letter that can be used after a single dash.
119+
func IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string) {
120+
CommandLine.VarP(newIPNetSliceValue(value, p), name, shorthand, usage)
121+
}
122+
123+
// IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string.
124+
// The return value is the address of a []net.IPNet variable that stores the value of that flag.
125+
func (f *FlagSet) IPNetSlice(name string, value []net.IPNet, usage string) *[]net.IPNet {
126+
p := []net.IPNet{}
127+
f.IPNetSliceVarP(&p, name, "", value, usage)
128+
return &p
129+
}
130+
131+
// IPNetSliceP is like IPNetSlice, but accepts a shorthand letter that can be used after a single dash.
132+
func (f *FlagSet) IPNetSliceP(name, shorthand string, value []net.IPNet, usage string) *[]net.IPNet {
133+
p := []net.IPNet{}
134+
f.IPNetSliceVarP(&p, name, shorthand, value, usage)
135+
return &p
136+
}
137+
138+
// IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string.
139+
// The return value is the address of a []net.IP variable that stores the value of the flag.
140+
func IPNetSlice(name string, value []net.IPNet, usage string) *[]net.IPNet {
141+
return CommandLine.IPNetSliceP(name, "", value, usage)
142+
}
143+
144+
// IPNetSliceP is like IPNetSlice, but accepts a shorthand letter that can be used after a single dash.
145+
func IPNetSliceP(name, shorthand string, value []net.IPNet, usage string) *[]net.IPNet {
146+
return CommandLine.IPNetSliceP(name, shorthand, value, usage)
147+
}

ipnet_slice_test.go

+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
package pflag
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"strings"
7+
"testing"
8+
)
9+
10+
// Helper function to set static slices
11+
func getCIDR(ip net.IP, cidr *net.IPNet, err error) net.IPNet {
12+
return *cidr
13+
}
14+
15+
func equalCIDR(c1 net.IPNet, c2 net.IPNet) bool {
16+
if c1.String() == c2.String() {
17+
return true
18+
}
19+
return false
20+
}
21+
22+
func setUpIPNetFlagSet(ipsp *[]net.IPNet) *FlagSet {
23+
f := NewFlagSet("test", ContinueOnError)
24+
f.IPNetSliceVar(ipsp, "cidrs", []net.IPNet{}, "Command separated list!")
25+
return f
26+
}
27+
28+
func setUpIPNetFlagSetWithDefault(ipsp *[]net.IPNet) *FlagSet {
29+
f := NewFlagSet("test", ContinueOnError)
30+
f.IPNetSliceVar(ipsp, "cidrs",
31+
[]net.IPNet{
32+
getCIDR(net.ParseCIDR("192.168.1.1/16")),
33+
getCIDR(net.ParseCIDR("fd00::/64")),
34+
},
35+
"Command separated list!")
36+
return f
37+
}
38+
39+
func TestEmptyIPNet(t *testing.T) {
40+
var cidrs []net.IPNet
41+
f := setUpIPNetFlagSet(&cidrs)
42+
err := f.Parse([]string{})
43+
if err != nil {
44+
t.Fatal("expected no error; got", err)
45+
}
46+
47+
getIPNet, err := f.GetIPNetSlice("cidrs")
48+
if err != nil {
49+
t.Fatal("got an error from GetIPNetSlice():", err)
50+
}
51+
if len(getIPNet) != 0 {
52+
t.Fatalf("got ips %v with len=%d but expected length=0", getIPNet, len(getIPNet))
53+
}
54+
}
55+
56+
func TestIPNets(t *testing.T) {
57+
var ips []net.IPNet
58+
f := setUpIPNetFlagSet(&ips)
59+
60+
vals := []string{"192.168.1.1/24", "10.0.0.1/16", "fd00:0:0:0:0:0:0:2/64"}
61+
arg := fmt.Sprintf("--cidrs=%s", strings.Join(vals, ","))
62+
err := f.Parse([]string{arg})
63+
if err != nil {
64+
t.Fatal("expected no error; got", err)
65+
}
66+
for i, v := range ips {
67+
if _, cidr, _ := net.ParseCIDR(vals[i]); cidr == nil {
68+
t.Fatalf("invalid string being converted to CIDR: %s", vals[i])
69+
} else if !equalCIDR(*cidr, v) {
70+
t.Fatalf("expected ips[%d] to be %s but got: %s from GetIPSlice", i, vals[i], v)
71+
}
72+
}
73+
}
74+
75+
func TestIPNetDefault(t *testing.T) {
76+
var cidrs []net.IPNet
77+
f := setUpIPNetFlagSetWithDefault(&cidrs)
78+
79+
vals := []string{"192.168.1.1/16", "fd00::/64"}
80+
err := f.Parse([]string{})
81+
if err != nil {
82+
t.Fatal("expected no error; got", err)
83+
}
84+
for i, v := range cidrs {
85+
if _, cidr, _ := net.ParseCIDR(vals[i]); cidr == nil {
86+
t.Fatalf("invalid string being converted to CIDR: %s", vals[i])
87+
} else if !equalCIDR(*cidr, v) {
88+
t.Fatalf("expected cidrs[%d] to be %s but got: %s", i, vals[i], v)
89+
}
90+
}
91+
92+
getIPNet, err := f.GetIPNetSlice("cidrs")
93+
if err != nil {
94+
t.Fatal("got an error from GetIPNetSlice")
95+
}
96+
for i, v := range getIPNet {
97+
if _, cidr, _ := net.ParseCIDR(vals[i]); cidr == nil {
98+
t.Fatalf("invalid string being converted to CIDR: %s", vals[i])
99+
} else if !equalCIDR(*cidr, v) {
100+
t.Fatalf("expected cidrs[%d] to be %s but got: %s", i, vals[i], v)
101+
}
102+
}
103+
}
104+
105+
func TestIPNetWithDefault(t *testing.T) {
106+
var cidrs []net.IPNet
107+
f := setUpIPNetFlagSetWithDefault(&cidrs)
108+
109+
vals := []string{"192.168.1.1/16", "fd00::/64"}
110+
arg := fmt.Sprintf("--cidrs=%s", strings.Join(vals, ","))
111+
err := f.Parse([]string{arg})
112+
if err != nil {
113+
t.Fatal("expected no error; got", err)
114+
}
115+
for i, v := range cidrs {
116+
if _, cidr, _ := net.ParseCIDR(vals[i]); cidr == nil {
117+
t.Fatalf("invalid string being converted to CIDR: %s", vals[i])
118+
} else if !equalCIDR(*cidr, v) {
119+
t.Fatalf("expected cidrs[%d] to be %s but got: %s", i, vals[i], v)
120+
}
121+
}
122+
123+
getIPNet, err := f.GetIPNetSlice("cidrs")
124+
if err != nil {
125+
t.Fatal("got an error from GetIPNetSlice")
126+
}
127+
for i, v := range getIPNet {
128+
if _, cidr, _ := net.ParseCIDR(vals[i]); cidr == nil {
129+
t.Fatalf("invalid string being converted to CIDR: %s", vals[i])
130+
} else if !equalCIDR(*cidr, v) {
131+
t.Fatalf("expected cidrs[%d] to be %s but got: %s", i, vals[i], v)
132+
}
133+
}
134+
}
135+
136+
func TestIPNetCalledTwice(t *testing.T) {
137+
var cidrs []net.IPNet
138+
f := setUpIPNetFlagSet(&cidrs)
139+
140+
in := []string{"192.168.1.2/16,fd00::/64", "10.0.0.1/24"}
141+
142+
expected := []net.IPNet{
143+
getCIDR(net.ParseCIDR("192.168.1.2/16")),
144+
getCIDR(net.ParseCIDR("fd00::/64")),
145+
getCIDR(net.ParseCIDR("10.0.0.1/24")),
146+
}
147+
argfmt := "--cidrs=%s"
148+
arg1 := fmt.Sprintf(argfmt, in[0])
149+
arg2 := fmt.Sprintf(argfmt, in[1])
150+
err := f.Parse([]string{arg1, arg2})
151+
if err != nil {
152+
t.Fatal("expected no error; got", err)
153+
}
154+
for i, v := range cidrs {
155+
if !equalCIDR(expected[i], v) {
156+
t.Fatalf("expected cidrs[%d] to be %s but got: %s", i, expected[i], v)
157+
}
158+
}
159+
}
160+
161+
func TestIPNetBadQuoting(t *testing.T) {
162+
163+
tests := []struct {
164+
Want []net.IPNet
165+
FlagArg []string
166+
}{
167+
{
168+
Want: []net.IPNet{
169+
getCIDR(net.ParseCIDR("a4ab:61d:f03e:5d7d:fad7:d4c2:a1a5:568/128")),
170+
getCIDR(net.ParseCIDR("203.107.49.208/32")),
171+
getCIDR(net.ParseCIDR("14.57.204.90/32")),
172+
},
173+
FlagArg: []string{
174+
"a4ab:61d:f03e:5d7d:fad7:d4c2:a1a5:568/128",
175+
"203.107.49.208/32",
176+
"14.57.204.90/32",
177+
},
178+
},
179+
{
180+
Want: []net.IPNet{
181+
getCIDR(net.ParseCIDR("204.228.73.195/32")),
182+
getCIDR(net.ParseCIDR("86.141.15.94/32")),
183+
},
184+
FlagArg: []string{
185+
"204.228.73.195/32",
186+
"86.141.15.94/32",
187+
},
188+
},
189+
{
190+
Want: []net.IPNet{
191+
getCIDR(net.ParseCIDR("c70c:db36:3001:890f:c6ea:3f9b:7a39:cc3f/128")),
192+
getCIDR(net.ParseCIDR("4d17:1d6e:e699:bd7a:88c5:5e7e:ac6a:4472/128")),
193+
},
194+
FlagArg: []string{
195+
"c70c:db36:3001:890f:c6ea:3f9b:7a39:cc3f/128",
196+
"4d17:1d6e:e699:bd7a:88c5:5e7e:ac6a:4472/128",
197+
},
198+
},
199+
{
200+
Want: []net.IPNet{
201+
getCIDR(net.ParseCIDR("5170:f971:cfac:7be3:512a:af37:952c:bc33/128")),
202+
getCIDR(net.ParseCIDR("93.21.145.140/32")),
203+
getCIDR(net.ParseCIDR("2cac:61d3:c5ff:6caf:73e0:1b1a:c336:c1ca/128")),
204+
},
205+
FlagArg: []string{
206+
" 5170:f971:cfac:7be3:512a:af37:952c:bc33/128 , 93.21.145.140/32 ",
207+
"2cac:61d3:c5ff:6caf:73e0:1b1a:c336:c1ca/128",
208+
},
209+
},
210+
{
211+
Want: []net.IPNet{
212+
getCIDR(net.ParseCIDR("2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128")),
213+
getCIDR(net.ParseCIDR("2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128")),
214+
getCIDR(net.ParseCIDR("2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128")),
215+
getCIDR(net.ParseCIDR("2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128")),
216+
},
217+
FlagArg: []string{
218+
`"2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128, 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128,2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128 "`,
219+
" 2e5e:66b2:6441:848:5b74:76ea:574c:3a7b/128"},
220+
},
221+
}
222+
223+
for i, test := range tests {
224+
225+
var cidrs []net.IPNet
226+
f := setUpIPNetFlagSet(&cidrs)
227+
228+
if err := f.Parse([]string{fmt.Sprintf("--cidrs=%s", strings.Join(test.FlagArg, ","))}); err != nil {
229+
t.Fatalf("flag parsing failed with error: %s\nparsing:\t%#v\nwant:\t\t%s",
230+
err, test.FlagArg, test.Want[i])
231+
}
232+
233+
for j, b := range cidrs {
234+
if !equalCIDR(b, test.Want[j]) {
235+
t.Fatalf("bad value parsed for test %d on net.IP %d:\nwant:\t%s\ngot:\t%s", i, j, test.Want[j], b)
236+
}
237+
}
238+
}
239+
}

0 commit comments

Comments
 (0)