summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_router_test.go
blob: f492c22350e163238c5f94d0eb3e3f3d86642348 (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
package dhcpv4

import (
	"net"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestOptRoutersInterfaceMethods(t *testing.T) {
	routers := []net.IP{
		net.IPv4(192, 168, 0, 10),
		net.IPv4(192, 168, 0, 20),
	}
	o := OptRouter{Routers: routers}
	require.Equal(t, OptionRouter, o.Code(), "Code")
	require.Equal(t, net.IPv4len*len(routers), o.Length(), "Length")
	require.Equal(t, routers, o.Routers, "Routers")
}

func TestParseOptRouter(t *testing.T) {
	data := []byte{
		byte(OptionRouter),
		8,               // Length
		192, 168, 0, 10, // Router #1
		192, 168, 0, 20, // Router #2
	}
	o, err := ParseOptRouter(data)
	require.NoError(t, err)
	routers := []net.IP{
		net.IPv4(192, 168, 0, 10),
		net.IPv4(192, 168, 0, 20),
	}
	require.Equal(t, &OptRouter{Routers: routers}, o)

	// Short byte stream
	data = []byte{byte(OptionRouter)}
	_, err = ParseOptRouter(data)
	require.Error(t, err, "should get error from short byte stream")

	// Wrong code
	data = []byte{54, 2, 1, 1}
	_, err = ParseOptRouter(data)
	require.Error(t, err, "should get error from wrong code")

	// Bad length
	data = []byte{byte(OptionRouter), 6, 1, 1, 1}
	_, err = ParseOptRouter(data)
	require.Error(t, err, "should get error from bad length")
}

func TestParseOptRouterNoRouters(t *testing.T) {
	// RFC2132 requires that at least one Router IP is specified
	data := []byte{
		byte(OptionRouter),
		0, // Length
	}
	_, err := ParseOptRouter(data)
	require.Error(t, err)
}

func TestOptRouterString(t *testing.T) {
	o := OptRouter{Routers: []net.IP{net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10)}}
	require.Equal(t, "Routers -> 192.168.0.1, 192.168.0.10", o.String())
}