mirror of
https://gitlab.com/pnx-tools/dns-updater.git
synced 2026-06-16 05:54:56 +02:00
ip/helpers.go: adding ParseIP()
This commit is contained in:
parent
adbb1feccd
commit
2204be7777
2 changed files with 40 additions and 0 deletions
|
|
@ -47,3 +47,15 @@ func AddrToIP(addr net.Addr) (net.IP, error) {
|
|||
}
|
||||
return nil, errors.New("could not find ip")
|
||||
}
|
||||
|
||||
func ParseIP(s string) (net.IP, error) {
|
||||
var err error = nil
|
||||
ip := net.ParseIP(s)
|
||||
if ip == nil {
|
||||
err = &net.ParseError{
|
||||
Type: "IP address",
|
||||
Text: s,
|
||||
}
|
||||
}
|
||||
return ip, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,3 +67,31 @@ func TestAddrToIP(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want net.IP
|
||||
wantErr bool
|
||||
}{
|
||||
{"localhost", "127.0.0.1", net.IPv4(127, 0, 0, 1), false},
|
||||
{"Private#1", "10.4.0.11", net.IPv4(10, 4, 0, 11), false},
|
||||
{"Private#2", "192.168.1.12", net.IPv4(192, 168, 1, 12), false},
|
||||
{"Public#1", "82.249.10.254", net.IPv4(82, 249, 10, 254), false},
|
||||
{"Public#2", "57.167.50.222", net.IPv4(57, 167, 50, 222), false},
|
||||
{"Invalid", "xx", nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ParseIP(tt.input)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ParseIP() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("ParseIP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue