mirror of
https://github.com/go-ping/ping.git
synced 2025-04-28 02:40:17 +00:00
Return an error when addr is empty
This commit is contained in:
parent
e9da6dae98
commit
e8ae07c3ce
4
ping.go
4
ping.go
@ -46,6 +46,7 @@ package ping
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
@ -230,6 +231,9 @@ func (p *Pinger) IPAddr() *net.IPAddr {
|
||||
|
||||
// Resolve does the DNS lookup for the Pinger address and sets IP protocol.
|
||||
func (p *Pinger) Resolve() error {
|
||||
if len(p.addr) == 0 {
|
||||
return errors.New("addr cannot be empty")
|
||||
}
|
||||
ipaddr, err := net.ResolveIPAddr(p.network, p.addr)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -358,6 +358,11 @@ func TestSetIPAddr(t *testing.T) {
|
||||
AssertEqualStrings(t, googleaddr.String(), p.Addr())
|
||||
}
|
||||
|
||||
func TestEmptyIPAddr(t *testing.T) {
|
||||
_, err := NewPinger("")
|
||||
AssertError(t, err, "empty pinger did not return an error")
|
||||
}
|
||||
|
||||
func TestStatisticsSunny(t *testing.T) {
|
||||
// Create a localhost ipv4 pinger
|
||||
p := New("localhost")
|
||||
|
Loading…
Reference in New Issue
Block a user