summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_strings_test.go
blob: cd5d712c17110e8fda349cc21e9019c846f0fd1a (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 (
	"testing"

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

func TestParseStringsMultiple(t *testing.T) {
	var opt Strings
	expected := []byte{
		9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
		4, 't', 'e', 's', 't',
	}
	err := opt.FromBytes(expected)
	require.NoError(t, err)
	require.Equal(t, len(opt), 2)
	require.Equal(t, "linuxboot", opt[0])
	require.Equal(t, "test", opt[1])
}

func TestParseStringsNone(t *testing.T) {
	var opt Strings
	expected := []byte{}
	err := opt.FromBytes(expected)
	require.Error(t, err)
}

func TestParseStrings(t *testing.T) {
	var opt Strings
	expected := []byte{
		9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
	}
	err := opt.FromBytes(expected)
	require.NoError(t, err)
	require.Equal(t, 1, len(opt))
	require.Equal(t, "linuxboot", opt[0])
}

func TestParseStringsZeroLength(t *testing.T) {
	var opt Strings
	err := opt.FromBytes([]byte{0, 0})
	require.Error(t, err)
}

func TestOptRFC3004UserClass(t *testing.T) {
	opt := OptRFC3004UserClass(Strings([]string{"linuxboot"}))
	data := opt.Value.ToBytes()
	expected := []byte{
		9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
	}
	require.Equal(t, expected, data)
}

func TestOptRFC3004UserClassMultiple(t *testing.T) {
	opt := OptRFC3004UserClass(
		[]string{
			"linuxboot",
			"test",
		},
	)
	data := opt.Value.ToBytes()
	expected := []byte{
		9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
		4, 't', 'e', 's', 't',
	}
	require.Equal(t, expected, data)
}