| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package bcrypt |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestDecodeSecret(t *testing.T) { |
| 15 |
testCases := []struct { |
| 16 |
name string |
| 17 |
have []byte |
| 18 |
expected []byte |
| 19 |
expectedSalt []byte |
| 20 |
shouldPanic bool |
| 21 |
}{ |
| 22 |
{ |
| 23 |
"ShouldDecode", |
| 24 |
[]byte("gKt7Q/V29tf48wMkEoYlNOk.XttO4IJdWKVDQPHLxkZMs9VEFwfv2"), |
| 25 |
[]byte("k.XttO4IJdWKVDQPHLxkZMs9VEFwfv2"), |
| 26 |
[]byte("gKt7Q/V29tf48wMkEoYlNO"), |
| 27 |
false, |
| 28 |
}, |
| 29 |
{ |
| 30 |
"ShouldPanicOnInvalidSize", |
| 31 |
[]byte("gKt7Q/V29tf48wMkEoYlN"), |
| 32 |
nil, |
| 33 |
nil, |
| 34 |
true, |
| 35 |
}, |
| 36 |
{ |
| 37 |
"ShouldPanicWithEmptySecret", |
| 38 |
[]byte(""), |
| 39 |
nil, |
| 40 |
nil, |
| 41 |
true, |
| 42 |
}, |
| 43 |
} |
| 44 |
|
| 45 |
for _, tc := range testCases { |
| 46 |
t.Run(tc.name, func(t *testing.T) { |
| 47 |
if tc.shouldPanic { |
| 48 |
assert.Panics(t, func() { |
| 49 |
_, _ = DecodeSecret(tc.have) |
| 50 |
}) |
| 51 |
} else { |
| 52 |
assert.NotPanics(t, func() { |
| 53 |
salt, key := DecodeSecret(tc.have) |
| 54 |
|
| 55 |
assert.Equal(t, tc.expected, key) |
| 56 |
assert.Equal(t, tc.expectedSalt, salt) |
| 57 |
}) |
| 58 |
} |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestBcryptingIsEasy(t *testing.T) { |
| 64 |
pass := []byte("mypassword") |
| 65 |
hp, err := GenerateFromPassword(pass, 0) |
| 66 |
if err != nil { |
| 67 |
t.Fatalf("GenerateFromPassword error: %s", err) |
| 68 |
} |
| 69 |
|
| 70 |
if CompareHashAndPassword(hp, pass) != nil { |
| 71 |
t.Errorf("%v should hash %s correctly", hp, pass) |
| 72 |
} |
| 73 |
|
| 74 |
notPass := "notthepass" |
| 75 |
err = CompareHashAndPassword(hp, []byte(notPass)) |
| 76 |
if err != ErrMismatchedHashAndPassword { |
| 77 |
t.Errorf("%v and %s should be mismatched", hp, notPass) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestBcryptingIsCorrect(t *testing.T) { |
| 82 |
pass := []byte("allmine") |
| 83 |
salt := []byte("XajjQvNhvvRt5GSeFk1xFe") |
| 84 |
expectedHash := []byte("$2a$10$XajjQvNhvvRt5GSeFk1xFeyqRrsxkhBkUiQeg0dt.wU1qD4aFDcga") |
| 85 |
|
| 86 |
hash, err := bcrypt(pass, 10, salt) |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("bcrypt blew up: %v", err) |
| 89 |
} |
| 90 |
if !bytes.HasSuffix(expectedHash, hash) { |
| 91 |
t.Errorf("%v should be the suffix of %v", hash, expectedHash) |
| 92 |
} |
| 93 |
|
| 94 |
h, err := newFromHash(expectedHash) |
| 95 |
if err != nil { |
| 96 |
t.Errorf("Unable to parse %s: %v", string(expectedHash), err) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if err == nil && !bytes.Equal(expectedHash, h.Hash()) { |
| 102 |
t.Errorf("Parsed hash %v should equal %v", h.Hash(), expectedHash) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func TestVeryShortPasswords(t *testing.T) { |
| 107 |
key := []byte("k") |
| 108 |
salt := []byte("XajjQvNhvvRt5GSeFk1xFe") |
| 109 |
_, err := bcrypt(key, 10, salt) |
| 110 |
if err != nil { |
| 111 |
t.Errorf("One byte key resulted in error: %s", err) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func TestTooLongPasswordsWork(t *testing.T) { |
| 116 |
salt := []byte("XajjQvNhvvRt5GSeFk1xFe") |
| 117 |
|
| 118 |
tooLongPass := []byte("012345678901234567890123456789012345678901234567890123456") |
| 119 |
tooLongExpected := []byte("$2a$10$XajjQvNhvvRt5GSeFk1xFe5l47dONXg781AmZtd869sO8zfsHuw7C") |
| 120 |
hash, err := bcrypt(tooLongPass, 10, salt) |
| 121 |
if err != nil { |
| 122 |
t.Fatalf("bcrypt blew up on long password: %v", err) |
| 123 |
} |
| 124 |
if !bytes.HasSuffix(tooLongExpected, hash) { |
| 125 |
t.Errorf("%v should be the suffix of %v", hash, tooLongExpected) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
type InvalidHashTest struct { |
| 130 |
err error |
| 131 |
hash []byte |
| 132 |
} |
| 133 |
|
| 134 |
var invalidTests = []InvalidHashTest{ |
| 135 |
{ErrHashTooShort, []byte("$2a$10$fooo")}, |
| 136 |
{ErrHashTooShort, []byte("$2a")}, |
| 137 |
{HashVersionTooNewError('3'), []byte("$3a$10$sssssssssssssssssssssshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh")}, |
| 138 |
{InvalidHashPrefixError('%'), []byte("%2a$10$sssssssssssssssssssssshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh")}, |
| 139 |
{InvalidCostError(32), []byte("$2a$32$sssssssssssssssssssssshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh")}, |
| 140 |
} |
| 141 |
|
| 142 |
func TestInvalidHashErrors(t *testing.T) { |
| 143 |
check := func(name string, expected, err error) { |
| 144 |
if err == nil { |
| 145 |
t.Errorf("%s: Should have returned an error", name) |
| 146 |
} |
| 147 |
if err != nil && err != expected { |
| 148 |
t.Errorf("%s gave err %v but should have given %v", name, err, expected) |
| 149 |
} |
| 150 |
} |
| 151 |
for _, iht := range invalidTests { |
| 152 |
_, err := newFromHash(iht.hash) |
| 153 |
check("newFromHash", iht.err, err) |
| 154 |
err = CompareHashAndPassword(iht.hash, []byte("anything")) |
| 155 |
check("CompareHashAndPassword", iht.err, err) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func TestUnpaddedBase64Encoding(t *testing.T) { |
| 160 |
original := []byte{101, 201, 101, 75, 19, 227, 199, 20, 239, 236, 133, 32, 30, 109, 243, 30} |
| 161 |
encodedOriginal := []byte("XajjQvNhvvRt5GSeFk1xFe") |
| 162 |
|
| 163 |
encoded := Base64Encode(original) |
| 164 |
|
| 165 |
if !bytes.Equal(encodedOriginal, encoded) { |
| 166 |
t.Errorf("Encoded %v should have equaled %v", encoded, encodedOriginal) |
| 167 |
} |
| 168 |
|
| 169 |
decoded, err := Base64Decode(encodedOriginal) |
| 170 |
if err != nil { |
| 171 |
t.Fatalf("base64Decode blew up: %s", err) |
| 172 |
} |
| 173 |
|
| 174 |
if !bytes.Equal(decoded, original) { |
| 175 |
t.Errorf("Decoded %v should have equaled %v", decoded, original) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestCost(t *testing.T) { |
| 180 |
suffix := "XajjQvNhvvRt5GSeFk1xFe5l47dONXg781AmZtd869sO8zfsHuw7C" |
| 181 |
for _, vers := range []string{"2a", "2"} { |
| 182 |
for _, cost := range []int{4, 10} { |
| 183 |
s := fmt.Sprintf("$%s$%02d$%s", vers, cost, suffix) |
| 184 |
h := []byte(s) |
| 185 |
actual, err := Cost(h) |
| 186 |
if err != nil { |
| 187 |
t.Errorf("Cost, error: %s", err) |
| 188 |
continue |
| 189 |
} |
| 190 |
if actual != cost { |
| 191 |
t.Errorf("Cost, expected: %d, actual: %d", cost, actual) |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
_, err := Cost([]byte("$a$a$" + suffix)) |
| 196 |
if err == nil { |
| 197 |
t.Errorf("Cost, malformed but no error returned") |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func TestCostValidationInHash(t *testing.T) { |
| 202 |
if testing.Short() { |
| 203 |
return |
| 204 |
} |
| 205 |
|
| 206 |
pass := []byte("mypassword") |
| 207 |
|
| 208 |
for c := 0; c < MinCost; c++ { |
| 209 |
p, _ := newFromPassword(pass, c) |
| 210 |
if p.cost != DefaultCost { |
| 211 |
t.Errorf("newFromPassword should default costs below %d to %d, but was %d", MinCost, DefaultCost, p.cost) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
p, _ := newFromPassword(pass, 14) |
| 216 |
if p.cost != 14 { |
| 217 |
t.Errorf("newFromPassword should default cost to 14, but was %d", p.cost) |
| 218 |
} |
| 219 |
|
| 220 |
hp, _ := newFromHash(p.Hash()) |
| 221 |
if p.cost != hp.cost { |
| 222 |
t.Errorf("newFromHash should maintain the cost at %d, but was %d", p.cost, hp.cost) |
| 223 |
} |
| 224 |
|
| 225 |
_, err := newFromPassword(pass, 32) |
| 226 |
if err == nil { |
| 227 |
t.Fatalf("newFromPassword: should return a cost error") |
| 228 |
} |
| 229 |
if err != InvalidCostError(32) { |
| 230 |
t.Errorf("newFromPassword: should return cost error, got %#v", err) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
func TestCostReturnsWithLeadingZeroes(t *testing.T) { |
| 235 |
hp, _ := newFromPassword([]byte("abcdefgh"), 7) |
| 236 |
cost := hp.Hash()[4:7] |
| 237 |
expected := []byte("07$") |
| 238 |
|
| 239 |
if !bytes.Equal(expected, cost) { |
| 240 |
t.Errorf("single digit costs in hash should have leading zeros: was %v instead of %v", cost, expected) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func TestMinorNotRequired(t *testing.T) { |
| 245 |
noMinorHash := []byte("$2$10$XajjQvNhvvRt5GSeFk1xFeyqRrsxkhBkUiQeg0dt.wU1qD4aFDcga") |
| 246 |
h, err := newFromHash(noMinorHash) |
| 247 |
if err != nil { |
| 248 |
t.Fatalf("No minor hash blew up: %s", err) |
| 249 |
} |
| 250 |
if h.minor != 0 { |
| 251 |
t.Errorf("Should leave minor version at 0, but was %d", h.minor) |
| 252 |
} |
| 253 |
|
| 254 |
if !bytes.Equal(noMinorHash, h.Hash()) { |
| 255 |
t.Errorf("Should generate hash %v, but created %v", noMinorHash, h.Hash()) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
func BenchmarkEqual(b *testing.B) { |
| 260 |
b.StopTimer() |
| 261 |
passwd := []byte("somepasswordyoulike") |
| 262 |
hash, _ := GenerateFromPassword(passwd, DefaultCost) |
| 263 |
b.StartTimer() |
| 264 |
for i := 0; i < b.N; i++ { |
| 265 |
CompareHashAndPassword(hash, passwd) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
func BenchmarkDefaultCost(b *testing.B) { |
| 270 |
b.StopTimer() |
| 271 |
passwd := []byte("mylongpassword1234") |
| 272 |
b.StartTimer() |
| 273 |
for i := 0; i < b.N; i++ { |
| 274 |
GenerateFromPassword(passwd, DefaultCost) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
func TestNoSideEffectsFromCompare(t *testing.T) { |
| 280 |
source := []byte("passw0rd123456") |
| 281 |
password := source[:len(source)-6] |
| 282 |
token := source[len(source)-6:] |
| 283 |
want := make([]byte, len(source)) |
| 284 |
copy(want, source) |
| 285 |
|
| 286 |
wantHash := []byte("$2a$10$LK9XRuhNxHHCvjX3tdkRKei1QiCDUKrJRhZv7WWZPuQGRUM92rOUa") |
| 287 |
_ = CompareHashAndPassword(wantHash, password) |
| 288 |
|
| 289 |
got := bytes.Join([][]byte{password, token}, []byte("")) |
| 290 |
if !bytes.Equal(got, want) { |
| 291 |
t.Errorf("got=%q want=%q", got, want) |
| 292 |
} |
| 293 |
} |