summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_domainsearchlist_test.go
blob: 0b4b6b09a430787e09e9bf620d734d980f292c3b (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
package dhcpv6

import (
	"testing"

	"github.com/insomniacslk/dhcp/rfc1035label"
	"github.com/stretchr/testify/require"
)

func TestParseOptDomainSearchList(t *testing.T) {
	data := []byte{
		7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
		6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'o', 'r', 'g', 0,
	}
	opt, err := ParseOptDomainSearchList(data)
	require.NoError(t, err)
	require.Equal(t, OptionDomainSearchList, opt.Code())
	require.Equal(t, 2, len(opt.DomainSearchList.Labels))
	require.Equal(t, len(data), opt.DomainSearchList.Length())
	require.Equal(t, "example.com", opt.DomainSearchList.Labels[0])
	require.Equal(t, "subnet.example.org", opt.DomainSearchList.Labels[1])
	require.Contains(t, opt.String(), "searchlist=[example.com subnet.example.org]", "String() should contain the correct domain search output")
}

func TestOptDomainSearchListToBytes(t *testing.T) {
	expected := []byte{
		0, 24, // OptionDomainSearchList
		0, 33, // length
		7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
		6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'o', 'r', 'g', 0,
	}
	opt := OptDomainSearchList{
		DomainSearchList: &rfc1035label.Labels{
			Labels: []string{
				"example.com",
				"subnet.example.org",
			},
		},
	}
	require.Equal(t, expected, opt.ToBytes())
}

func TestParseOptDomainSearchListInvalidLength(t *testing.T) {
	data := []byte{
		7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
		6, 's', 'u', 'b', 'n', 'e', 't', 7, 'e', // truncated
	}
	_, err := ParseOptDomainSearchList(data)
	require.Error(t, err, "A truncated OptDomainSearchList should return an error")
}