summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/server_test.go
blob: 587e249045fe39c14f5976c756ea726f8efafec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package dhcpv6

import (
	"log"
	"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,
	}
	s := NewServer(laddr, handler)
	go s.ActivateAndServe()

	c := 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,
	}

	return c, s
}

func TestNewServer(t *testing.T) {
	laddr := net.UDPAddr{
		IP:   net.ParseIP("::1"),
		Port: 0,
	}
	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.NotNil(t, s.Handler)
}

func TestServerActivateAndServe(t *testing.T) {
	handler := func(conn net.PacketConn, peer net.Addr, m DHCPv6) {
		adv, err := NewAdvertiseFromSolicit(m)
		if err != nil {
			log.Printf("NewAdvertiseFromSolicit failed: %v", err)
			return
		}
		if _, err := conn.WriteTo(adv.ToBytes(), peer); err != nil {
			log.Printf("Cannot reply to client: %v", err)
		}
	}
	c, s := setUpClientAndServer(handler)
	defer s.Close()

	iface, err := getLoopbackInterface()
	require.NoError(t, err)

	_, _, err = c.Solicit(iface, nil)
	require.NoError(t, err)
}