repetitions.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var bcrypt = require('../bcrypt');
  2. var EXPECTED = 2500; //number of times to iterate these tests...
  3. module.exports = {
  4. test_salt_length: function(assert) {
  5. assert.expect(EXPECTED);
  6. var n = 0;
  7. for (var i = 0; i < EXPECTED; i++) {
  8. bcrypt.genSalt(10, function(err, salt) {
  9. assert.equals(29, salt.length, "Salt ("+salt+") isn't the correct length. It is: " + salt.length);
  10. n++;
  11. });
  12. }
  13. function checkVal() {
  14. if (n == EXPECTED) {
  15. assert.done();
  16. } else {
  17. setTimeout(checkVal, 100);
  18. }
  19. }
  20. setTimeout(checkVal, 100);
  21. },
  22. test_hash_length: function(assert) {
  23. assert.expect(EXPECTED);
  24. var SALT = '$2a$04$TnjywYklQbbZjdjBgBoA4e';
  25. var n = 0;
  26. for (var i = 0; i < EXPECTED; i++) {
  27. bcrypt.hash('test', SALT, function(err, crypted) {
  28. assert.equals(60, crypted.length, "Encrypted ("+crypted+") isn't the correct length. It is: " + crypted.length);
  29. n++;
  30. });
  31. }
  32. function checkVal() {
  33. if (n == EXPECTED) {
  34. assert.done();
  35. } else {
  36. setTimeout(checkVal, 100);
  37. }
  38. }
  39. setTimeout(checkVal, 100);
  40. },
  41. test_compare: function(assert) {
  42. assert.expect(EXPECTED);
  43. var HASH = '$2a$04$TnjywYklQbbZjdjBgBoA4e9G7RJt9blgMgsCvUvus4Iv4TENB5nHy';
  44. var n = 0;
  45. for (var i = 0; i < EXPECTED; i++) {
  46. bcrypt.compare('test', HASH, function(err, match) {
  47. assert.equal(true, match, "No match.");
  48. n++;
  49. });
  50. }
  51. function checkVal() {
  52. if (n == EXPECTED) {
  53. assert.done();
  54. } else {
  55. setTimeout(checkVal, 100);
  56. }
  57. }
  58. setTimeout(checkVal, 100);
  59. },
  60. test_hash_and_compare: function(assert) {
  61. assert.expect((EXPECTED-1)*3);
  62. var salt = bcrypt.genSaltSync(4),
  63. idx = 0,
  64. good_done = false,
  65. bad_done = false;
  66. function next() {
  67. return test('secret' + Math.random());
  68. }
  69. function test(password) {
  70. idx += 1;
  71. return bcrypt.hash(password, salt, function(err, hash) {
  72. if (err) throw err;
  73. //console.log('\nbcrypt iter ' + idx);
  74. assert.ok(hash);
  75. bcrypt.compare(password, hash, function(err, res) {
  76. //if (err) throw err;
  77. assert.ok(res);
  78. if (idx >= (EXPECTED-1)) {
  79. good_done = true;
  80. }
  81. });
  82. bcrypt.compare('bad' + password, hash, function(err, res) {
  83. //if (err) throw err;
  84. assert.ok(!res);
  85. if (idx >= (EXPECTED-1)) {
  86. bad_done = true;
  87. }
  88. });
  89. if (idx < ((EXPECTED)-1)) {
  90. next();
  91. } else {
  92. function checkDone() {
  93. if (idx >= (EXPECTED-1) && good_done && bad_done) {
  94. assert.done();
  95. } else {
  96. setTimeout(checkDone, 100);
  97. }
  98. }
  99. setTimeout(checkDone, 100);
  100. }
  101. });
  102. }
  103. next();
  104. }
  105. };