-
Notifications
You must be signed in to change notification settings - Fork 10
/
crypto_test.go
47 lines (42 loc) · 949 Bytes
/
crypto_test.go
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
package cuid
import (
"crypto/rand"
"reflect"
"testing"
)
func Test_CUIDCryptoType(t *testing.T) {
c, e := NewCrypto(rand.Reader)
if e != nil {
t.Errorf("Failed to generate crypto cuid: %v", e)
}
if reflect.TypeOf(c).Name() != "string" {
t.Error("Incorrect CUID type")
}
}
func Test_CUIDCryptoFormat(t *testing.T) {
c, e := NewCrypto(rand.Reader)
if e != nil {
t.Errorf("Failed to generate crypto cuid: %v", e)
}
if err := IsCuid(c); err != nil {
t.Error("Incorrect format")
}
}
func Test_CUIDCryptoCollisions(t *testing.T) {
ids := map[string]bool{}
for i := 0; i < 600000; i++ {
id, e := NewCrypto(rand.Reader)
if e != nil {
t.Errorf("Failed to generate crypto cuid, at iteration %d: %v", i, e)
}
if ids[id] == true {
t.Errorf("Collision detected, at iteration %d", i)
}
ids[id] = true
}
}
func Benchmark_CryptoCUIDGeneration(b *testing.B) {
for i := 0; i < b.N; i++ {
NewCrypto(rand.Reader)
}
}