summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_message_type_test.go
blob: 4ba5338c7bda5088197acd07924a92fe01d6eca3 (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
package dhcpv4

import (
	"testing"

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

func TestOptMessageType(t *testing.T) {
	o := OptMessageType(MessageTypeDiscover)
	require.Equal(t, OptionDHCPMessageType, o.Code, "Code")
	require.Equal(t, []byte{1}, o.Value.ToBytes(), "ToBytes")
	require.Equal(t, "DHCP Message Type: DISCOVER", o.String())

	// unknown
	o = OptMessageType(99)
	require.Equal(t, "DHCP Message Type: unknown (99)", o.String())
}

func TestParseOptMessageType(t *testing.T) {
	var m MessageType
	data := []byte{1} // DISCOVER
	err := m.FromBytes(data)
	require.NoError(t, err)
	require.Equal(t, MessageTypeDiscover, m)

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

func TestGetMessageType(t *testing.T) {
	m, _ := New(WithMessageType(MessageTypeDiscover))
	require.Equal(t, MessageTypeDiscover, m.MessageType())

	m, _ = New()
	require.Equal(t, MessageTypeNone, m.MessageType())
}