summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_vendorclass_test.go
blob: 7774b252edca5080dd9962be20f747b9ef5f6565 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package dhcpv6

import (
	"testing"

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

func TestParseOptVendorClass(t *testing.T) {
	data := []byte{
		0xaa, 0xbb, 0xcc, 0xdd, // EnterpriseNumber
		0, 10, 'H', 'T', 'T', 'P', 'C', 'l', 'i', 'e', 'n', 't',
		0, 4, 't', 'e', 's', 't',
	}
	opt, err := ParseOptVendorClass(data)
	require.NoError(t, err)
	require.Equal(t, OptionVendorClass, opt.Code())
	require.Equal(t, 2, len(opt.Data))
	require.Equal(t, uint32(0xaabbccdd), opt.EnterpriseNumber)
	require.Equal(t, []byte("HTTPClient"), opt.Data[0])
	require.Equal(t, []byte("test"), opt.Data[1])
}

func TestOptVendorClassToBytes(t *testing.T) {
	opt := OptVendorClass{
		EnterpriseNumber: uint32(0xaabbccdd),
		Data: [][]byte{
			[]byte("HTTPClient"),
			[]byte("test"),
		},
	}
	data := opt.ToBytes()
	expected := []byte{
		0, 16, // OptionVendorClass
		0, 22, // length
		0xaa, 0xbb, 0xcc, 0xdd, // EnterpriseNumber
		0, 10, 'H', 'T', 'T', 'P', 'C', 'l', 'i', 'e', 'n', 't',
		0, 4, 't', 'e', 's', 't',
	}
	require.Equal(t, expected, data)
}

func TestOptVendorClassParseOptVendorClassMalformed(t *testing.T) {
	buf := []byte{
		0xaa, 0xbb, // truncated EnterpriseNumber
	}
	_, err := ParseOptVendorClass(buf)
	require.Error(t, err, "ParseOptVendorClass() should error if given truncated EnterpriseNumber")

	buf = []byte{
		0xaa, 0xbb, 0xcc, 0xdd, // EnterpriseNumber
	}
	_, err = ParseOptVendorClass(buf)
	require.Error(t, err, "ParseOptVendorClass() should error if given no vendor classes")

	buf = []byte{
		0xaa, 0xbb, 0xcc, 0xdd, // EnterpriseNumber
		0, 9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
		0, 4, 't', 'e',
	}
	_, err = ParseOptVendorClass(buf)
	require.Error(t, err, "ParseOptVendorClass() should error if given truncated vendor classes")

	buf = []byte{
		0xaa, 0xbb, 0xcc, 0xdd, // EnterpriseNumber
		0, 9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
		0,
	}
	_, err = ParseOptVendorClass(buf)
	require.Error(t, err, "ParseOptVendorClass() should error if given a truncated length")
}

func TestOptVendorClassString(t *testing.T) {
	data := []byte{
		0xaa, 0xbb, 0xcc, 0xdd, // EnterpriseNumber
		0, 9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't',
		0, 4, 't', 'e', 's', 't',
	}
	opt, err := ParseOptVendorClass(data)
	require.NoError(t, err)

	str := opt.String()
	require.Contains(
		t,
		str,
		"enterprisenum=2864434397",
		"String() should contain the enterprisenum",
	)
	require.Contains(
		t,
		str,
		"data=[linuxboot, test]",
		"String() should contain the list of vendor classes",
	)
}