summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/server6/server_test.go
blob: f7f70083fca070f2e5960a624171f4ddde7ea53a (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package server6

import (
	"context"
	"log"
	"net"
	"sync"
	"testing"
	"time"

	"github.com/insomniacslk/dhcp/dhcpv6"
	"github.com/insomniacslk/dhcp/dhcpv6/nclient6"
	"github.com/insomniacslk/dhcp/interfaces"
	"github.com/stretchr/testify/require"
)

// Turns a connected UDP conn into an "unconnected" UDP conn.
type unconnectedConn struct {
	*net.UDPConn
}

func (f unconnectedConn) WriteTo(b []byte, _ net.Addr) (int, error) {
	return f.UDPConn.Write(b)
}

func (f unconnectedConn) 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, logger *customLogger) (*nclient6.Client, *Server) {
	laddr := &net.UDPAddr{
		IP:   net.ParseIP("::1"),
		Port: 0,
	}
	s, err := NewServer("", laddr, handler)
	if err != nil {
		panic(err)
	}

	if logger != nil {
		s.logger = logger
	}

	go func() {
		_ = s.Serve()
	}()

	clientConn, err := net.DialUDP("udp6", &net.UDPAddr{IP: net.ParseIP("::1")}, s.conn.LocalAddr().(*net.UDPAddr))
	if err != nil {
		panic(err)
	}

	c, err := nclient6.NewWithConn(unconnectedConn{clientConn}, net.HardwareAddr{1, 2, 3, 4, 5, 6})
	if err != nil {
		panic(err)
	}
	return c, s
}

type customLogger struct {
	tb testing.TB
	called bool
	mux    sync.Mutex
}

func (s *customLogger) Printf(format string, v ...interface{}) {
	s.mux.Lock()
	s.called = true
	s.mux.Unlock()
	s.tb.Logf("===CustomLogger BEGIN===")
	s.tb.Logf(format, v...)
	s.tb.Logf("===CustomLogger END===")
}

func (s *customLogger) PrintMessage(prefix string, message *dhcpv6.Message) {
	s.mux.Lock()
	s.called = true
	s.mux.Unlock()
	s.tb.Logf("===CustomLogger BEGIN===")
	s.tb.Logf("%s: %s", prefix, message)
	s.tb.Logf("===CustomLogger END===")
}

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)
		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, nil)
	defer s.Close()

	ifaces, err := interfaces.GetLoopbackInterfaces()
	require.NoError(t, err)
	require.NotEqual(t, 0, len(ifaces))

	_, err = c.Solicit(context.Background(), dhcpv6.WithRapidCommit)
	require.NoError(t, err)
}

func TestCustomLoggerForServer(t *testing.T) {
	handler := func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) {
		msg := m.(*dhcpv6.Message)
		adv, err := dhcpv6.NewAdvertiseFromSolicit(msg)
		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, &customLogger{
		tb: t,
	})
	defer s.Close()

	ifaces, err := interfaces.GetLoopbackInterfaces()
	require.NoError(t, err)
	require.NotEqual(t, 0, len(ifaces))

	_, err = c.Solicit(context.Background(), dhcpv6.WithRapidCommit)
	require.NoError(t, err)
	go func() {
		time.Sleep(time.Second * 5)
		require.Equal(t, true, s.logger.(*customLogger).called)
	}()
}

func TestServerInstantiationWithCustomLogger(t *testing.T) {
	handler := func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) {
		msg := m.(*dhcpv6.Message)
		adv, err := dhcpv6.NewAdvertiseFromSolicit(msg)
		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)
		}
	}

	laddr := &net.UDPAddr{
		IP:   net.ParseIP("::1"),
		Port: 0,
	}
	s, err := NewServer("", laddr, handler, WithLogger(&customLogger{
		tb: t,
	}))
	if err != nil {
		t.Fatal(err)
	}
	require.NotNil(t, s)
}

func TestServerInstantiationWithSummaryLogger(t *testing.T) {
	handler := func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) {
		msg := m.(*dhcpv6.Message)
		adv, err := dhcpv6.NewAdvertiseFromSolicit(msg)
		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)
		}
	}

	laddr := &net.UDPAddr{
		IP:   net.ParseIP("::1"),
		Port: 0,
	}
	s, err := NewServer("", laddr, handler, WithSummaryLogger())
	if err != nil {
		t.Fatal(err)
	}
	require.NotNil(t, s)
}

func TestServerInstantiationWithDebugLogger(t *testing.T) {
	handler := func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) {
		msg := m.(*dhcpv6.Message)
		adv, err := dhcpv6.NewAdvertiseFromSolicit(msg)
		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)
		}
	}

	laddr := &net.UDPAddr{
		IP:   net.ParseIP("::1"),
		Port: 0,
	}
	s, err := NewServer("", laddr, handler, WithDebugLogger())
	if err != nil {
		t.Fatal(err)
	}
	require.NotNil(t, s)
}