...
1
2
3
4
5
6
7
8 package common
9
10 import (
11 "strconv"
12 "strings"
13 "testing"
14 )
15
16 var _Salt = &Salt{
17 MagicPrefix: []byte("$foo$"),
18 SaltLenMin: 1,
19 SaltLenMax: 8,
20 RoundsMin: 1000,
21 RoundsMax: 999999999,
22 RoundsDefault: 5000,
23 }
24
25 func TestGenerateSalt(t *testing.T) {
26 magicPrefixLen := len(_Salt.MagicPrefix)
27
28 salt := _Salt.Generate(0)
29 if len(salt) != magicPrefixLen+1 {
30 t.Errorf("Expected len 1, got len %d", len(salt))
31 }
32
33 for i := _Salt.SaltLenMin; i <= _Salt.SaltLenMax; i++ {
34 salt = _Salt.Generate(i)
35 if len(salt) != magicPrefixLen+i {
36 t.Errorf("Expected len %d, got len %d", i, len(salt))
37 }
38 }
39
40 salt = _Salt.Generate(9)
41 if len(salt) != magicPrefixLen+8 {
42 t.Errorf("Expected len 8, got len %d", len(salt))
43 }
44 }
45
46 func TestGenerateSaltWRounds(t *testing.T) {
47 const rounds = 5001
48 salt := _Salt.GenerateWRounds(_Salt.SaltLenMax, rounds)
49 if salt == nil {
50 t.Errorf("salt should not be nil")
51 }
52
53 expectedPrefix := string(_Salt.MagicPrefix) + "rounds=" + strconv.Itoa(rounds) + "$"
54 if !strings.HasPrefix(string(salt), expectedPrefix) {
55 t.Errorf("salt '%s' should start with prefix '%s' but didn't", salt, expectedPrefix)
56 }
57 }
58
View as plain text