| 1 |
package uuid |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/rand" |
| 5 |
"errors" |
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
"sync" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
) |
| 11 |
|
| 12 |
var ( |
| 13 |
nodeBytes = []byte{0xdd, 0xee, 0xff, 0xaa, 0xbb, 0x44, 0xcc} |
| 14 |
) |
| 15 |
|
| 16 |
func init() { |
| 17 |
generator.init() |
| 18 |
} |
| 19 |
|
| 20 |
func TestGenerator_V1(t *testing.T) { |
| 21 |
u := generator.NewV1() |
| 22 |
|
| 23 |
assert.Equal(t, One, u.Version(), "Expected correct version") |
| 24 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 25 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 26 |
} |
| 27 |
|
| 28 |
func TestGenerator_V2(t *testing.T) { |
| 29 |
u := generator.NewV2(DomainGroup) |
| 30 |
|
| 31 |
assert.Equal(t, Two, u.Version(), "Expected correct version") |
| 32 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 33 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 34 |
assert.Equal(t, uint8(DomainGroup), u.Bytes()[9], "Expected string representation to be valid") |
| 35 |
|
| 36 |
u = generator.NewV2(DomainUser) |
| 37 |
|
| 38 |
assert.Equal(t, Two, u.Version(), "Expected correct version") |
| 39 |
assert.Equal(t, VariantRFC4122, u.Variant(), "Expected correct variant") |
| 40 |
assert.True(t, parseUUIDRegex.MatchString(u.String()), "Expected string representation to be valid") |
| 41 |
assert.Equal(t, uint8(DomainUser), u.Bytes()[9], "Expected string representation to be valid") |
| 42 |
} |
| 43 |
|
| 44 |
func TestRegisterGenerator(t *testing.T) { |
| 45 |
g1 := GeneratorConfig{ |
| 46 |
nil, |
| 47 |
func() Timestamp { |
| 48 |
return Timestamp(145876) |
| 49 |
}, 0, |
| 50 |
func() Node { |
| 51 |
return []byte{0x11, 0xaa, 0xbb, 0xaa, 0xbb, 0xcc} |
| 52 |
}, |
| 53 |
func([]byte) (int, error) { |
| 54 |
return 58, nil |
| 55 |
}, nil} |
| 56 |
|
| 57 |
once = new(sync.Once) |
| 58 |
RegisterGenerator(g1) |
| 59 |
|
| 60 |
assert.Equal(t, g1.Next(), generator.Next(), "These values should be the same") |
| 61 |
assert.Equal(t, g1.Id(), generator.Id(), "These values should be the same") |
| 62 |
|
| 63 |
n1, err1 := generator.Random(nil) |
| 64 |
n, err := generator.Random(nil) |
| 65 |
assert.Equal(t, n1, n, "Values should be the same") |
| 66 |
assert.Equal(t, err, err1, "Values should be the same") |
| 67 |
assert.NoError(t, err) |
| 68 |
|
| 69 |
assert.True(t, didRegisterGeneratorPanic(g1), "Should panic when invalid") |
| 70 |
} |
| 71 |
|
| 72 |
func didRegisterGeneratorPanic(gen GeneratorConfig) bool { |
| 73 |
return func() (didPanic bool) { |
| 74 |
defer func() { |
| 75 |
if recover() != nil { |
| 76 |
didPanic = true |
| 77 |
} |
| 78 |
}() |
| 79 |
|
| 80 |
RegisterGenerator(gen) |
| 81 |
return |
| 82 |
}() |
| 83 |
} |
| 84 |
|
| 85 |
func TestNewGenerator(t *testing.T) { |
| 86 |
gen := NewGenerator(GeneratorConfig{}) |
| 87 |
|
| 88 |
assert.NotNil(t, gen.Next, "There shoud be a default Next function") |
| 89 |
assert.NotNil(t, gen.Random, "There shoud be a default Random function") |
| 90 |
assert.NotNil(t, gen.HandleError, "There shoud be a default HandleError function") |
| 91 |
assert.NotNil(t, gen.Id, "There shoud be a default Id function") |
| 92 |
|
| 93 |
assert.Equal(t, findFirstHardwareAddress(), gen.Id(), "There shoud be the gieen Id function") |
| 94 |
|
| 95 |
gen = NewGenerator(GeneratorConfig{ |
| 96 |
Id: func() Node { |
| 97 |
return Node{0xaa, 0xff} |
| 98 |
}, |
| 99 |
Next: func() Timestamp { |
| 100 |
return Timestamp(2) |
| 101 |
}, |
| 102 |
HandleError: func(error) bool { |
| 103 |
return true |
| 104 |
}, |
| 105 |
Random: func([]byte) (int, error) { |
| 106 |
return 1, nil |
| 107 |
}, |
| 108 |
Resolution: 0, |
| 109 |
}) |
| 110 |
|
| 111 |
assert.NotNil(t, gen.Next, "There shoud be a default Next function") |
| 112 |
assert.NotNil(t, gen.Random, "There shoud be a default Random function") |
| 113 |
assert.NotNil(t, gen.HandleError, "There shoud be a default HandleError function") |
| 114 |
assert.NotNil(t, gen.Id, "There shoud be a default Id function") |
| 115 |
|
| 116 |
n, err := gen.Random(nil) |
| 117 |
|
| 118 |
assert.Equal(t, Timestamp(2), gen.Next(), "There shoud be the given Next function") |
| 119 |
assert.Equal(t, 1, n, "There shoud be the given Random function") |
| 120 |
assert.NoError(t, err, "There shoud be the given Random function") |
| 121 |
assert.Equal(t, true, gen.HandleError(nil), "There shoud be the given HandleError function") |
| 122 |
assert.Equal(t, Node{0xaa, 0xff}, gen.Id(), "There shoud be the gieen Id function") |
| 123 |
|
| 124 |
gen = NewGenerator(GeneratorConfig{ |
| 125 |
Id: func() Node { |
| 126 |
return []byte{0xaa, 0xff} |
| 127 |
}, |
| 128 |
Next: nil, |
| 129 |
HandleError: func(error) bool { |
| 130 |
return true |
| 131 |
}, |
| 132 |
Random: func([]byte) (int, error) { |
| 133 |
return 1, nil |
| 134 |
}, |
| 135 |
Resolution: 4096, |
| 136 |
}) |
| 137 |
|
| 138 |
n, err = gen.Random(nil) |
| 139 |
|
| 140 |
assert.NotNil(t, gen.Next, "There shoud be a default Next function") |
| 141 |
assert.NotNil(t, gen.Random, "There shoud be a default Random function") |
| 142 |
assert.NotNil(t, gen.HandleError, "There shoud be a default HandleError function") |
| 143 |
assert.NotNil(t, gen.Id, "There shoud be a default Id function") |
| 144 |
|
| 145 |
assert.Equal(t, 1, n, "There shoud be the given Random function") |
| 146 |
assert.NoError(t, err, "There shoud be the given Random function") |
| 147 |
assert.Equal(t, true, gen.HandleError(nil), "There shoud be the given HandleError function") |
| 148 |
assert.Equal(t, Node{0xaa, 0xff}, gen.Id(), "There shoud be the gieen Id function") |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
func TestGeneratorInit(t *testing.T) { |
| 153 |
// A new time that is older than stored time should cause the sequence to increment |
| 154 |
now, node := registerTestGenerator(Now(), nodeBytes) |
| 155 |
storageStamp := registerSaver(now.Add(time.Second), node) |
| 156 |
|
| 157 |
assert.NotNil(t, generator.Store, "Generator should not return an empty store") |
| 158 |
assert.True(t, generator.Timestamp < storageStamp, "Increment sequence when old timestamp newer than new") |
| 159 |
assert.Equal(t, Sequence(3), generator.Sequence, "Successfull read should have incremented sequence") |
| 160 |
|
| 161 |
// Nodes not the same should generate a random sequence |
| 162 |
now, node = registerTestGenerator(Now(), nodeBytes) |
| 163 |
storageStamp = registerSaver(now.Sub(time.Second), []byte{0xaa, 0xee, 0xaa, 0xbb, 0x44, 0xcc}) |
| 164 |
|
| 165 |
assert.NotNil(t, generator.Store, "Generator should not return an empty store") |
| 166 |
assert.True(t, generator.Timestamp > storageStamp, "New timestamp should be newer than old") |
| 167 |
assert.NotEqual(t, Sequence(2), generator.Sequence, "Sequence should not be same as storage") |
| 168 |
assert.NotEqual(t, Sequence(3), generator.Sequence, "Sequence should not be incremented but be random") |
| 169 |
assert.Equal(t, generator.Node, node, generator.Sequence, "Node should be equal") |
| 170 |
|
| 171 |
now, node = registerTestGenerator(Now(), nodeBytes) |
| 172 |
|
| 173 |
// Random read error should alert user |
| 174 |
generator.Random = func(b []byte) (int, error) { |
| 175 |
return 0, errors.New("EOF") |
| 176 |
} |
| 177 |
|
| 178 |
storageStamp = registerSaver(now.Sub(time.Second), []byte{0xaa, 0xee, 0xaa, 0xbb, 0x44, 0xcc}) |
| 179 |
|
| 180 |
assert.Error(t, generator.err, "Read error should exist") |
| 181 |
|
| 182 |
now, node = registerTestGenerator(Now(), nil) |
| 183 |
// Random read error should alert user |
| 184 |
generator.Random = func(b []byte) (int, error) { |
| 185 |
return 0, errors.New("EOF") |
| 186 |
} |
| 187 |
|
| 188 |
storageStamp = registerSaver(now.Sub(time.Second), []byte{0xaa, 0xee, 0xaa, 0xbb, 0x44, 0xcc}) |
| 189 |
|
| 190 |
assert.Error(t, generator.Error(), "Read error should exist") |
| 191 |
|
| 192 |
registerDefaultGenerator() |
| 193 |
} |
| 194 |
|
| 195 |
func TestGeneratorRead(t *testing.T) { |
| 196 |
// A new time that is older than stored time should cause the sequence to increment |
| 197 |
now := Now() |
| 198 |
i := 0 |
| 199 |
|
| 200 |
timestamps := []Timestamp{ |
| 201 |
now.Sub(time.Second), |
| 202 |
now.Sub(time.Second * 2), |
| 203 |
} |
| 204 |
|
| 205 |
generator = NewGenerator(GeneratorConfig{ |
| 206 |
nil, |
| 207 |
func() Timestamp { |
| 208 |
return timestamps[i] |
| 209 |
}, 0, |
| 210 |
func() Node { |
| 211 |
return nodeBytes |
| 212 |
}, |
| 213 |
rand.Read, |
| 214 |
nil}) |
| 215 |
|
| 216 |
storageStamp := registerSaver(now.Add(time.Second), nodeBytes) |
| 217 |
|
| 218 |
i++ |
| 219 |
|
| 220 |
generator.read() |
| 221 |
|
| 222 |
assert.True(t, generator.Timestamp != 0, "Should not return an empty store") |
| 223 |
assert.True(t, generator.Timestamp != 0, "Should not return an empty store") |
| 224 |
assert.NotEmpty(t, generator.Node, "Should not return an empty store") |
| 225 |
|
| 226 |
assert.True(t, generator.Timestamp < storageStamp, "Increment sequence when old timestamp newer than new") |
| 227 |
assert.Equal(t, Sequence(4), generator.Sequence, "Successfull read should have incremented sequence") |
| 228 |
|
| 229 |
// A new time that is older than stored time should cause the sequence to increment |
| 230 |
now, node := registerTestGenerator(Now().Sub(time.Second), nodeBytes) |
| 231 |
storageStamp = registerSaver(now.Add(time.Second), node) |
| 232 |
|
| 233 |
generator.read() |
| 234 |
|
| 235 |
assert.NotEqual(t, 0, generator.Sequence, "Should return an empty store") |
| 236 |
assert.NotEmpty(t, generator.Node, "Should not return an empty store") |
| 237 |
|
| 238 |
// A new time that is older than stored time should cause the sequence to increment |
| 239 |
registerTestGenerator(Now().Sub(time.Second), nil) |
| 240 |
storageStamp = registerSaver(now.Add(time.Second), []byte{0xdd, 0xee, 0xff, 0xaa, 0xbb}) |
| 241 |
|
| 242 |
generator.read() |
| 243 |
|
| 244 |
assert.NotEmpty(t, generator.Store, "Should not return an empty store") |
| 245 |
assert.NotEqual(t, []byte{0xdd, 0xee, 0xff, 0xaa, 0xbb}, generator.Node, "Should not return an empty store") |
| 246 |
|
| 247 |
registerDefaultGenerator() |
| 248 |
} |
| 249 |
|
| 250 |
func TestGeneratorRandom(t *testing.T) { |
| 251 |
registerTestGenerator(Now(), []byte{0xdd, 0xee, 0xff, 0xaa, 0xbb}) |
| 252 |
|
| 253 |
b := make([]byte, 6) |
| 254 |
n, err := generator.Random(b) |
| 255 |
|
| 256 |
assert.NoError(t, err, "There should No be an error", err) |
| 257 |
assert.NotEmpty(t, b, "There should be random data in the slice") |
| 258 |
assert.Equal(t, 6, n, "Amount read should be same as length") |
| 259 |
|
| 260 |
generator.Random = func(b []byte) (int, error) { |
| 261 |
for i := 0; i < len(b); i++ { |
| 262 |
b[i] = byte(i) |
| 263 |
} |
| 264 |
return len(b), nil |
| 265 |
} |
| 266 |
|
| 267 |
b = make([]byte, 6) |
| 268 |
n, err = generator.Random(b) |
| 269 |
assert.NoError(t, err, "There should No be an error", err) |
| 270 |
assert.NotEmpty(t, b, "There should be random data in the slice") |
| 271 |
assert.Equal(t, 6, n, "Amount read should be same as length") |
| 272 |
|
| 273 |
generator.Random = func(b []byte) (int, error) { |
| 274 |
return 0, errors.New("EOF") |
| 275 |
} |
| 276 |
|
| 277 |
generator.HandleError = func(error) bool { |
| 278 |
return false |
| 279 |
} |
| 280 |
|
| 281 |
b = make([]byte, 6) |
| 282 |
c := []byte{} |
| 283 |
c = append(c, b...) |
| 284 |
|
| 285 |
n, err = generator.Random(b) |
| 286 |
assert.Error(t, err, "There should be an error", err) |
| 287 |
assert.Equal(t, 0, n, "Amount read should be same as length") |
| 288 |
assert.Equal(t, c, b, "Slice should be empty") |
| 289 |
|
| 290 |
id := NewV4() |
| 291 |
assert.Nil(t, id, "There should be no id") |
| 292 |
assert.Error(t, generator.err, "There should be an error [%s]", err) |
| 293 |
|
| 294 |
generator.HandleError = func(error) bool { |
| 295 |
return true |
| 296 |
} |
| 297 |
|
| 298 |
assert.Nil(t, NewV4(), "NewV4 should be nil") |
| 299 |
assert.Error(t, generator.err, "There should be an error [%s]", err) |
| 300 |
|
| 301 |
generator.HandleError = runHandleError |
| 302 |
|
| 303 |
assert.Panics(t, didNewV4Panic, "NewV4 should panic when invalid") |
| 304 |
assert.Error(t, generator.err, "There should be an error [%s]", err) |
| 305 |
|
| 306 |
generator.HandleError = func(error) bool { |
| 307 |
generator.Random = func([]byte) (int, error) { |
| 308 |
return 1, nil |
| 309 |
} |
| 310 |
return true |
| 311 |
} |
| 312 |
|
| 313 |
id = NewV4() |
| 314 |
assert.NotNil(t, id, "Should get an id") |
| 315 |
assert.NoError(t, generator.err, "There should be no error [%s]", err) |
| 316 |
|
| 317 |
registerDefaultGenerator() |
| 318 |
} |
| 319 |
|
| 320 |
func didNewV4Panic() { |
| 321 |
NewV4() |
| 322 |
} |
| 323 |
|
| 324 |
func TestGeneratorSave(t *testing.T) { |
| 325 |
registerTestGenerator(Now(), []byte{0xdd, 0xee, 0xff, 0xaa, 0xbb}) |
| 326 |
generator.Do(generator.init) |
| 327 |
generator.read() |
| 328 |
generator.save() |
| 329 |
registerDefaultGenerator() |
| 330 |
} |
| 331 |
|
| 332 |
func TestGetHardwareAddress(t *testing.T) { |
| 333 |
a := findFirstHardwareAddress() |
| 334 |
assert.NotEmpty(t, a, "There should be a node id") |
| 335 |
} |
| 336 |
|
| 337 |
func registerTestGenerator(pNow Timestamp, pId Node) (Timestamp, Node) { |
| 338 |
generator = NewGenerator(GeneratorConfig{ |
| 339 |
nil, |
| 340 |
func() Timestamp { |
| 341 |
return pNow |
| 342 |
}, 0, |
| 343 |
func() Node { |
| 344 |
return pId |
| 345 |
}, rand.Read, |
| 346 |
nil}) |
| 347 |
return pNow, pId |
| 348 |
} |
| 349 |
|
| 350 |
func registerDefaultGenerator() { |
| 351 |
generator = NewGenerator(GeneratorConfig{}) |
| 352 |
} |