|
5 | 5 | "encoding/base64" |
6 | 6 | "encoding/pem" |
7 | 7 | "fmt" |
| 8 | + "strconv" |
8 | 9 | "strings" |
9 | 10 | "testing" |
10 | 11 |
|
@@ -54,16 +55,80 @@ func TestAdler32Sum(t *testing.T) { |
54 | 55 | } |
55 | 56 | } |
56 | 57 |
|
57 | | -func TestBcrypt(t *testing.T) { |
| 58 | +func TestBcryptDefaultCost(t *testing.T) { |
58 | 59 | out, err := runRaw(`{{"abc" | bcrypt}}`, nil) |
59 | 60 | if err != nil { |
60 | 61 | t.Error(err) |
61 | 62 | } |
| 63 | + result := strings.Split(out, "$") |
| 64 | + passwordCost, err := strconv.Atoi(result[2]) |
| 65 | + if err != nil { |
| 66 | + t.Error(err) |
| 67 | + } |
| 68 | + if passwordCost != bcrypt_lib.DefaultCost { |
| 69 | + t.Error("Generated hash has not the expected cost. Got:", passwordCost, " Expected:", bcrypt_lib.DefaultCost) |
| 70 | + } |
| 71 | + if bcrypt_lib.CompareHashAndPassword([]byte(out), []byte("abc")) != nil { |
| 72 | + t.Error("Generated hash is not the equivalent for password:", "abc") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestBcryptCustomCost(t *testing.T) { // 4 >= validCost <= 31 |
| 77 | + out, err := runRaw(`{{bcrypt "abc" 12 }}`, nil) |
| 78 | + if err != nil { |
| 79 | + t.Error(err) |
| 80 | + } |
| 81 | + result := strings.Split(out, "$") |
| 82 | + passwordCost, err := strconv.Atoi(result[2]) |
| 83 | + if err != nil { |
| 84 | + t.Error(err) |
| 85 | + } |
| 86 | + if passwordCost != 12 { |
| 87 | + t.Error("Generated hash has not the expected cost. got:", passwordCost, "expected:", 12) |
| 88 | + } |
| 89 | + if bcrypt_lib.CompareHashAndPassword([]byte(out), []byte("abc")) != nil { |
| 90 | + t.Error("Generated hash is not the equivalent for password:", "abc") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestBcryptMinCost(t *testing.T) { |
| 95 | + out, err := runRaw(`{{bcrypt "abc" 3 }}`, nil) |
| 96 | + if err != nil { |
| 97 | + t.Error(err) |
| 98 | + } |
| 99 | + result := strings.Split(out, "$") |
| 100 | + passwordCost, err := strconv.Atoi(result[2]) |
| 101 | + if err != nil { |
| 102 | + t.Error(err) |
| 103 | + } |
| 104 | + if passwordCost != bcrypt_lib.DefaultCost { |
| 105 | + t.Error("Generated hash has not the expected cost. got:", passwordCost, "expected:", bcrypt_lib.DefaultCost) |
| 106 | + } |
62 | 107 | if bcrypt_lib.CompareHashAndPassword([]byte(out), []byte("abc")) != nil { |
63 | 108 | t.Error("Generated hash is not the equivalent for password:", "abc") |
64 | 109 | } |
65 | 110 | } |
66 | 111 |
|
| 112 | +func TestBcryptMaxCost(t *testing.T) { |
| 113 | + out, err := runRaw(`{{bcrypt "abc" 32 }}`, nil) |
| 114 | + if err != nil { |
| 115 | + t.Error(err) |
| 116 | + } |
| 117 | + if !strings.HasPrefix(out, "failed to encrypt string with bcrypt") { |
| 118 | + t.Error("Generated string should be a failure message, got:", out, "expected: failed to encrypt string with bcrypt") |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestBcryptMultipleCostParameter(t *testing.T) { |
| 123 | + out, err := runRaw(`{{bcrypt "abc" 10 10 }}`, nil) |
| 124 | + if err != nil { |
| 125 | + t.Error(err) |
| 126 | + } |
| 127 | + if !strings.HasPrefix(out, "multiple cost parameter set") { |
| 128 | + t.Error("Generated string should be a failure message, got:", out, "expected: multiple cost parameter set") |
| 129 | + } |
| 130 | +} |
| 131 | + |
67 | 132 | type HtpasswdCred struct { |
68 | 133 | Username string |
69 | 134 | Password string |
|
0 commit comments