async.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.equals(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('b', 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_hash: function(assert) {
  27. assert.expect(1);
  28. bcrypt.genSalt(10, function(err, salt) {
  29. bcrypt.hash('password', salt, function(err, res) {
  30. assert.ok(res, "Res should be defined.");
  31. assert.done();
  32. });
  33. });
  34. },
  35. test_hash_rounds: function(assert) {
  36. assert.expect(1);
  37. bcrypt.hash('bacon', 8, function(err, hash) {
  38. assert.equals(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
  39. assert.done();
  40. });
  41. },
  42. test_hash_empty_strings: function(assert) {
  43. assert.expect(2);
  44. bcrypt.genSalt(10, function(err, salt) {
  45. bcrypt.hash('', salt, function(err, res) {
  46. assert.ok(res, "Res should be defined even with an empty pw.");
  47. bcrypt.hash('', '', function(err, res) {
  48. if (err) {
  49. assert.ok(err);
  50. } else {
  51. assert.fail();
  52. }
  53. assert.done();
  54. });
  55. });
  56. });
  57. },
  58. test_hash_no_params: function(assert) {
  59. bcrypt.hash(function (err, hash) {
  60. assert.ok(err, "Should be an error. No params.");
  61. assert.done();
  62. });
  63. },
  64. test_hash_one_param: function(assert) {
  65. bcrypt.hash('password', function (err, hash) {
  66. assert.ok(err, "Should be an Error. No salt.");
  67. assert.done();
  68. });
  69. },
  70. test_hash_salt_validity: function(assert) {
  71. assert.expect(3);
  72. bcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse', function(err, enc) {
  73. assert.equal(err, undefined);
  74. bcrypt.hash('password', 'some$value', function(err, enc) {
  75. assert.notEqual(err, undefined);
  76. assert.equal(err.message, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
  77. assert.done();
  78. });
  79. });
  80. },
  81. test_verify_salt: function(assert) {
  82. assert.expect(2);
  83. bcrypt.genSalt(10, function(err, salt) {
  84. var split_salt = salt.split('$');
  85. assert.ok(split_salt[1], '2a');
  86. assert.ok(split_salt[2], '10');
  87. assert.done();
  88. });
  89. },
  90. test_verify_salt_min_rounds: function(assert) {
  91. assert.expect(2);
  92. bcrypt.genSalt(1, function(err, salt) {
  93. var split_salt = salt.split('$');
  94. assert.ok(split_salt[1], '2a');
  95. assert.ok(split_salt[2], '4');
  96. assert.done();
  97. });
  98. },
  99. test_verify_salt_max_rounds: function(assert) {
  100. assert.expect(2);
  101. bcrypt.genSalt(100, function(err, salt) {
  102. var split_salt = salt.split('$');
  103. assert.ok(split_salt[1], '2a');
  104. assert.ok(split_salt[2], '31');
  105. assert.done();
  106. });
  107. },
  108. test_hash_compare: function(assert) {
  109. assert.expect(3);
  110. bcrypt.genSalt(10, function(err, salt) {
  111. assert.equals(29, salt.length, "Salt isn't the correct length.");
  112. bcrypt.hash("test", salt, function(err, hash) {
  113. bcrypt.compare("test", hash, function(err, res) {
  114. assert.equal(res, true, "These hashes should be equal.");
  115. bcrypt.compare("blah", hash, function(err, res) {
  116. assert.equal(res, false, "These hashes should not be equal.");
  117. assert.done();
  118. });
  119. });
  120. });
  121. });
  122. },
  123. test_hash_compare_empty_strings: function(assert) {
  124. assert.expect(2);
  125. var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(10));
  126. bcrypt.compare("", hash, function(err, res) {
  127. assert.equal(res, false, "These hashes should be equal.");
  128. bcrypt.compare("", "", function(err, res) {
  129. assert.equal(res, false, "These hashes should be equal.");
  130. assert.done();
  131. });
  132. });
  133. },
  134. test_hash_compare_invalid_strings: function(assert) {
  135. var fullString = 'envy1362987212538';
  136. var hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
  137. var wut = ':';
  138. bcrypt.compare(fullString, hash, function(err, res) {
  139. assert.ok(res);
  140. bcrypt.compare(fullString, wut, function(err, res) {
  141. assert.ok(!res);
  142. assert.done();
  143. });
  144. });
  145. }
  146. };