suite.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var path = require("path"),
  2. fs = require("fs"),
  3. binding = require("bcrypt"),
  4. bcrypt = require(path.join(__dirname, '..', 'index.js'))/*,
  5. isaac = eval(
  6. fs.readFileSync(path.join(__dirname, "..", "src", "bcrypt", "prng", "accum.js"))+
  7. fs.readFileSync(path.join(__dirname, "..", "src", "bcrypt", "prng", "isaac.js"))+
  8. " accum.start();"+
  9. " isaac"
  10. )*/;
  11. module.exports = {
  12. "encodeBase64": function(test) {
  13. var str = bcrypt.encodeBase64([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10], 16);
  14. test.strictEqual(str, "..CA.uOD/eaGAOmJB.yMBu");
  15. test.done();
  16. },
  17. "decodeBase64": function(test) {
  18. var bytes = bcrypt.decodeBase64("..CA.uOD/eaGAOmJB.yMBv.", 16);
  19. test.deepEqual(bytes, [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]);
  20. test.done();
  21. },
  22. "genSaltSync": function(test) {
  23. var salt = bcrypt.genSaltSync(10);
  24. test.ok(salt);
  25. test.ok(typeof salt == 'string');
  26. test.ok(salt.length > 0);
  27. test.done();
  28. },
  29. "genSalt": function(test) {
  30. bcrypt.genSalt(10, function(err, salt) {
  31. test.ok(salt);
  32. test.ok(typeof salt == 'string');
  33. test.ok(salt.length > 0);
  34. test.done();
  35. });
  36. },
  37. "hashSync": function(test) {
  38. test.doesNotThrow(function() {
  39. bcrypt.hashSync("hello", 10);
  40. });
  41. test.notEqual(bcrypt.hashSync("hello", 10), bcrypt.hashSync("hello", 10));
  42. test.done();
  43. },
  44. "hash": function(test) {
  45. bcrypt.hash("hello", 10, function(err, hash) {
  46. test.notOk(err);
  47. test.ok(hash);
  48. test.done();
  49. });
  50. },
  51. "compareSync": function(test) {
  52. var salt1 = bcrypt.genSaltSync(),
  53. hash1 = bcrypt.hashSync("hello", salt1); // $2a$
  54. var salt2 = bcrypt.genSaltSync().replace(/\$2a\$/, "$2y$"),
  55. hash2 = bcrypt.hashSync("world", salt2);
  56. var salt3 = bcrypt.genSaltSync().replace(/\$2a\$/, "$2b$"),
  57. hash3 = bcrypt.hashSync("hello world", salt3);
  58. test.strictEqual(hash1.substring(0,4), "$2a$");
  59. test.ok(bcrypt.compareSync("hello", hash1));
  60. test.notOk(bcrypt.compareSync("hello", hash2));
  61. test.notOk(bcrypt.compareSync("hello", hash3));
  62. test.strictEqual(hash2.substring(0,4), "$2y$");
  63. test.ok(bcrypt.compareSync("world", hash2));
  64. test.notOk(bcrypt.compareSync("world", hash1));
  65. test.notOk(bcrypt.compareSync("world", hash3));
  66. test.strictEqual(hash3.substring(0,4), "$2b$");
  67. test.ok(bcrypt.compareSync("hello world", hash3));
  68. test.notOk(bcrypt.compareSync("hello world", hash1));
  69. test.notOk(bcrypt.compareSync("hello world", hash2));
  70. test.done();
  71. },
  72. "compare": function(test) {
  73. var salt1 = bcrypt.genSaltSync(),
  74. hash1 = bcrypt.hashSync("hello", salt1); // $2a$
  75. var salt2 = bcrypt.genSaltSync();
  76. salt2 = salt2.substring(0,2)+'y'+salt2.substring(3); // $2y$
  77. var hash2 = bcrypt.hashSync("world", salt2);
  78. bcrypt.compare("hello", hash1, function(err, same) {
  79. test.notOk(err);
  80. test.ok(same);
  81. bcrypt.compare("hello", hash2, function(err, same) {
  82. test.notOk(err);
  83. test.notOk(same);
  84. bcrypt.compare("world", hash2, function(err, same) {
  85. test.notOk(err);
  86. test.ok(same);
  87. bcrypt.compare("world", hash1, function(err, same) {
  88. test.notOk(err);
  89. test.notOk(same);
  90. test.done();
  91. });
  92. });
  93. });
  94. });
  95. },
  96. "getSalt": function(test) {
  97. var hash1 = bcrypt.hashSync("hello", bcrypt.genSaltSync());
  98. var salt = bcrypt.getSalt(hash1);
  99. var hash2 = bcrypt.hashSync("hello", salt);
  100. test.equal(hash1, hash2);
  101. test.done();
  102. },
  103. "getRounds": function(test) {
  104. var hash1 = bcrypt.hashSync("hello", bcrypt.genSaltSync());
  105. test.equal(bcrypt.getRounds(hash1), 10);
  106. test.done();
  107. },
  108. "progress": function(test) {
  109. bcrypt.genSalt(12, function(err, salt) {
  110. test.ok(!err);
  111. var progress = [];
  112. bcrypt.hash("hello world", salt, function(err, hash) {
  113. test.ok(!err);
  114. test.ok(typeof hash === 'string');
  115. test.ok(progress.length >= 2);
  116. test.strictEqual(progress[0], 0);
  117. test.strictEqual(progress[progress.length-1], 1);
  118. test.done();
  119. }, function(n) {
  120. progress.push(n);
  121. });
  122. });
  123. },
  124. "promise": function(test) {
  125. bcrypt.genSalt(10)
  126. .then(function(salt) {
  127. bcrypt.hash("hello", salt)
  128. .then(function(hash) {
  129. test.ok(hash);
  130. bcrypt.compare("hello", hash)
  131. .then(function(result) {
  132. test.ok(result);
  133. bcrypt.genSalt(/* no args */)
  134. .then(function(salt) {
  135. test.ok(salt);
  136. test.done();
  137. }, function(err) {
  138. test.fail(err, null, "promise rejected");
  139. });
  140. }, function(err) {
  141. test.fail(err, null, "promise rejected");
  142. });
  143. }, function(err) {
  144. test.fail(err, null, 'promise rejected');
  145. });
  146. }, function(err) {
  147. test.fail(err, null, "promise rejected");
  148. });
  149. },
  150. "compat": {
  151. "quickbrown": function(test) {
  152. var pass = fs.readFileSync(path.join(__dirname, "quickbrown.txt"))+"",
  153. salt = bcrypt.genSaltSync(),
  154. hash1 = binding.hashSync(pass, salt),
  155. hash2 = bcrypt.hashSync(pass, salt);
  156. test.equal(hash1, hash2);
  157. test.done();
  158. },
  159. "roundsOOB": function(test) {
  160. var salt1 = bcrypt.genSaltSync(0), // $10$ like not set
  161. salt2 = binding.genSaltSync(0);
  162. test.strictEqual(salt1.substring(0, 7), "$2a$10$");
  163. test.strictEqual(salt2.substring(0, 7), "$2a$10$");
  164. salt1 = bcrypt.genSaltSync(3); // $04$ is lower cap
  165. salt2 = bcrypt.genSaltSync(3);
  166. test.strictEqual(salt1.substring(0, 7), "$2a$04$");
  167. test.strictEqual(salt2.substring(0, 7), "$2a$04$");
  168. salt1 = bcrypt.genSaltSync(32); // $31$ is upper cap
  169. salt2 = bcrypt.genSaltSync(32);
  170. test.strictEqual(salt1.substring(0, 7), "$2a$31$");
  171. test.strictEqual(salt2.substring(0, 7), "$2a$31$");
  172. test.done();
  173. }
  174. }
  175. };