summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_nii_test.go
blob: a355122f8df2725d3ac81e765dc9fe3ba707e425 (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
package dhcpv6

import (
	"testing"

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

func TestOptNetworkInterfaceIdParse(t *testing.T) {
	expected := []byte{
		1,  // type (UNDI)
		3,  // major revision
		20, // minor revision
	}
	opt, err := ParseOptNetworkInterfaceId(expected)
	require.NoError(t, err, "ParseOptNetworkInterfaceId() should not return an error with correct bytes")
	require.Equal(t, OptionNII, opt.Code(), OptionNII, "Code() should return 62 for OptNetworkInterfaceId")
	require.Equal(t, uint8(1), opt.Type(), "Type() should return 1 for UNDI")
	require.Equal(t, uint8(3), opt.Major(), "Major() should return 1 for UNDI")
	require.Equal(t, uint8(20), opt.Minor(), "Minor() should return 1 for UNDI")
}

func TestOptNetworkInterfaceIdToBytes(t *testing.T) {
	expected := []byte{
		1,  // type (UNDI)
		3,  // major revision
		20, // minor revision
	}
	opt := OptNetworkInterfaceId{}
	opt.SetType(1)
	opt.SetMajor(3)
	opt.SetMinor(20)
	require.Equal(t, expected, opt.ToBytes())
}

func TestOptNetworkInterfaceIdTooShort(t *testing.T) {
	buf := []byte{
		1, // type (UNDI)
		// missing major/minor revision bytes
	}
	_, err := ParseOptNetworkInterfaceId(buf)
	require.Error(t, err, "ParseOptNetworkInterfaceId() should return an error on truncated options")
}

func TestOptNetworkInterfaceIdString(t *testing.T) {
	buf := []byte{
		1,  // type (UNDI)
		3,  // major revision
		20, // minor revision
	}
	opt, err := ParseOptNetworkInterfaceId(buf)
	require.NoError(t, err)
	require.Contains(
		t,
		opt.String(),
		"type=First gen. PXE boot ROMs, revision=3.20",
		"String() should contain the type and revision",
	)
	opt.SetType(200)
	require.Contains(
		t, opt.String(),
		"type=Unknown",
		"String() should contain unknown for an unknown type",
	)
}