Return an error when addr is empty

This commit is contained in:
Lifei Chen 2020-09-18 20:04:29 +08:00 committed by GitHub
parent e9da6dae98
commit e8ae07c3ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View File

@ -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

View File

@ -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")