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

import (
	"bytes"
	"testing"

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

func TestOptRemoteId(t *testing.T) {
	expected := []byte{0xaa, 0xbb, 0xcc, 0xdd}
	remoteId := []byte("DSLAM01 eth2/1/01/21")
	expected = append(expected, remoteId...)
	opt, err := ParseOptRemoteId(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 rid := opt.RemoteID(); !bytes.Equal(rid, remoteId) {
		t.Fatalf("Invalid Remote ID. Expected %v, got %v", expected, rid)
	}
}

func TestOptRemoteIdToBytes(t *testing.T) {
	remoteId := []byte("DSLAM01 eth2/1/01/21")
	expected := []byte{00, 37, 00, byte(len(remoteId) + 4), 00, 00, 00, 00}
	expected = append(expected, remoteId...)
	opt := OptRemoteId{
		remoteId: remoteId,
	}
	toBytes := opt.ToBytes()
	if !bytes.Equal(toBytes, expected) {
		t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes)
	}
}

func TestOptRemoteIdSet(t *testing.T) {
	enterpriseNumber := uint32(12345)
	remoteID := []byte("DSLAM01 eth2/1/01/21")
	opt := OptRemoteId{}
	opt.SetEnterpriseNumber(enterpriseNumber)
	opt.SetRemoteID(remoteID)

	require.Equal(t, uint32(12345), opt.EnterpriseNumber())
	require.Equal(t, []byte("DSLAM01 eth2/1/01/21"), opt.RemoteID())
}

func TestOptRemoteIdParseOptRemoteIdTooShort(t *testing.T) {
	buf := []byte{0xaa, 0xbb, 0xcc}
	_, err := ParseOptRemoteId(buf)
	require.Error(t, err, "A short option should return an error")
}

func TestOptRemoteIdString(t *testing.T) {
	buf := []byte{0xaa, 0xbb, 0xcc, 0xdd}
	remoteId := []byte("Test1234")
	buf = append(buf, remoteId...)

	opt, err := ParseOptRemoteId(buf)
	require.NoError(t, err)
	str := opt.String()
	require.Contains(
		t,
		str,
		"enterprisenum=2864434397",
		"String() should contain the enterprisenum",
	)
	require.Contains(
		t,
		str,
		"remoteid=[84 101 115 116 49 50 51 52]",
		"String() should contain the remoteid bytes",
	)
}