github.com/safing/portbase@v0.19.5/utils/uuid_test.go (about) 1 package utils 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/gofrs/uuid" 8 ) 9 10 func TestUUID(t *testing.T) { 11 t.Parallel() 12 13 // check randomness 14 a := RandomUUID("") 15 a2 := RandomUUID("") 16 if a.String() == a2.String() { 17 t.Error("should not match") 18 } 19 20 // check with input 21 b := RandomUUID("b") 22 b2 := RandomUUID("b") 23 if b.String() == b2.String() { 24 t.Error("should not match") 25 } 26 27 // check with long input 28 c := RandomUUID("TG8UkxS+4rVrDxHtDAaNab1CBpygzmX1g5mJA37jbQ5q2uE4rVrDxHtDAaNab1CBpygzmX1g5mJA37jbQ5q2uE") 29 c2 := RandomUUID("TG8UkxS+4rVrDxHtDAaNab1CBpygzmX1g5mJA37jbQ5q2uE4rVrDxHtDAaNab1CBpygzmX1g5mJA37jbQ5q2uE") 30 if c.String() == c2.String() { 31 t.Error("should not match") 32 } 33 34 // check for nanosecond precision 35 d := uuidFromTime() 36 time.Sleep(2 * time.Nanosecond) 37 d2 := uuidFromTime() 38 if d.String() == d2.String() { 39 t.Error("should not match") 40 } 41 42 // check mixing 43 timeUUID := uuidFromTime() 44 e := uuid.NewV5(timeUUID, "e") 45 e2 := uuid.NewV5(timeUUID, "e2") 46 if e.String() == e2.String() { 47 t.Error("should not match") 48 } 49 50 // check deriving 51 f := DerivedUUID("f") 52 f2 := DerivedUUID("f") 53 f3 := DerivedUUID("f3") 54 if f.String() != f2.String() { 55 t.Error("should match") 56 } 57 if f.String() == f3.String() { 58 t.Error("should not match") 59 } 60 61 // check instance deriving 62 g := DerivedInstanceUUID("g") 63 g2 := DerivedInstanceUUID("g") 64 g3 := DerivedInstanceUUID("g3") 65 if g.String() != g2.String() { 66 t.Error("should match") 67 } 68 if g.String() == g3.String() { 69 t.Error("should not match") 70 } 71 }