LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package bcrypt | 5 package bcrypt |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "os" | 9 "os" |
10 "testing" | 10 "testing" |
11 ) | 11 ) |
12 | 12 |
13 func TestBcryptingIsEasy(t *testing.T) { | 13 func TestBcryptingIsEasy(t *testing.T) { |
14 pass := []byte("mypassword") | 14 pass := []byte("mypassword") |
15 hp, err := GenerateFromPassword(pass, 0) | 15 hp, err := GenerateFromPassword(pass, 0) |
16 if err != nil { | 16 if err != nil { |
17 t.Fatalf("GenerateFromPassword error: %s", err) | 17 t.Fatalf("GenerateFromPassword error: %s", err) |
18 } | 18 } |
19 | 19 |
20 if CompareHashAndPassword(hp, pass) != nil { | 20 if CompareHashAndPassword(hp, pass) != nil { |
21 t.Errorf("%v should hash %s correctly", hp, pass) | 21 t.Errorf("%v should hash %s correctly", hp, pass) |
| 22 } |
| 23 |
| 24 notPass := "notthepass" |
| 25 err = CompareHashAndPassword(hp, []byte(notPass)) |
| 26 if err != MismatchedHashAndPasswordError { |
| 27 t.Errorf("%v and %s should be mismatched", hp, notPass) |
22 } | 28 } |
23 } | 29 } |
24 | 30 |
25 func TestBcryptingIsCorrect(t *testing.T) { | 31 func TestBcryptingIsCorrect(t *testing.T) { |
26 pass := []byte("allmine") | 32 pass := []byte("allmine") |
27 salt := []byte("XajjQvNhvvRt5GSeFk1xFe") | 33 salt := []byte("XajjQvNhvvRt5GSeFk1xFe") |
28 expectedHash := []byte("$2a$10$XajjQvNhvvRt5GSeFk1xFeyqRrsxkhBkUiQeg0dt.
wU1qD4aFDcga") | 34 expectedHash := []byte("$2a$10$XajjQvNhvvRt5GSeFk1xFeyqRrsxkhBkUiQeg0dt.
wU1qD4aFDcga") |
29 | 35 |
30 hash, err := bcrypt(pass, 10, salt) | 36 hash, err := bcrypt(pass, 10, salt) |
31 if err != nil { | 37 if err != nil { |
(...skipping 28 matching lines...) Expand all Loading... |
60 t.Errorf("%v should be the suffix of %v", hash, tooLongExpected) | 66 t.Errorf("%v should be the suffix of %v", hash, tooLongExpected) |
61 } | 67 } |
62 } | 68 } |
63 | 69 |
64 type InvalidHashTest struct { | 70 type InvalidHashTest struct { |
65 err os.Error | 71 err os.Error |
66 hash []byte | 72 hash []byte |
67 } | 73 } |
68 | 74 |
69 var invalidTests = []InvalidHashTest{ | 75 var invalidTests = []InvalidHashTest{ |
70 » {HashTooShort, []byte("$2a$10$fooo")}, | 76 » {HashTooShortError, []byte("$2a$10$fooo")}, |
71 » {HashVersionTooNew('3'), []byte("$3a$10$sssssssssssssssssssssshhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhh")}, | 77 » {HashTooShortError, []byte("$2a")}, |
72 » {InvalidHashPrefix('%'), []byte("%2a$10$sssssssssssssssssssssshhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhh")}, | 78 » {HashVersionTooNewError('3'), []byte("$3a$10$sssssssssssssssssssssshhhhh
hhhhhhhhhhhhhhhhhhhhhhhhhh")}, |
73 » {InvalidCost(32), []byte("$2a$32$sssssssssssssssssssssshhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhh")}, | 79 » {InvalidHashPrefixError('%'), []byte("%2a$10$sssssssssssssssssssssshhhhh
hhhhhhhhhhhhhhhhhhhhhhhhhh")}, |
| 80 » {InvalidCostError(32), []byte("$2a$32$sssssssssssssssssssssshhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhh")}, |
74 } | 81 } |
75 | 82 |
76 func TestInvalidHashErrors(t *testing.T) { | 83 func TestInvalidHashErrors(t *testing.T) { |
77 check := func(name string, expected, err os.Error) { | 84 check := func(name string, expected, err os.Error) { |
78 if err == nil { | 85 if err == nil { |
79 t.Errorf("%s: Should have returned an error", name) | 86 t.Errorf("%s: Should have returned an error", name) |
80 } | 87 } |
81 if err != nil && err != expected { | 88 if err != nil && err != expected { |
82 t.Errorf("%s gave err %v but should have given %v", name
, err.String(), expected.String()) | 89 t.Errorf("%s gave err %v but should have given %v", name
, err.String(), expected.String()) |
83 } | 90 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 134 |
128 hp, _ := newFromHash(p.Hash()) | 135 hp, _ := newFromHash(p.Hash()) |
129 if p.cost != hp.cost { | 136 if p.cost != hp.cost { |
130 t.Errorf("newFromHash should maintain the cost at %d, but was %d
", p.cost, hp.cost) | 137 t.Errorf("newFromHash should maintain the cost at %d, but was %d
", p.cost, hp.cost) |
131 } | 138 } |
132 | 139 |
133 _, err := newFromPassword(pass, 32) | 140 _, err := newFromPassword(pass, 32) |
134 if err == nil { | 141 if err == nil { |
135 t.Fatalf("newFromPassword: should return a cost error") | 142 t.Fatalf("newFromPassword: should return a cost error") |
136 } | 143 } |
137 » if err != InvalidCost(32) { | 144 » if err != InvalidCostError(32) { |
138 t.Errorf("newFromPassword: should return cost error, got %#v", e
rr) | 145 t.Errorf("newFromPassword: should return cost error, got %#v", e
rr) |
139 } | 146 } |
140 } | 147 } |
141 | 148 |
142 func TestCostReturnsWithLeadingZeroes(t *testing.T) { | 149 func TestCostReturnsWithLeadingZeroes(t *testing.T) { |
143 hp, _ := newFromPassword([]byte("abcdefgh"), 7) | 150 hp, _ := newFromPassword([]byte("abcdefgh"), 7) |
144 cost := hp.Hash()[4:7] | 151 cost := hp.Hash()[4:7] |
145 expected := []byte("07$") | 152 expected := []byte("07$") |
146 | 153 |
147 if !bytes.Equal(expected, cost) { | 154 if !bytes.Equal(expected, cost) { |
(...skipping 27 matching lines...) Expand all Loading... |
175 } | 182 } |
176 | 183 |
177 func BenchmarkGeneration(b *testing.B) { | 184 func BenchmarkGeneration(b *testing.B) { |
178 b.StopTimer() | 185 b.StopTimer() |
179 passwd := []byte("mylongpassword1234") | 186 passwd := []byte("mylongpassword1234") |
180 b.StartTimer() | 187 b.StartTimer() |
181 for i := 0; i < b.N; i++ { | 188 for i := 0; i < b.N; i++ { |
182 GenerateFromPassword(passwd, 10) | 189 GenerateFromPassword(passwd, 10) |
183 } | 190 } |
184 } | 191 } |
LEFT | RIGHT |