-
Notifications
You must be signed in to change notification settings - Fork 30
/
uuid_test.go
430 lines (383 loc) · 11.1 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package uuid
/****************
* Date: 3/02/14
* Time: 10:59 PM
***************/
import (
"crypto/md5"
"crypto/sha1"
"fmt"
"testing"
"regexp"
)
const (
secondsPerHour = 60 * 60
secondsPerDay = 24 * secondsPerHour
unixToInternal int64 = (1969 * 365 + 1969 / 4 - 1969 / 100 + 1969 / 400) * secondsPerDay
internalToUnix int64 = -unixToInternal
)
var uuid_goLang Name = "https://google.com/golang.org?q=golang"
var printer bool = false
var uuid_bytes = []byte{
0xAA, 0xCF, 0xEE, 0x12,
0xD4, 0x00,
0x27, 0x23,
0x00,
0xD3,
0x23, 0x12, 0x4A, 0x11, 0x89, 0xFF,
}
var uuid_variants = []byte{
ReservedNCS, ReservedRFC4122, ReservedMicrosoft, ReservedFuture,
}
var namespaceUuids = []UUID {
NamespaceDNS, NamespaceURL, NamespaceOID, NamespaceX500,
}
var invalidHexStrings = [...]string{
"foo",
"6ba7b814-9dad-11d1-80b4-",
"6ba7b814--9dad-11d1-80b4--00c04fd430c8",
"6ba7b814-9dad7-11d1-80b4-00c04fd430c8999",
"{6ba7b814-9dad-1180b4-00c04fd430c8",
"{6ba7b814--11d1-80b4-00c04fd430c8}",
"urn:uuid:6ba7b814-9dad-1666666680b4-00c04fd430c8",
}
var validHexStrings = [...]string{
"6ba7b8149dad-11d1-80b4-00c04fd430c8}",
"{6ba7b8149dad-11d1-80b400c04fd430c8}",
"{6ba7b814-9dad11d180b400c04fd430c8}",
"6ba7b8149dad-11d1-80b4-00c04fd430c8",
"6ba7b814-9dad11d1-80b4-00c04fd430c8",
"6ba7b814-9dad-11d180b4-00c04fd430c8",
"6ba7b814-9dad-11d1-80b400c04fd430c8",
"6ba7b8149dad11d180b400c04fd430c8",
"6ba7b814-9dad-11d1-80b4-00c04fd430c8",
"{6ba7b814-9dad-11d1-80b4-00c04fd430c8}",
"{6ba7b814-9dad-11d1-80b4-00c04fd430c8",
"6ba7b814-9dad-11d1-80b4-00c04fd430c8}",
"(6ba7b814-9dad-11d1-80b4-00c04fd430c8)",
"urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8",
}
func TestUUIDNew(t *testing.T) {
u := New(uuid_bytes)
if u == nil {
t.Error("Expected a valid UUID")
}
if u.Version() != 2 {
t.Errorf("Expected correct version %d, but got %d", 2, u.Version())
}
if u.Variant() != ReservedNCS {
t.Errorf("Expected ReservedNCS variant %x, but got %x", ReservedNCS, u.Variant())
}
if !parseUUIDRegex.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given: %s", u.String())
}
}
func TestUUID_NewBulk(t *testing.T) {
for i := 0; i < 1000000; i++ {
New(uuid_bytes)
}
}
func TestUUID_GoIdBulk(t *testing.T) {
for i := 0; i < 100; i++ {
u := GoId(NamespaceX500, Name(""), md5.New())
outputLn(Formatter(u, GoIdFormat))
}
}
const (
clean = `[A-Fa-f0-9]{8}[A-Fa-f0-9]{4}[1-5fF][A-Fa-f0-9]{3}[A-Fa-f0-9]{4}[A-Fa-f0-9]{12}`
cleanHexPattern = `^` + clean + `$`
curlyHexPattern = `^\{` + clean + `\}$`
bracketHexPattern = `^\(` + clean + `\)$`
hyphen = `[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[1-5fF][A-Fa-f0-9]{3}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}`
cleanHyphenHexPattern = `^` + hyphen + `$`
curlyHyphenHexPattern = `^\{` + hyphen + `\}$`
bracketHyphenHexPattern = `^\(` + hyphen + `\)$`
goIdHexPattern = `^\[[A-F0-9]{8}-[A-F0-9]{4}-[1-5fF][a-f0-9]{3}-[A-F0-9]{4}-[a-f0-9]{12}\]$`
)
func TestUUID_Formats_String(t *testing.T) {
ids := []UUID {NewV4(), NewV1(), GoId(NewV4(), Name("RULEZ"), md5.New())}
SwitchFormatUpper(CurlyHyphen)
for _, u := range ids {
// CurlyHyphen - default
if !regexp.MustCompile(curlyHyphenHexPattern).MatchString(u.String()) {
t.Error("Curly hyphen UUID string output got", u)
}
outputLn(u)
// Clean
SwitchFormat(Clean)
if !regexp.MustCompile(cleanHexPattern).MatchString(u.String()) {
t.Error("Clean UUID string output got", u)
}
outputLn(u)
// Curly
SwitchFormat(Curly)
if !regexp.MustCompile(curlyHexPattern).MatchString(u.String()) {
t.Error("Curly clean UUID string output got", u)
}
outputLn(u)
// Bracket
SwitchFormat(Bracket)
if !regexp.MustCompile(bracketHexPattern).MatchString(u.String()) {
t.Error("Bracket clean UUID string output got", u)
}
outputLn(u)
// Clean Hyphen
SwitchFormat(CleanHyphen)
if !regexp.MustCompile(cleanHyphenHexPattern).MatchString(u.String()) {
t.Error("Clean hyphen UUID string output got", u)
}
outputLn(u)
// Bracket Hyphen
SwitchFormat(BracketHyphen)
if !regexp.MustCompile(bracketHyphenHexPattern).MatchString(u.String()) {
t.Error("Bracket hyphen UUID string output got", u)
}
outputLn(u)
// Bracket Hyphen
SwitchFormat(GoIdFormat)
if !regexp.MustCompile(goIdHexPattern).MatchString(u.String()) {
t.Error("GoId UUID string output expected", u)
}
outputLn(u)
// Reset default
SwitchFormatUpper(CurlyHyphen)
}
}
func TestUUID_Formats(t *testing.T) {
ids := []UUID {NewV4(), NewV1(), GoId(NewV4(), Name("RULEZ"), md5.New())}
for _, u := range ids {
// CurlyHyphen - default
if !regexp.MustCompile(curlyHyphenHexPattern).MatchString(Formatter(u, CurlyHyphen)) {
t.Error("Curly hyphen UUID string output got", Formatter(u, CurlyHyphen))
}
outputLn(Formatter(u, CurlyHyphen))
// Clean
if !regexp.MustCompile(cleanHexPattern).MatchString(Formatter(u, Clean)) {
t.Error("Clean UUID string output got", Formatter(u, Clean))
}
outputLn(Formatter(u, Clean))
// Curly
if !regexp.MustCompile(curlyHexPattern).MatchString(Formatter(u, Curly)) {
t.Error("Curly clean UUID string output", Formatter(u, Curly))
}
outputLn(Formatter(u, Curly))
// Bracket
if !regexp.MustCompile(bracketHexPattern).MatchString(Formatter(u, Bracket)) {
t.Error("Bracket clean UUID string output", Formatter(u, Bracket))
}
outputLn(Formatter(u, Bracket))
// Clean Hyphen
if !regexp.MustCompile(cleanHyphenHexPattern).MatchString(Formatter(u, CleanHyphen)) {
t.Error("Clean hyphen UUID string output", Formatter(u, CleanHyphen))
}
outputLn(Formatter(u, CleanHyphen))
// Bracket Hyphen
if !regexp.MustCompile(bracketHyphenHexPattern).MatchString(Formatter(u, BracketHyphen)) {
t.Error("Bracket hyphen UUID string output", Formatter(u, BracketHyphen))
}
outputLn(Formatter(u, BracketHyphen))
// GoId Format
if !regexp.MustCompile(goIdHexPattern).MatchString(Formatter(u, GoIdFormat)) {
t.Error("GoId UUID string output expected", Formatter(u, GoIdFormat))
}
outputLn(Formatter(u, GoIdFormat))
}
}
func TestUUIDNewHex(t *testing.T) {
s := "f3593cffee9240df408687825b523f13"
u := NewHex(s)
if u == nil {
t.Error("Expected a valid UUID")
}
if u.Version() != 4 {
t.Errorf("Expected correct version %d, but got %d", 4, u.Version())
}
if u.Variant() != ReservedNCS {
t.Errorf("Expected ReservedNCS variant %x, but got %x", ReservedNCS, u.Variant())
}
if !parseUUIDRegex.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given: %s", u.String())
}
}
func TestUUID_NewHexBulk(t *testing.T) {
for i := 0; i < 1000000; i++ {
s := "f3593cffee9240df408687825b523f13"
NewHex(s)
}
}
// Tests PasreUUID
func TestUUIDParseUUID(t *testing.T) {
for _, v := range invalidHexStrings {
_, err := ParseUUID(v)
if err == nil {
t.Error("Expected error due to invalid UUID string:", v)
}
}
for _, v := range validHexStrings {
_, err := ParseUUID(v)
if err != nil {
t.Error("Expected valid UUID string but got error:", v)
}
}
for _, id := range namespaceUuids {
_, err := ParseUUID(id.String())
if err != nil {
t.Error("Expected valid UUID string but got error:", err)
}
}
}
func TestUUIDSum(t *testing.T) {
u := new(UUIDArray)
Digest(u, NamespaceDNS, uuid_goLang, md5.New())
if u.Bytes() == nil {
t.Error("Expected new data in bytes")
}
output(u.Bytes())
u = new(UUIDArray)
Digest(u, NamespaceDNS, uuid_goLang, sha1.New())
if u.Bytes() == nil {
t.Error("Expected new data in bytes")
}
output(u.Bytes())
}
// Tests all possible version numbers and that
// each number returned is the same
func TestUUIDStruct_VersionBits(t *testing.T) {
uStruct := new(UUIDStruct)
uStruct.size = length
for v := 0; v < 16; v++ {
for i := 0; i <= 255; i ++ {
uuid_bytes[versionIndex] = byte(i)
uStruct.Unmarshal(uuid_bytes)
uStruct.setVersion(v)
output(uStruct)
if uStruct.Version() != v {
t.Errorf("%x does not resolve to %x", byte(uStruct.Version()), v)
}
output("\n")
}
}
}
// Tests all possible variants with their respective bits set
// Tests whether the expected output comes out on each byte case
func TestUUIDStruct_VarientBits(t *testing.T) {
for _, v := range uuid_variants {
for i := 0; i <= 255; i ++ {
uuid_bytes[variantIndex] = byte(i)
uStruct := createUUIDStruct(uuid_bytes, 4, v)
b := uStruct.sequenceHiAndVariant >> 4
tVariantConstraint(v, b, uStruct, t)
if uStruct.Variant() != v {
t.Errorf("%d does not resolve to %x: get %x", i, v, uStruct.Variant())
}
}
}
}
// Tests all possible variants with their respective bits set
// Tests whether the expected output comes out on each byte case
func TestUUIDArray_VarientBits(t *testing.T) {
for _, v := range uuid_variants {
for i := 0; i <= 255; i ++ {
uuid_bytes[variantIndex] = byte(i)
uArray := createUUIDArray(uuid_bytes, 4, v)
b := uArray[variantIndex] >> 4
tVariantConstraint(v, b, uArray, t)
if uArray.Variant() != v {
t.Errorf("%d does not resolve to %x", i, v)
}
}
}
}
// Tests all possible version numbers and that
// each number returned is the same
func TestUUIDArray_VersionBits(t *testing.T) {
uArray := new(UUIDArray)
for v := 0; v < 16; v++ {
for i := 0; i <= 255; i ++ {
uuid_bytes[versionIndex] = byte(i)
uArray.Unmarshal(uuid_bytes)
uArray.setVersion(v)
output(uArray)
if uArray.Version() != v {
t.Errorf("%x does not resolve to %x", byte(uArray.Version()), v)
}
output("\n")
}
}
}
func BenchmarkParseUUID(b *testing.B) {
s := "f3593cff-ee92-40df-4086-87825b523f13"
for i := 0; i < b.N; i++ {
_, err := ParseUUID(s)
if err != nil {
b.Fatal(err)
}
}
b.StopTimer()
b.ReportAllocs()
}
// *******************************************************
func createUUIDStruct(pData []byte, pVersion int, pVariant byte) *UUIDStruct {
o := new(UUIDStruct)
o.size = length
o.Unmarshal(pData)
o.setVersion(pVersion)
o.setVariant(pVariant)
return o
}
func createUUIDArray(pData []byte, pVersion int, pVariant byte) *UUIDArray {
o := new(UUIDArray)
o.Unmarshal(pData)
o.setVersion(pVersion)
o.setVariant(pVariant)
return o
}
func tVariantConstraint(v byte, b byte, o UUID, t *testing.T) {
output(o)
switch v {
case ReservedNCS:
switch b {
case 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07:
outputF(": %X ", b)
break
default: t.Errorf("%X most high bits do not resolve to 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07", b)
}
case ReservedRFC4122:
switch b {
case 0x08, 0x09, 0x0A, 0x0B:
outputF(": %X ", b)
break
default: t.Errorf("%X most high bits do not resolve to 0x08, 0x09, 0x0A, 0x0B", b)
}
case ReservedMicrosoft:
switch b {
case 0x0C, 0x0D:
outputF(": %X ", b)
break
default: t.Errorf("%X most high bits do not resolve to 0x0C, 0x0D", b)
}
case ReservedFuture:
switch b {
case 0x0E, 0x0F:
outputF(": %X ", b)
break
default: t.Errorf("%X most high bits do not resolve to 0x0E, 0x0F", b)
}
}
output("\n")
}
func output(a... interface{}) {
if printer {
fmt.Print(a...)
}
}
func outputLn(a... interface{}) {
if printer {
fmt.Println(a...)
}
}
func outputF(format string, a... interface{}) {
if printer {
fmt.Printf(format, a)
}
}