-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptogen_test.go
102 lines (82 loc) · 1.58 KB
/
cryptogen_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
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
package vbcore
import (
"testing"
)
func TestCryptoGenBytes_Unique(t *testing.T) {
store := make(map[string]byte)
n := 200
for i := 0; i < 1000; i++ {
buf, err := CryptoGenBytes(n)
if err != nil {
t.Fail()
}
if len(buf) != 200 {
t.Fail()
}
if _, ok := store[string(buf)]; ok {
t.Fail()
} else {
store[string(buf)] = 0
}
}
}
func TestCryptoGen_Unique(t *testing.T) {
keys := make(map[string]byte)
for i := 0; i < 1000; i++ {
k, err := CryptoGen()
if err != nil {
t.Fail()
}
if _, ok := keys[k]; ok {
t.Fail()
} else {
keys[k] = 0
}
}
}
func TestCryptoGenString_Unique(t *testing.T) {
store := make(map[string]byte)
n := 200
for i := 0; i < 1000; i++ {
str, err := CryptoGenString(n)
if err != nil {
t.Fail()
}
if len(str) != n {
t.Fail()
}
if _, ok := store[str]; ok {
t.Fail()
} else {
store[str] = 0
}
}
}
func TestCryptoGenX_Threshold(t *testing.T) {
hasCorrectError := func(err error) bool {
if err != nil {
if err.Error() != "vbcore.CryptoGenBytes: CryptoGenThreshold mustn't be less than 1 but is 0" {
return false
}
} else {
return false
}
return true
}
// Set CryptoGenThreshold to test for custom errors inside CryptoGenX funcs
CryptoGenThreshold = 0
key, err := CryptoGen()
if len(key) > 0 || !hasCorrectError(err) {
t.Fail()
}
buf, err := CryptoGenBytes(100)
if len(buf) > 0 || !hasCorrectError(err) {
t.Fail()
}
str, err := CryptoGenString(100)
if len(str) > 0 || !hasCorrectError(err) {
t.Fail()
}
// Reset CryptoGenThreshold
CryptoGenThreshold = 10
}