sync.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var bcrypt = require('../bcrypt');
  2. module.exports = {
  3. test_salt_length: function(assert) {
  4. var salt = bcrypt.genSaltSync(10);
  5. assert.equals(29, salt.length, "Salt isn't the correct length.");
  6. var split_salt = salt.split('$');
  7. assert.ok(split_salt[1], '2a');
  8. assert.ok(split_salt[2], '10');
  9. assert.done();
  10. },
  11. test_salt_no_params: function(assert) {
  12. // same as test_verify_salt except using default rounds of 10
  13. var salt = bcrypt.genSaltSync();
  14. var split_salt = salt.split('$');
  15. assert.ok(split_salt[1], '2a');
  16. assert.ok(split_salt[2], '10');
  17. assert.done();
  18. },
  19. test_salt_rounds_is_string_number: function(assert) {
  20. assert.throws(function() {bcrypt.genSaltSync('10');}, "Should throw an Error. No params.");
  21. assert.done();
  22. },
  23. test_salt_rounds_is_NaN: function(assert) {
  24. assert.throws(function() {bcrypt.genSaltSync('b');}, "Should throw an Error. gen_salt requires rounds to be a number.");
  25. assert.done();
  26. },
  27. test_hash: function(assert) {
  28. assert.ok(bcrypt.hashSync('password', bcrypt.genSaltSync(10)), "Shouldn't throw an Error.");
  29. assert.done();
  30. },
  31. test_hash_rounds: function(assert) {
  32. var hash = bcrypt.hashSync('password', 8);
  33. assert.equals(bcrypt.getRounds(hash), 8, "Number of rounds should equal 8.");
  34. assert.done();
  35. },
  36. test_hash_empty_string: function(assert) {
  37. assert.ok(bcrypt.hashSync('', bcrypt.genSaltSync(10)), "Shouldn't throw an Error.");
  38. assert.throws(function() {bcrypt.hashSync('password', '')}, "Should have thrown an Error related to the salt.");
  39. assert.throws(function() {bcrypt.hashSync('', '')}, "Should have thrown an Error related to the salt.");
  40. assert.done();
  41. },
  42. test_hash_pw_no_params: function(assert) {
  43. assert.throws(function() {bcrypt.hashSync();}, "Should throw an Error. No Params.");
  44. assert.done();
  45. },
  46. test_hash_pw_one_param: function(assert) {
  47. assert.throws(function() {bcrypt.hashSync('password');}, "Should throw an Error. No salt.");
  48. assert.done();
  49. },
  50. test_hash_pw_not_hash_str: function(assert) {
  51. assert.throws(function() {bcrypt.hashSync('password', {});}, "Should throw an Error. hash should be a string or number.");
  52. assert.done();
  53. },
  54. test_hash_salt_validity: function(assert) {
  55. assert.expect(2);
  56. assert.ok(bcrypt.hashSync('password', '$2a$10$somesaltyvaluertsetrse'));
  57. assert.throws(function() {
  58. bcrypt.hashSync('password', 'some$value');
  59. });
  60. assert.done();
  61. },
  62. test_verify_salt: function(assert) {
  63. var salt = bcrypt.genSaltSync(10);
  64. var split_salt = salt.split('$');
  65. assert.ok(split_salt[1], '2a');
  66. assert.ok(split_salt[2], '10');
  67. assert.done();
  68. },
  69. test_verify_salt_min_rounds: function(assert) {
  70. var salt = bcrypt.genSaltSync(1);
  71. var split_salt = salt.split('$');
  72. assert.ok(split_salt[1], '2a');
  73. assert.ok(split_salt[2], '4');
  74. assert.done();
  75. },
  76. test_verify_salt_max_rounds: function(assert) {
  77. var salt = bcrypt.genSaltSync(100);
  78. var split_salt = salt.split('$');
  79. assert.ok(split_salt[1], '2a');
  80. assert.ok(split_salt[2], '31');
  81. assert.done();
  82. },
  83. test_hash_compare: function(assert) {
  84. var salt = bcrypt.genSaltSync(10);
  85. assert.equals(29, salt.length, "Salt isn't the correct length.");
  86. var hash = bcrypt.hashSync("test", salt);
  87. assert.ok(bcrypt.compareSync("test", hash), "These hashes should be equal.");
  88. assert.ok(!(bcrypt.compareSync("blah", hash)), "These hashes should not be equal.");
  89. assert.done();
  90. },
  91. test_hash_compare_empty_strings: function(assert) {
  92. assert.ok(!(bcrypt.compareSync("", "password")), "These hashes should not be equal.");
  93. assert.ok(!(bcrypt.compareSync("", "")), "These hashes should not be equal.");
  94. assert.ok(!(bcrypt.compareSync("password", "")), "These hashes should not be equal.");
  95. assert.done();
  96. },
  97. test_hash_compare_invalid_strings: function(assert) {
  98. var fullString = 'envy1362987212538';
  99. var hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
  100. var wut = ':';
  101. bcrypt.compareSync(fullString, hash, function(err, res) {
  102. assert.ok(res);
  103. });
  104. bcrypt.compareSync(fullString, wut, function(err, res) {
  105. assert.ok(!res)
  106. });
  107. assert.done();
  108. },
  109. test_getRounds: function(assert) {
  110. var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(9));
  111. assert.equals(9, bcrypt.getRounds(hash), "getRounds can't extract rounds");
  112. assert.done();
  113. },
  114. test_getRounds: function(assert) {
  115. var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(9));
  116. assert.equals(9, bcrypt.getRounds(hash), "getRounds can't extract rounds");
  117. assert.throws(function() {bcrypt.getRounds(''); }, "Must pass a valid hash to getRounds");
  118. assert.done();
  119. }
  120. };