-
Notifications
You must be signed in to change notification settings - Fork 16
/
crypt_test.go
77 lines (64 loc) · 1.48 KB
/
crypt_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
package crypt
import (
"testing"
)
func TestCryptSHA512(t *testing.T) {
var (
pass = "password"
salt = "$6$saltedsalted64$"
hash = "$6$saltedsalted64$BG5.9QC/0aBR3V4CX82Fx1Ia244NogxWChRfDH4YyB5tjDVJd.loeTzjMWF4XTfynLPI53mEzp04eKaBaVVPp1"
)
enc, err := Crypt(pass, salt)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if enc != hash {
t.Errorf("hashed password mismatch, expected [%s], got [%s]", hash, enc)
}
}
func TestCryptMD5(t *testing.T) {
var (
pass = "password"
salt = "XY"
hash = "XYGpusIMIT/IM"
)
enc, err := Crypt(pass, salt)
if err != nil {
t.Errorf("unexpected error: %t, (got %s)", err, enc)
}
if enc != hash {
t.Errorf("hashed password mismatch, expected [%s] but got [%s]", hash, enc)
}
}
func TestCryptErrors(t *testing.T) {
if !checkGlibCVersion() {
t.Skip("Skipping glibc specific error tests")
}
tests := [][]string{
{"no salt", ""},
{"single char", "/"},
{"first char bad", "!x"},
{"second char bad", "Z%"},
{"both chars bad", ":@"},
{"un$upported algorithm", "$2$"},
{"unsupported_algorithm", "_1"},
}
for _, test := range tests {
enc, err := Crypt("password", test[1])
if err == nil {
t.Errorf("Expected error when testing %s, instead got %s", test[0], enc)
}
}
}
func TestCheckMemoryAllocation(t *testing.T) {
var (
pass = "password"
salt = "$6$saltedsalted64$"
niter = 512
)
for i := 0; i < niter; i++ {
h, _ := Crypt(pass, salt)
_ = h
}
// TODO: check all mem allocations are free'd
}