| 1 |
package uuid |
| 2 |
|
| 3 |
/**************** |
| 4 |
* Date: 3/02/14 |
| 5 |
* Time: 10:59 PM |
| 6 |
***************/ |
| 7 |
|
| 8 |
import ( |
| 9 |
"crypto/md5" |
| 10 |
"crypto/sha1" |
| 11 |
"encoding/binary" |
| 12 |
"fmt" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"net/url" |
| 15 |
"testing" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
generate = 10000 |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
goLang Name = "https://google.com/golang.org?q=golang" |
| 24 |
|
| 25 |
uuidBytes = []byte{ |
| 26 |
0xaa, 0xcf, 0xee, 0x12, |
| 27 |
0xd4, 0x00, |
| 28 |
0x27, 0x23, |
| 29 |
0x00, |
| 30 |
0xd3, |
| 31 |
0x23, 0x12, 0x4a, 0x11, 0x89, 0xbb, |
| 32 |
} |
| 33 |
|
| 34 |
idString = "aacfee12-d400-2723-00d3-23124a1189bb" |
| 35 |
|
| 36 |
uuidVariants = []byte{ |
| 37 |
VariantNCS, VariantRFC4122, VariantMicrosoft, VariantFuture, |
| 38 |
} |
| 39 |
|
| 40 |
namespaces = make(map[UUID]string) |
| 41 |
|
| 42 |
invalidHexStrings = [...]string{ |
| 43 |
"foo", |
| 44 |
"6ba7b814-9dad-11d1-80b4-", |
| 45 |
"6ba7b814--9dad-11d1-80b4--00c04fd430c8", |
| 46 |
"6ba7b814-9dad7-11d1-80b4-00c04fd430c8999", |
| 47 |
"{6ba7b814-9dad-1180b4-00c04fd430c8", |
| 48 |
"{6ba7b814--11d1-80b4-00c04fd430c8}", |
| 49 |
"urn:uuid:6ba7b814-9dad-1666666680b4-00c04fd430c8", |
| 50 |
} |
| 51 |
|
| 52 |
validHexStrings = [...]string{ |
| 53 |
"6ba7b8149dad-11d1-80b4-00c04fd430c8}", |
| 54 |
"{6ba7b8149dad-11d1-80b400c04fd430c8}", |
| 55 |
"{6ba7b814-9dad11d180b400c04fd430c8}", |
| 56 |
"6ba7b8149dad-11d1-80b4-00c04fd430c8", |
| 57 |
"6ba7b814-9dad11d1-80b4-00c04fd430c8", |
| 58 |
"6ba7b814-9dad-11d180b4-00c04fd430c8", |
| 59 |
"6ba7b814-9dad-11d1-80b400c04fd430c8", |
| 60 |
"6ba7b8149dad11d180b400c04fd430c8", |
| 61 |
"6ba7b814-9dad-11d1-80b4-00c04fd430c8", |
| 62 |
"{6ba7b814-9dad-11d1-80b4-00c04fd430c8}", |
| 63 |
"{6ba7b814-9dad-11d1-80b4-00c04fd430c8", |
| 64 |
"6ba7b814-9dad-11d1-80b4-00c04fd430c8}", |
| 65 |
"(6ba7b814-9dad-11d1-80b4-00c04fd430c8)", |
| 66 |
"urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8", |
| 67 |
} |
| 68 |
) |
| 69 |
|
| 70 |
func init() { |
| 71 |
namespaces[NameSpaceX500] = "6ba7b814-9dad-11d1-80b4-00c04fd430c8" |
| 72 |
namespaces[NameSpaceOID] = "6ba7b812-9dad-11d1-80b4-00c04fd430c8" |
| 73 |
namespaces[NameSpaceURL] = "6ba7b811-9dad-11d1-80b4-00c04fd430c8" |
| 74 |
namespaces[NameSpaceDNS] = "6ba7b810-9dad-11d1-80b4-00c04fd430c8" |
| 75 |
generator.init() |
| 76 |
} |
| 77 |
|
| 78 |
func TestEqual(t *testing.T) { |
| 79 |
for k, v := range namespaces { |
| 80 |
u, _ := Parse(v) |
| 81 |
assert.True(t, Equal(k, u), "Id's should be equal") |
| 82 |
assert.Equal(t, k.String(), u.String(), "Stringer versions should equal") |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func TestCompare(t *testing.T) { |
| 87 |
assert.True(t, Compare(NameSpaceDNS, NameSpaceDNS) == 0, "SDNS should be equal to DNS") |
| 88 |
assert.True(t, Compare(NameSpaceDNS, NameSpaceURL) == -1, "DNS should be less than URL") |
| 89 |
assert.True(t, Compare(NameSpaceURL, NameSpaceDNS) == 1, "URL should be greater than DNS") |
| 90 |
|
| 91 |
assert.True(t, Compare(nil, NameSpaceDNS) == -1, "Nil should be less than DNS") |
| 92 |
assert.True(t, Compare(NameSpaceDNS, nil) == 1, "DNS should be greater than Nil") |
| 93 |
assert.True(t, Compare(nil, nil) == 0, "nil should equal to nil") |
| 94 |
|
| 95 |
assert.True(t, Compare(Nil, NameSpaceDNS) == -1, "Nil should be less than DNS") |
| 96 |
assert.True(t, Compare(NameSpaceDNS, Nil) == 1, "DNS should be greater than Nil") |
| 97 |
assert.True(t, Compare(Nil, Nil) == 0, "Nil should equal to Nil") |
| 98 |
|
| 99 |
b1 := Uuid([]byte{ |
| 100 |
0x01, 0x09, 0x09, 0x00, |
| 101 |
0xff, 0x02, |
| 102 |
0xff, 0x03, |
| 103 |
0x00, |
| 104 |
0x00, |
| 105 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) |
| 106 |
|
| 107 |
b2 := Uuid([]byte{ |
| 108 |
0x01, 0x09, 0x09, 0x00, |
| 109 |
0xff, 0x02, |
| 110 |
0xff, 0x03, |
| 111 |
0x00, |
| 112 |
0x00, |
| 113 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) |
| 114 |
|
| 115 |
assert.Equal(t, 0, Compare(b1, b2), "Should equal") |
| 116 |
|
| 117 |
binary.BigEndian.PutUint32(b1[:4], 16779999) |
| 118 |
binary.BigEndian.PutUint32(b2[:4], 16780000) |
| 119 |
assert.Equal(t, -1, Compare(b1, b2), "Should be less") |
| 120 |
|
| 121 |
binary.BigEndian.PutUint32(b1[:4], 16780000) |
| 122 |
binary.BigEndian.PutUint32(b2[:4], 16779999) |
| 123 |
assert.Equal(t, 1, Compare(b1, b2), "Should be greater") |
| 124 |
|
| 125 |
binary.BigEndian.PutUint32(b2[:4], 16780000) |
| 126 |
assert.Equal(t, 0, Compare(b1, b2), "Should equal") |
| 127 |
|
| 128 |
binary.BigEndian.PutUint16(b1[4:6], 25000) |
| 129 |
binary.BigEndian.PutUint16(b2[4:6], 25001) |
| 130 |
assert.Equal(t, -1, Compare(b1, b2), "Should be less") |
| 131 |
|
| 132 |
binary.BigEndian.PutUint16(b1[4:6], 25001) |
| 133 |
binary.BigEndian.PutUint16(b2[4:6], 25000) |
| 134 |
assert.Equal(t, 1, Compare(b1, b2), "Should be greater") |
| 135 |
|
| 136 |
binary.BigEndian.PutUint16(b2[4:6], 25001) |
| 137 |
assert.Equal(t, 0, Compare(b1, b2), "Should equal") |
| 138 |
|
| 139 |
binary.BigEndian.PutUint16(b1[6:8], 25000) |
| 140 |
binary.BigEndian.PutUint16(b2[6:8], 25001) |
| 141 |
assert.Equal(t, -1, Compare(b1, b2), "Should be less") |
| 142 |
|
| 143 |
binary.BigEndian.PutUint16(b1[6:8], 25001) |
| 144 |
binary.BigEndian.PutUint16(b2[6:8], 25000) |
| 145 |
assert.Equal(t, 1, Compare(b1, b2), "Should be greater") |
| 146 |
|
| 147 |
binary.BigEndian.PutUint16(b2[6:8], 25001) |
| 148 |
assert.Equal(t, 0, Compare(b1, b2), "Should equal") |
| 149 |
|
| 150 |
b2[8] = 1 |
| 151 |
assert.Equal(t, -1, Compare(b1, b2), "Should be less") |
| 152 |
|
| 153 |
b1[8] = 3 |
| 154 |
assert.Equal(t, 1, Compare(b1, b2), "Should be greater") |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
func TestNewHex(t *testing.T) { |
| 159 |
s := "e902893a9d223c7ea7b8d6e313b71d9f" |
| 160 |
u := NewHex(s) |
| 161 |
assert.Equal(t, Three, u.Version(), "Expected correct version") |
| 162 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 163 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 164 |
|
| 165 |
assert.True(t, didNewHexPanic(), "Hex string should panic when invalid") |
| 166 |
} |
| 167 |
|
| 168 |
func didNewHexPanic() bool { |
| 169 |
return func() (didPanic bool) { |
| 170 |
defer func() { |
| 171 |
if recover() != nil { |
| 172 |
didPanic = true |
| 173 |
} |
| 174 |
}() |
| 175 |
|
| 176 |
NewHex("*********-------)()()()()(") |
| 177 |
return |
| 178 |
}() |
| 179 |
} |
| 180 |
|
| 181 |
func TestParse(t *testing.T) { |
| 182 |
for _, v := range invalidHexStrings { |
| 183 |
_, err := Parse(v) |
| 184 |
assert.Error(t, err, "Expected error due to invalid UUID string") |
| 185 |
} |
| 186 |
for _, v := range validHexStrings { |
| 187 |
_, err := Parse(v) |
| 188 |
assert.NoError(t, err, "Expected valid UUID string but got error") |
| 189 |
} |
| 190 |
for _, v := range namespaces { |
| 191 |
_, err := Parse(v) |
| 192 |
assert.NoError(t, err, "Expected valid UUID string but got error") |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func TestNew(t *testing.T) { |
| 197 |
for k := range namespaces { |
| 198 |
|
| 199 |
u := New(k.Bytes()) |
| 200 |
|
| 201 |
assert.NotNil(t, u, "Expected a valid non nil UUID") |
| 202 |
assert.Equal(t, One, u.Version(), "Expected correct version %d, but got %d", One, u.Version()) |
| 203 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected ReservedNCS variant %x, but got %x", VariantNCS, u.Variant()) |
| 204 |
assert.Equal(t, k.String(), u.String(), "Stringer versions should equal") |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func TestUUID_NewBulk(t *testing.T) { |
| 209 |
for i := 0; i < 1000000; i++ { |
| 210 |
New(uuidBytes[:]) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
func TestUUID_NewHexBulk(t *testing.T) { |
| 215 |
for i := 0; i < 1000000; i++ { |
| 216 |
s := "f3593cffee9240df408687825b523f13" |
| 217 |
NewHex(s) |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func TestDigest(t *testing.T) { |
| 222 |
id := digest(md5.New(), []byte(NameSpaceDNS), goLang) |
| 223 |
u := Uuid(id) |
| 224 |
if u.Bytes() == nil { |
| 225 |
t.Error("Expected new data in bytes") |
| 226 |
} |
| 227 |
id = digest(sha1.New(), []byte(NameSpaceDNS), goLang) |
| 228 |
u = Uuid(id) |
| 229 |
if u.Bytes() == nil { |
| 230 |
t.Error("Expected new data in bytes") |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
func TestNewV1(t *testing.T) { |
| 235 |
generator.Do(generator.init) |
| 236 |
u := NewV1() |
| 237 |
assert.Equal(t, One, u.Version(), "Expected correct version") |
| 238 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 239 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 240 |
} |
| 241 |
|
| 242 |
func TestNewV2(t *testing.T) { |
| 243 |
u := NewV2(DomainGroup) |
| 244 |
|
| 245 |
assert.Equal(t, Two, u.Version(), "Expected correct version") |
| 246 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 247 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 248 |
} |
| 249 |
|
| 250 |
func TestNewV3(t *testing.T) { |
| 251 |
u := NewV3(NameSpaceURL, goLang) |
| 252 |
|
| 253 |
assert.Equal(t, Three, u.Version(), "Expected correct version") |
| 254 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 255 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 256 |
|
| 257 |
ur, _ := url.Parse(string(goLang)) |
| 258 |
|
| 259 |
// Same NS same name MUST be equal |
| 260 |
u2 := NewV3(NameSpaceURL, ur) |
| 261 |
assert.Equal(t, u, u2, "Expected UUIDs generated with same namespace and name to equal") |
| 262 |
|
| 263 |
// Different NS same name MUST NOT be equal |
| 264 |
u3 := NewV3(NameSpaceDNS, ur) |
| 265 |
assert.NotEqual(t, u, u3, "Expected UUIDs generated with different namespace and same name to be different") |
| 266 |
|
| 267 |
// Same NS different name MUST NOT be equal |
| 268 |
u4 := NewV3(NameSpaceURL, u) |
| 269 |
assert.NotEqual(t, u, u4, "Expected UUIDs generated with the same namespace and different names to be different") |
| 270 |
|
| 271 |
ids := []UUID{ |
| 272 |
u, u2, u3, u4, |
| 273 |
} |
| 274 |
|
| 275 |
for j, id := range ids { |
| 276 |
i := NewV3(NameSpaceURL, Name(string(j)), id) |
| 277 |
assert.NotEqual(t, id, i, "Expected UUIDs generated with the same namespace and different names to be different") |
| 278 |
} |
| 279 |
|
| 280 |
u = NewV3(NameSpaceDNS, Name("www.example.com")) |
| 281 |
assert.Equal(t, "5df41881-3aed-3515-88a7-2f4a814cf09e", u.String()) |
| 282 |
|
| 283 |
u = NewV3(NameSpaceDNS, Name("python.org")) |
| 284 |
assert.Equal(t, "6fa459ea-ee8a-3ca4-894e-db77e160355e", u.String()) |
| 285 |
} |
| 286 |
|
| 287 |
func TestNewV4(t *testing.T) { |
| 288 |
u := NewV4() |
| 289 |
assert.Equal(t, Four, u.Version(), "Expected correct version") |
| 290 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 291 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 292 |
} |
| 293 |
|
| 294 |
func TestNewV5(t *testing.T) { |
| 295 |
u := NewV5(NameSpaceURL, goLang) |
| 296 |
|
| 297 |
assert.Equal(t, Five, u.Version(), "Expected correct version") |
| 298 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 299 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 300 |
|
| 301 |
ur, _ := url.Parse(string(goLang)) |
| 302 |
|
| 303 |
// Same NS same name MUST be equal |
| 304 |
u2 := NewV5(NameSpaceURL, ur) |
| 305 |
assert.Equal(t, u, u2, "Expected UUIDs generated with same namespace and name to equal") |
| 306 |
|
| 307 |
// Different NS same name MUST NOT be equal |
| 308 |
u3 := NewV5(NameSpaceDNS, ur) |
| 309 |
assert.NotEqual(t, u, u3, "Expected UUIDs generated with different namespace and same name to be different") |
| 310 |
|
| 311 |
// Same NS different name MUST NOT be equal |
| 312 |
u4 := NewV5(NameSpaceURL, u) |
| 313 |
assert.NotEqual(t, u, u4, "Expected UUIDs generated with the same namespace and different names to be different") |
| 314 |
|
| 315 |
ids := []UUID{ |
| 316 |
u, u2, u3, u4, |
| 317 |
} |
| 318 |
|
| 319 |
for j, id := range ids { |
| 320 |
i := NewV5(NameSpaceURL, Name(string(j)), id) |
| 321 |
assert.NotEqual(t, i, id, "Expected UUIDs generated with the same namespace and different names to be different") |
| 322 |
} |
| 323 |
|
| 324 |
u = NewV5(NameSpaceDNS, Name("python.org")) |
| 325 |
assert.Equal(t, "886313e1-3b8a-5372-9b90-0c9aee199e5d", u.String()) |
| 326 |
} |
| 327 |
|
| 328 |
var printIt = false |
| 329 |
|
| 330 |
func printer(pId UUID) { |
| 331 |
if printIt { |
| 332 |
fmt.Println(pId) |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
func TestUUID_NewV1Bulk(t *testing.T) { |
| 337 |
for i := 0; i < generate; i++ { |
| 338 |
printer(NewV1()) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func TestUUID_NewV3Bulk(t *testing.T) { |
| 343 |
for i := 0; i < generate; i++ { |
| 344 |
printer(NewV3(NameSpaceDNS, goLang, Name(string(i)))) |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
func TestUUID_NewV4Bulk(t *testing.T) { |
| 349 |
for i := 0; i < generate; i++ { |
| 350 |
printer(NewV4()) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
func TestUUID_NewV5Bulk(t *testing.T) { |
| 355 |
for i := 0; i < generate; i++ { |
| 356 |
printer(NewV5(NameSpaceDNS, goLang, Name(string(i)))) |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func Test_EachIsUnique(t *testing.T) { |
| 361 |
|
| 362 |
// Run half way through to avoid running within default resolution only |
| 363 |
|
| 364 |
spin := int(defaultSpinResolution / 2) |
| 365 |
|
| 366 |
for i := 0; i < spin; i++ { |
| 367 |
NewV1() |
| 368 |
} |
| 369 |
|
| 370 |
s := int(defaultSpinResolution) |
| 371 |
|
| 372 |
ids := make([]UUID, s) |
| 373 |
for i := 0; i < s; i++ { |
| 374 |
u := NewV1() |
| 375 |
ids[i] = u |
| 376 |
for j := 0; j < i; j++ { |
| 377 |
if b := assert.NotEqual(t, u.String(), ids[j].String(), "Should not create the same V1 UUID"); !b { |
| 378 |
break |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
//ids = make([]UUID, s) |
| 383 |
//for i := 0; i < s; i++ { |
| 384 |
// u := NewV2(DomainGroup) |
| 385 |
// ids[i] = u |
| 386 |
// for j := 0; j < i; j++ { |
| 387 |
// assert.NotEqual(t, u.String(), ids[j].String(), "Should not create the same V2 UUID") |
| 388 |
// } |
| 389 |
//} |
| 390 |
ids = make([]UUID, s) |
| 391 |
for i := 0; i < s; i++ { |
| 392 |
u := NewV3(NameSpaceDNS, Name(string(i)), goLang) |
| 393 |
ids[i] = u |
| 394 |
for j := 0; j < i; j++ { |
| 395 |
if b := assert.NotEqual(t, u.String(), ids[j].String(), "Should not create the same V3 UUID"); !b { |
| 396 |
break |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
ids = make([]UUID, s) |
| 401 |
for i := 0; i < s; i++ { |
| 402 |
u := NewV4() |
| 403 |
ids[i] = u |
| 404 |
for j := 0; j < i; j++ { |
| 405 |
if b := assert.NotEqual(t, u.String(), ids[j].String(), "Should not create the same V4 UUID"); !b { |
| 406 |
break |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
ids = make([]UUID, s) |
| 411 |
for i := 0; i < s; i++ { |
| 412 |
u := NewV5(NameSpaceDNS, Name(string(i)), goLang) |
| 413 |
ids[i] = u |
| 414 |
for j := 0; j < i; j++ { |
| 415 |
if b := assert.NotEqual(t, u.String(), ids[j].String(), "Should not create the same V5 UUID"); !b { |
| 416 |
break |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
func Test_NameSpaceUUIDs(t *testing.T) { |
| 423 |
for k, v := range namespaces { |
| 424 |
|
| 425 |
arrayId, _ := Parse(v) |
| 426 |
uuidId := array{} |
| 427 |
uuidId.unmarshal(arrayId.Bytes()) |
| 428 |
assert.Equal(t, v, arrayId.String()) |
| 429 |
assert.Equal(t, v, k.String()) |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
func TestNewV12(t *testing.T) { |
| 434 |
id := array{} |
| 435 |
|
| 436 |
makeUuid(&id, |
| 437 |
0x6ba7b810, |
| 438 |
0x9dad, |
| 439 |
0x11d1, |
| 440 |
0x80b4, |
| 441 |
[]byte{0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}) |
| 442 |
|
| 443 |
assert.Equal(t, id[:], NameSpaceDNS.Bytes()) |
| 444 |
fmt.Println(Uuid(id[:])) |
| 445 |
} |