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

import (
	"net"
	"testing"

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

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

	require.Equal(t, OptionBroadcastAddress, o.Code(), "Code")
	require.Equal(t, []byte{192, 168, 0, 1}, o.ToBytes(), "ToBytes")
	require.Equal(t, "Broadcast Address -> 192.168.0.1", o.String(), "String")
}

func TestParseOptBroadcastAddress(t *testing.T) {
	o, err := ParseOptBroadcastAddress([]byte{})
	require.Error(t, err, "empty byte stream")

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

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

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

	require.Equal(t, OptionRequestedIPAddress, o.Code(), "Code")
	require.Equal(t, []byte{192, 168, 0, 1}, o.ToBytes(), "ToBytes")
	require.Equal(t, "Requested IP Address -> 192.168.0.1", o.String(), "String")
}

func TestParseOptRequestedIPAddress(t *testing.T) {
	o, err := ParseOptRequestedIPAddress([]byte{})
	require.Error(t, err, "empty byte stream")

	o, err = ParseOptRequestedIPAddress([]byte{192})
	require.Error(t, err, "wrong IP length")

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

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

	require.Equal(t, OptionServerIdentifier, o.Code(), "Code")
	require.Equal(t, []byte{192, 168, 0, 1}, o.ToBytes(), "ToBytes")
	require.Equal(t, "Server Identifier -> 192.168.0.1", o.String(), "String")
}

func TestParseOptServerIdentifier(t *testing.T) {
	o, err := ParseOptServerIdentifier([]byte{})
	require.Error(t, err, "empty byte stream")

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

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