async.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. var bcrypt = require('../bcrypt');
  2. module.exports = {
  3. test_salt_length: function(assert) {
  4. assert.expect(1);
  5. bcrypt.genSalt(10, function(err, salt) {
  6. assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
  7. assert.done();
  8. });
  9. },
  10. test_salt_only_cb: function(assert) {
  11. assert.doesNotThrow(function() {bcrypt.genSalt(function(err, salt) {});}, "Should not throw an Error. Rounds and seed length are optional.");
  12. assert.done();
  13. },
  14. test_salt_rounds_is_string_number: function(assert) {
  15. bcrypt.genSalt('10', void 0, function (err, salt) {
  16. assert.ok((err instanceof Error), "Should be an Error. genSalt requires round to be of type number.");
  17. assert.done();
  18. });
  19. },
  20. test_salt_rounds_is_string_non_number: function(assert) {
  21. bcrypt.genSalt('z', function (err, salt) {
  22. assert.ok((err instanceof Error), "Should throw an Error. genSalt requires rounds to of type number.");
  23. assert.done();
  24. });
  25. },
  26. test_salt_minor: function(assert) {
  27. assert.expect(3);
  28. bcrypt.genSalt(10, 'a', function(err, salt) {
  29. assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
  30. var split_salt = salt.split('$');
  31. assert.strictEqual(split_salt[1], '2a');
  32. assert.strictEqual(split_salt[2], '10');
  33. assert.done();
  34. });
  35. },
  36. test_salt_minor_b: function(assert) {
  37. assert.expect(3);
  38. bcrypt.genSalt(10, 'b', function(err, salt) {
  39. assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
  40. var split_salt = salt.split('$');
  41. assert.strictEqual(split_salt[1], '2b');
  42. assert.strictEqual(split_salt[2], '10');
  43. assert.done();
  44. });
  45. },
  46. test_hash: function(assert) {
  47. assert.expect(1);
  48. bcrypt.genSalt(10, function(err, salt) {
  49. bcrypt.hash('password', salt, function(err, res) {
  50. assert.ok(res, "Res should be defined.");
  51. assert.done();
  52. });
  53. });
  54. },
  55. test_hash_rounds: function(assert) {
  56. assert.expect(1);
  57. bcrypt.hash('bacon', 8, function(err, hash) {
  58. assert.strictEqual(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
  59. assert.done();
  60. });
  61. },
  62. test_hash_empty_strings: function(assert) {
  63. assert.expect(2);
  64. bcrypt.genSalt(10, function(err, salt) {
  65. bcrypt.hash('', salt, function(err, res) {
  66. assert.ok(res, "Res should be defined even with an empty pw.");
  67. bcrypt.hash('', '', function(err, res) {
  68. if (err) {
  69. assert.ok(err);
  70. } else {
  71. assert.fail();
  72. }
  73. assert.done();
  74. });
  75. });
  76. });
  77. },
  78. test_hash_no_params: function(assert) {
  79. bcrypt.hash(function (err, hash) {
  80. assert.ok(err, "Should be an error. No params.");
  81. assert.done();
  82. });
  83. },
  84. test_hash_one_param: function(assert) {
  85. bcrypt.hash('password', function (err, hash) {
  86. assert.ok(err, "Should be an Error. No salt.");
  87. assert.done();
  88. });
  89. },
  90. test_hash_salt_validity: function(assert) {
  91. assert.expect(3);
  92. bcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse', function(err, enc) {
  93. assert.strictEqual(err, undefined);
  94. bcrypt.hash('password', 'some$value', function(err, enc) {
  95. assert.notEqual(err, undefined);
  96. assert.strictEqual(err.message, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
  97. assert.done();
  98. });
  99. });
  100. },
  101. test_verify_salt: function(assert) {
  102. assert.expect(2);
  103. bcrypt.genSalt(10, function(err, salt) {
  104. var split_salt = salt.split('$');
  105. assert.strictEqual(split_salt[1], '2b');
  106. assert.strictEqual(split_salt[2], '10');
  107. assert.done();
  108. });
  109. },
  110. test_verify_salt_min_rounds: function(assert) {
  111. assert.expect(2);
  112. bcrypt.genSalt(1, function(err, salt) {
  113. var split_salt = salt.split('$');
  114. assert.strictEqual(split_salt[1], '2b');
  115. assert.strictEqual(split_salt[2], '04');
  116. assert.done();
  117. });
  118. },
  119. test_verify_salt_max_rounds: function(assert) {
  120. assert.expect(2);
  121. bcrypt.genSalt(100, function(err, salt) {
  122. var split_salt = salt.split('$');
  123. assert.strictEqual(split_salt[1], '2b');
  124. assert.strictEqual(split_salt[2], '31');
  125. assert.done();
  126. });
  127. },
  128. test_hash_compare: function(assert) {
  129. assert.expect(3);
  130. bcrypt.genSalt(10, function(err, salt) {
  131. assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
  132. bcrypt.hash("test", salt, function(err, hash) {
  133. bcrypt.compare("test", hash, function(err, res) {
  134. assert.strictEqual(res, true, "These hashes should be equal.");
  135. bcrypt.compare("blah", hash, function(err, res) {
  136. assert.strictEqual(res, false, "These hashes should not be equal.");
  137. assert.done();
  138. });
  139. });
  140. });
  141. });
  142. },
  143. test_hash_compare_empty_strings: function(assert) {
  144. assert.expect(2);
  145. var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(10));
  146. bcrypt.compare("", hash, function(err, res) {
  147. assert.strictEqual(res, false, "These hashes should not be equal.");
  148. bcrypt.compare("", "", function(err, res) {
  149. assert.strictEqual(res, false, "These hashes should not be equal.");
  150. assert.done();
  151. });
  152. });
  153. },
  154. test_hash_compare_invalid_strings: function(assert) {
  155. var fullString = 'envy1362987212538';
  156. var hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
  157. var wut = ':';
  158. bcrypt.compare(fullString, hash, function(err, res) {
  159. assert.ok(res);
  160. bcrypt.compare(fullString, wut, function(err, res) {
  161. assert.ok(!res);
  162. assert.done();
  163. });
  164. });
  165. },
  166. test_compare_no_params: function(assert) {
  167. bcrypt.compare(function(err, hash) {
  168. assert.ok(err, 'Should be an error. No params.');
  169. assert.done();
  170. });
  171. },
  172. test_hash_compare_one_param: function(assert) {
  173. bcrypt.compare('password', function(err, hash) {
  174. assert.ok(err, 'Should be an Error. No hash.');
  175. assert.done();
  176. });
  177. }
  178. };