diff --git a/defs/pinger_test.go b/defs/pinger_test.go new file mode 100644 index 0000000..4123fa9 --- /dev/null +++ b/defs/pinger_test.go @@ -0,0 +1,64 @@ +package defs + +import ( + "net" + "testing" +) + +// --ipv4 and --ipv6 name the family the whole test runs over, so the ICMP +// ping must not resolve its target in the other one. +func TestNewPingerResolvesWithinTheRequestedFamily(t *testing.T) { + cases := []struct { + host string + network string + wantErr bool + }{ + {"127.0.0.1", "ip", false}, + {"127.0.0.1", "ip4", false}, + {"127.0.0.1", "ip6", true}, + {"::1", "ip", false}, + {"::1", "ip6", false}, + {"::1", "ip4", true}, + } + + for _, c := range cases { + t.Run(c.host+" over "+c.network, func(t *testing.T) { + p, err := newPinger(c.host, c.network) + if c.wantErr { + if err == nil { + t.Fatalf("resolved %s to %s, want an error", c.host, p.IPAddr()) + } + return + } + if err != nil { + t.Fatalf("newPinger: %v", err) + } + if got := p.IPAddr(); got == nil || !got.IP.Equal(net.ParseIP(c.host)) { + t.Errorf("resolved to %v, want %s", got, c.host) + } + }) + } +} + +// The family was ignored for a name with addresses in both: the name was +// resolved before the family was set. localhost is such a name on most +// systems. Where it has no address in a family, there is nothing to tell +// apart, and that family is skipped. +func TestNewPingerResolvesANameWithinTheRequestedFamily(t *testing.T) { + for _, network := range []string{"ip4", "ip6"} { + t.Run(network, func(t *testing.T) { + if _, err := net.ResolveIPAddr(network, "localhost"); err != nil { + t.Skipf("localhost has no %s address here: %v", network, err) + } + + p, err := newPinger("localhost", network) + if err != nil { + t.Fatalf("newPinger: %v", err) + } + got := p.IPAddr() + if got == nil || (got.IP.To4() != nil) != (network == "ip4") { + t.Errorf("resolved to %v over %s", got, network) + } + }) + } +} diff --git a/defs/server.go b/defs/server.go index c5349d0..f25b0a8 100644 --- a/defs/server.go +++ b/defs/server.go @@ -160,13 +160,12 @@ func (s *Server) ICMPPingAndJitter(count int, srcIp, network string) (float64, f return 0, 0, err } - p, err := probing.NewPinger(u.Hostname()) + p, err := newPinger(u.Hostname(), network) if err != nil { output.WriteDebug("Failed to resolve ping target: %s\n", err) output.WriteDebug("Will try TCP ping\n") return s.PingAndJitter(count + 2) } - p.SetNetwork(network) p.Count = count p.Timeout = time.Duration(count) * time.Second if srcIp != "" { @@ -228,6 +227,18 @@ func (s *Server) ICMPPingAndJitter(count int, srcIp, network string) (float64, f return rttMillis(stats.AvgRtt), jitter, nil } +// newPinger returns a pinger for host, resolved within network ("ip", "ip4" +// or "ip6"). The network is set before resolving: probing.NewPinger resolves +// at once with "ip", and a pinger that has an address does not resolve again. +func newPinger(host, network string) (*probing.Pinger, error) { + p := probing.New(host) + p.SetNetwork(network) + if err := p.Resolve(); err != nil { + return nil, err + } + return p, nil +} + // addressFamily names the IP version of an address, for reporting which path a // measurement took. Accepts either a bare address or one with a port. func addressFamily(addr string) string {