summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/server6/server_test.go
diff options
context:
space:
mode:
authorChristopher Koch <chrisko@google.com>2019-03-04 12:47:50 -0800
committerinsomniac <insomniacslk@users.noreply.github.com>2019-04-04 12:49:08 +0100
commit175868d67987770d2d729186f7676e0b98f925df (patch)
tree2d56b1cb28fddd14ad935fa3c41dcc368b31e30c /dhcpv6/server6/server_test.go
parentb40bd52ae58aee37cec9ef81008e24488350c98f (diff)
client6: new async DHCPv6 client like #250.
- Race-condition-averse. - Supports multiple concurrent requests. - Tested. - Requires a fully compatible net.PacketConn. Signed-off-by: Christopher Koch <chrisko@google.com>
Diffstat (limited to 'dhcpv6/server6/server_test.go')
-rw-r--r--dhcpv6/server6/server_test.go69
1 files changed, 33 insertions, 36 deletions
diff --git a/dhcpv6/server6/server_test.go b/dhcpv6/server6/server_test.go
index 3d2a365..05d62cb 100644
--- a/dhcpv6/server6/server_test.go
+++ b/dhcpv6/server6/server_test.go
@@ -1,62 +1,58 @@
package server6
import (
+ "context"
"log"
"net"
"testing"
- "time"
"github.com/insomniacslk/dhcp/dhcpv6"
- "github.com/insomniacslk/dhcp/dhcpv6/client6"
+ "github.com/insomniacslk/dhcp/dhcpv6/nclient6"
"github.com/insomniacslk/dhcp/interfaces"
"github.com/stretchr/testify/require"
)
+type fakeUnconnectedConn struct {
+ *net.UDPConn
+}
+
+func (f fakeUnconnectedConn) WriteTo(b []byte, _ net.Addr) (int, error) {
+ return f.UDPConn.Write(b)
+}
+
+func (f fakeUnconnectedConn) ReadFrom(b []byte) (int, net.Addr, error) {
+ n, err := f.Read(b)
+ return n, nil, err
+}
+
// 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) (*client6.Client, *Server) {
- laddr := net.UDPAddr{
+func setUpClientAndServer(handler Handler) (*nclient6.Client, *Server) {
+ laddr := &net.UDPAddr{
IP: net.ParseIP("::1"),
Port: 0,
}
- s := NewServer(laddr, handler)
- go s.ActivateAndServe()
-
- c := client6.NewClient()
- c.LocalAddr = &net.UDPAddr{
- IP: net.ParseIP("::1"),
- }
- 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,
+ s, err := NewServer(laddr, handler)
+ if err != nil {
+ panic(err)
}
+ go s.Serve()
- return c, s
-}
+ clientConn, err := net.DialUDP("udp6", &net.UDPAddr{IP: net.ParseIP("::1")}, s.conn.LocalAddr().(*net.UDPAddr))
+ if err != nil {
+ panic(err)
+ }
-func TestNewServer(t *testing.T) {
- laddr := net.UDPAddr{
- IP: net.ParseIP("::1"),
- Port: 0,
+ c, err := nclient6.New(net.HardwareAddr{1, 2, 3, 4, 5, 6},
+ nclient6.WithConn(fakeUnconnectedConn{clientConn}))
+ if err != nil {
+ panic(err)
}
- handler := func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) {}
- s := NewServer(laddr, handler)
- defer s.Close()
- require.NotNil(t, s)
- require.Nil(t, s.conn)
- require.Equal(t, laddr, s.localAddr)
- require.NotNil(t, s.Handler)
+ return c, s
}
-func TestServerActivateAndServe(t *testing.T) {
+func TestServer(t *testing.T) {
handler := func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) {
msg := m.(*dhcpv6.Message)
adv, err := dhcpv6.NewAdvertiseFromSolicit(msg)
@@ -68,6 +64,7 @@ func TestServerActivateAndServe(t *testing.T) {
log.Printf("Cannot reply to client: %v", err)
}
}
+
c, s := setUpClientAndServer(handler)
defer s.Close()
@@ -75,6 +72,6 @@ func TestServerActivateAndServe(t *testing.T) {
require.NoError(t, err)
require.NotEqual(t, 0, len(ifaces))
- _, _, err = c.Solicit(ifaces[0].Name)
+ _, err = c.Solicit(context.Background(), dhcpv6.WithRapidCommit)
require.NoError(t, err)
}