added tests for IP address parsing
This commit is contained in:
parent
e389d7f782
commit
9a30acda05
|
@ -1,6 +1,7 @@
|
||||||
package arg
|
package arg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -541,3 +542,40 @@ func TestSliceUnmarhsaler(t *testing.T) {
|
||||||
assert.EqualValues(t, 5, (*args.Foo)[0])
|
assert.EqualValues(t, 5, (*args.Foo)[0])
|
||||||
assert.Equal(t, "xyz", args.Bar)
|
assert.Equal(t, "xyz", args.Bar)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIP(t *testing.T) {
|
||||||
|
var args struct {
|
||||||
|
Host net.IP
|
||||||
|
}
|
||||||
|
err := parse("--host 192.168.0.1", &args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "192.168.0.1", args.Host.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPtrToIP(t *testing.T) {
|
||||||
|
var args struct {
|
||||||
|
Host *net.IP
|
||||||
|
}
|
||||||
|
err := parse("--host 192.168.0.1", &args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "192.168.0.1", args.Host.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIPSlice(t *testing.T) {
|
||||||
|
var args struct {
|
||||||
|
Host []net.IP
|
||||||
|
}
|
||||||
|
err := parse("--host 192.168.0.1 127.0.0.1", &args)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, args.Host, 2)
|
||||||
|
assert.Equal(t, "192.168.0.1", args.Host[0].String())
|
||||||
|
assert.Equal(t, "127.0.0.1", args.Host[1].String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidIPAddress(t *testing.T) {
|
||||||
|
var args struct {
|
||||||
|
Host net.IP
|
||||||
|
}
|
||||||
|
err := parse("--host xxx", &args)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
|
@ -93,18 +93,21 @@ func setScalar(v reflect.Value, s string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Set(reflect.ValueOf(*addr))
|
v.Set(reflect.ValueOf(*addr))
|
||||||
|
return nil
|
||||||
case net.IP:
|
case net.IP:
|
||||||
ip := net.ParseIP(s)
|
ip := net.ParseIP(s)
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return fmt.Errorf(`invalid IP address: "%s"`, s)
|
return fmt.Errorf(`invalid IP address: "%s"`, s)
|
||||||
}
|
}
|
||||||
v.Set(reflect.ValueOf(ip))
|
v.Set(reflect.ValueOf(ip))
|
||||||
|
return nil
|
||||||
case net.HardwareAddr:
|
case net.HardwareAddr:
|
||||||
ip, err := net.ParseMAC(s)
|
ip, err := net.ParseMAC(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Set(reflect.ValueOf(ip))
|
v.Set(reflect.ValueOf(ip))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Switch on kind so that we can handle derived types
|
// Switch on kind so that we can handle derived types
|
||||||
|
|
Loading…
Reference in New Issue