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

import (
	"testing"

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

func TestParseOptionGeneric(t *testing.T) {
	// Empty bytestream produces error
	_, err := ParseOptionGeneric([]byte{})
	require.Error(t, err, "error from empty bytestream")
}

func TestOptionGenericCode(t *testing.T) {
	o := OptionGeneric{
		OptionCode: OptionDHCPMessageType,
		Data:       []byte{byte(MessageTypeDiscover)},
	}
	require.Equal(t, OptionDHCPMessageType, o.Code())
}

func TestOptionGenericData(t *testing.T) {
	o := OptionGeneric{
		OptionCode: OptionNameServer,
		Data:       []byte{192, 168, 0, 1},
	}
	require.Equal(t, []byte{192, 168, 0, 1}, o.Data)
}

func TestOptionGenericToBytes(t *testing.T) {
	o := OptionGeneric{
		OptionCode: OptionDHCPMessageType,
		Data:       []byte{byte(MessageTypeDiscover)},
	}
	serialized := o.ToBytes()
	expected := []byte{53, 1, 1}
	require.Equal(t, expected, serialized)
}

func TestOptionGenericToBytesZeroOptions(t *testing.T) {
	o := OptionGeneric{OptionCode: OptionEnd}
	serialized := o.ToBytes()
	expected := []byte{255}
	require.Equal(t, expected, serialized)

	o = OptionGeneric{OptionCode: OptionPad}
	serialized = o.ToBytes()
	expected = []byte{0}
	require.Equal(t, expected, serialized)
}

func TestOptionGenericString(t *testing.T) {
	o := OptionGeneric{
		OptionCode: OptionDHCPMessageType,
		Data:       []byte{byte(MessageTypeDiscover)},
	}
	require.Equal(t, "DHCP Message Type -> [1]", o.String())
}

func TestOptionGenericStringUnknown(t *testing.T) {
	o := OptionGeneric{
		OptionCode: 102, // Returend option code.
		Data:       []byte{byte(MessageTypeDiscover)},
	}
	require.Equal(t, "Unknown -> [1]", o.String())
}

func TestOptionGenericLength(t *testing.T) {
	filename := "/path/to/file"
	o := OptionGeneric{
		OptionCode: OptionBootfileName,
		Data:       []byte(filename),
	}
	require.Equal(t, len(filename), o.Length())
}