summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/duid_test.go
blob: 7174dc538d5c24a6f91a9083556f041292766d23 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package dhcpv6

import (
	"bytes"
	"net"
	"reflect"
	"testing"

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

func TestDuidInvalidTooShort(t *testing.T) {
	// too short DUID at all (must be at least 2 bytes)
	_, err := DUIDFromBytes([]byte{0})
	require.Error(t, err)

	// too short DUID_LL (must be at least 4 bytes)
	_, err = DUIDFromBytes([]byte{0, 3, 0xa})
	require.Error(t, err)

	// too short DUID_EN (must be at least 6 bytes)
	_, err = DUIDFromBytes([]byte{0, 2, 0xa, 0xb, 0xc})
	require.Error(t, err)

	// too short DUID_LLT (must be at least 8 bytes)
	_, err = DUIDFromBytes([]byte{0, 1, 0xa, 0xb, 0xc, 0xd, 0xe})
	require.Error(t, err)

	// too short DUID_UUID (must be at least 18 bytes)
	_, err = DUIDFromBytes([]byte{0, 4, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf})
	require.Error(t, err)
}

func TestFromBytes(t *testing.T) {
	for _, tt := range []struct {
		name     string
		buf      []byte
		want     DUID
		stringer string
	}{
		{
			name: "DUID-LLT",
			buf: []byte{
				0, 1, // DUID_LLT
				0, 1, // HwTypeEthernet
				0x01, 0x02, 0x03, 0x04, // time
				0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, // link-layer addr
			},
			want: &DUIDLLT{
				Time:          0x01020304,
				HWType:        iana.HWTypeEthernet,
				LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
			},
			stringer: "DUID-LLT{HWType=Ethernet HWAddr=aa:bb:cc:dd:ee:ff Time=16909060}",
		},
		{
			name: "DUID-LL",
			buf: []byte{
				0, 3, // DUID_LL
				0, 1, // HwTypeEthernet
				0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, // link-layer addr
			},
			want: &DUIDLL{
				HWType:        iana.HWTypeEthernet,
				LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
			},
			stringer: "DUID-LL{HWType=Ethernet HWAddr=aa:bb:cc:dd:ee:ff}",
		},
		{
			name: "DUID-EN",
			buf: []byte{
				0, 2, // DUID_EN
				0, 0, 0, 1, // EnterpriseNumber
				0x66, 0x6f, 0x6f, // "foo"
			},
			want: &DUIDEN{
				EnterpriseNumber:     0x1,
				EnterpriseIdentifier: []byte("foo"),
			},
			stringer: "DUID-EN{EnterpriseNumber=1 EnterpriseIdentifier=foo}",
		},
		{
			name: "DUID-UUID",
			buf: []byte{
				0x00, 0x04, // DUID_UUID
				0x01, 0x02, 0x03, 0x04, // UUID
				0x01, 0x02, 0x03, 0x04, // UUID
				0x01, 0x02, 0x03, 0x04, // UUID
				0x01, 0x02, 0x03, 0x04, // UUID
			},
			want: &DUIDUUID{
				UUID: [16]byte{
					0x01, 0x02, 0x03, 0x04,
					0x01, 0x02, 0x03, 0x04,
					0x01, 0x02, 0x03, 0x04,
					0x01, 0x02, 0x03, 0x04,
				},
			},
			stringer: "DUID-UUID{0x01020304010203040102030401020304}",
		},
		{
			name: "DUIDOpaque",
			buf: []byte{
				0x00, 0x05, // unknown DUID
				0x01, 0x02, 0x03, // Opaque
			},
			want: &DUIDOpaque{
				Type: 0x5,
				Data: []byte{0x01, 0x02, 0x03},
			},
			stringer: "DUID-Opaque{Type=5 Data=0x010203}",
		},
	} {
		t.Run(tt.name, func(t *testing.T) {
			// FromBytes
			got, err := DUIDFromBytes(tt.buf)
			if err != nil {
				t.Errorf("DUIDFromBytes = %v", err)
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("DUIDFromBytes = %v, want %v", got, tt.want)
			}

			// ToBytes
			buf := tt.want.ToBytes()
			if !bytes.Equal(buf, tt.buf) {
				t.Errorf("ToBytes() = %#x, want %#x", buf, tt.buf)
			}

			// Stringer
			s := tt.want.String()
			if s != tt.stringer {
				t.Errorf("String() = %s, want %s", s, tt.stringer)
			}
		})
	}
}

func TestEqual(t *testing.T) {
	for _, tt := range []struct {
		name string
		a    DUID
		b    DUID
		want bool
	}{
		{
			name: "DUID-LL-equal",
			a:    &DUIDLL{HWType: iana.HWTypeEthernet, LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
			b:    &DUIDLL{HWType: iana.HWTypeEthernet, LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
			want: true,
		},
		{
			name: "DUID-LL-not-equal",
			a:    &DUIDLL{HWType: iana.HWTypeEthernet, LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
			b:    &DUIDLL{HWType: iana.HWTypeEthernet, LinkLayerAddr: net.HardwareAddr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}},
			want: false,
		},
		{
			name: "DUID-LL-and-DUID-EN",
			a:    &DUIDLL{HWType: iana.HWTypeEthernet, LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
			b:    &DUIDEN{EnterpriseNumber: 5, EnterpriseIdentifier: []byte("foo")},
			want: false,
		},
		{
			name: "DUID-EN-equal",
			a:    &DUIDEN{EnterpriseNumber: 5, EnterpriseIdentifier: []byte("foo")},
			b:    &DUIDEN{EnterpriseNumber: 5, EnterpriseIdentifier: []byte("foo")},
			want: true,
		},
		{
			name: "DUID-EN-not-equal",
			a:    &DUIDEN{EnterpriseNumber: 5, EnterpriseIdentifier: []byte("foo")},
			b:    &DUIDEN{EnterpriseNumber: 5, EnterpriseIdentifier: []byte("bar")},
			want: false,
		},
		{
			name: "DUID-LLT-equal",
			a:    &DUIDLLT{HWType: iana.HWTypeEthernet, Time: 10, LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
			b:    &DUIDLLT{HWType: iana.HWTypeEthernet, Time: 10, LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
			want: true,
		},
		{
			name: "DUID-LLT-not-equal",
			a:    &DUIDLLT{HWType: iana.HWTypeEthernet, Time: 10, LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
			b:    &DUIDLLT{HWType: iana.HWTypeEthernet, Time: 10, LinkLayerAddr: net.HardwareAddr{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa}},
			want: false,
		},
		{
			name: "DUID-LLT-and-DUID-UUID",
			a:    &DUIDLLT{HWType: iana.HWTypeEthernet, Time: 10, LinkLayerAddr: net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}},
			b:    &DUIDUUID{UUID: [16]byte{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04}},
			want: false,
		},
		{
			name: "DUID-UUID-equal",
			a:    &DUIDUUID{UUID: [16]byte{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04}},
			b:    &DUIDUUID{UUID: [16]byte{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04}},
			want: true,
		},
		{
			name: "DUID-UUID-not-equal",
			a:    &DUIDUUID{UUID: [16]byte{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04}},
			b:    &DUIDUUID{UUID: [16]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
			want: false,
		},
		{
			name: "DUID-UUID-and-DUID-Opaque",
			a:    &DUIDOpaque{Type: 5, Data: []byte{0x1}},
			b:    &DUIDUUID{UUID: [16]byte{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04}},
			want: false,
		},
		{
			name: "DUID-Opaque-equal",
			a:    &DUIDOpaque{Type: 5, Data: []byte{0x1}},
			b:    &DUIDOpaque{Type: 5, Data: []byte{0x1}},
			want: true,
		},
		{
			name: "DUID-Opaque-not-equal",
			a:    &DUIDOpaque{Type: 5, Data: []byte{0x1}},
			b:    &DUIDOpaque{Type: 5, Data: []byte{0x2}},
			want: false,
		},
	} {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.a.Equal(tt.b); got != tt.want {
				t.Errorf("%s.Equal(%s) = %v, want %v", tt.a, tt.b, got, tt.want)
			}

			if got := tt.b.Equal(tt.a); got != tt.want {
				t.Errorf("%s.Equal(%s) = %v, want %v", tt.b, tt.a, got, tt.want)
			}
		})
	}
}