diff options
Diffstat (limited to 'dhcpv4/server4/server_test.go')
-rw-r--r-- | dhcpv4/server4/server_test.go | 77 |
1 files changed, 34 insertions, 43 deletions
diff --git a/dhcpv4/server4/server_test.go b/dhcpv4/server4/server_test.go index f6ce18e..a6895d2 100644 --- a/dhcpv4/server4/server_test.go +++ b/dhcpv4/server4/server_test.go @@ -1,8 +1,9 @@ -// +build integration +// +build go1.12 package server4 import ( + "context" "log" "math/rand" "net" @@ -10,7 +11,7 @@ import ( "time" "github.com/insomniacslk/dhcp/dhcpv4" - "github.com/insomniacslk/dhcp/dhcpv4/client4" + "github.com/insomniacslk/dhcp/dhcpv4/nclient4" "github.com/insomniacslk/dhcp/interfaces" "github.com/stretchr/testify/require" ) @@ -61,68 +62,58 @@ func DORAHandler(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4) { // 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) (*client4.Client, *Server) { +func setUpClientAndServer(t *testing.T, iface net.Interface, handler Handler) (*nclient4.Client, *Server) { // strong assumption, I know loAddr := net.ParseIP("127.0.0.1") - laddr := net.UDPAddr{ + saddr := net.UDPAddr{ IP: loAddr, Port: randPort(), } - s := NewServer(laddr, handler) - go s.ActivateAndServe() - - c := client4.NewClient() - // FIXME this doesn't deal well with raw sockets, the actual 0 will be used - // in the UDP header as source port - c.LocalAddr = &net.UDPAddr{IP: loAddr, Port: randPort()} - for { - if s.LocalAddr() != nil { - break - } - time.Sleep(10 * time.Millisecond) - log.Printf("Waiting for server to run...") + caddr := net.UDPAddr{ + IP: loAddr, + Port: randPort(), } - c.RemoteAddr = s.LocalAddr() - log.Printf("Client.RemoteAddr: %s", c.RemoteAddr) - - return c, s -} - -func TestNewServer(t *testing.T) { - laddr := net.UDPAddr{ - IP: net.ParseIP("127.0.0.1"), - Port: 0, + s, err := NewServer(&saddr, handler) + if err != nil { + t.Fatal(err) } - s := NewServer(laddr, DORAHandler) - defer s.Close() + go s.Serve() - require.NotNil(t, s) - require.Nil(t, s.conn) - require.Equal(t, laddr, s.localAddr) - require.NotNil(t, s.Handler) + clientConn, err := nclient4.NewIPv4UDPConn("", caddr.Port) + if err != nil { + t.Fatal(err) + } + c := nclient4.NewWithConn(clientConn, iface.HardwareAddr, nclient4.WithServerAddr(&saddr)) + return c, s } -func TestServerActivateAndServe(t *testing.T) { - c, s := setUpClientAndServer(DORAHandler) - defer s.Close() - +func TestServer(t *testing.T) { ifaces, err := interfaces.GetLoopbackInterfaces() require.NoError(t, err) require.NotEqual(t, 0, len(ifaces)) + // lo has a HardwareAddr of "nil". The client will drop all packets + // that don't match the HWAddr of the client interface. + hwaddr := net.HardwareAddr{1, 2, 3, 4, 5, 6} + ifaces[0].HardwareAddr = hwaddr + + c, s := setUpClientAndServer(t, ifaces[0], DORAHandler) + defer func() { + require.Nil(t, s.Close()) + }() + xid := dhcpv4.TransactionID{0xaa, 0xbb, 0xcc, 0xdd} - hwaddr := net.HardwareAddr{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf} modifiers := []dhcpv4.Modifier{ dhcpv4.WithTransactionID(xid), - dhcpv4.WithHwAddr(hwaddr), + dhcpv4.WithHwAddr(ifaces[0].HardwareAddr), } - conv, err := c.Exchange(ifaces[0].Name, modifiers...) + offer, ack, err := c.Request(context.Background(), modifiers...) require.NoError(t, err) - require.Equal(t, 4, len(conv)) - for _, p := range conv { + require.NotNil(t, offer, ack) + for _, p := range []*dhcpv4.DHCPv4{offer, ack} { require.Equal(t, xid, p.TransactionID) - require.Equal(t, hwaddr, p.ClientHWAddr) + require.Equal(t, ifaces[0].HardwareAddr, p.ClientHWAddr) } } |