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

import (
	"testing"

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

var (
	sampleVIVCOpt = OptVIVC{
		Identifiers: []VIVCIdentifier{
			{EntID: 9, Data: []byte("CiscoIdentifier")},
			{EntID: 18, Data: []byte("WellfleetIdentifier")},
		},
	}
	sampleVIVCOptRaw = []byte{
		byte(OptionVendorIdentifyingVendorClass), 44, // option header
		0x0, 0x0, 0x0, 0x9, // enterprise id 9
		0xf, // length
		'C', 'i', 's', 'c', 'o', 'I', 'd', 'e', 'n', 't', 'i', 'f', 'i', 'e', 'r',
		0x0, 0x0, 0x0, 0x12, // enterprise id 18
		0x13, // length
		'W', 'e', 'l', 'l', 'f', 'l', 'e', 'e', 't', 'I', 'd', 'e', 'n', 't', 'i', 'f', 'i', 'e', 'r',
	}
)

func TestOptVIVCInterfaceMethods(t *testing.T) {
	require.Equal(t, OptionVendorIdentifyingVendorClass, sampleVIVCOpt.Code(), "Code")
	require.Equal(t, 44, sampleVIVCOpt.Length(), "Length")
	require.Equal(t, sampleVIVCOptRaw, sampleVIVCOpt.ToBytes(), "ToBytes")
}

func TestParseOptVICO(t *testing.T) {
	o, err := ParseOptVIVC(sampleVIVCOptRaw)
	require.NoError(t, err)
	require.Equal(t, &sampleVIVCOpt, o)

	// Short byte stream
	data := []byte{byte(OptionVendorIdentifyingVendorClass)}
	_, err = ParseOptVIVC(data)
	require.Error(t, err, "should get error from short byte stream")

	// Wrong code
	data = []byte{54, 2, 1, 1}
	_, err = ParseOptVIVC(data)
	require.Error(t, err, "should get error from wrong code")

	// Bad length
	data = []byte{byte(OptionVendorIdentifyingVendorClass), 6, 1, 1, 1}
	_, err = ParseOptVIVC(data)
	require.Error(t, err, "should get error from bad length")

	// Identifier len too long
	data = make([]byte, len(sampleVIVCOptRaw))
	copy(data, sampleVIVCOptRaw)
	data[6] = 40
	_, err = ParseOptVIVC(data)
	require.Error(t, err, "should get error from bad length")

	// Longer than length
	data[1] = 10
	data[6] = 5
	o, err = ParseOptVIVC(data)
	require.NoError(t, err)
	require.Equal(t, o.Identifiers[0].Data, []byte("Cisco"))
}

func TestOptVIVCString(t *testing.T) {
	require.Equal(t, "Vendor-Identifying Vendor Class -> 9:'CiscoIdentifier', 18:'WellfleetIdentifier'",
		sampleVIVCOpt.String())
}