-
Notifications
You must be signed in to change notification settings - Fork 7
/
id_test.go
248 lines (234 loc) · 5.88 KB
/
id_test.go
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
237
238
239
240
241
242
243
244
245
246
247
248
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
//
// SPDX-License-Identifier: Apache-2.0
package wrp
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseDeviceID(t *testing.T) {
assert := assert.New(t)
testData := []struct {
id string
expected DeviceID
prefix string
literalID string
expectsError bool
}{
{
id: "MAC:11:22:33:44:55:66",
expected: "mac:112233445566",
prefix: "mac",
literalID: "112233445566",
}, {
id: "MAC:11aaBB445566",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "mac:11-aa-BB-44-55-66",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "mac:11,aa,BB,44,55,66",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "uuid:anything Goes!",
expected: "uuid:anything Goes!",
prefix: "uuid",
literalID: "anything Goes!",
}, {
id: "dns:anything Goes!",
expected: "dns:anything Goes!",
prefix: "dns",
literalID: "anything Goes!",
}, {
id: "serial:1234",
expected: "serial:1234",
prefix: "serial",
literalID: "1234",
}, {
id: "mac:11-aa-BB-44-55-66/service",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "mac:11-aa-BB-44-55-66/service/",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "MAC:11-aa-BB-44-55-66/service/ignoreMe",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "mac:11-aa-BB-44-55-66/service/foo/bar",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "invalid:a-BB-44-55",
expectsError: true,
}, {
id: "mac:11-aa-BB-44-55",
expectsError: true,
}, {
id: "MAC:invalid45566",
expectsError: true,
}, {
id: "invalid:random stuff",
expectsError: true,
}, {
id: "mac:11223344556w",
expectsError: true,
}, {
id: "mac:481d70187fef",
expected: "mac:481d70187fef",
prefix: "mac",
literalID: "481d70187fef",
}, {
id: "mac:481d70187fef/parodus/tag/test0",
expected: "mac:481d70187fef",
prefix: "mac",
literalID: "481d70187fef",
},
}
for _, record := range testData {
t.Run(record.id, func(t *testing.T) {
id, err := ParseDeviceID(record.id)
assert.Equal(record.expected, id)
assert.Equal(record.expectsError, err != nil)
assert.Equal([]byte(record.expected), id.Bytes())
assert.Equal(record.prefix, id.Prefix())
assert.Equal(record.literalID, id.ID())
})
}
}
func TestParseLocator(t *testing.T) {
tests := []struct {
description string
locator string
want *Locator
str string
expectedErr error
}{
{
description: "cpe locator",
locator: "mac:112233445566",
str: "mac:112233445566",
want: &Locator{
Scheme: "mac",
Authority: "112233445566",
ID: "mac:112233445566",
},
}, {
description: "cpe locator ensure lowercase",
locator: "Mac:112233445566",
str: "mac:112233445566",
want: &Locator{
Scheme: "mac",
Authority: "112233445566",
ID: "mac:112233445566",
},
}, {
description: "locator with service",
locator: "DNS:foo.bar.com/service",
str: "dns:foo.bar.com/service",
want: &Locator{
Scheme: "dns",
Authority: "foo.bar.com",
Service: "service",
},
}, {
description: "locator with service everything",
locator: "event:something/service/ignored",
str: "event:something/service/ignored",
want: &Locator{
Scheme: "event",
Authority: "something",
Service: "service",
Ignored: "/ignored",
},
},
// Validate all the schemes
{
description: "dns scheme",
locator: "dns:foo.bar.com",
str: "dns:foo.bar.com",
want: &Locator{
Scheme: "dns",
Authority: "foo.bar.com",
},
}, {
description: "event scheme",
locator: "event:targetedEvent",
str: "event:targetedEvent",
want: &Locator{
Scheme: "event",
Authority: "targetedEvent",
},
}, {
description: "mac scheme",
locator: "mac:112233445566",
str: "mac:112233445566",
want: &Locator{
Scheme: "mac",
Authority: "112233445566",
ID: "mac:112233445566",
},
}, {
description: "serial scheme",
locator: "serial:AsdfSerial",
str: "serial:AsdfSerial",
want: &Locator{
Scheme: "serial",
Authority: "AsdfSerial",
ID: "serial:AsdfSerial",
},
}, {
description: "uuid scheme",
locator: "uuid:bbee1f69-2f64-4aa9-a422-27d68b40b152",
str: "uuid:bbee1f69-2f64-4aa9-a422-27d68b40b152",
want: &Locator{
Scheme: "uuid",
Authority: "bbee1f69-2f64-4aa9-a422-27d68b40b152",
ID: "uuid:bbee1f69-2f64-4aa9-a422-27d68b40b152",
},
},
// Validate invalid locators are caught
{
description: "empty",
expectedErr: ErrorInvalidLocator,
}, {
description: "invalid scheme",
locator: "invalid:foo.bar.com",
expectedErr: ErrorInvalidLocator,
}, {
description: "invalid mac scheme",
locator: "mac:112invalid66",
expectedErr: ErrorInvalidDeviceName,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
got, err := ParseLocator(tc.locator)
assert.ErrorIs(err, tc.expectedErr)
assert.Equal(tc.want, got)
if tc.want != nil {
assert.Equal(tc.str, got.String())
}
})
}
}
func TestLocatorDeviceID(t *testing.T) {
assert := assert.New(t)
l, err := ParseLocator("mac:112233445566")
assert.NoError(err)
assert.True(l.HasDeviceID())
assert.NotEqual(l.ID, "")
}