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
|
// Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package uuid
import (
"bytes"
"crypto/rand"
"fmt"
"net"
"testing"
"time"
)
func TestGenerator(t *testing.T) {
t.Run("NewV1", testNewV1)
t.Run("NewV2", testNewV2)
t.Run("NewV3", testNewV3)
t.Run("NewV4", testNewV4)
t.Run("NewV5", testNewV5)
}
func testNewV1(t *testing.T) {
t.Run("Basic", testNewV1Basic)
t.Run("DifferentAcrossCalls", testNewV1DifferentAcrossCalls)
t.Run("StaleEpoch", testNewV1StaleEpoch)
t.Run("FaultyRand", testNewV1FaultyRand)
t.Run("MissingNetwork", testNewV1MissingNetwork)
t.Run("MissingNetworkFaultyRand", testNewV1MissingNetworkFaultyRand)
}
func TestNewGenWithHWAF(t *testing.T) {
addr := []byte{0, 1, 2, 3, 4, 42}
fn := func() (net.HardwareAddr, error) {
return addr, nil
}
var g *Gen
var err error
var uuid UUID
g = NewGenWithHWAF(fn)
if g == nil {
t.Fatal("g is unexpectedly nil")
}
uuid, err = g.NewV1()
if err != nil {
t.Fatalf("g.NewV1() err = %v, want <nil>", err)
}
node := uuid[10:]
if !bytes.Equal(addr, node) {
t.Fatalf("node = %v, want %v", node, addr)
}
}
func testNewV1Basic(t *testing.T) {
u, err := NewV1()
if err != nil {
t.Fatal(err)
}
if got, want := u.Version(), V1; got != want {
t.Errorf("generated UUID with version %d, want %d", got, want)
}
if got, want := u.Variant(), VariantRFC4122; got != want {
t.Errorf("generated UUID with variant %d, want %d", got, want)
}
}
func testNewV1DifferentAcrossCalls(t *testing.T) {
u1, err := NewV1()
if err != nil {
t.Fatal(err)
}
u2, err := NewV1()
if err != nil {
t.Fatal(err)
}
if u1 == u2 {
t.Errorf("generated identical UUIDs across calls: %v", u1)
}
}
func testNewV1StaleEpoch(t *testing.T) {
g := &Gen{
epochFunc: func() time.Time {
return time.Unix(0, 0)
},
hwAddrFunc: defaultHWAddrFunc,
rand: rand.Reader,
}
u1, err := g.NewV1()
if err != nil {
t.Fatal(err)
}
u2, err := g.NewV1()
if err != nil {
t.Fatal(err)
}
if u1 == u2 {
t.Errorf("generated identical UUIDs across calls: %v", u1)
}
}
func testNewV1FaultyRand(t *testing.T) {
g := &Gen{
epochFunc: time.Now,
hwAddrFunc: defaultHWAddrFunc,
rand: &faultyReader{
readToFail: 0, // fail immediately
},
}
u, err := g.NewV1()
if err == nil {
t.Fatalf("got %v, want error", u)
}
if u != Nil {
t.Fatalf("got %v on error, want Nil", u)
}
}
func testNewV1MissingNetwork(t *testing.T) {
g := &Gen{
epochFunc: time.Now,
hwAddrFunc: func() (net.HardwareAddr, error) {
return []byte{}, fmt.Errorf("uuid: no hw address found")
},
rand: rand.Reader,
}
_, err := g.NewV1()
if err != nil {
t.Errorf("did not handle missing network interfaces: %v", err)
}
}
func testNewV1MissingNetworkFaultyRand(t *testing.T) {
g := &Gen{
epochFunc: time.Now,
hwAddrFunc: func() (net.HardwareAddr, error) {
return []byte{}, fmt.Errorf("uuid: no hw address found")
},
rand: &faultyReader{
readToFail: 1,
},
}
u, err := g.NewV1()
if err == nil {
t.Errorf("did not error on faulty reader and missing network, got %v", u)
}
}
func testNewV2(t *testing.T) {
t.Run("Basic", testNewV2Basic)
t.Run("DifferentAcrossCalls", testNewV2DifferentAcrossCalls)
t.Run("FaultyRand", testNewV2FaultyRand)
}
func testNewV2Basic(t *testing.T) {
domains := []byte{
DomainPerson,
DomainGroup,
DomainOrg,
}
for _, domain := range domains {
u, err := NewV2(domain)
if err != nil {
t.Errorf("NewV2(%d): %v", domain, err)
}
if got, want := u.Version(), V2; got != want {
t.Errorf("NewV2(%d) generated UUID with version %d, want %d", domain, got, want)
}
if got, want := u.Variant(), VariantRFC4122; got != want {
t.Errorf("NewV2(%d) generated UUID with variant %d, want %d", domain, got, want)
}
}
}
func testNewV2DifferentAcrossCalls(t *testing.T) {
u1, err := NewV2(DomainOrg)
if err != nil {
t.Fatal(err)
}
u2, err := NewV2(DomainOrg)
if err != nil {
t.Fatal(err)
}
if u1 == u2 {
t.Errorf("generated identical UUIDs across calls: %v", u1)
}
}
func testNewV2FaultyRand(t *testing.T) {
g := &Gen{
epochFunc: time.Now,
hwAddrFunc: defaultHWAddrFunc,
rand: &faultyReader{
readToFail: 0, // fail immediately
},
}
u, err := g.NewV2(DomainPerson)
if err == nil {
t.Fatalf("got %v, want error", u)
}
if u != Nil {
t.Fatalf("got %v on error, want Nil", u)
}
}
func testNewV3(t *testing.T) {
t.Run("Basic", testNewV3Basic)
t.Run("EqualNames", testNewV3EqualNames)
t.Run("DifferentNamespaces", testNewV3DifferentNamespaces)
}
func testNewV3Basic(t *testing.T) {
ns := NamespaceDNS
name := "www.example.com"
u := NewV3(ns, name)
if got, want := u.Version(), V3; got != want {
t.Errorf("NewV3(%v, %q): got version %d, want %d", ns, name, got, want)
}
if got, want := u.Variant(), VariantRFC4122; got != want {
t.Errorf("NewV3(%v, %q): got variant %d, want %d", ns, name, got, want)
}
want := "5df41881-3aed-3515-88a7-2f4a814cf09e"
if got := u.String(); got != want {
t.Errorf("NewV3(%v, %q) = %q, want %q", ns, name, got, want)
}
}
func testNewV3EqualNames(t *testing.T) {
ns := NamespaceDNS
name := "example.com"
u1 := NewV3(ns, name)
u2 := NewV3(ns, name)
if u1 != u2 {
t.Errorf("NewV3(%v, %q) generated %v and %v across two calls", ns, name, u1, u2)
}
}
func testNewV3DifferentNamespaces(t *testing.T) {
name := "example.com"
ns1 := NamespaceDNS
ns2 := NamespaceURL
u1 := NewV3(ns1, name)
u2 := NewV3(ns2, name)
if u1 == u2 {
t.Errorf("NewV3(%v, %q) == NewV3(%d, %q) (%v)", ns1, name, ns2, name, u1)
}
}
func testNewV4(t *testing.T) {
t.Run("Basic", testNewV4Basic)
t.Run("DifferentAcrossCalls", testNewV4DifferentAcrossCalls)
t.Run("FaultyRand", testNewV4FaultyRand)
t.Run("ShortRandomRead", testNewV4ShortRandomRead)
}
func testNewV4Basic(t *testing.T) {
u, err := NewV4()
if err != nil {
t.Fatal(err)
}
if got, want := u.Version(), V4; got != want {
t.Errorf("got version %d, want %d", got, want)
}
if got, want := u.Variant(), VariantRFC4122; got != want {
t.Errorf("got variant %d, want %d", got, want)
}
}
func testNewV4DifferentAcrossCalls(t *testing.T) {
u1, err := NewV4()
if err != nil {
t.Fatal(err)
}
u2, err := NewV4()
if err != nil {
t.Fatal(err)
}
if u1 == u2 {
t.Errorf("generated identical UUIDs across calls: %v", u1)
}
}
func testNewV4FaultyRand(t *testing.T) {
g := &Gen{
epochFunc: time.Now,
hwAddrFunc: defaultHWAddrFunc,
rand: &faultyReader{
readToFail: 0, // fail immediately
},
}
u, err := g.NewV4()
if err == nil {
t.Errorf("got %v, nil error", u)
}
}
func testNewV4ShortRandomRead(t *testing.T) {
g := &Gen{
epochFunc: time.Now,
hwAddrFunc: func() (net.HardwareAddr, error) {
return []byte{}, fmt.Errorf("uuid: no hw address found")
},
rand: bytes.NewReader([]byte{42}),
}
u, err := g.NewV4()
if err == nil {
t.Errorf("got %v, nil error", u)
}
}
func testNewV5(t *testing.T) {
t.Run("Basic", testNewV5Basic)
t.Run("EqualNames", testNewV5EqualNames)
t.Run("DifferentNamespaces", testNewV5DifferentNamespaces)
}
func testNewV5Basic(t *testing.T) {
ns := NamespaceDNS
name := "www.example.com"
u := NewV5(ns, name)
if got, want := u.Version(), V5; got != want {
t.Errorf("NewV5(%v, %q): got version %d, want %d", ns, name, got, want)
}
if got, want := u.Variant(), VariantRFC4122; got != want {
t.Errorf("NewV5(%v, %q): got variant %d, want %d", ns, name, got, want)
}
want := "2ed6657d-e927-568b-95e1-2665a8aea6a2"
if got := u.String(); got != want {
t.Errorf("NewV5(%v, %q) = %q, want %q", ns, name, got, want)
}
}
func testNewV5EqualNames(t *testing.T) {
ns := NamespaceDNS
name := "example.com"
u1 := NewV5(ns, name)
u2 := NewV5(ns, name)
if u1 != u2 {
t.Errorf("NewV5(%v, %q) generated %v and %v across two calls", ns, name, u1, u2)
}
}
func testNewV5DifferentNamespaces(t *testing.T) {
name := "example.com"
ns1 := NamespaceDNS
ns2 := NamespaceURL
u1 := NewV5(ns1, name)
u2 := NewV5(ns2, name)
if u1 == u2 {
t.Errorf("NewV5(%v, %q) == NewV5(%v, %q) (%v)", ns1, name, ns2, name, u1)
}
}
func BenchmarkGenerator(b *testing.B) {
b.Run("NewV1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV1()
}
})
b.Run("NewV2", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV2(DomainOrg)
}
})
b.Run("NewV3", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV3(NamespaceDNS, "www.example.com")
}
})
b.Run("NewV4", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV4()
}
})
b.Run("NewV5", func(b *testing.B) {
for i := 0; i < b.N; i++ {
NewV5(NamespaceDNS, "www.example.com")
}
})
}
type faultyReader struct {
callsNum int
readToFail int // Read call number to fail
}
func (r *faultyReader) Read(dest []byte) (int, error) {
r.callsNum++
if (r.callsNum - 1) == r.readToFail {
return 0, fmt.Errorf("io: reader is faulty")
}
return rand.Read(dest)
}
|