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

import (
	"net"
	"testing"

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

func TestOptBroadcastAddress(t *testing.T) {
	ip := net.IP{192, 168, 0, 1}
	o := OptBroadcastAddress(ip)

	require.Equal(t, OptionBroadcastAddress, o.Code, "Code")
	require.Equal(t, []byte(ip), o.Value.ToBytes(), "ToBytes")
	require.Equal(t, "Broadcast Address: 192.168.0.1", o.String(), "String")
}

func TestGetIPs(t *testing.T) {
	o := Options{102: []byte{}}
	i := GetIPs(optionCode(102), o)
	require.Nil(t, i)

	o = Options{102: []byte{192, 168, 0}}
	i = GetIPs(optionCode(102), o)
	require.Nil(t, i)

	o = Options{102: []byte{192, 168, 0, 1}}
	i = GetIPs(optionCode(102), o)
	require.Equal(t, i, []net.IP{{192, 168, 0, 1}})

	o = Options{102: []byte{192, 168, 0, 1, 192, 168, 0, 2}}
	i = GetIPs(optionCode(102), o)
	require.Equal(t, i, []net.IP{{192, 168, 0, 1}, {192, 168, 0, 2}})
}

func TestParseIP(t *testing.T) {
	var ip IP
	err := ip.FromBytes([]byte{})
	require.Error(t, err, "empty byte stream")

	err = ip.FromBytes([]byte{192, 168, 0})
	require.Error(t, err, "wrong IP length")

	err = ip.FromBytes([]byte{192, 168, 0, 1})
	require.NoError(t, err)
	require.Equal(t, net.IP{192, 168, 0, 1}, net.IP(ip))
}

func TestOptRequestedIPAddress(t *testing.T) {
	ip := net.IP{192, 168, 0, 1}
	o := OptRequestedIPAddress(ip)

	require.Equal(t, OptionRequestedIPAddress, o.Code, "Code")
	require.Equal(t, []byte(ip), o.Value.ToBytes(), "ToBytes")
	require.Equal(t, "Requested IP Address: 192.168.0.1", o.String(), "String")
}

func TestOptServerIdentifier(t *testing.T) {
	ip := net.IP{192, 168, 0, 1}
	o := OptServerIdentifier(ip)

	require.Equal(t, OptionServerIdentifier, o.Code, "Code")
	require.Equal(t, []byte(ip), o.Value.ToBytes(), "ToBytes")
	require.Equal(t, "Server Identifier: 192.168.0.1", o.String(), "String")
}