sync.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var bcrypt = require('../bcrypt');
  2. module.exports = {
  3. test_salt_length: function(assert) {
  4. var salt = bcrypt.genSaltSync(10);
  5. assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
  6. var split_salt = salt.split('$');
  7. assert.strictEqual(split_salt[1], '2b');
  8. assert.strictEqual(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.strictEqual(split_salt[1], '2b');
  16. assert.strictEqual(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_salt_minor_a: function(assert) {
  28. var salt = bcrypt.genSaltSync(10, 'a');
  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. test_salt_minor_b: function(assert) {
  36. var salt = bcrypt.genSaltSync(10, 'b');
  37. assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
  38. var split_salt = salt.split('$');
  39. assert.strictEqual(split_salt[1], '2b');
  40. assert.strictEqual(split_salt[2], '10');
  41. assert.done();
  42. },
  43. test_hash: function(assert) {
  44. assert.ok(bcrypt.hashSync('password', bcrypt.genSaltSync(10)), "Shouldn't throw an Error.");
  45. assert.done();
  46. },
  47. test_hash_rounds: function(assert) {
  48. var hash = bcrypt.hashSync('password', 8);
  49. assert.strictEqual(bcrypt.getRounds(hash), 8, "Number of rounds should equal 8.");
  50. assert.done();
  51. },
  52. test_hash_empty_string: function(assert) {
  53. assert.ok(bcrypt.hashSync('', bcrypt.genSaltSync(10)), "Shouldn't throw an Error.");
  54. assert.throws(function() {bcrypt.hashSync('password', '')}, "Should have thrown an Error related to the salt.");
  55. assert.throws(function() {bcrypt.hashSync('', '')}, "Should have thrown an Error related to the salt.");
  56. assert.done();
  57. },
  58. test_hash_pw_no_params: function(assert) {
  59. assert.throws(function() {bcrypt.hashSync();}, "Should throw an Error. No Params.");
  60. assert.done();
  61. },
  62. test_hash_pw_one_param: function(assert) {
  63. assert.throws(function() {bcrypt.hashSync('password');}, "Should throw an Error. No salt.");
  64. assert.done();
  65. },
  66. test_hash_pw_not_hash_str: function(assert) {
  67. assert.throws(function() {bcrypt.hashSync('password', {});}, "Should throw an Error. hash should be a string or number.");
  68. assert.done();
  69. },
  70. test_hash_salt_validity: function(assert) {
  71. assert.expect(2);
  72. assert.ok(bcrypt.hashSync('password', '$2a$10$somesaltyvaluertsetrse'));
  73. assert.throws(function() {
  74. bcrypt.hashSync('password', 'some$value');
  75. });
  76. assert.done();
  77. },
  78. test_verify_salt: function(assert) {
  79. var salt = bcrypt.genSaltSync(10);
  80. var split_salt = salt.split('$');
  81. assert.strictEqual(split_salt[1], '2b');
  82. assert.strictEqual(split_salt[2], '10');
  83. assert.done();
  84. },
  85. test_verify_salt_min_rounds: function(assert) {
  86. var salt = bcrypt.genSaltSync(1);
  87. var split_salt = salt.split('$');
  88. assert.strictEqual(split_salt[1], '2b');
  89. assert.strictEqual(split_salt[2], '04');
  90. assert.done();
  91. },
  92. test_verify_salt_max_rounds: function(assert) {
  93. var salt = bcrypt.genSaltSync(100);
  94. var split_salt = salt.split('$');
  95. assert.strictEqual(split_salt[1], '2b');
  96. assert.strictEqual(split_salt[2], '31');
  97. assert.done();
  98. },
  99. test_hash_compare: function(assert) {
  100. var salt = bcrypt.genSaltSync(10);
  101. assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
  102. var hash = bcrypt.hashSync("test", salt);
  103. assert.ok(bcrypt.compareSync("test", hash), "These hashes should be equal.");
  104. assert.ok(!(bcrypt.compareSync("blah", hash)), "These hashes should not be equal.");
  105. assert.done();
  106. },
  107. test_hash_compare_empty_strings: function(assert) {
  108. assert.ok(!(bcrypt.compareSync("", "password")), "These hashes should not be equal.");
  109. assert.ok(!(bcrypt.compareSync("", "")), "These hashes should not be equal.");
  110. assert.ok(!(bcrypt.compareSync("password", "")), "These hashes should not be equal.");
  111. assert.done();
  112. },
  113. test_hash_compare_invalid_strings: function(assert) {
  114. var fullString = 'envy1362987212538';
  115. var hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
  116. var wut = ':';
  117. bcrypt.compareSync(fullString, hash, function(err, res) {
  118. assert.ok(res);
  119. });
  120. bcrypt.compareSync(fullString, wut, function(err, res) {
  121. assert.ok(!res)
  122. });
  123. assert.done();
  124. },
  125. test_getRounds: function(assert) {
  126. var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(9));
  127. assert.strictEqual(9, bcrypt.getRounds(hash), "getRounds can't extract rounds");
  128. assert.done();
  129. },
  130. test_getRounds: function(assert) {
  131. var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(9));
  132. assert.strictEqual(9, bcrypt.getRounds(hash), "getRounds can't extract rounds");
  133. assert.throws(function() {bcrypt.getRounds(''); }, "Must pass a valid hash to getRounds");
  134. assert.done();
  135. }
  136. };