Skip to content

Commit 7914916

Browse files
authoredSep 23, 2017
Merge pull request #545 from agathver/test-improvements
Improve test cases
2 parents 7d53afa + f3a34bd commit 7914916

File tree

4 files changed

+78
-51
lines changed

4 files changed

+78
-51
lines changed
 

‎test/async.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
test_salt_length: function(assert) {
55
assert.expect(1);
66
bcrypt.genSalt(10, function(err, salt) {
7-
assert.equals(29, salt.length, "Salt isn't the correct length.");
7+
assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
88
assert.done();
99
});
1010
},
@@ -36,7 +36,7 @@ module.exports = {
3636
test_hash_rounds: function(assert) {
3737
assert.expect(1);
3838
bcrypt.hash('bacon', 8, function(err, hash) {
39-
assert.equals(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
39+
assert.strictEqual(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
4040
assert.done();
4141
});
4242
},
@@ -72,10 +72,10 @@ module.exports = {
7272
test_hash_salt_validity: function(assert) {
7373
assert.expect(3);
7474
bcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse', function(err, enc) {
75-
assert.equal(err, undefined);
75+
assert.strictEqual(err, undefined);
7676
bcrypt.hash('password', 'some$value', function(err, enc) {
7777
assert.notEqual(err, undefined);
78-
assert.equal(err.message, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
78+
assert.strictEqual(err.message, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
7979
assert.done();
8080
});
8181
});
@@ -84,38 +84,38 @@ module.exports = {
8484
assert.expect(2);
8585
bcrypt.genSalt(10, function(err, salt) {
8686
var split_salt = salt.split('$');
87-
assert.ok(split_salt[1], '2a');
88-
assert.ok(split_salt[2], '10');
87+
assert.strictEqual(split_salt[1], '2a');
88+
assert.strictEqual(split_salt[2], '10');
8989
assert.done();
9090
});
9191
},
9292
test_verify_salt_min_rounds: function(assert) {
9393
assert.expect(2);
9494
bcrypt.genSalt(1, function(err, salt) {
9595
var split_salt = salt.split('$');
96-
assert.ok(split_salt[1], '2a');
97-
assert.ok(split_salt[2], '4');
96+
assert.strictEqual(split_salt[1], '2a');
97+
assert.strictEqual(split_salt[2], '04');
9898
assert.done();
9999
});
100100
},
101101
test_verify_salt_max_rounds: function(assert) {
102102
assert.expect(2);
103103
bcrypt.genSalt(100, function(err, salt) {
104104
var split_salt = salt.split('$');
105-
assert.ok(split_salt[1], '2a');
106-
assert.ok(split_salt[2], '31');
105+
assert.strictEqual(split_salt[1], '2a');
106+
assert.strictEqual(split_salt[2], '31');
107107
assert.done();
108108
});
109109
},
110110
test_hash_compare: function(assert) {
111111
assert.expect(3);
112112
bcrypt.genSalt(10, function(err, salt) {
113-
assert.equals(29, salt.length, "Salt isn't the correct length.");
113+
assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
114114
bcrypt.hash("test", salt, function(err, hash) {
115115
bcrypt.compare("test", hash, function(err, res) {
116-
assert.equal(res, true, "These hashes should be equal.");
116+
assert.strictEqual(res, true, "These hashes should be equal.");
117117
bcrypt.compare("blah", hash, function(err, res) {
118-
assert.equal(res, false, "These hashes should not be equal.");
118+
assert.strictEqual(res, false, "These hashes should not be equal.");
119119
assert.done();
120120
});
121121
});
@@ -127,9 +127,9 @@ module.exports = {
127127
var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(10));
128128

129129
bcrypt.compare("", hash, function(err, res) {
130-
assert.equal(res, false, "These hashes should be equal.");
130+
assert.strictEqual(res, false, "These hashes should not be equal.");
131131
bcrypt.compare("", "", function(err, res) {
132-
assert.equal(res, false, "These hashes should be equal.");
132+
assert.strictEqual(res, false, "These hashes should not be equal.");
133133
assert.done();
134134
});
135135
});

‎test/implementation.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var bcrypt = require('../bcrypt');
2+
3+
// some tests were adapted from https://github.com/riverrun/bcrypt_elixir/blob/master/test/base_test.exs
4+
// which are under the BSD LICENSE
5+
module.exports = {
6+
openwall_bcrypt_tests: function(assert) {
7+
assert.strictEqual(bcrypt.hashSync("U*U", "$2a$05$CCCCCCCCCCCCCCCCCCCCC."), "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW");
8+
assert.strictEqual(bcrypt.hashSync("U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCC."), "$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK");
9+
assert.strictEqual(bcrypt.hashSync("U*U*U", "$2a$05$XXXXXXXXXXXXXXXXXXXXXO"), "$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a");
10+
assert.strictEqual(bcrypt.hashSync("", "$2a$05$CCCCCCCCCCCCCCCCCCCCC."), "$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy");
11+
assert.strictEqual(bcrypt.hashSync("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "$2a$05$abcdefghijklmnopqrstuu"), "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui");
12+
assert.done();
13+
},
14+
test_long_passwords: function(assert) {
15+
// bcrypt wrap-around bug in $2a$
16+
assert.strictEqual(bcrypt.hashSync("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", "$2a$05$CCCCCCCCCCCCCCCCCCCCC."), "$2a$05$CCCCCCCCCCCCCCCCCCCCC.6.O1dLNbjod2uo0DVcW.jHucKbPDdHS");
17+
assert.strictEqual(bcrypt.hashSync("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345", "$2a$05$CCCCCCCCCCCCCCCCCCCCC."), "$2a$05$CCCCCCCCCCCCCCCCCCCCC.6.O1dLNbjod2uo0DVcW.jHucKbPDdHS");
18+
assert.done();
19+
},
20+
test_shorten_salt_to_128_bits: function(assert) {
21+
assert.strictEqual(bcrypt.hashSync("test", "$2a$10$1234567899123456789012"), "$2a$10$123456789912345678901u.OtL1A1eGK5wmvBKUDYKvuVKI7h2XBu");
22+
assert.strictEqual(bcrypt.hashSync("U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCCh"), "$2a$05$CCCCCCCCCCCCCCCCCCCCCeUQ7VjYZ2hd4bLYZdhuPpZMUpEUJDw1S");
23+
assert.strictEqual(bcrypt.hashSync("U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCCM"), "$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK");
24+
assert.strictEqual(bcrypt.hashSync("U*U*", "$2a$05$CCCCCCCCCCCCCCCCCCCCCA"), "$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK");
25+
assert.done();
26+
}
27+
}

‎test/promise.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ if (typeof Promise !== 'undefined') {
1111
test_salt_returns_promise_on_no_args: function(assert) {
1212
// make sure test passes with non-native implementations such as bluebird
1313
// http://stackoverflow.com/questions/27746304/how-do-i-tell-if-an-object-is-a-promise
14-
assert.ok(typeof bcrypt.genSalt().then === 'function', "Should return a promise");
14+
assert.strictEqual(typeof bcrypt.genSalt().then, 'function', "Should return a promise");
1515
assert.done();
1616
},
1717
test_salt_returns_promise_on_null_callback: function(assert) {
18-
assert.ok(typeof bcrypt.genSalt(13, null, null).then === 'function', "Should return a promise");
18+
assert.strictEqual(typeof bcrypt.genSalt(13, null, null).then,'function', "Should return a promise");
1919
assert.done();
2020
},
2121
test_salt_length: function(assert) {
2222
assert.expect(2);
2323
bcrypt.genSalt(10).then(function(salt) {
24-
assert.ok(typeof salt !== 'undefined', 'salt must not be undefined');
25-
assert.equals(29, salt.length, "Salt isn't the correct length.");
24+
assert.ok(salt,'salt must be defined');
25+
assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
2626
assert.done();
2727
});
2828
},
@@ -47,7 +47,7 @@ if (typeof Promise !== 'undefined') {
4747
});
4848
},
4949
test_hash_returns_promise_on_null_callback: function(assert) {
50-
assert.ok(typeof bcrypt.hash('password', 10, null).then === 'function', "Should return a promise");
50+
assert.strictEqual(typeof bcrypt.hash('password', 10, null).then,'function', "Should return a promise");
5151
assert.done();
5252
},
5353
test_hash: function(assert) {
@@ -62,7 +62,7 @@ if (typeof Promise !== 'undefined') {
6262
test_hash_rounds: function(assert) {
6363
assert.expect(1);
6464
bcrypt.hash('bacon', 8).then(function(hash) {
65-
assert.equals(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
65+
assert.strictEqual(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
6666
assert.done();
6767
});
6868
},
@@ -114,7 +114,7 @@ if (typeof Promise !== 'undefined') {
114114
fail(assert, "should not resolve");
115115
}).catch(function(err) {
116116
assert.notEqual(err, undefined);
117-
assert.equal(err.message, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
117+
assert.strictEqual(err.message, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
118118
})
119119
]).then(function() {
120120
assert.done();
@@ -124,46 +124,46 @@ if (typeof Promise !== 'undefined') {
124124
assert.expect(2);
125125
bcrypt.genSalt(10).then(function(salt) {
126126
var split_salt = salt.split('$');
127-
assert.ok(split_salt[1], '2a');
128-
assert.ok(split_salt[2], '10');
127+
assert.strictEqual(split_salt[1], '2a');
128+
assert.strictEqual(split_salt[2], '10');
129129
assert.done();
130130
});
131131
},
132132
test_verify_salt_min_rounds: function(assert) {
133133
assert.expect(2);
134134
bcrypt.genSalt(1).then(function(salt) {
135135
var split_salt = salt.split('$');
136-
assert.ok(split_salt[1], '2a');
137-
assert.ok(split_salt[2], '4');
136+
assert.strictEqual(split_salt[1], '2a');
137+
assert.strictEqual(split_salt[2], '04');
138138
assert.done();
139139
});
140140
},
141141
test_verify_salt_max_rounds: function(assert) {
142142
assert.expect(2);
143143
bcrypt.genSalt(100).then(function(salt) {
144144
var split_salt = salt.split('$');
145-
assert.ok(split_salt[1], '2a');
146-
assert.ok(split_salt[2], '31');
145+
assert.strictEqual(split_salt[1], '2a');
146+
assert.strictEqual(split_salt[2], '31');
147147
assert.done();
148148
});
149149
},
150150
test_hash_compare_returns_promise_on_null_callback: function(assert) {
151-
assert.ok(typeof bcrypt.compare('password', 'something', null).then === 'function', "Should return a promise");
151+
assert.strictEqual(typeof bcrypt.compare('password', 'something', null).then, 'function', "Should return a promise");
152152
assert.done();
153153
},
154154
test_hash_compare: function(assert) {
155155
assert.expect(3);
156156
bcrypt.genSalt(10).then(function(salt) {
157-
assert.equals(29, salt.length, "Salt isn't the correct length.");
157+
assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
158158
return bcrypt.hash("test", salt);
159159
}).then(function(hash) {
160160
return Promise.all(
161161
[
162162
bcrypt.compare("test", hash).then(function(res) {
163-
assert.equal(res, true, "These hashes should be equal.");
163+
assert.strictEqual(res, true, "These hashes should be equal.");
164164
}),
165165
bcrypt.compare("blah", hash).then(function(res) {
166-
assert.equal(res, false, "These hashes should not be equal.");
166+
assert.strictEqual(res, false, "These hashes should not be equal.");
167167
})
168168
]).then(function() {
169169
assert.done();
@@ -174,10 +174,10 @@ if (typeof Promise !== 'undefined') {
174174
assert.expect(2);
175175
var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(10));
176176
bcrypt.compare("", hash).then(function(res) {
177-
assert.equal(res, false, "These hashes should be equal.");
177+
assert.strictEqual(res, false, "These hashes should not be equal.");
178178
return bcrypt.compare("", "");
179179
}).then(function(res) {
180-
assert.equal(res, false, "These hashes should be equal.");
180+
assert.strictEqual(res, false, "These hashes should not be equal.");
181181
assert.done();
182182
});
183183
},
@@ -201,7 +201,7 @@ if (typeof Promise !== 'undefined') {
201201
bcrypt.compare().then(function() {
202202
fail(assert, 'Should not resolve');
203203
}).catch(function(err) {
204-
assert.equal(err.message, 'data and hash arguments required', 'Promise should be rejected when no parameters are supplied');
204+
assert.strictEqual(err.message, 'data and hash arguments required', 'Promise should be rejected when no parameters are supplied');
205205
}).then(function() {
206206
assert.done();
207207
});
@@ -211,7 +211,7 @@ if (typeof Promise !== 'undefined') {
211211
bcrypt.compare('password').then(function() {
212212
fail(assert, 'Should not resolve');
213213
}).catch(function(err) {
214-
assert.equal(err.message, 'data and hash arguments required', 'Promise should be rejected when no parameters are supplied');
214+
assert.strictEqual(err.message, 'data and hash arguments required', 'Promise should be rejected when no parameters are supplied');
215215
}).then(function() {
216216
assert.done();
217217
});

‎test/sync.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ var bcrypt = require('../bcrypt');
33
module.exports = {
44
test_salt_length: function(assert) {
55
var salt = bcrypt.genSaltSync(10);
6-
assert.equals(29, salt.length, "Salt isn't the correct length.");
6+
assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
77
var split_salt = salt.split('$');
8-
assert.ok(split_salt[1], '2a');
9-
assert.ok(split_salt[2], '10');
8+
assert.strictEqual(split_salt[1], '2a');
9+
assert.strictEqual(split_salt[2], '10');
1010
assert.done();
1111
},
1212
test_salt_no_params: function(assert) {
1313
// same as test_verify_salt except using default rounds of 10
1414
var salt = bcrypt.genSaltSync();
1515
var split_salt = salt.split('$');
16-
assert.ok(split_salt[1], '2a');
17-
assert.ok(split_salt[2], '10');
16+
assert.strictEqual(split_salt[1], '2a');
17+
assert.strictEqual(split_salt[2], '10');
1818
assert.done();
1919
},
2020
test_salt_rounds_is_string_number: function(assert) {
@@ -31,7 +31,7 @@ module.exports = {
3131
},
3232
test_hash_rounds: function(assert) {
3333
var hash = bcrypt.hashSync('password', 8);
34-
assert.equals(bcrypt.getRounds(hash), 8, "Number of rounds should equal 8.");
34+
assert.strictEqual(bcrypt.getRounds(hash), 8, "Number of rounds should equal 8.");
3535
assert.done();
3636
},
3737
test_hash_empty_string: function(assert) {
@@ -63,27 +63,27 @@ module.exports = {
6363
test_verify_salt: function(assert) {
6464
var salt = bcrypt.genSaltSync(10);
6565
var split_salt = salt.split('$');
66-
assert.ok(split_salt[1], '2a');
67-
assert.ok(split_salt[2], '10');
66+
assert.strictEqual(split_salt[1], '2a');
67+
assert.strictEqual(split_salt[2], '10');
6868
assert.done();
6969
},
7070
test_verify_salt_min_rounds: function(assert) {
7171
var salt = bcrypt.genSaltSync(1);
7272
var split_salt = salt.split('$');
73-
assert.ok(split_salt[1], '2a');
74-
assert.ok(split_salt[2], '4');
73+
assert.strictEqual(split_salt[1], '2a');
74+
assert.strictEqual(split_salt[2], '04');
7575
assert.done();
7676
},
7777
test_verify_salt_max_rounds: function(assert) {
7878
var salt = bcrypt.genSaltSync(100);
7979
var split_salt = salt.split('$');
80-
assert.ok(split_salt[1], '2a');
81-
assert.ok(split_salt[2], '31');
80+
assert.strictEqual(split_salt[1], '2a');
81+
assert.strictEqual(split_salt[2], '31');
8282
assert.done();
8383
},
8484
test_hash_compare: function(assert) {
8585
var salt = bcrypt.genSaltSync(10);
86-
assert.equals(29, salt.length, "Salt isn't the correct length.");
86+
assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
8787
var hash = bcrypt.hashSync("test", salt);
8888
assert.ok(bcrypt.compareSync("test", hash), "These hashes should be equal.");
8989
assert.ok(!(bcrypt.compareSync("blah", hash)), "These hashes should not be equal.");
@@ -109,12 +109,12 @@ module.exports = {
109109
},
110110
test_getRounds: function(assert) {
111111
var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(9));
112-
assert.equals(9, bcrypt.getRounds(hash), "getRounds can't extract rounds");
112+
assert.strictEqual(9, bcrypt.getRounds(hash), "getRounds can't extract rounds");
113113
assert.done();
114114
},
115115
test_getRounds: function(assert) {
116116
var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(9));
117-
assert.equals(9, bcrypt.getRounds(hash), "getRounds can't extract rounds");
117+
assert.strictEqual(9, bcrypt.getRounds(hash), "getRounds can't extract rounds");
118118
assert.throws(function() {bcrypt.getRounds(''); }, "Must pass a valid hash to getRounds");
119119
assert.done();
120120
}

0 commit comments

Comments
 (0)
Please sign in to comment.