summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_ips_test.go
blob: 5efd5375b81750c6072cb90b210a87c63be087cb (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
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, 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[2:])
	require.NoError(t, err)
	routers := []net.IP{
		net.IP{192, 168, 0, 10},
		net.IP{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")
}

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.IP{192, 168, 0, 1}, net.IP{192, 168, 0, 10}}}
	require.Equal(t, "Routers -> 192.168.0.1, 192.168.0.10", o.String())
}

func TestOptDomainNameServerInterfaceMethods(t *testing.T) {
	servers := []net.IP{
		net.IPv4(192, 168, 0, 10),
		net.IPv4(192, 168, 0, 20),
	}
	o := OptDomainNameServer{NameServers: servers}
	require.Equal(t, OptionDomainNameServer, o.Code(), "Code")
	require.Equal(t, servers, o.NameServers, "NameServers")
}

func TestParseOptDomainNameServer(t *testing.T) {
	data := []byte{
		byte(OptionDomainNameServer),
		8,               // Length
		192, 168, 0, 10, // DNS #1
		192, 168, 0, 20, // DNS #2
	}
	o, err := ParseOptDomainNameServer(data[2:])
	require.NoError(t, err)
	servers := []net.IP{
		net.IP{192, 168, 0, 10},
		net.IP{192, 168, 0, 20},
	}
	require.Equal(t, &OptDomainNameServer{NameServers: servers}, o)

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

func TestParseOptDomainNameServerNoServers(t *testing.T) {
	// RFC2132 requires that at least one DNS server IP is specified
	_, err := ParseOptDomainNameServer([]byte{})
	require.Error(t, err)
}

func TestOptDomainNameServerString(t *testing.T) {
	o := OptDomainNameServer{NameServers: []net.IP{net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10)}}
	require.Equal(t, "Domain Name Servers -> 192.168.0.1, 192.168.0.10", o.String())
}

func TestOptNTPServersInterfaceMethods(t *testing.T) {
	ntpServers := []net.IP{
		net.IPv4(192, 168, 0, 10),
		net.IPv4(192, 168, 0, 20),
	}
	o := OptNTPServers{NTPServers: ntpServers}
	require.Equal(t, OptionNTPServers, o.Code(), "Code")
	require.Equal(t, ntpServers, o.NTPServers, "NTPServers")
}

func TestParseOptNTPServers(t *testing.T) {
	data := []byte{
		byte(OptionNTPServers),
		8,               // Length
		192, 168, 0, 10, // NTP server #1
		192, 168, 0, 20, // NTP server #2
	}
	o, err := ParseOptNTPServers(data[2:])
	require.NoError(t, err)
	ntpServers := []net.IP{
		net.IP{192, 168, 0, 10},
		net.IP{192, 168, 0, 20},
	}
	require.Equal(t, &OptNTPServers{NTPServers: ntpServers}, o)

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

func TestParseOptNTPserversNoNTPServers(t *testing.T) {
	// RFC2132 requires that at least one NTP server IP is specified
	_, err := ParseOptNTPServers([]byte{})
	require.Error(t, err)
}

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