| 1 | // Copyright (C) 2013-2018 by Maxim Bublis <[email protected]> |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining |
| 4 | // a copy of this software and associated documentation files (the |
| 5 | // "Software"), to deal in the Software without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Software, and to |
| 8 | // permit persons to whom the Software is furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be |
| 12 | // included in all copies or substantial portions of the Software. |
| 13 | // |
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 18 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 19 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 20 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | package uuid |
| 23 | |
| 24 | import ( |
| 25 | "bytes" |
| 26 | "crypto/rand" |
| 27 | "encoding/binary" |
| 28 | "errors" |
| 29 | "fmt" |
| 30 | "net" |
| 31 | "strings" |
| 32 | "sync" |
| 33 | "testing" |
| 34 | "time" |
| 35 | ) |
| 36 | |
| 37 | func TestGenerator(t *testing.T) { |
| 38 | t.Run("NewV1", testNewV1) |
| 39 | t.Run("NewV3", testNewV3) |
| 40 | t.Run("NewV4", testNewV4) |
| 41 | t.Run("NewV5", testNewV5) |
| 42 | t.Run("NewV6", testNewV6) |
| 43 | t.Run("NewV7", testNewV7) |
| 44 | t.Run("NewV8", testNewV8) |
| 45 | } |
| 46 | |
| 47 | func testNewV1(t *testing.T) { |
| 48 | t.Run("Basic", testNewV1Basic) |
| 49 | t.Run("BasicWithOptions", testNewV1BasicWithOptions) |
| 50 | t.Run("DifferentAcrossCalls", testNewV1DifferentAcrossCalls) |
| 51 | t.Run("StaleEpoch", testNewV1StaleEpoch) |
| 52 | t.Run("FaultyRand", testNewV1FaultyRand) |
| 53 | t.Run("FaultyRandWithOptions", testNewV1FaultyRandWithOptions) |
| 54 | t.Run("MissingNetwork", testNewV1MissingNetwork) |
| 55 | t.Run("MissingNetworkWithOptions", testNewV1MissingNetworkWithOptions) |
| 56 | t.Run("MissingNetworkFaultyRand", testNewV1MissingNetworkFaultyRand) |
| 57 | t.Run("MissingNetworkFaultyRandWithOptions", testNewV1MissingNetworkFaultyRandWithOptions) |
| 58 | t.Run("AtSpecificTime", testNewV1AtTime) |
| 59 | } |
| 60 | |
| 61 | func TestNewGenWithHWAF(t *testing.T) { |
| 62 | addr := []byte{0, 1, 2, 3, 4, 42} |
| 63 | |
| 64 | fn := func() (net.HardwareAddr, error) { |
| 65 | return addr, nil |
| 66 | } |
| 67 | |
| 68 | var g *Gen |
| 69 | var err error |
| 70 | var uuid UUID |
| 71 | |
| 72 | g = NewGenWithHWAF(fn) |
| 73 | |
| 74 | if g == nil { |
| 75 | t.Fatal("g is unexpectedly nil") |
| 76 | } |
| 77 | |
| 78 | uuid, err = g.NewV1() |
| 79 | if err != nil { |
| 80 | t.Fatalf("g.NewV1() err = %v, want <nil>", err) |
| 81 | } |
| 82 | |
| 83 | node := uuid[10:] |
| 84 | |
| 85 | if !bytes.Equal(addr, node) { |
| 86 | t.Fatalf("node = %v, want %v", node, addr) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func testNewV1Basic(t *testing.T) { |
| 91 | u, err := NewV1() |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if got, want := u.Version(), V1; got != want { |
| 96 | t.Errorf("generated UUID with version %d, want %d", got, want) |
| 97 | } |
| 98 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 99 | t.Errorf("generated UUID with variant %d, want %d", got, want) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func testNewV1BasicWithOptions(t *testing.T) { |
| 104 | g := NewGenWithOptions( |
| 105 | WithHWAddrFunc(nil), |
| 106 | WithEpochFunc(nil), |
| 107 | WithRandomReader(nil), |
| 108 | ) |
| 109 | u, err := g.NewV1() |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | if got, want := u.Version(), V1; got != want { |
| 114 | t.Errorf("generated UUID with version %d, want %d", got, want) |
| 115 | } |
| 116 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 117 | t.Errorf("generated UUID with variant %d, want %d", got, want) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func testNewV1DifferentAcrossCalls(t *testing.T) { |
| 122 | u1, err := NewV1() |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | u2, err := NewV1() |
| 127 | if err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | if u1 == u2 { |
| 131 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func testNewV1StaleEpoch(t *testing.T) { |
| 136 | g := &Gen{ |
| 137 | epochFunc: func() time.Time { |
| 138 | return time.Unix(0, 0) |
| 139 | }, |
| 140 | hwAddrFunc: defaultHWAddrFunc, |
| 141 | rand: rand.Reader, |
| 142 | } |
| 143 | u1, err := g.NewV1() |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | u2, err := g.NewV1() |
| 148 | if err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | if u1 == u2 { |
| 152 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func testNewV1FaultyRand(t *testing.T) { |
| 157 | g := &Gen{ |
| 158 | epochFunc: time.Now, |
| 159 | hwAddrFunc: defaultHWAddrFunc, |
| 160 | rand: &faultyReader{ |
| 161 | readToFail: 0, // fail immediately |
| 162 | }, |
| 163 | } |
| 164 | u, err := g.NewV1() |
| 165 | if err == nil { |
| 166 | t.Fatalf("got %v, want error", u) |
| 167 | } |
| 168 | if u != Nil { |
| 169 | t.Fatalf("got %v on error, want Nil", u) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func testNewV1MissingNetwork(t *testing.T) { |
| 174 | g := &Gen{ |
| 175 | epochFunc: time.Now, |
| 176 | hwAddrFunc: func() (net.HardwareAddr, error) { |
| 177 | return []byte{}, fmt.Errorf("uuid: no hw address found") |
| 178 | }, |
| 179 | rand: rand.Reader, |
| 180 | } |
| 181 | _, err := g.NewV1() |
| 182 | if err != nil { |
| 183 | t.Errorf("did not handle missing network interfaces: %v", err) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func testNewV1MissingNetworkWithOptions(t *testing.T) { |
| 188 | g := NewGenWithOptions( |
| 189 | WithHWAddrFunc(func() (net.HardwareAddr, error) { |
| 190 | return []byte{}, fmt.Errorf("uuid: no hw address found") |
| 191 | }), |
| 192 | ) |
| 193 | _, err := g.NewV1() |
| 194 | if err != nil { |
| 195 | t.Errorf("did not handle missing network interfaces: %v", err) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func testNewV1MissingNetworkFaultyRand(t *testing.T) { |
| 200 | g := &Gen{ |
| 201 | epochFunc: time.Now, |
| 202 | hwAddrFunc: func() (net.HardwareAddr, error) { |
| 203 | return []byte{}, fmt.Errorf("uuid: no hw address found") |
| 204 | }, |
| 205 | rand: &faultyReader{ |
| 206 | readToFail: 1, |
| 207 | }, |
| 208 | } |
| 209 | u, err := g.NewV1() |
| 210 | if err == nil { |
| 211 | t.Errorf("did not error on faulty reader and missing network, got %v", u) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | func testNewV1MissingNetworkFaultyRandWithOptions(t *testing.T) { |
| 216 | g := NewGenWithOptions( |
| 217 | WithHWAddrFunc(func() (net.HardwareAddr, error) { |
| 218 | return []byte{}, fmt.Errorf("uuid: no hw address found") |
| 219 | }), |
| 220 | WithRandomReader(&faultyReader{ |
| 221 | readToFail: 1, |
| 222 | }), |
| 223 | ) |
| 224 | |
| 225 | u, err := g.NewV1() |
| 226 | if err == nil { |
| 227 | t.Errorf("did not error on faulty reader and missing network, got %v", u) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | func testNewV1AtTime(t *testing.T) { |
| 232 | atTime := time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC) |
| 233 | |
| 234 | u1, err := NewV1AtTime(atTime) |
| 235 | if err != nil { |
| 236 | t.Fatal(err) |
| 237 | } |
| 238 | |
| 239 | u2, err := NewV1AtTime(atTime) |
| 240 | if err != nil { |
| 241 | t.Fatal(err) |
| 242 | } |
| 243 | |
| 244 | // Even with the same timestamp, there is still a monotonically increasing portion, |
| 245 | // so they should not be 100% identical. Bytes 0-7 and 10-16 should be identical. |
| 246 | u1Bytes := u1.Bytes() |
| 247 | u2Bytes := u2.Bytes() |
| 248 | binary.BigEndian.PutUint16(u1Bytes[8:], 0) |
| 249 | binary.BigEndian.PutUint16(u2Bytes[8:], 0) |
| 250 | if !bytes.Equal(u1Bytes, u2Bytes) { |
| 251 | t.Errorf("generated different UUIDs across calls with same timestamp: %v / %v", u1, u2) |
| 252 | } |
| 253 | |
| 254 | ts1, err := TimestampFromV1(u1) |
| 255 | if err != nil { |
| 256 | t.Fatal(err) |
| 257 | } |
| 258 | time1, err := ts1.Time() |
| 259 | if err != nil { |
| 260 | t.Fatal(err) |
| 261 | } |
| 262 | if time1.Equal(atTime) { |
| 263 | t.Errorf("extracted time is incorrect: was %v, expected %v", time1, atTime) |
| 264 | } |
| 265 | ts2, err := TimestampFromV1(u2) |
| 266 | if err != nil { |
| 267 | t.Fatal(err) |
| 268 | } |
| 269 | time2, err := ts2.Time() |
| 270 | if err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | if time2.Equal(atTime) { |
| 274 | t.Errorf("extracted time is incorrect: was %v, expected %v", time1, atTime) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | func testNewV1FaultyRandWithOptions(t *testing.T) { |
| 279 | g := NewGenWithOptions(WithRandomReader(&faultyReader{ |
| 280 | readToFail: 0, // fail immediately |
| 281 | }), |
| 282 | ) |
| 283 | u, err := g.NewV1() |
| 284 | if err == nil { |
| 285 | t.Errorf("did not error on faulty reader and missing network, got %v", u) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | func testNewV3(t *testing.T) { |
| 290 | t.Run("Basic", testNewV3Basic) |
| 291 | t.Run("EqualNames", testNewV3EqualNames) |
| 292 | t.Run("DifferentNamespaces", testNewV3DifferentNamespaces) |
| 293 | } |
| 294 | |
| 295 | func testNewV3Basic(t *testing.T) { |
| 296 | ns := NamespaceDNS |
| 297 | name := "www.example.com" |
| 298 | u := NewV3(ns, name) |
| 299 | if got, want := u.Version(), V3; got != want { |
| 300 | t.Errorf("NewV3(%v, %q): got version %d, want %d", ns, name, got, want) |
| 301 | } |
| 302 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 303 | t.Errorf("NewV3(%v, %q): got variant %d, want %d", ns, name, got, want) |
| 304 | } |
| 305 | want := "5df41881-3aed-3515-88a7-2f4a814cf09e" |
| 306 | if got := u.String(); got != want { |
| 307 | t.Errorf("NewV3(%v, %q) = %q, want %q", ns, name, got, want) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | func testNewV3EqualNames(t *testing.T) { |
| 312 | ns := NamespaceDNS |
| 313 | name := "example.com" |
| 314 | u1 := NewV3(ns, name) |
| 315 | u2 := NewV3(ns, name) |
| 316 | if u1 != u2 { |
| 317 | t.Errorf("NewV3(%v, %q) generated %v and %v across two calls", ns, name, u1, u2) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | func testNewV3DifferentNamespaces(t *testing.T) { |
| 322 | name := "example.com" |
| 323 | ns1 := NamespaceDNS |
| 324 | ns2 := NamespaceURL |
| 325 | u1 := NewV3(ns1, name) |
| 326 | u2 := NewV3(ns2, name) |
| 327 | if u1 == u2 { |
| 328 | t.Errorf("NewV3(%v, %q) == NewV3(%d, %q) (%v)", ns1, name, ns2, name, u1) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | func testNewV4(t *testing.T) { |
| 333 | t.Run("Basic", testNewV4Basic) |
| 334 | t.Run("DifferentAcrossCalls", testNewV4DifferentAcrossCalls) |
| 335 | t.Run("FaultyRand", testNewV4FaultyRand) |
| 336 | t.Run("FaultyRandWithOptions", testNewV4FaultyRandWithOptions) |
| 337 | t.Run("ShortRandomRead", testNewV4ShortRandomRead) |
| 338 | t.Run("ShortRandomReadWithOptions", testNewV4ShortRandomReadWithOptions) |
| 339 | } |
| 340 | |
| 341 | func testNewV4Basic(t *testing.T) { |
| 342 | u, err := NewV4() |
| 343 | if err != nil { |
| 344 | t.Fatal(err) |
| 345 | } |
| 346 | if got, want := u.Version(), V4; got != want { |
| 347 | t.Errorf("got version %d, want %d", got, want) |
| 348 | } |
| 349 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 350 | t.Errorf("got variant %d, want %d", got, want) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | func testNewV4DifferentAcrossCalls(t *testing.T) { |
| 355 | u1, err := NewV4() |
| 356 | if err != nil { |
| 357 | t.Fatal(err) |
| 358 | } |
| 359 | u2, err := NewV4() |
| 360 | if err != nil { |
| 361 | t.Fatal(err) |
| 362 | } |
| 363 | if u1 == u2 { |
| 364 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | func testNewV4FaultyRand(t *testing.T) { |
| 369 | g := &Gen{ |
| 370 | epochFunc: time.Now, |
| 371 | hwAddrFunc: defaultHWAddrFunc, |
| 372 | rand: &faultyReader{ |
| 373 | readToFail: 0, // fail immediately |
| 374 | }, |
| 375 | } |
| 376 | u, err := g.NewV4() |
| 377 | if err == nil { |
| 378 | t.Errorf("got %v, nil error", u) |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | func testNewV4FaultyRandWithOptions(t *testing.T) { |
| 383 | g := NewGenWithOptions( |
| 384 | WithRandomReader(&faultyReader{ |
| 385 | readToFail: 0, // fail immediately |
| 386 | }), |
| 387 | ) |
| 388 | u, err := g.NewV4() |
| 389 | if err == nil { |
| 390 | t.Errorf("got %v, nil error", u) |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | func testNewV4ShortRandomRead(t *testing.T) { |
| 395 | g := &Gen{ |
| 396 | epochFunc: time.Now, |
| 397 | hwAddrFunc: func() (net.HardwareAddr, error) { |
| 398 | return []byte{}, fmt.Errorf("uuid: no hw address found") |
| 399 | }, |
| 400 | rand: bytes.NewReader([]byte{42}), |
| 401 | } |
| 402 | u, err := g.NewV4() |
| 403 | if err == nil { |
| 404 | t.Errorf("got %v, nil error", u) |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | func testNewV4ShortRandomReadWithOptions(t *testing.T) { |
| 409 | g := NewGenWithOptions( |
| 410 | WithHWAddrFunc(func() (net.HardwareAddr, error) { |
| 411 | return []byte{}, fmt.Errorf("uuid: no hw address found") |
| 412 | }), |
| 413 | WithRandomReader(&faultyReader{ |
| 414 | readToFail: 0, // fail immediately |
| 415 | }), |
| 416 | ) |
| 417 | u, err := g.NewV4() |
| 418 | if err == nil { |
| 419 | t.Errorf("got %v, nil error", u) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | func testNewV5(t *testing.T) { |
| 424 | t.Run("Basic", testNewV5Basic) |
| 425 | t.Run("EqualNames", testNewV5EqualNames) |
| 426 | t.Run("DifferentNamespaces", testNewV5DifferentNamespaces) |
| 427 | } |
| 428 | |
| 429 | func testNewV5Basic(t *testing.T) { |
| 430 | ns := NamespaceDNS |
| 431 | name := "www.example.com" |
| 432 | u := NewV5(ns, name) |
| 433 | if got, want := u.Version(), V5; got != want { |
| 434 | t.Errorf("NewV5(%v, %q): got version %d, want %d", ns, name, got, want) |
| 435 | } |
| 436 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 437 | t.Errorf("NewV5(%v, %q): got variant %d, want %d", ns, name, got, want) |
| 438 | } |
| 439 | want := "2ed6657d-e927-568b-95e1-2665a8aea6a2" |
| 440 | if got := u.String(); got != want { |
| 441 | t.Errorf("NewV5(%v, %q) = %q, want %q", ns, name, got, want) |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | func testNewV5EqualNames(t *testing.T) { |
| 446 | ns := NamespaceDNS |
| 447 | name := "example.com" |
| 448 | u1 := NewV5(ns, name) |
| 449 | u2 := NewV5(ns, name) |
| 450 | if u1 != u2 { |
| 451 | t.Errorf("NewV5(%v, %q) generated %v and %v across two calls", ns, name, u1, u2) |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | func testNewV5DifferentNamespaces(t *testing.T) { |
| 456 | name := "example.com" |
| 457 | ns1 := NamespaceDNS |
| 458 | ns2 := NamespaceURL |
| 459 | u1 := NewV5(ns1, name) |
| 460 | u2 := NewV5(ns2, name) |
| 461 | if u1 == u2 { |
| 462 | t.Errorf("NewV5(%v, %q) == NewV5(%v, %q) (%v)", ns1, name, ns2, name, u1) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | func testNewV6(t *testing.T) { |
| 467 | t.Run("Basic", testNewV6Basic) |
| 468 | t.Run("DifferentAcrossCalls", testNewV6DifferentAcrossCalls) |
| 469 | t.Run("StaleEpoch", testNewV6StaleEpoch) |
| 470 | t.Run("StaleEpochWithOptions", testNewV6StaleEpochWithOptions) |
| 471 | t.Run("FaultyRand", testNewV6FaultyRand) |
| 472 | t.Run("FaultyRandWithOptions", testNewV6FaultyRandWithOptions) |
| 473 | t.Run("ShortRandomRead", testNewV6ShortRandomRead) |
| 474 | t.Run("ShortRandomReadWithOptions", testNewV6ShortRandomReadWithOptions) |
| 475 | t.Run("KSortable", testNewV6KSortable) |
| 476 | t.Run("AtSpecificTime", testNewV6AtTime) |
| 477 | } |
| 478 | |
| 479 | func testNewV6Basic(t *testing.T) { |
| 480 | u, err := NewV6() |
| 481 | if err != nil { |
| 482 | t.Fatal(err) |
| 483 | } |
| 484 | if got, want := u.Version(), V6; got != want { |
| 485 | t.Errorf("generated UUID with version %d, want %d", got, want) |
| 486 | } |
| 487 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 488 | t.Errorf("generated UUID with variant %d, want %d", got, want) |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | func testNewV6DifferentAcrossCalls(t *testing.T) { |
| 493 | u1, err := NewV6() |
| 494 | if err != nil { |
| 495 | t.Fatal(err) |
| 496 | } |
| 497 | u2, err := NewV6() |
| 498 | if err != nil { |
| 499 | t.Fatal(err) |
| 500 | } |
| 501 | if u1 == u2 { |
| 502 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | func testNewV6StaleEpoch(t *testing.T) { |
| 507 | g := &Gen{ |
| 508 | epochFunc: func() time.Time { |
| 509 | return time.Unix(0, 0) |
| 510 | }, |
| 511 | hwAddrFunc: defaultHWAddrFunc, |
| 512 | rand: rand.Reader, |
| 513 | } |
| 514 | u1, err := g.NewV6() |
| 515 | if err != nil { |
| 516 | t.Fatal(err) |
| 517 | } |
| 518 | u2, err := g.NewV6() |
| 519 | if err != nil { |
| 520 | t.Fatal(err) |
| 521 | } |
| 522 | if u1 == u2 { |
| 523 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | func testNewV6StaleEpochWithOptions(t *testing.T) { |
| 528 | g := NewGenWithOptions( |
| 529 | WithEpochFunc(func() time.Time { |
| 530 | return time.Unix(0, 0) |
| 531 | }), |
| 532 | ) |
| 533 | u1, err := g.NewV6() |
| 534 | if err != nil { |
| 535 | t.Fatal(err) |
| 536 | } |
| 537 | u2, err := g.NewV6() |
| 538 | if err != nil { |
| 539 | t.Fatal(err) |
| 540 | } |
| 541 | if u1 == u2 { |
| 542 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | func testNewV6FaultyRand(t *testing.T) { |
| 547 | t.Run("randomData", func(t *testing.T) { |
| 548 | g := &Gen{ |
| 549 | epochFunc: time.Now, |
| 550 | hwAddrFunc: defaultHWAddrFunc, |
| 551 | rand: &faultyReader{ |
| 552 | readToFail: 0, // fail immediately |
| 553 | }, |
| 554 | } |
| 555 | u, err := g.NewV6() |
| 556 | if err == nil { |
| 557 | t.Fatalf("got %v, want error", u) |
| 558 | } |
| 559 | if u != Nil { |
| 560 | t.Fatalf("got %v on error, want Nil", u) |
| 561 | } |
| 562 | }) |
| 563 | |
| 564 | t.Run("clockSequence", func(t *testing.T) { |
| 565 | g := &Gen{ |
| 566 | epochFunc: time.Now, |
| 567 | hwAddrFunc: defaultHWAddrFunc, |
| 568 | rand: &faultyReader{ |
| 569 | readToFail: 1, // fail immediately |
| 570 | }, |
| 571 | } |
| 572 | u, err := g.NewV6() |
| 573 | if err == nil { |
| 574 | t.Fatalf("got %v, want error", u) |
| 575 | } |
| 576 | if u != Nil { |
| 577 | t.Fatalf("got %v on error, want Nil", u) |
| 578 | } |
| 579 | }) |
| 580 | } |
| 581 | |
| 582 | func testNewV6FaultyRandWithOptions(t *testing.T) { |
| 583 | t.Run("randomData", func(t *testing.T) { |
| 584 | g := NewGenWithOptions( |
| 585 | WithRandomReader(&faultyReader{ |
| 586 | readToFail: 0, // fail immediately |
| 587 | }), |
| 588 | ) |
| 589 | u, err := g.NewV6() |
| 590 | if err == nil { |
| 591 | t.Fatalf("got %v, want error", u) |
| 592 | } |
| 593 | if u != Nil { |
| 594 | t.Fatalf("got %v on error, want Nil", u) |
| 595 | } |
| 596 | }) |
| 597 | |
| 598 | t.Run("clockSequence", func(t *testing.T) { |
| 599 | g := NewGenWithOptions( |
| 600 | WithRandomReader(&faultyReader{ |
| 601 | readToFail: 1, // fail immediately |
| 602 | }), |
| 603 | ) |
| 604 | u, err := g.NewV6() |
| 605 | if err == nil { |
| 606 | t.Fatalf("got %v, want error", u) |
| 607 | } |
| 608 | if u != Nil { |
| 609 | t.Fatalf("got %v on error, want Nil", u) |
| 610 | } |
| 611 | }) |
| 612 | } |
| 613 | |
| 614 | func testNewV6ShortRandomRead(t *testing.T) { |
| 615 | g := &Gen{ |
| 616 | epochFunc: time.Now, |
| 617 | rand: bytes.NewReader([]byte{42}), |
| 618 | } |
| 619 | u, err := g.NewV6() |
| 620 | if err == nil { |
| 621 | t.Errorf("got %v, nil error", u) |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | func testNewV6ShortRandomReadWithOptions(t *testing.T) { |
| 626 | g := NewGenWithOptions( |
| 627 | WithRandomReader(bytes.NewReader([]byte{42})), |
| 628 | ) |
| 629 | u, err := g.NewV6() |
| 630 | if err == nil { |
| 631 | t.Errorf("got %v, nil error", u) |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | func testNewV6KSortable(t *testing.T) { |
| 636 | uuids := make([]UUID, 10) |
| 637 | for i := range uuids { |
| 638 | u, err := NewV6() |
| 639 | testErrCheck(t, "NewV6()", "", err) |
| 640 | |
| 641 | uuids[i] = u |
| 642 | |
| 643 | time.Sleep(time.Microsecond) |
| 644 | } |
| 645 | |
| 646 | for i := 1; i < len(uuids); i++ { |
| 647 | p, n := uuids[i-1], uuids[i] |
| 648 | isLess := p.String() < n.String() |
| 649 | if !isLess { |
| 650 | t.Errorf("uuids[%d] (%s) not less than uuids[%d] (%s)", i-1, p, i, n) |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | func testNewV6AtTime(t *testing.T) { |
| 656 | atTime := time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC) |
| 657 | |
| 658 | u1, err := NewV6AtTime(atTime) |
| 659 | if err != nil { |
| 660 | t.Fatal(err) |
| 661 | } |
| 662 | |
| 663 | u2, err := NewV6AtTime(atTime) |
| 664 | if err != nil { |
| 665 | t.Fatal(err) |
| 666 | } |
| 667 | |
| 668 | // Even with the same timestamp, there is still a random portion, |
| 669 | // so they should not be 100% identical. Bytes 0-8 are the timestamp so they should be identical. |
| 670 | u1Bytes := u1.Bytes()[:8] |
| 671 | u2Bytes := u2.Bytes()[:8] |
| 672 | if !bytes.Equal(u1Bytes, u2Bytes) { |
| 673 | t.Errorf("generated different UUIDs across calls with same timestamp: %v / %v", u1, u2) |
| 674 | } |
| 675 | |
| 676 | ts1, err := TimestampFromV6(u1) |
| 677 | if err != nil { |
| 678 | t.Fatal(err) |
| 679 | } |
| 680 | time1, err := ts1.Time() |
| 681 | if err != nil { |
| 682 | t.Fatal(err) |
| 683 | } |
| 684 | if time1.Equal(atTime) { |
| 685 | t.Errorf("extracted time is incorrect: was %v, expected %v", time1, atTime) |
| 686 | } |
| 687 | ts2, err := TimestampFromV6(u2) |
| 688 | if err != nil { |
| 689 | t.Fatal(err) |
| 690 | } |
| 691 | time2, err := ts2.Time() |
| 692 | if err != nil { |
| 693 | t.Fatal(err) |
| 694 | } |
| 695 | if time2.Equal(atTime) { |
| 696 | t.Errorf("extracted time is incorrect: was %v, expected %v", time1, atTime) |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | func testNewV7(t *testing.T) { |
| 701 | t.Run("Basic", makeTestNewV7Basic()) |
| 702 | t.Run("TestVector", makeTestNewV7TestVector()) |
| 703 | t.Run("Basic10M", makeTestNewV7Basic10M()) |
| 704 | t.Run("DifferentAcrossCalls", makeTestNewV7DifferentAcrossCalls()) |
| 705 | t.Run("StaleEpoch", makeTestNewV7StaleEpoch()) |
| 706 | t.Run("StaleEpochWithOptions", makeTestNewV7StaleEpochWithOptions()) |
| 707 | t.Run("FaultyRand", makeTestNewV7FaultyRand()) |
| 708 | t.Run("FaultyRandWithOptions", makeTestNewV7FaultyRandWithOptions()) |
| 709 | t.Run("ShortRandomRead", makeTestNewV7ShortRandomRead()) |
| 710 | t.Run("ShortRandomReadWithOptions", makeTestNewV7ShortRandomReadWithOptions()) |
| 711 | t.Run("KSortable", makeTestNewV7KSortable()) |
| 712 | t.Run("ClockSequence", makeTestNewV7ClockSequence()) |
| 713 | t.Run("CounterRollover", makeTestNewV7CounterRollover()) |
| 714 | t.Run("Sequential1M", makeTestNewV7Sequential1M()) |
| 715 | t.Run("BorrowIsBounded", makeTestNewV7BorrowIsBounded()) |
| 716 | t.Run("CounterReseedsOnNewTick", makeTestNewV7CounterReseedsOnNewTick()) |
| 717 | t.Run("MixedWithV1", makeTestNewV7MixedWithV1()) |
| 718 | t.Run("BackwardsClock", makeTestNewV7BackwardsClock()) |
| 719 | t.Run("AtTimeHistorical", makeTestNewV7AtTimeHistorical()) |
| 720 | t.Run("AtTimeUnrepresentable", makeTestNewV7AtTimeUnrepresentable()) |
| 721 | t.Run("Concurrent", makeTestNewV7Concurrent()) |
| 722 | t.Run("FaultyRandOnReseed", makeTestNewV7FaultyRandOnReseed()) |
| 723 | t.Run("FaultyRandOnRollover", makeTestNewV7FaultyRandOnRollover()) |
| 724 | t.Run("AtSpecificTime", makeTestNewV7AtTime()) |
| 725 | } |
| 726 | |
| 727 | // v7Counter returns the 12 bits of rand_a that hold the monotonic counter. |
| 728 | func v7Counter(u UUID) uint16 { |
| 729 | return binary.BigEndian.Uint16(u[6:8]) & maxV7Counter |
| 730 | } |
| 731 | |
| 732 | // assertV7Increasing fails on the first UUID that does not sort above its |
| 733 | // predecessor. A single generator should never produce one. |
| 734 | func assertV7Increasing(t *testing.T, uuids []UUID) { |
| 735 | t.Helper() |
| 736 | |
| 737 | for i := 1; i < len(uuids); i++ { |
| 738 | p, n := uuids[i-1], uuids[i] |
| 739 | if p.String() >= n.String() { |
| 740 | t.Fatalf("uuids[%d] (%s) not less than uuids[%d] (%s)", i-1, p, i, n) |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | func makeTestNewV7Basic() func(t *testing.T) { |
| 746 | return func(t *testing.T) { |
| 747 | u, err := NewV7() |
| 748 | if err != nil { |
| 749 | t.Fatal(err) |
| 750 | } |
| 751 | if got, want := u.Version(), V7; got != want { |
| 752 | t.Errorf("got version %d, want %d", got, want) |
| 753 | } |
| 754 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 755 | t.Errorf("got variant %d, want %d", got, want) |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | // makeTestNewV7TestVector as defined in Draft04 |
| 761 | func makeTestNewV7TestVector() func(t *testing.T) { |
| 762 | return func(t *testing.T) { |
| 763 | pRand := make([]byte, 10) |
| 764 | // First 2 bytes will be read to seed the counter (11 bits are used; the top bit is cleared as a guard). |
| 765 | // The first 4 bits of u[6] will be overridden by the Version field. |
| 766 | binary.LittleEndian.PutUint16(pRand[:2], uint16(0xCC3)) |
| 767 | // 8bytes will be read for rand_b. First 2 bits will be overridden by Variant |
| 768 | binary.LittleEndian.PutUint64(pRand[2:], uint64(0x18C4DC0C0C07398F)) |
| 769 | |
| 770 | g := &Gen{ |
| 771 | epochFunc: func() time.Time { |
| 772 | return time.UnixMilli(1645557742000) |
| 773 | }, |
| 774 | rand: bytes.NewReader(pRand), |
| 775 | } |
| 776 | u, err := g.NewV7() |
| 777 | if err != nil { |
| 778 | t.Fatal(err) |
| 779 | } |
| 780 | if got, want := u.Version(), V7; got != want { |
| 781 | t.Errorf("got version %d, want %d", got, want) |
| 782 | } |
| 783 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 784 | t.Errorf("got variant %d, want %d", got, want) |
| 785 | } |
| 786 | if got, want := u.String()[:15], "017f22e2-79b0-7"; got != want { |
| 787 | t.Errorf("got version %q, want %q", got, want) |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | func makeTestNewV7Basic10M() func(t *testing.T) { |
| 793 | return func(t *testing.T) { |
| 794 | if testing.Short() { |
| 795 | t.Skip("skipping test in short mode.") |
| 796 | } |
| 797 | |
| 798 | g := NewGen() |
| 799 | |
| 800 | for range 10_000_000 { |
| 801 | u, err := g.NewV7() |
| 802 | if err != nil { |
| 803 | t.Fatal(err) |
| 804 | } |
| 805 | if got, want := u.Version(), V7; got != want { |
| 806 | t.Errorf("got version %d, want %d", got, want) |
| 807 | } |
| 808 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 809 | t.Errorf("got variant %d, want %d", got, want) |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | func makeTestNewV7DifferentAcrossCalls() func(t *testing.T) { |
| 816 | return func(t *testing.T) { |
| 817 | g := NewGen() |
| 818 | |
| 819 | u1, err := g.NewV7() |
| 820 | if err != nil { |
| 821 | t.Fatal(err) |
| 822 | } |
| 823 | u2, err := g.NewV7() |
| 824 | if err != nil { |
| 825 | t.Fatal(err) |
| 826 | } |
| 827 | if u1 == u2 { |
| 828 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | func makeTestNewV7StaleEpoch() func(t *testing.T) { |
| 834 | return func(t *testing.T) { |
| 835 | g := &Gen{ |
| 836 | epochFunc: func() time.Time { |
| 837 | return time.Unix(0, 0) |
| 838 | }, |
| 839 | rand: rand.Reader, |
| 840 | } |
| 841 | u1, err := g.NewV7() |
| 842 | if err != nil { |
| 843 | t.Fatal(err) |
| 844 | } |
| 845 | u2, err := g.NewV7() |
| 846 | if err != nil { |
| 847 | t.Fatal(err) |
| 848 | } |
| 849 | if u1 == u2 { |
| 850 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | func makeTestNewV7StaleEpochWithOptions() func(t *testing.T) { |
| 856 | return func(t *testing.T) { |
| 857 | g := NewGenWithOptions( |
| 858 | WithEpochFunc(func() time.Time { |
| 859 | return time.Unix(0, 0) |
| 860 | }), |
| 861 | ) |
| 862 | u1, err := g.NewV7() |
| 863 | if err != nil { |
| 864 | t.Fatal(err) |
| 865 | } |
| 866 | u2, err := g.NewV7() |
| 867 | if err != nil { |
| 868 | t.Fatal(err) |
| 869 | } |
| 870 | if u1 == u2 { |
| 871 | t.Errorf("generated identical UUIDs across calls: %v", u1) |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | func makeTestNewV7FaultyRand() func(t *testing.T) { |
| 877 | return func(t *testing.T) { |
| 878 | g := &Gen{ |
| 879 | epochFunc: time.Now, |
| 880 | rand: &faultyReader{ |
| 881 | readToFail: 0, |
| 882 | }, |
| 883 | } |
| 884 | u, err := g.NewV7() |
| 885 | if err == nil { |
| 886 | t.Errorf("got %v, nil error for clockSequence", u) |
| 887 | } |
| 888 | |
| 889 | g = &Gen{ |
| 890 | epochFunc: time.Now, |
| 891 | rand: &faultyReader{ |
| 892 | readToFail: 1, |
| 893 | }, |
| 894 | } |
| 895 | u, err = g.NewV7() |
| 896 | if err == nil { |
| 897 | t.Errorf("got %v, nil error rand_b", u) |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | func makeTestNewV7FaultyRandWithOptions() func(t *testing.T) { |
| 903 | return func(t *testing.T) { |
| 904 | g := NewGenWithOptions( |
| 905 | WithRandomReader(&faultyReader{ |
| 906 | readToFail: 0, // fail immediately |
| 907 | }), |
| 908 | ) |
| 909 | u, err := g.NewV7() |
| 910 | if err == nil { |
| 911 | t.Errorf("got %v, nil error", u) |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | func makeTestNewV7ShortRandomRead() func(t *testing.T) { |
| 917 | return func(t *testing.T) { |
| 918 | g := &Gen{ |
| 919 | epochFunc: time.Now, |
| 920 | rand: bytes.NewReader([]byte{42}), |
| 921 | } |
| 922 | u, err := g.NewV7() |
| 923 | if err == nil { |
| 924 | t.Errorf("got %v, nil error", u) |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | func makeTestNewV7ShortRandomReadWithOptions() func(t *testing.T) { |
| 930 | return func(t *testing.T) { |
| 931 | g := NewGenWithOptions( |
| 932 | WithRandomReader(bytes.NewReader([]byte{42})), |
| 933 | ) |
| 934 | u, err := g.NewV7() |
| 935 | if err == nil { |
| 936 | t.Errorf("got %v, nil error", u) |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | func makeTestNewV7KSortable() func(t *testing.T) { |
| 942 | return func(t *testing.T) { |
| 943 | uuids := make([]UUID, 10) |
| 944 | for i := range uuids { |
| 945 | u, err := NewV7() |
| 946 | testErrCheck(t, "NewV7()", "", err) |
| 947 | |
| 948 | uuids[i] = u |
| 949 | time.Sleep(time.Millisecond) |
| 950 | } |
| 951 | |
| 952 | for i := 1; i < len(uuids); i++ { |
| 953 | p, n := uuids[i-1], uuids[i] |
| 954 | isLess := p.String() < n.String() |
| 955 | if !isLess { |
| 956 | t.Errorf("uuids[%d] (%s) not less than uuids[%d] (%s)", i-1, p, i, n) |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | func makeTestNewV7ClockSequence() func(t *testing.T) { |
| 963 | return func(t *testing.T) { |
| 964 | if testing.Short() { |
| 965 | t.Skip("skipping test in short mode.") |
| 966 | } |
| 967 | |
| 968 | g := NewGen() |
| 969 | //always return the same TS |
| 970 | g.epochFunc = func() time.Time { |
| 971 | return time.UnixMilli(1645557742000) |
| 972 | } |
| 973 | //by being KSortable with the same timestamp, it means the sequence is Not empty, and it is monotonic. |
| 974 | //The count is well past the 4096 values the 12 bit counter can hold, so a rollover has to be |
| 975 | //handled without breaking the ordering. |
| 976 | uuids := make([]UUID, 5000) |
| 977 | for i := range uuids { |
| 978 | u, err := g.NewV7() |
| 979 | testErrCheck(t, "NewV7()", "", err) |
| 980 | uuids[i] = u |
| 981 | } |
| 982 | |
| 983 | assertV7Increasing(t, uuids) |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // makeTestNewV7CounterRollover generates far more UUIDs within a single |
| 988 | // millisecond than the 12 bit counter can hold, which is where the counter used |
| 989 | // to wrap from 0xfff back to 0x000 and reorder UUIDs against their predecessor. |
| 990 | func makeTestNewV7CounterRollover() func(t *testing.T) { |
| 991 | return func(t *testing.T) { |
| 992 | if testing.Short() { |
| 993 | t.Skip("skipping test in short mode.") |
| 994 | } |
| 995 | |
| 996 | g := NewGenWithOptions(WithEpochFunc(func() time.Time { |
| 997 | return time.UnixMilli(1645557742000) |
| 998 | })) |
| 999 | |
| 1000 | uuids := make([]UUID, 50000) |
| 1001 | for i := range uuids { |
| 1002 | u, err := g.NewV7() |
| 1003 | testErrCheck(t, "NewV7()", "", err) |
| 1004 | uuids[i] = u |
| 1005 | } |
| 1006 | |
| 1007 | assertV7Increasing(t, uuids) |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | // makeTestNewV7Sequential1M is the ordering check at load, against the real |
| 1012 | // clock so that counter rollovers, the reseeding that follows them and the clock |
| 1013 | // advancing on its own all interleave. Sorting strictly above the previous UUID |
| 1014 | // also means differing from it, so this covers uniqueness across the run. UUIDs |
| 1015 | // are compared as they are generated rather than collected first, to keep the |
| 1016 | // count free to grow. |
| 1017 | func makeTestNewV7Sequential1M() func(t *testing.T) { |
| 1018 | return func(t *testing.T) { |
| 1019 | if testing.Short() { |
| 1020 | t.Skip("skipping test in short mode.") |
| 1021 | } |
| 1022 | |
| 1023 | g := NewGen() |
| 1024 | |
| 1025 | var prev string |
| 1026 | for i := range 1_000_000 { |
| 1027 | u, err := g.NewV7() |
| 1028 | testErrCheck(t, "NewV7()", "", err) |
| 1029 | |
| 1030 | cur := u.String() |
| 1031 | if cur <= prev { |
| 1032 | t.Fatalf("uuids[%d] (%s) not less than uuids[%d] (%s)", i-1, prev, i, cur) |
| 1033 | } |
| 1034 | prev = cur |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | // makeTestNewV7BorrowIsBounded checks the cost of the rollover handling: the |
| 1040 | // embedded timestamp runs ahead of the real one, but only by as many |
| 1041 | // milliseconds as there were counter rollovers. |
| 1042 | func makeTestNewV7BorrowIsBounded() func(t *testing.T) { |
| 1043 | return func(t *testing.T) { |
| 1044 | if testing.Short() { |
| 1045 | t.Skip("skipping test in short mode.") |
| 1046 | } |
| 1047 | |
| 1048 | const ( |
| 1049 | count = 50000 |
| 1050 | atMs = int64(1645557742000) |
| 1051 | usable = maxV7Counter - v7CounterSeedMask // increments guaranteed within a tick |
| 1052 | ) |
| 1053 | |
| 1054 | g := NewGenWithOptions(WithEpochFunc(func() time.Time { |
| 1055 | return time.UnixMilli(atMs) |
| 1056 | })) |
| 1057 | |
| 1058 | var last UUID |
| 1059 | for range count { |
| 1060 | u, err := g.NewV7() |
| 1061 | testErrCheck(t, "NewV7()", "", err) |
| 1062 | last = u |
| 1063 | } |
| 1064 | |
| 1065 | ts, err := TimestampFromV7(last) |
| 1066 | if err != nil { |
| 1067 | t.Fatal(err) |
| 1068 | } |
| 1069 | got, err := ts.Time() |
| 1070 | if err != nil { |
| 1071 | t.Fatal(err) |
| 1072 | } |
| 1073 | |
| 1074 | ahead := got.UnixMilli() - atMs |
| 1075 | if ahead < 0 { |
| 1076 | t.Fatalf("timestamp went backwards by %dms", -ahead) |
| 1077 | } |
| 1078 | if want := int64(count/usable + 1); ahead > want { |
| 1079 | t.Errorf("timestamp ran %dms ahead of the clock, want at most %dms", ahead, want) |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | // makeTestNewV7CounterReseedsOnNewTick pins the random reader so the counter is |
| 1085 | // predictable, then checks that a new millisecond reseeds it from that reader |
| 1086 | // rather than continuing to climb, and that all 12 bits survive SetVersion. |
| 1087 | func makeTestNewV7CounterReseedsOnNewTick() func(t *testing.T) { |
| 1088 | return func(t *testing.T) { |
| 1089 | // Two counter seeds, each followed by the 8 bytes read for rand_b. |
| 1090 | seeds := []uint16{0xabcd, 0x1234} |
| 1091 | buf := make([]byte, 0, 2*(2+8)) |
| 1092 | for _, seed := range seeds { |
| 1093 | buf = binary.BigEndian.AppendUint16(buf, seed) |
| 1094 | buf = append(buf, make([]byte, 8)...) |
| 1095 | } |
| 1096 | |
| 1097 | ms := int64(1645557742000) |
| 1098 | g := NewGenWithOptions( |
| 1099 | WithRandomReader(bytes.NewReader(buf)), |
| 1100 | WithEpochFunc(func() time.Time { return time.UnixMilli(ms) }), |
| 1101 | ) |
| 1102 | |
| 1103 | first, err := g.NewV7() |
| 1104 | if err != nil { |
| 1105 | t.Fatal(err) |
| 1106 | } |
| 1107 | ms++ |
| 1108 | second, err := g.NewV7() |
| 1109 | if err != nil { |
| 1110 | t.Fatal(err) |
| 1111 | } |
| 1112 | |
| 1113 | for i, u := range []UUID{first, second} { |
| 1114 | // The guard bit keeps the counter in the lower half of its range, |
| 1115 | // leaving room to increment within the tick. |
| 1116 | want := seeds[i] & v7CounterSeedMask |
| 1117 | if got := v7Counter(u); got != want { |
| 1118 | t.Errorf("uuid %d: got counter %#03x, want %#03x", i, got, want) |
| 1119 | } |
| 1120 | } |
| 1121 | if first.String() >= second.String() { |
| 1122 | t.Errorf("%v is not less than %v", first, second) |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | // makeTestNewV7MixedWithV1 guards against V7 sharing counter and timestamp state |
| 1128 | // with V1. V1 counts its epoch in 100 nanosecond intervals since 1582, which |
| 1129 | // dwarfs any millisecond value V7 compares against, and its clock sequence is 14 |
| 1130 | // bits wide rather than 12. The timestamp is pinned so that every UUID lands in |
| 1131 | // the same tick, leaving the counter as the only thing keeping them in order. |
| 1132 | func makeTestNewV7MixedWithV1() func(t *testing.T) { |
| 1133 | return func(t *testing.T) { |
| 1134 | if testing.Short() { |
| 1135 | t.Skip("skipping test in short mode.") |
| 1136 | } |
| 1137 | |
| 1138 | const atMs = int64(1645557742000) |
| 1139 | |
| 1140 | g := NewGenWithOptions(WithEpochFunc(func() time.Time { |
| 1141 | return time.UnixMilli(atMs) |
| 1142 | })) |
| 1143 | |
| 1144 | uuids := make([]UUID, 5000) |
| 1145 | for i := range uuids { |
| 1146 | if _, err := g.NewV1(); err != nil { |
| 1147 | t.Fatal(err) |
| 1148 | } |
| 1149 | u, err := g.NewV7() |
| 1150 | if err != nil { |
| 1151 | t.Fatal(err) |
| 1152 | } |
| 1153 | uuids[i] = u |
| 1154 | } |
| 1155 | |
| 1156 | assertV7Increasing(t, uuids) |
| 1157 | |
| 1158 | // The V1 epoch must not have been mistaken for a millisecond count and |
| 1159 | // left in the V7 timestamp field. |
| 1160 | ts, err := TimestampFromV7(uuids[0]) |
| 1161 | if err != nil { |
| 1162 | t.Fatal(err) |
| 1163 | } |
| 1164 | got, err := ts.Time() |
| 1165 | if err != nil { |
| 1166 | t.Fatal(err) |
| 1167 | } |
| 1168 | if got.UnixMilli() != atMs { |
| 1169 | t.Errorf("first UUID encodes %v (%dms), want the provided %dms", got.UTC(), got.UnixMilli(), atMs) |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | // makeTestNewV7BackwardsClock checks that a clock correction cannot reorder |
| 1175 | // UUIDs, per the monotonic error checking guidance in RFC 9562 section 6.2. |
| 1176 | func makeTestNewV7BackwardsClock() func(t *testing.T) { |
| 1177 | return func(t *testing.T) { |
| 1178 | ms := int64(1645557742000) |
| 1179 | g := NewGenWithOptions(WithEpochFunc(func() time.Time { |
| 1180 | return time.UnixMilli(ms) |
| 1181 | })) |
| 1182 | |
| 1183 | uuids := make([]UUID, 0, 30) |
| 1184 | for _, step := range []int64{1, 1, -5000, 1, -1, 2} { |
| 1185 | for range 5 { |
| 1186 | u, err := g.NewV7() |
| 1187 | if err != nil { |
| 1188 | t.Fatal(err) |
| 1189 | } |
| 1190 | uuids = append(uuids, u) |
| 1191 | } |
| 1192 | ms += step |
| 1193 | } |
| 1194 | |
| 1195 | assertV7Increasing(t, uuids) |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | // makeTestNewV7AtTimeHistorical checks that the protection against a backwards |
| 1200 | // clock does not leak into the explicit timestamp API: a caller asking for an |
| 1201 | // older timestamp gets that timestamp, and a batch at that timestamp stays |
| 1202 | // ordered. |
| 1203 | func makeTestNewV7AtTimeHistorical() func(t *testing.T) { |
| 1204 | return func(t *testing.T) { |
| 1205 | g := NewGen() |
| 1206 | |
| 1207 | if _, err := g.NewV7(); err != nil { |
| 1208 | t.Fatal(err) |
| 1209 | } |
| 1210 | |
| 1211 | atTime := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) |
| 1212 | uuids := make([]UUID, 10) |
| 1213 | for i := range uuids { |
| 1214 | u, err := g.NewV7AtTime(atTime) |
| 1215 | if err != nil { |
| 1216 | t.Fatal(err) |
| 1217 | } |
| 1218 | uuids[i] = u |
| 1219 | } |
| 1220 | |
| 1221 | assertV7Increasing(t, uuids) |
| 1222 | |
| 1223 | for i, u := range uuids { |
| 1224 | ts, err := TimestampFromV7(u) |
| 1225 | if err != nil { |
| 1226 | t.Fatal(err) |
| 1227 | } |
| 1228 | got, err := ts.Time() |
| 1229 | if err != nil { |
| 1230 | t.Fatal(err) |
| 1231 | } |
| 1232 | if !got.Equal(atTime) { |
| 1233 | t.Fatalf("uuids[%d] encodes %v, want the provided %v", i, got.UTC(), atTime) |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | // makeTestNewV7AtTimeUnrepresentable covers times the 48-bit millisecond field |
| 1240 | // cannot hold, such as the zero time. They are pinned to the nearest end of the |
| 1241 | // range, and must not drag the generator along with them: without that, the |
| 1242 | // negative millisecond count of a zero time would read as a timestamp far in the |
| 1243 | // future and hold every later UUID there. |
| 1244 | func makeTestNewV7AtTimeUnrepresentable() func(t *testing.T) { |
| 1245 | return func(t *testing.T) { |
| 1246 | for _, tt := range []struct { |
| 1247 | name string |
| 1248 | atTime time.Time |
| 1249 | want int64 |
| 1250 | }{ |
| 1251 | {name: "ZeroTime", atTime: time.Time{}, want: 0}, |
| 1252 | {name: "BeforeUnixEpoch", atTime: time.Date(1969, 7, 20, 20, 17, 0, 0, time.UTC), want: 0}, |
| 1253 | {name: "AfterTimestampRange", atTime: time.Date(20000, 1, 1, 0, 0, 0, 0, time.UTC), want: maxV7Timestamp}, |
| 1254 | } { |
| 1255 | t.Run(tt.name, func(t *testing.T) { |
| 1256 | g := NewGen() |
| 1257 | |
| 1258 | u, err := g.NewV7AtTime(tt.atTime) |
| 1259 | if err != nil { |
| 1260 | t.Fatal(err) |
| 1261 | } |
| 1262 | ts, err := TimestampFromV7(u) |
| 1263 | if err != nil { |
| 1264 | t.Fatal(err) |
| 1265 | } |
| 1266 | got, err := ts.Time() |
| 1267 | if err != nil { |
| 1268 | t.Fatal(err) |
| 1269 | } |
| 1270 | if got.UnixMilli() != tt.want { |
| 1271 | t.Errorf("%v encodes %dms, want %dms", u, got.UnixMilli(), tt.want) |
| 1272 | } |
| 1273 | |
| 1274 | next, err := g.NewV7() |
| 1275 | if err != nil { |
| 1276 | t.Fatal(err) |
| 1277 | } |
| 1278 | if u.String() >= next.String() { |
| 1279 | t.Errorf("%v is not less than %v", u, next) |
| 1280 | } |
| 1281 | |
| 1282 | if tt.want > 0 { |
| 1283 | // A timestamp ahead of the clock holds the generator there, |
| 1284 | // so there is nothing more to check. |
| 1285 | return |
| 1286 | } |
| 1287 | |
| 1288 | // A timestamp below the clock leaves the generator tracking it. |
| 1289 | ts, err = TimestampFromV7(next) |
| 1290 | if err != nil { |
| 1291 | t.Fatal(err) |
| 1292 | } |
| 1293 | got, err = ts.Time() |
| 1294 | if err != nil { |
| 1295 | t.Fatal(err) |
| 1296 | } |
| 1297 | if drift := time.Since(got); drift < 0 || drift > time.Minute { |
| 1298 | t.Errorf("%v is %v away from now", next, drift) |
| 1299 | } |
| 1300 | }) |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | // makeTestNewV7Concurrent checks that no two UUIDs from one generator share a |
| 1306 | // timestamp and counter, which is the pair the ordering guarantee rests on. |
| 1307 | func makeTestNewV7Concurrent() func(t *testing.T) { |
| 1308 | return func(t *testing.T) { |
| 1309 | const ( |
| 1310 | goroutines = 8 |
| 1311 | perRoutine = 2000 |
| 1312 | ) |
| 1313 | |
| 1314 | g := NewGen() |
| 1315 | |
| 1316 | var wg sync.WaitGroup |
| 1317 | results := make([][]UUID, goroutines) |
| 1318 | for i := range results { |
| 1319 | wg.Add(1) |
| 1320 | go func(i int) { |
| 1321 | defer wg.Done() |
| 1322 | |
| 1323 | uuids := make([]UUID, perRoutine) |
| 1324 | for j := range uuids { |
| 1325 | u, err := g.NewV7() |
| 1326 | if err != nil { |
| 1327 | t.Error(err) |
| 1328 | return |
| 1329 | } |
| 1330 | uuids[j] = u |
| 1331 | } |
| 1332 | results[i] = uuids |
| 1333 | }(i) |
| 1334 | } |
| 1335 | wg.Wait() |
| 1336 | |
| 1337 | if t.Failed() { |
| 1338 | return |
| 1339 | } |
| 1340 | |
| 1341 | type sequence struct { |
| 1342 | ms uint64 |
| 1343 | counter uint16 |
| 1344 | } |
| 1345 | seen := make(map[sequence]UUID, goroutines*perRoutine) |
| 1346 | for _, uuids := range results { |
| 1347 | for _, u := range uuids { |
| 1348 | ts, err := TimestampFromV7(u) |
| 1349 | if err != nil { |
| 1350 | t.Fatal(err) |
| 1351 | } |
| 1352 | key := sequence{ms: uint64(ts), counter: v7Counter(u)} |
| 1353 | if prev, ok := seen[key]; ok { |
| 1354 | t.Fatalf("%v and %v share a timestamp and counter", prev, u) |
| 1355 | } |
| 1356 | seen[key] = u |
| 1357 | } |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | // makeTestNewV7FaultyRandOnReseed fails the read that seeds the counter for a |
| 1363 | // new millisecond. |
| 1364 | func makeTestNewV7FaultyRandOnReseed() func(t *testing.T) { |
| 1365 | return func(t *testing.T) { |
| 1366 | ms := int64(1645557742000) |
| 1367 | g := NewGenWithOptions( |
| 1368 | WithRandomReader(&seedFailReader{failOn: 2}), |
| 1369 | WithEpochFunc(func() time.Time { return time.UnixMilli(ms) }), |
| 1370 | ) |
| 1371 | |
| 1372 | if _, err := g.NewV7(); err != nil { |
| 1373 | t.Fatal(err) |
| 1374 | } |
| 1375 | |
| 1376 | ms++ |
| 1377 | u, err := g.NewV7() |
| 1378 | if err == nil { |
| 1379 | t.Errorf("got %v, nil error for the counter reseed", u) |
| 1380 | } |
| 1381 | if u != Nil { |
| 1382 | t.Errorf("got %v on error, want Nil", u) |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | // makeTestNewV7FaultyRandOnRollover fails the read that reseeds the counter |
| 1388 | // after it is exhausted within a millisecond. Seeding the counter as high as the |
| 1389 | // guard bit allows also pins down the guaranteed headroom within a tick. |
| 1390 | func makeTestNewV7FaultyRandOnRollover() func(t *testing.T) { |
| 1391 | return func(t *testing.T) { |
| 1392 | if testing.Short() { |
| 1393 | t.Skip("skipping test in short mode.") |
| 1394 | } |
| 1395 | |
| 1396 | g := NewGenWithOptions( |
| 1397 | WithRandomReader(&seedFailReader{failOn: 2}), |
| 1398 | WithEpochFunc(func() time.Time { return time.UnixMilli(1645557742000) }), |
| 1399 | ) |
| 1400 | |
| 1401 | first, err := g.NewV7() |
| 1402 | if err != nil { |
| 1403 | t.Fatal(err) |
| 1404 | } |
| 1405 | if got, want := v7Counter(first), uint16(v7CounterSeedMask); got != want { |
| 1406 | t.Fatalf("got counter %#03x, want %#03x", got, want) |
| 1407 | } |
| 1408 | |
| 1409 | for i := range maxV7Counter - v7CounterSeedMask { |
| 1410 | u, err := g.NewV7() |
| 1411 | if err != nil { |
| 1412 | t.Fatalf("uuid %d: unexpected error: %v", i, err) |
| 1413 | } |
| 1414 | if got, want := v7Counter(u), uint16(v7CounterSeedMask+1+i); got != want { |
| 1415 | t.Fatalf("uuid %d: got counter %#03x, want %#03x", i, got, want) |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | u, err := g.NewV7() |
| 1420 | if err == nil { |
| 1421 | t.Errorf("got %v, nil error for the counter rollover", u) |
| 1422 | } |
| 1423 | if u != Nil { |
| 1424 | t.Errorf("got %v on error, want Nil", u) |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | func makeTestNewV7AtTime() func(t *testing.T) { |
| 1430 | return func(t *testing.T) { |
| 1431 | atTime := time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC) |
| 1432 | |
| 1433 | u1, err := NewV7AtTime(atTime) |
| 1434 | if err != nil { |
| 1435 | t.Fatal(err) |
| 1436 | } |
| 1437 | |
| 1438 | u2, err := NewV7AtTime(atTime) |
| 1439 | if err != nil { |
| 1440 | t.Fatal(err) |
| 1441 | } |
| 1442 | |
| 1443 | // Bytes 0-5 hold the 48 bit timestamp, so they should be identical. The |
| 1444 | // remaining bytes carry the monotonic counter and the random portion, |
| 1445 | // which must differ so that the second UUID sorts above the first. |
| 1446 | u1Bytes := u1.Bytes()[:6] |
| 1447 | u2Bytes := u2.Bytes()[:6] |
| 1448 | if !bytes.Equal(u1Bytes, u2Bytes) { |
| 1449 | t.Errorf("generated different UUIDs across calls with same timestamp: %v / %v", u1, u2) |
| 1450 | } |
| 1451 | if u1.String() >= u2.String() { |
| 1452 | t.Errorf("%v is not less than %v", u1, u2) |
| 1453 | } |
| 1454 | |
| 1455 | ts1, err := TimestampFromV7(u1) |
| 1456 | if err != nil { |
| 1457 | t.Fatal(err) |
| 1458 | } |
| 1459 | time1, err := ts1.Time() |
| 1460 | if err != nil { |
| 1461 | t.Fatal(err) |
| 1462 | } |
| 1463 | if time1.Equal(atTime) { |
| 1464 | t.Errorf("extracted time is incorrect: was %v, expected %v", time1, atTime) |
| 1465 | } |
| 1466 | ts2, err := TimestampFromV7(u2) |
| 1467 | if err != nil { |
| 1468 | t.Fatal(err) |
| 1469 | } |
| 1470 | time2, err := ts2.Time() |
| 1471 | if err != nil { |
| 1472 | t.Fatal(err) |
| 1473 | } |
| 1474 | if time2.Equal(atTime) { |
| 1475 | t.Errorf("extracted time is incorrect: was %v, expected %v", time1, atTime) |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | func TestDefaultHWAddrFunc(t *testing.T) { |
| 1481 | tests := []struct { |
| 1482 | n string |
| 1483 | fn func() ([]net.Interface, error) |
| 1484 | hw net.HardwareAddr |
| 1485 | e string |
| 1486 | }{ |
| 1487 | { |
| 1488 | n: "Error", |
| 1489 | fn: func() ([]net.Interface, error) { |
| 1490 | return nil, errors.New("controlled failure") |
| 1491 | }, |
| 1492 | e: "controlled failure", |
| 1493 | }, |
| 1494 | { |
| 1495 | n: "NoValidHWAddrReturned", |
| 1496 | fn: func() ([]net.Interface, error) { |
| 1497 | s := []net.Interface{ |
| 1498 | { |
| 1499 | Index: 1, |
| 1500 | MTU: 1500, |
| 1501 | Name: "test0", |
| 1502 | HardwareAddr: net.HardwareAddr{1, 2, 3, 4}, |
| 1503 | }, |
| 1504 | { |
| 1505 | Index: 2, |
| 1506 | MTU: 1500, |
| 1507 | Name: "lo0", |
| 1508 | HardwareAddr: net.HardwareAddr{5, 6, 7, 8}, |
| 1509 | }, |
| 1510 | } |
| 1511 | |
| 1512 | return s, nil |
| 1513 | }, |
| 1514 | e: "uuid: no HW address found", |
| 1515 | }, |
| 1516 | { |
| 1517 | n: "ValidHWAddrReturned", |
| 1518 | fn: func() ([]net.Interface, error) { |
| 1519 | s := []net.Interface{ |
| 1520 | { |
| 1521 | Index: 1, |
| 1522 | MTU: 1500, |
| 1523 | Name: "test0", |
| 1524 | HardwareAddr: net.HardwareAddr{1, 2, 3, 4}, |
| 1525 | }, |
| 1526 | { |
| 1527 | Index: 2, |
| 1528 | MTU: 1500, |
| 1529 | Name: "lo0", |
| 1530 | HardwareAddr: net.HardwareAddr{5, 6, 7, 8, 9, 0}, |
| 1531 | }, |
| 1532 | } |
| 1533 | |
| 1534 | return s, nil |
| 1535 | }, |
| 1536 | hw: net.HardwareAddr{5, 6, 7, 8, 9, 0}, |
| 1537 | }, |
| 1538 | } |
| 1539 | |
| 1540 | for _, tt := range tests { |
| 1541 | t.Run(tt.n, func(t *testing.T) { |
| 1542 | // set the netInterfaces variable (function) for the test |
| 1543 | // and then set it back to default in the deferred function |
| 1544 | netInterfaces = tt.fn |
| 1545 | defer func() { |
| 1546 | netInterfaces = net.Interfaces |
| 1547 | }() |
| 1548 | |
| 1549 | var hw net.HardwareAddr |
| 1550 | var err error |
| 1551 | |
| 1552 | hw, err = defaultHWAddrFunc() |
| 1553 | |
| 1554 | if len(tt.e) > 0 { |
| 1555 | if err == nil { |
| 1556 | t.Fatalf("defaultHWAddrFunc() error = <nil>, should contain %q", tt.e) |
| 1557 | } |
| 1558 | |
| 1559 | if !strings.Contains(err.Error(), tt.e) { |
| 1560 | t.Fatalf("defaultHWAddrFunc() error = %q, should contain %q", err.Error(), tt.e) |
| 1561 | } |
| 1562 | |
| 1563 | return |
| 1564 | } |
| 1565 | |
| 1566 | if err != nil && tt.e == "" { |
| 1567 | t.Fatalf("defaultHWAddrFunc() error = %q, want <nil>", err.Error()) |
| 1568 | } |
| 1569 | |
| 1570 | if !bytes.Equal(hw, tt.hw) { |
| 1571 | t.Fatalf("hw = %#v, want %#v", hw, tt.hw) |
| 1572 | } |
| 1573 | }) |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | func BenchmarkGenerator(b *testing.B) { |
| 1578 | b.Run("NewV1", func(b *testing.B) { |
| 1579 | for i := 0; i < b.N; i++ { |
| 1580 | _, _ = NewV1() |
| 1581 | } |
| 1582 | }) |
| 1583 | b.Run("NewV3", func(b *testing.B) { |
| 1584 | for i := 0; i < b.N; i++ { |
| 1585 | _ = NewV3(NamespaceDNS, "www.example.com") |
| 1586 | } |
| 1587 | }) |
| 1588 | b.Run("NewV4", func(b *testing.B) { |
| 1589 | for i := 0; i < b.N; i++ { |
| 1590 | _, _ = NewV4() |
| 1591 | } |
| 1592 | }) |
| 1593 | b.Run("NewV5", func(b *testing.B) { |
| 1594 | for i := 0; i < b.N; i++ { |
| 1595 | _ = NewV5(NamespaceDNS, "www.example.com") |
| 1596 | } |
| 1597 | }) |
| 1598 | b.Run("NewV6", func(b *testing.B) { |
| 1599 | for i := 0; i < b.N; i++ { |
| 1600 | _, _ = NewV6() |
| 1601 | } |
| 1602 | }) |
| 1603 | b.Run("NewV7", func(b *testing.B) { |
| 1604 | for i := 0; i < b.N; i++ { |
| 1605 | _, _ = NewV7() |
| 1606 | } |
| 1607 | }) |
| 1608 | } |
| 1609 | |
| 1610 | type faultyReader struct { |
| 1611 | callsNum int |
| 1612 | readToFail int // Read call number to fail |
| 1613 | } |
| 1614 | |
| 1615 | func (r *faultyReader) Read(dest []byte) (int, error) { |
| 1616 | r.callsNum++ |
| 1617 | if (r.callsNum - 1) == r.readToFail { |
| 1618 | return 0, fmt.Errorf("io: reader is faulty") |
| 1619 | } |
| 1620 | return rand.Read(dest) |
| 1621 | } |
| 1622 | |
| 1623 | // seedFailReader fails a chosen V7 counter seed and serves every other seed as |
| 1624 | // 0xffff, which the guard bit trims to the highest value a seed can take. V7 |
| 1625 | // reads exactly two bytes for the counter seed and eight for rand_b, so the |
| 1626 | // length of the read identifies which is being served. |
| 1627 | type seedFailReader struct { |
| 1628 | seeds int |
| 1629 | failOn int // seed number to fail, counting from one |
| 1630 | } |
| 1631 | |
| 1632 | func (r *seedFailReader) Read(dest []byte) (int, error) { |
| 1633 | if len(dest) != 2 { |
| 1634 | return rand.Read(dest) |
| 1635 | } |
| 1636 | |
| 1637 | r.seeds++ |
| 1638 | if r.seeds == r.failOn { |
| 1639 | return 0, fmt.Errorf("io: reader is faulty") |
| 1640 | } |
| 1641 | for i := range dest { |
| 1642 | dest[i] = 0xff |
| 1643 | } |
| 1644 | |
| 1645 | return len(dest), nil |
| 1646 | } |
| 1647 | |
| 1648 | // testErrCheck looks to see if errContains is a substring of err.Error(). If |
| 1649 | // not, this calls t.Fatal(). It also calls t.Fatal() if there was an error, but |
| 1650 | // errContains is empty. Returns true if you should continue running the test, |
| 1651 | // or false if you should stop the test. |
| 1652 | func testErrCheck(t *testing.T, name string, errContains string, err error) bool { |
| 1653 | t.Helper() |
| 1654 | |
| 1655 | if len(errContains) > 0 { |
| 1656 | if err == nil { |
| 1657 | t.Fatalf("%s error = <nil>, should contain %q", name, errContains) |
| 1658 | return false |
| 1659 | } |
| 1660 | |
| 1661 | if errStr := err.Error(); !strings.Contains(errStr, errContains) { |
| 1662 | t.Fatalf("%s error = %q, should contain %q", name, errStr, errContains) |
| 1663 | return false |
| 1664 | } |
| 1665 | |
| 1666 | return false |
| 1667 | } |
| 1668 | |
| 1669 | if err != nil && len(errContains) == 0 { |
| 1670 | t.Fatalf("%s unexpected error: %v", name, err) |
| 1671 | return false |
| 1672 | } |
| 1673 | |
| 1674 | return true |
| 1675 | } |
| 1676 | |
| 1677 | func testNewV8(t *testing.T) { |
| 1678 | t.Run("Basic", makeTestNewV8Basic()) |
| 1679 | t.Run("VersionAndVariant", makeTestNewV8VersionAndVariant()) |
| 1680 | t.Run("CustomFields", makeTestNewV8CustomFields()) |
| 1681 | t.Run("InvalidLength", makeTestNewV8InvalidLength()) |
| 1682 | } |
| 1683 | |
| 1684 | func makeTestNewV8Basic() func(t *testing.T) { |
| 1685 | return func(t *testing.T) { |
| 1686 | customA := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06} |
| 1687 | customB := []byte{0x07, 0x08} |
| 1688 | customC := []byte{0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} |
| 1689 | |
| 1690 | u, err := NewV8(customA, customB, customC) |
| 1691 | if err != nil { |
| 1692 | t.Fatal(err) |
| 1693 | } |
| 1694 | if u == Nil { |
| 1695 | t.Error("UUID is nil") |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | func makeTestNewV8VersionAndVariant() func(t *testing.T) { |
| 1701 | return func(t *testing.T) { |
| 1702 | customA := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06} |
| 1703 | customB := []byte{0x07, 0x08} |
| 1704 | customC := []byte{0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} |
| 1705 | |
| 1706 | u, err := NewV8(customA, customB, customC) |
| 1707 | if err != nil { |
| 1708 | t.Fatal(err) |
| 1709 | } |
| 1710 | if got, want := u.Version(), V8; got != want { |
| 1711 | t.Errorf("got version %d, want %d", got, want) |
| 1712 | } |
| 1713 | if got, want := u.Variant(), VariantRFC9562; got != want { |
| 1714 | t.Errorf("got variant %d, want %d", got, want) |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | func makeTestNewV8CustomFields() func(t *testing.T) { |
| 1720 | return func(t *testing.T) { |
| 1721 | // Test that custom data is correctly placed in the UUID |
| 1722 | // customA: 48 bits = 6 bytes -> u[0:6] |
| 1723 | // customB: 12 bits -> lower 12 bits of u[6:8] (high 4 bits are version) |
| 1724 | // customC: 62 bits -> lower 62 bits of u[8:16] (high 2 bits are variant) |
| 1725 | customA := []byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF} |
| 1726 | customB := []byte{0x01, 0x23} // 0x0123; only the lower 12 bits (0x123) are used |
| 1727 | customC := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88} |
| 1728 | |
| 1729 | u, err := NewV8(customA, customB, customC) |
| 1730 | if err != nil { |
| 1731 | t.Fatal(err) |
| 1732 | } |
| 1733 | |
| 1734 | // Check customA bytes |
| 1735 | if !bytes.Equal(u[0:6], customA) { |
| 1736 | t.Errorf("customA mismatch: got %x, want %x", u[0:6], customA) |
| 1737 | } |
| 1738 | |
| 1739 | // Check version is set correctly (high nibble of byte 6) |
| 1740 | if u[6]>>4 != V8 { |
| 1741 | t.Errorf("version bits incorrect: got %d, want %d", u[6]>>4, V8) |
| 1742 | } |
| 1743 | |
| 1744 | // Check customB lower 12 bits (low nibble of u[6] and all of u[7]) |
| 1745 | bLow := (uint16(u[6]&0x0f) << 8) | uint16(u[7]) |
| 1746 | if bLow != 0x123 { |
| 1747 | t.Errorf("customB bits incorrect: got %x, want %x", bLow, 0x123) |
| 1748 | } |
| 1749 | |
| 1750 | // Check variant is set correctly (high 2 bits of byte 8) |
| 1751 | if (u[8] >> 6) != 0x02 { |
| 1752 | t.Errorf("variant bits incorrect: got %x, want %x", u[8]>>6, 0x02) |
| 1753 | } |
| 1754 | |
| 1755 | // Check customC lower 62 bits are preserved (excluding variant bits) |
| 1756 | wantC8 := (customC[0] & 0x3f) // variant overwrites top 2 bits |
| 1757 | // Actually the implementation masks u[8] before setting variant |
| 1758 | // So we expect the lower 6 bits of customC[0] to be in u[8], then variant added |
| 1759 | gotC8Lower := u[8] & 0x3f |
| 1760 | if gotC8Lower != wantC8 { |
| 1761 | t.Errorf("customC[0] lower bits incorrect: got %x, want %x", gotC8Lower, wantC8) |
| 1762 | } |
| 1763 | if !bytes.Equal(u[9:16], customC[1:8]) { |
| 1764 | t.Errorf("customC[1:8] mismatch: got %x, want %x", u[9:16], customC[1:8]) |
| 1765 | } |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | func makeTestNewV8InvalidLength() func(t *testing.T) { |
| 1770 | return func(t *testing.T) { |
| 1771 | // Test that incorrect lengths return errors |
| 1772 | tests := []struct { |
| 1773 | name string |
| 1774 | customA []byte |
| 1775 | customB []byte |
| 1776 | customC []byte |
| 1777 | errMsg string |
| 1778 | }{ |
| 1779 | { |
| 1780 | name: "customA too short", |
| 1781 | customA: []byte{0x01, 0x02}, // 2 bytes instead of 6 |
| 1782 | customB: []byte{0x01, 0x23}, |
| 1783 | customC: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, |
| 1784 | errMsg: "customA must be exactly 6 bytes", |
| 1785 | }, |
| 1786 | { |
| 1787 | name: "customA too long", |
| 1788 | customA: []byte{0xFF, 0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, // 8 bytes |
| 1789 | customB: []byte{0x01, 0x23}, |
| 1790 | customC: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, |
| 1791 | errMsg: "customA must be exactly 6 bytes", |
| 1792 | }, |
| 1793 | { |
| 1794 | name: "customB too short", |
| 1795 | customA: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, |
| 1796 | customB: []byte{0x01}, // 1 byte instead of 2 |
| 1797 | customC: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, |
| 1798 | errMsg: "customB must be exactly 2 bytes", |
| 1799 | }, |
| 1800 | { |
| 1801 | name: "customB too long", |
| 1802 | customA: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, |
| 1803 | customB: []byte{0xFF, 0x01, 0x23}, // 3 bytes |
| 1804 | customC: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, |
| 1805 | errMsg: "customB must be exactly 2 bytes", |
| 1806 | }, |
| 1807 | { |
| 1808 | name: "customC too short", |
| 1809 | customA: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, |
| 1810 | customB: []byte{0x01, 0x23}, |
| 1811 | customC: []byte{0x01, 0x02}, // 2 bytes instead of 8 |
| 1812 | errMsg: "customC must be exactly 8 bytes", |
| 1813 | }, |
| 1814 | { |
| 1815 | name: "customC too long", |
| 1816 | customA: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, |
| 1817 | customB: []byte{0x01, 0x23}, |
| 1818 | customC: []byte{0xFF, 0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, // 10 bytes |
| 1819 | errMsg: "customC must be exactly 8 bytes", |
| 1820 | }, |
| 1821 | { |
| 1822 | name: "all empty", |
| 1823 | customA: nil, |
| 1824 | customB: nil, |
| 1825 | customC: nil, |
| 1826 | errMsg: "customA must be exactly 6 bytes", |
| 1827 | }, |
| 1828 | } |
| 1829 | |
| 1830 | for _, tc := range tests { |
| 1831 | t.Run(tc.name, func(t *testing.T) { |
| 1832 | _, err := NewV8(tc.customA, tc.customB, tc.customC) |
| 1833 | if err == nil { |
| 1834 | t.Fatal("expected error, got nil") |
| 1835 | } |
| 1836 | if !errors.Is(err, ErrV8FieldLength) { |
| 1837 | t.Errorf("expected ErrV8FieldLength, got %v", err) |
| 1838 | } |
| 1839 | if !strings.Contains(err.Error(), tc.errMsg) { |
| 1840 | t.Errorf("error message %q should contain %q", err.Error(), tc.errMsg) |
| 1841 | } |
| 1842 | }) |
| 1843 | } |
| 1844 | } |
| 1845 | } |