mirror of
https://github.com/go-ping/ping.git
synced 2025-07-11 13:03:09 +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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"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.
|
// Resolve does the DNS lookup for the Pinger address and sets IP protocol.
|
||||||
func (p *Pinger) Resolve() error {
|
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)
|
ipaddr, err := net.ResolveIPAddr(p.network, p.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -358,6 +358,11 @@ func TestSetIPAddr(t *testing.T) {
|
|||||||
AssertEqualStrings(t, googleaddr.String(), p.Addr())
|
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) {
|
func TestStatisticsSunny(t *testing.T) {
|
||||||
// Create a localhost ipv4 pinger
|
// Create a localhost ipv4 pinger
|
||||||
p := New("localhost")
|
p := New("localhost")
|
||||||
|
Loading…
Reference in New Issue
Block a user