summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_serverid_test.go
blob: bb4a928380b0632eeabf490b63d4e4fa729265bd (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
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
111
112
113
114
115
116
117
118
119
package dhcpv6

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

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

func TestServerIDParseAndGetter(t *testing.T) {
	for i, tt := range []struct {
		buf  []byte
		err  error
		want DUID
	}{
		{
			buf: []byte{
				0, 2, // Server ID option
				0, 10, // length
				0, 3, // DUID_LL
				0, 1, // hwtype ethernet
				0, 1, 2, 3, 4, 5, // HW addr
			},
			want: &DUIDLL{HWType: iana.HWTypeEthernet, LinkLayerAddr: net.HardwareAddr{0, 1, 2, 3, 4, 5}},
		},
		{
			buf:  nil,
			want: nil,
		},
		{
			buf:  []byte{0, 1, 0, 1, 0},
			want: nil,
			err:  uio.ErrBufferTooShort,
		},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			var mo MessageOptions
			if err := mo.FromBytes(tt.buf); !errors.Is(err, tt.err) {
				t.Errorf("FromBytes = %v, want %v", err, tt.err)
			}
			if got := mo.ServerID(); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("ServerID = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestServerID(t *testing.T) {
	for i, tt := range []struct {
		buf  []byte
		want optServerID
		err  error
	}{
		{
			buf: []byte{
				0, 3, // DUID_LL
				0, 1, // hwtype ethernet
				0, 1, 2, 3, 4, 5, // hw addr
			},
			want: optServerID{
				&DUIDLL{
					HWType:        iana.HWTypeEthernet,
					LinkLayerAddr: net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5}),
				},
			},
		},
		{
			buf: []byte{0},
			err: uio.ErrBufferTooShort,
		},
		{
			buf: []byte{0, 3, 0},
			err: uio.ErrBufferTooShort,
		},
		{
			buf: nil,
			err: uio.ErrBufferTooShort,
		},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			var opt optServerID
			if err := opt.FromBytes(tt.buf); !errors.Is(err, tt.err) {
				t.Errorf("FromBytes = %v, want %v", err, tt.err)
			}
			if tt.err == nil {
				if !reflect.DeepEqual(tt.want, opt) {
					t.Errorf("FromBytes = %v, want %v", opt, tt.want)
				}

				out := tt.want.ToBytes()
				if diff := cmp.Diff(tt.buf, out); diff != "" {
					t.Errorf("ToBytes mismatch: (-want, +got):\n%s", diff)
				}
			}
		})
	}
}

func TestOptionServerIDString(t *testing.T) {
	opt := OptServerID(
		&DUIDLL{
			HWType:        iana.HWTypeEthernet,
			LinkLayerAddr: net.HardwareAddr([]byte{0xde, 0xad, 0, 0, 0xbe, 0xef}),
		},
	)
	require.Equal(t, OptionServerID, opt.Code())
	require.Contains(
		t,
		opt.String(),
		"Server ID: DUID-LL{HWType=Ethernet HWAddr=de:ad:00:00:be:ef}",
		"String() should contain the correct cid output",
	)
}