summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_ips_test.go
blob: 05b29393f3e6482c6935b213e4c639005ea9c12d (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
package dhcpv4

import (
	"net"
	"testing"

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

func TestParseIPs(t *testing.T) {
	var i IPs
	data := []byte{
		192, 168, 0, 10, // DNS #1
		192, 168, 0, 20, // DNS #2
	}
	err := i.FromBytes(data)
	require.NoError(t, err)
	servers := []net.IP{
		net.IP{192, 168, 0, 10},
		net.IP{192, 168, 0, 20},
	}
	require.Equal(t, servers, []net.IP(i))

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

	// RFC2132 requires that at least one IP is specified for each IP field.
	err = i.FromBytes([]byte{})
	require.Error(t, err)
}

func TestOptDomainNameServer(t *testing.T) {
	o := OptDNS(net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10))
	require.Equal(t, OptionDomainNameServer, o.Code)
	require.Equal(t, []byte{192, 168, 0, 1, 192, 168, 0, 10}, o.Value.ToBytes())
	require.Equal(t, "Domain Name Server: 192.168.0.1, 192.168.0.10", o.String())
}

func TestGetDomainNameServer(t *testing.T) {
	ips := []net.IP{
		net.IP{192, 168, 0, 1},
		net.IP{192, 168, 0, 10},
	}
	m, _ := New(WithOption(OptDNS(ips...)))
	require.Equal(t, ips, m.DNS())

	m, _ = New()
	require.Nil(t, m.DNS())
}

func TestOptNTPServers(t *testing.T) {
	o := OptNTPServers(net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10))
	require.Equal(t, OptionNTPServers, o.Code)
	require.Equal(t, []byte{192, 168, 0, 1, 192, 168, 0, 10}, o.Value.ToBytes())
	require.Equal(t, "NTP Servers: 192.168.0.1, 192.168.0.10", o.String())
}

func TestGetNTPServers(t *testing.T) {
	ips := []net.IP{
		net.IP{192, 168, 0, 1},
		net.IP{192, 168, 0, 10},
	}
	m, _ := New(WithOption(OptNTPServers(ips...)))
	require.Equal(t, ips, m.NTPServers())

	m, _ = New()
	require.Nil(t, m.NTPServers())
}

func TestOptRouter(t *testing.T) {
	o := OptRouter(net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10))
	require.Equal(t, OptionRouter, o.Code)
	require.Equal(t, []byte{192, 168, 0, 1, 192, 168, 0, 10}, o.Value.ToBytes())
	require.Equal(t, "Router: 192.168.0.1, 192.168.0.10", o.String())
}

func TestGetRouter(t *testing.T) {
	ips := []net.IP{
		net.IP{192, 168, 0, 1},
		net.IP{192, 168, 0, 10},
	}
	m, _ := New(WithOption(OptRouter(ips...)))
	require.Equal(t, ips, m.Router())

	m, _ = New()
	require.Nil(t, m.Router())
}