summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_interfaceid_test.go
blob: 640c06a94bbde8afa0809534eaf6e25ac4e37313 (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
package dhcpv6

import (
	"errors"
	"fmt"
	"reflect"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/stretchr/testify/require"
	"github.com/u-root/uio/uio"
)

func TestInterfaceIDParseAndGetter(t *testing.T) {
	for i, tt := range []struct {
		buf  []byte
		err  error
		want []byte
	}{
		{
			buf: []byte{
				0, 18, // Interface ID
				0, 4, // length
				'S', 'L', 'A', 'M',
			},
			want: []byte("SLAM"),
		},
		{
			buf: []byte{
				0, 18,
				0, 0,
			},
		},
		{
			buf: []byte{0, 18, 0},
			err: uio.ErrUnreadBytes,
		},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			var ro RelayOptions
			if err := ro.FromBytes(tt.buf); !errors.Is(err, tt.err) {
				t.Errorf("FromBytes = %v, want %v", err, tt.err)
			}
			if got := ro.InterfaceID(); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("InterfaceID = %v, want %v", got, tt.want)
			}

			if tt.want != nil {
				var m RelayOptions
				m.Add(OptInterfaceID(tt.want))
				got := m.ToBytes()
				if diff := cmp.Diff(tt.buf, got); diff != "" {
					t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
				}
			}
		})
	}
}

func TestOptInterfaceID(t *testing.T) {
	opt := OptInterfaceID([]byte("DSLAM01 eth2/1/01/21"))
	require.Contains(
		t,
		opt.String(),
		"68 83 76 65 77 48 49 32 101 116 104 50 47 49 47 48 49 47 50 49",
		"String() should return the interfaceId as bytes",
	)
}