-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_test.go
executable file
·64 lines (55 loc) · 1.21 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package campid
import (
"encoding/base64"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestCrypto_String(t *testing.T) {
samples := "abcdefghijklmnopqrstuv"
for i := 0; i <= 10; i++ {
ln := 50
random, err := String(ln, samples)
if err != nil {
t.Fatal("failed to generate random string", err)
}
if len(random) != 50 {
t.Error("incorrect character count", cmp.Diff(
len(random), 50,
))
}
for _, v := range random {
s := string(v)
if !strings.Contains(samples, s) {
t.Errorf("invalid character used in random string: %s", s)
}
}
}
}
func TestCrypto_StringB64(t *testing.T) {
b64str, err := StringB64(50)
if err != nil {
t.Error("error generating random string", err)
}
_, err = base64.StdEncoding.DecodeString(b64str)
if err != nil {
t.Error("failed to decode base64 encoded string")
}
}
func TestCrypto_Hash(t *testing.T) {
str := "the quick brown fox"
hash, err := Hash(str)
if err != nil {
t.Error("error generating hash", err)
}
if str == hash {
t.Error("string not hashed")
}
hash2, err := Hash(str)
if err != nil {
t.Error("error generating hash", err)
}
if hash != hash2 {
t.Error("hashes do not match", cmp.Diff(hash, hash2))
}
}