-
Notifications
You must be signed in to change notification settings - Fork 6
/
uuid_test.go
325 lines (270 loc) · 8.58 KB
/
uuid_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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package uuid
import (
"fmt"
"testing"
)
var (
testStringUUID = "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"
testZeroString = "00000000-0000-0000-0000-000000000000"
testByteUUID = []byte("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
testMixed = map[string]validator{
"00000000-0000-0000-0000-00000000000f": valid{"00000000-0000-0000-0000-00000000000f"},
"00000000-0000-0000-0000-0000000000f0": valid{"00000000-0000-0000-0000-0000000000f0"},
"00000000-0000-0000-0000-100000000000": valid{"00000000-0000-0000-0000-100000000000"},
"00000000-0000-0000-f000-000000000000": valid{"00000000-0000-0000-f000-000000000000"},
"00000000-0000-f000-0000-000000000000": valid{"00000000-0000-f000-0000-000000000000"},
"00000000-f000-0000-0000-000000000000": valid{"00000000-f000-0000-0000-000000000000"},
"f0000000-0000-0000-0000-000000000000": valid{"f0000000-0000-0000-0000-000000000000"},
"0f000000-0000-0000-0000-000000000000": valid{"0f000000-0000-0000-0000-000000000000"},
"000f0000-0000-0000-0000-000000000000": valid{"000f0000-0000-0000-0000-000000000000"},
"00000000-00f0-0000-0000-000000000000": valid{"00000000-00f0-0000-0000-000000000000"},
"f0000000-f000-f000-f000-f00000000000": valid{"f0000000-f000-f000-f000-f00000000000"},
"12345678-9abc-deff-edcb-a98765432100": valid{"12345678-9abc-deff-edcb-a98765432100"},
"ffffffff-ffff-ffff-ffff-ffffffffffff": valid{"ffffffff-ffff-ffff-ffff-ffffffffffff"},
"12345678-1234-5678-1234-567812345678": valid{"12345678-1234-5678-1234-567812345678"},
"c56a4180-65aa-42ec-a945-5fd21dec0538": valid{"c56a4180-65aa-42ec-a945-5fd21dec0538"},
"00000000-0000-0000-0000-000000000000": valid{"00000000-0000-0000-0000-000000000000"},
"00000000-0000-0000-0000-00000000000": invalid{},
"": invalid{},
"0": invalid{},
"abcdefg": invalid{},
"fffffff-ffff-ffff-ffff-fffffffffffff": invalid{}, /* all - shifted one to the left */
"fffffffff-ffff-ffff-ffff-fffffffffff": invalid{}, /* all - shifted one to the right */
"ffffffff-ffff-ffff-ffff-fffffffffff": invalid{}, /* one char too short */
"ffffffff-ffff-ffff-ffff-fffffffffffff": invalid{}, /* one char too long */
"ffffffff-ffff-ffff-ffff-ffffffffffffff": invalid{}, /* two char too long */
"ffffffff-ffff-ffff-ffff--fffffffffff": invalid{}, /* one dash instead of char */
"C56A4180-65AA-42EC-A945-5FD21DEC": invalid{},
"x56a4180-h5aa-42ec-a945-5fd21dec0538": invalid{},
"null": invalid{},
"something invalid here": invalid{},
"A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11": valid{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"},
"{a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}": valid{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"},
"a0eebc999c0b4ef8bb6d6bb9bd380a11": valid{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"},
"a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11": valid{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"},
"{a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}": valid{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"},
}
)
// validator validates errors and UUIDs for tests
type validator interface {
ReadError(error) error
Validate(UUID) error
ValidateEmpty(UUID) error
}
// valid is an expected valid UUID read.
type valid struct {
Str string
}
// ReadError validates any errors which might arise when reading the UUID.
func (v valid) ReadError(err error) error {
if err != nil {
return fmt.Errorf("expected '%s', got error: %s", v.Str, err.Error())
}
return nil
}
// Validate validates a UUID with the expectation that it should not validate
// upon read failure.
func (v valid) Validate(u UUID) error {
if v.Str != u.String() {
return fmt.Errorf("string representation '%s' does not match '%s'.", u.String(), v.Str)
}
return nil
}
// ValidateEmpty validates a UUID where an error condition must set the UUID
// to zero.
func (v valid) ValidateEmpty(u UUID) error {
return v.Validate(u)
}
// invalid is an expected failure to read a UUID.
type invalid struct{}
// ReadError validates any errors which might arise when reading the UUID.
func (v invalid) ReadError(err error) error {
if err == nil {
return fmt.Errorf("expected error")
}
return nil
}
// Validate validates a UUID with the expectation that it should not validate
// upon read failure.
func (v invalid) Validate(u UUID) error {
return nil
}
// ValidateEmpty validates a UUID where an error condition must set the UUID
// to zero.
func (v invalid) ValidateEmpty(u UUID) error {
if u != [16]byte{} {
return fmt.Errorf("expected empty UUID, got '%s'", u.String())
}
return nil
}
func TestZero(t *testing.T) {
u := UUID{}
if !u.IsZero() {
t.Error("Default value is not zero")
}
}
func TestZero2(t *testing.T) {
u := UUID{}
if u.String() != testZeroString {
t.Error("Default zero value is not zero uuid")
}
}
func TestFromString(t *testing.T) {
for i, v := range testMixed {
u, err := FromString(i)
if e := v.ReadError(err); e != nil {
t.Errorf("FromString(%s): %s", i, e.Error())
} else if e := v.Validate(u); e != nil {
t.Errorf("FromString(%s): %s", i, e.Error())
}
}
}
func TestFromStringZero(t *testing.T) {
u, err := FromString(testZeroString)
if err != nil {
t.Error("Failed to read zero-uuid")
}
if !u.IsZero() {
t.Error("Zero-uuid from string does not yield zero-uuid")
}
if u.String() != testZeroString {
t.Error("zero uuid is not zero uuid")
}
}
func TestReadBytes(t *testing.T) {
for i, v := range testMixed {
u := UUID{}
err := u.ReadBytes([]byte(i))
if e := v.ReadError(err); e != nil {
t.Errorf("FromString(%s): %s", i, e.Error())
} else if e := v.Validate(u); e != nil {
t.Errorf("FromString(%s): %s", i, e.Error())
}
}
}
func TestSetString(t *testing.T) {
for i, v := range testMixed {
u := UUID{}
err := u.SetString(i)
if e := v.ReadError(err); e != nil {
t.Errorf("SetString(%s): %s", i, e.Error())
} else if e := v.Validate(u); e != nil {
t.Errorf("SetString(%s): %s", i, e.Error())
}
}
}
func TestMaybeFromString(t *testing.T) {
for i, v := range testMixed {
u := MaybeFromString(i)
if e := v.ValidateEmpty(u); e != nil {
t.Errorf("MaybeFromString(%s): %s", i, e.Error())
}
}
}
func TestMustFromString(t *testing.T) {
for i, v := range testMixed {
testMustFromString(t, i, v)
}
}
func testMustFromString(t *testing.T, i string, v validator) {
var u UUID
defer func() {
r := recover()
err, ok := r.(error)
if !ok && r != nil {
panic(fmt.Sprintf("in test for MustFromString: got non-error: %+v", r))
}
if e := v.ReadError(err); e != nil {
t.Errorf("MustFromString(%s): %s", i, e.Error())
}
}()
u = MustFromString(i)
if e := v.Validate(u); e != nil {
t.Errorf("MustFromString(%s): %s", i, e.Error())
}
}
func TestV4(t *testing.T) {
u, err := V4()
if err != nil {
panic(err)
}
if u[6]&0xf0 != 0x40 {
t.Errorf("UUID generated from V4() does not have the version byte set to 4: '%s'.", u.String())
}
if u[8]&0xf0 < 0x80 || u[8]&0xf0 > 0xB0 {
t.Errorf("UUID generated from V4() does not have the 9th byte beginning with 8, 9, A or B: '%s'.", u.String())
}
}
func TestSetZero(t *testing.T) {
u, err := FromString("12345678-9abc-deff-edcb-a98765432100")
if err != nil {
panic(err)
}
if u == [16]byte{} {
panic("zero UUID returned from FromString")
}
u.SetZero()
if u != [16]byte{} {
t.Errorf("SetZero() did not zero UUID")
}
}
func TestVersion(t *testing.T) {
list := map[string]int{
"10a7f7c0-1011-11e5-ad77-0002a5d5c51b": 1,
"22220da4-d863-3451-98ce-02cc7288bf9a": 3,
"ebd435d3-63eb-43c6-8e92-342238da6b58": 4,
"92ce3c5b-5c3e-51e5-8490-2a2334346357": 5,
}
for i, v := range list {
u := MustFromString(i)
if u.Version() != v {
t.Errorf("Version(%s) returned %d, expected %d", i, u.Version(), v)
}
}
}
func BenchmarkFromString(b *testing.B) {
for i := 0; i < b.N; i++ {
FromString("12345678-9abc-deff-edcb-a98765432100")
}
}
func BenchmarkFromString2(b *testing.B) {
for i := 0; i < b.N; i++ {
FromString("123456789abcdeffedcba98765432100")
}
}
func BenchmarkSetString(b *testing.B) {
for i := 0; i < b.N; i++ {
u := UUID{}
u.SetString(testStringUUID)
}
}
func BenchmarkReadBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
u := UUID{}
u.ReadBytes(testByteUUID)
}
}
func BenchmarkString(b *testing.B) {
u, err := FromString("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
if err != nil {
panic(err)
}
for i := 0; i < b.N; i++ {
u.String()
}
}
func BenchmarkV4(b *testing.B) {
for i := 0; i < b.N; i++ {
V4()
}
}
func BenchmarkMaybeFromStringOk(b *testing.B) {
for i := 0; i < b.N; i++ {
MaybeFromString("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
}
}
func BenchmarkMaybeFromStringFail(b *testing.B) {
for i := 0; i < b.N; i++ {
MaybeFromString("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11abcdef")
}
}