From c3fc64094fbed445c0d131b77fcc13e56da991e7 Mon Sep 17 00:00:00 2001 From: David Barr <38654497+davebarrau@users.noreply.github.com> Date: Sun, 7 Oct 2018 14:12:45 +1100 Subject: Dynamically determine loopback interface for server test (#167) * Dynamically determine loopback interface for server test "lo" isn't configured everywhere. In FreeBSD for example, TestServerActivateAndServe() fails as the loopback address is "lo0", not "lo". Also, I don't believe the Zone is required for net.UDPAddr as ::1 is being used, not an fe80:: address. * Add error handling to getLoopbackInterface() - My editor also snuck in some space indentation --- dhcpv6/server_test.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'dhcpv6/server_test.go') diff --git a/dhcpv6/server_test.go b/dhcpv6/server_test.go index 066fe3a..587e249 100644 --- a/dhcpv6/server_test.go +++ b/dhcpv6/server_test.go @@ -5,17 +5,34 @@ import ( "net" "testing" "time" + "errors" "github.com/stretchr/testify/require" ) +// utility function to return the loopback interface name +func getLoopbackInterface() (string, error) { + var ifaces []net.Interface + var err error + if ifaces, err = net.Interfaces(); err != nil { + return "", err + } + + for _, iface := range ifaces { + if iface.Flags & net.FlagLoopback != 0 || iface.Name[:2] == "lo" { + return iface.Name, nil + } + } + + return "", errors.New("No loopback interface found") +} + // utility function to set up a client and a server instance and run it in // background. The caller needs to call Server.Close() once finished. func setUpClientAndServer(handler Handler) (*Client, *Server) { laddr := net.UDPAddr{ IP: net.ParseIP("::1"), Port: 0, - Zone: "lo", } s := NewServer(laddr, handler) go s.ActivateAndServe() @@ -23,7 +40,6 @@ func setUpClientAndServer(handler Handler) (*Client, *Server) { c := NewClient() c.LocalAddr = &net.UDPAddr{ IP: net.ParseIP("::1"), - Zone: "lo", } for { if s.LocalAddr() != nil { @@ -35,7 +51,6 @@ func setUpClientAndServer(handler Handler) (*Client, *Server) { c.RemoteAddr = &net.UDPAddr{ IP: net.ParseIP("::1"), Port: s.LocalAddr().(*net.UDPAddr).Port, - Zone: "lo", } return c, s @@ -45,7 +60,6 @@ func TestNewServer(t *testing.T) { laddr := net.UDPAddr{ IP: net.ParseIP("::1"), Port: 0, - Zone: "lo", } handler := func(conn net.PacketConn, peer net.Addr, m DHCPv6) {} s := NewServer(laddr, handler) @@ -71,7 +85,9 @@ func TestServerActivateAndServe(t *testing.T) { c, s := setUpClientAndServer(handler) defer s.Close() - _, _, err := c.Solicit("lo", nil) + iface, err := getLoopbackInterface() + require.NoError(t, err) + _, _, err = c.Solicit(iface, nil) require.NoError(t, err) } -- cgit v1.2.3