summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_vendor_opts_test.go
blob: 8e62506c7b515713091018fa04ab05d17049cc59 (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
96
97
98
99
100
101
package dhcpv6

import (
	"bytes"
	"fmt"
	"reflect"
	"testing"
)

func TestOptVendorOpts(t *testing.T) {
	optData := []byte("Arista;DCS-7304;01.00;HSH14425148")
	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)
	if err != nil {
		t.Fatal(err)
	}

	if optLen := opt.Length(); optLen != len(expected) {
		t.Fatalf("Invalid length. Expected %v, got %v", len(expected), optLen)
	}
	if en := opt.EnterpriseNumber; en != 0xaabbccdd {
		t.Fatalf("Invalid Enterprise Number. Expected 0xaabbccdd, got %v", en)
	}
	if !reflect.DeepEqual(opt.VendorOpts, expectedOpts.VendorOpts) {
		t.Fatalf("Invalid Vendor Option Data. Expected %v, got %v", expected, expectedOpts.VendorOpts)
	}

	shortData := make([]byte, 1)
	opt, err = ParseOptVendorOpts(shortData)
	if err == nil {
		t.Fatalf("Short data (<4 bytes) did not cause an error when it should have")
	}

}

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()
	if !bytes.Equal(toBytes, expected) {
		t.Fatalf("Invalid ToBytes result. Expected %v, got %v", 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)
	if err != nil {
		fmt.Println(err)
	}
	if !reflect.DeepEqual(opt, expected) {
		t.Fatalf("Invalid Vendor Parse Option result. Expected %v, got %v", expected, opt)
	}


	shortData := make([]byte, 1) // data length too small
	opt, err = vendParseOption(shortData)
	if err == nil {
		t.Fatalf("Short data (<4 bytes) did not cause an error when it should have")
	}

	shortData = []byte{0, 0, 0, 0} // missing actual vendor data.
	opt, err = vendParseOption(shortData)
	if err == nil {
		t.Fatalf("Missing VendorData option. An error should have been returned but wasn't")
	}

	shortData = []byte{0, 0,
		0, 4, // declared length
		0} // data starts here, length of 1
	opt, err = vendParseOption(shortData)
	if err == nil {
		t.Fatalf("Declared length does not match actual data length. An error should have been returned but wasn't")
	}

}