File: vendoropts_test.go

package info (click to toggle)
golang-github-mdlayher-dhcp6 0.0~git20190311.2a67805-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 400 kB
  • sloc: makefile: 3
file content (110 lines) | stat: -rw-r--r-- 2,369 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
package dhcp6opts

import (
	"bytes"
	"io"
	"reflect"
	"testing"

	"github.com/mdlayher/dhcp6"
)

// TestVendorOptsMarshalBinary verifies that VendorOpts marshals properly for the input values.
func TestVendorOptsMarshalBinary(t *testing.T) {
	var tests = []struct {
		desc       string
		buf        []byte
		vendorOpts *VendorOpts
	}{
		{
			desc:       "all zero values",
			buf:        bytes.Repeat([]byte{0}, 4),
			vendorOpts: &VendorOpts{},
		},
		{
			desc: "[0, 0, 5, 0x58] EnterpriseNumber, [1: []byte{3, 4}, 2: []byte{0x04, 0xa3, 0x9e}] vendorOpts",
			buf: []byte{
				0, 0, 5, 0x58,
				0, 1, 0, 2, 3, 4,
				0, 2, 0, 3, 0x04, 0xa3, 0x9e,
			},
			vendorOpts: &VendorOpts{
				EnterpriseNumber: 1368,
				Options: dhcp6.Options{
					1: [][]byte{
						{3, 4},
					},
					2: [][]byte{
						{0x04, 0xa3, 0x9e},
					},
				},
			},
		},
	}

	for i, tt := range tests {
		want := tt.buf
		got, err := tt.vendorOpts.MarshalBinary()
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(want, got) {
			t.Fatalf("[%02d] test %q, unexpected VendorOpts bytes for MarshalBinary(%v)\n- want: %v\n-  got: %v",
				i, tt.desc, tt.buf, want, got)
		}
	}
}

// TestVendorOptsUnmarshalBinary verifies that VendorOpts unmarshals properly for the input values.
func TestVendorOptsUnmarshalBinary(t *testing.T) {
	var tests = []struct {
		buf        []byte
		vendorOpts *VendorOpts
		err        error
	}{
		{
			buf: bytes.Repeat([]byte{0}, 3),
			err: io.ErrUnexpectedEOF,
		},
		{
			buf: []byte{
				0, 0, 5, 0x58,
				0, 1, 0, 0xa,
			},
			err: dhcp6.ErrInvalidPacket,
		},
		{
			buf: []byte{
				0, 0, 5, 0x58,
				0, 1, 0, 2, 3, 4,
				0, 2, 0, 3, 0x04, 0xa3, 0x9e,
			},
			vendorOpts: &VendorOpts{
				EnterpriseNumber: 1368,
				Options: dhcp6.Options{
					1: [][]byte{
						{3, 4},
					},
					2: [][]byte{
						{0x04, 0xa3, 0x9e},
					},
				},
			},
		},
	}

	for i, tt := range tests {
		vendorOpts := new(VendorOpts)
		if want, got := tt.err, vendorOpts.UnmarshalBinary(tt.buf); want != got {
			t.Fatalf("[%02d] unexpected error for parseVendorOpts(%v):\n- want: %v\n-  got: %v", i, tt.buf, want, got)
		}

		if tt.err != nil {
			continue
		}

		if want, got := tt.vendorOpts, vendorOpts; !reflect.DeepEqual(want, got) {
			t.Fatalf("[%02d] unexpected VendorOpts for parseVendorOpts(%v):\n- want: %v\n-  got: %v", i, tt.buf, want, got)
		}
	}
}