summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_vendor_opts_test.go
blob: 468fea52727a3818fbb586c08474679ba8a1bd5f (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
package dhcpv6

import (
	"testing"

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

func TestOptVendorOpts(t *testing.T) {
	optData := []byte("Arista;DCS-7304;01.00;HSH14425148")
	// NOTE: this should be aware of endianness
	expected := []byte{0xaa, 0xbb, 0xcc, 0xdd}
	expected = append(expected, []byte{0, 1, //code
		0, byte(len(optData)), //length
	}...)
	expected = append(expected, optData...)
	expectedOpts := OptVendorOpts{}
	var vendorOpts []Option
	expectedOpts.VendorOpts = append(vendorOpts, &OptionGeneric{OptionCode: 1, OptionData: optData})
	opt, err := ParseOptVendorOpts(expected)
	require.NoError(t, err)
	require.Equal(t, len(expected), opt.Length())
	require.Equal(t, uint32(0xaabbccdd), opt.EnterpriseNumber)
	require.Equal(t, expectedOpts.VendorOpts, opt.VendorOpts)

	shortData := make([]byte, 1)
	_, err = ParseOptVendorOpts(shortData)
	require.Error(t, err)
}

func TestOptVendorOptsToBytes(t *testing.T) {
	optData := []byte("Arista;DCS-7304;01.00;HSH14425148")
	var opts []Option
	opts = append(opts, &OptionGeneric{OptionCode: 1, OptionData: optData})

	var expected []byte
	expected = append(expected, []byte{0, 17, // VendorOption Code 17
		0, byte(len(optData) + 8), // Length of optionData + 4 (code & length of sub-option) + 4 for EnterpriseNumber Length
		0, 0, 0, 0, // EnterpriseNumber
		0, 1, // Sub-Option code from vendor
		0, byte(len(optData))}...) // Length of optionData only
	expected = append(expected, optData...)

	opt := OptVendorOpts{
		EnterpriseNumber: 0000,
		VendorOpts:       opts,
	}
	toBytes := opt.ToBytes()
	require.Equal(t, expected, toBytes)
}

func TestVendParseOption(t *testing.T) {
	var buf []byte
	buf = append(buf, []byte{00, 1, 00, 33}...)
	buf = append(buf, []byte("Arista;DCS-7304;01.00;HSH14425148")...)

	expected := &OptionGeneric{OptionCode: 1, OptionData: []byte("Arista;DCS-7304;01.00;HSH14425148")}
	opt, err := vendParseOption(buf)
	require.NoError(t, err)
	require.Equal(t, expected, opt)

	shortData := make([]byte, 1) // data length too small
	_, err = vendParseOption(shortData)
	require.Error(t, err)

	shortData = []byte{0, 0, 0, 0} // missing actual vendor data.
	_, err = vendParseOption(shortData)
	require.Error(t, err)

	shortData = []byte{0, 0,
		0, 4, // declared length
		0} // data starts here, length of 1
	_, err = vendParseOption(shortData)
	require.Error(t, err)
}