Skip to content
Browse files

No errors thrown by asynchronous functions.

  • Loading branch information...
1 parent 14f6955 commit d51e65ce74da2528cf4bb59e2077bb52a7535cb2 @shovon shovon committed May 26, 2013
Showing with 37 additions and 25 deletions.
  1. +17 −11 bcrypt.js
  2. +20 −14 test/async.js
View
28 bcrypt.js
@@ -56,18 +56,18 @@ module.exports.genSalt = function(rounds, seed_length, cb) {
if (!rounds) {
rounds = 10;
} else if (typeof rounds !== 'number') {
- throw new Error('rounds must be a number');
+ return cb(new Error('rounds must be a number'));
}
// default length 20
if (!seed_length) {
seed_length = 20;
} else if (typeof seed_length !== 'number') {
- throw new Error('seed_length must be a number');
+ return cb(new Error('seed_length must be a number'));
}
if (!cb) {
- throw new Error('callback required for gen_salt');
+ return;
}
return bindings.gen_salt(rounds, seed_length, cb);
@@ -108,16 +108,22 @@ module.exports.encrypt = function(data, salt, cb) {
}
module.exports.hash = function(data, salt, cb) {
+ if (typeof data === 'function') {
+ return data(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
+ }
+ if (typeof salt === 'function') {
+ return salt(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
+ }
if (data == null || data == undefined || salt == null || salt == undefined) {
- throw new Error('data and salt arguments required');
+ return cb(new Error('data and salt arguments required'));
} else if (typeof data !== 'string' && (typeof salt !== 'string' || typeof salt !== 'number')) {
- throw new Error('data must be a string and salt must either be a salt string or a number of rounds');
+ return cb(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
}
if (!cb) {
- throw new Error('callback required for async compare');
+ return;
} else if (typeof cb !== 'function') {
- throw new Error('callback must be a function');
+ return;
}
if (typeof salt === 'number') {
@@ -155,15 +161,15 @@ module.exports.compareSync = function(data, hash) {
/// @param {Function} cb callback(err, matched) - matched is true if hashed data matches hash
module.exports.compare = function(data, hash, cb) {
if (data == null || data == undefined || hash == null || hash == undefined) {
- throw new Error('data and hash arguments required');
+ return cb(new Error('data and hash arguments required'));
} else if (typeof data !== 'string' || typeof hash !== 'string') {
- throw new Error('data and hash must be strings');
+ return cb(new Error('data and hash must be strings'));
}
if (!cb) {
- throw new Error('callback required for async compare');
+ return;
} else if (typeof cb !== 'function') {
- throw new Error('callback must be a function');
+ return;
}
return bindings.compare(data, hash, cb);
View
34 test/async.js
@@ -8,21 +8,21 @@ module.exports = {
assert.done();
});
},
- test_salt_no_params: function(assert) {
- assert.throws(function() {bcrypt.genSalt();}, "Should throw an Error. genSalt requires a callback.");
- assert.done();
- },
test_salt_only_cb: function(assert) {
assert.doesNotThrow(function() {bcrypt.genSalt(function(err, salt) {});}, "Should not throw an Error. Rounds and seed length are optional.");
assert.done();
},
test_salt_rounds_is_string_number: function(assert) {
- assert.throws(function() {bcrypt.genSalt('10');}, "Should throw an Error. No params.");
- assert.done();
+ bcrypt.genSalt('10', void 0, function (err, salt) {
+ assert.ok((err instanceof Error), "Should be an Error. genSalt requires round to be of type number.");
+ assert.done();
+ });
},
- test_salt_rounds_is_NaN: function(assert) {
- assert.throws(function() {bcrypt.genSalt('b');}, "Should throw an Error. genSalt requires rounds to be a number.");
- assert.done();
+ test_salt_rounds_is_string_non_number: function(assert) {
+ bcrypt.genSalt('b', function (err, salt) {
+ assert.ok((err instanceof Error), "Should throw an Error. genSalt requires rounds to of type number.");
+ assert.done();
+ });
},
test_hash: function(assert) {
assert.expect(1);
@@ -58,15 +58,21 @@ module.exports = {
});
},
test_hash_no_params: function(assert) {
- assert.throws(function() {bcrypt.hash();}, "Should throw an Error. No Params.");
- assert.done();
+ bcrypt.hash(function (err, hash) {
+ assert.ok(err, "Should be an error. No params.");
+ assert.done();
+ });
},
test_hash_one_param: function(assert) {
- assert.throws(function() {bcrypt.hash('password');}, "Should throw an Error. No salt.");
- assert.done();
+ bcrypt.hash('password', function (err, hash) {
+ assert.ok(err, "Should be an Error. No salt.");
+ assert.done();
+ });
},
test_hash_not_hash_str: function(assert) {
- assert.throws(function() {bcrypt.hash('password', 1);}, "Should throw an Error. hash should be a string.");
+ bcrypt.hash('password', 1, function (err, hash) {
+ assert.ok(err, "Should be an Error. hash should be a string.")
+ });
assert.done();
},
test_hash_salt_validity: function(assert) {

0 comments on commit d51e65c

Please sign in to comment.
Something went wrong with that request. Please try again.