diff options
author | Andrea Barberio <insomniac@slackware.it> | 2018-09-27 14:26:43 +0100 |
---|---|---|
committer | Andrea Barberio <insomniac@slackware.it> | 2018-09-27 14:26:43 +0100 |
commit | 11ae3a9e108ac03ff64d2156af4127ed503c28ee (patch) | |
tree | 6b6e189805c4b95b7e837dac8eefb927beead5b1 /dhcpv6/server_test.go | |
parent | de8157f8da4ae7516c90d7846cb327daa209f579 (diff) |
Async read and tests
Diffstat (limited to 'dhcpv6/server_test.go')
-rw-r--r-- | dhcpv6/server_test.go | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/dhcpv6/server_test.go b/dhcpv6/server_test.go index 3794a4f..4fc919b 100644 --- a/dhcpv6/server_test.go +++ b/dhcpv6/server_test.go @@ -1,21 +1,73 @@ package dhcpv6 import ( + "log" "net" "testing" + "time" "github.com/stretchr/testify/require" ) +// 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() + + c := NewClient() + c.LocalAddr = &net.UDPAddr{ + IP: net.ParseIP("::1"), + Zone: "lo", + } + for { + if s.LocalAddr() != nil { + break + } + time.Sleep(10 * time.Millisecond) + log.Printf("Waiting for server to run...") + } + c.RemoteAddr = &net.UDPAddr{ + IP: net.ParseIP("::1"), + Port: s.LocalAddr().(*net.UDPAddr).Port, + Zone: "lo", + } + + return c, s +} + 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) + defer s.Close() + require.NotNil(t, s) require.Nil(t, s.conn) - require.Equal(t, laddr, s.LocalAddr) + require.Equal(t, laddr, s.localAddr) require.NotNil(t, s.Handler) } + +func TestServerActivateAndServe(t *testing.T) { + handler := func(conn net.PacketConn, peer net.Addr, m DHCPv6) { + log.Printf("MESSAGE from %s, reply with %v", peer, m.ToBytes()) + if _, err := conn.WriteTo(m.ToBytes(), peer); err != nil { + log.Printf("Cannot reply to client: %v", err) + } + } + c, s := setUpClientAndServer(handler) + defer s.Close() + + _, _, err := c.Solicit("lo", nil) + + require.NoError(t, err) +} |