index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Load modules
  2. var Code = require('code');
  3. var Cryptiles = require('..');
  4. var Lab = require('lab');
  5. // Declare internals
  6. var internals = {};
  7. // Test shortcuts
  8. var lab = exports.lab = Lab.script();
  9. var describe = lab.describe;
  10. var it = lab.it;
  11. var expect = Code.expect;
  12. describe('randomString()', function () {
  13. it('should generate the right length string', function (done) {
  14. for (var i = 1; i <= 1000; ++i) {
  15. expect(Cryptiles.randomString(i).length).to.equal(i);
  16. }
  17. done();
  18. });
  19. it('returns an error on invalid bits size', function (done) {
  20. expect(Cryptiles.randomString(99999999999999999999).message).to.match(/Failed generating random bits/);
  21. done();
  22. });
  23. });
  24. describe('randomBits()', function () {
  25. it('returns an error on invalid input', function (done) {
  26. expect(Cryptiles.randomBits(0).message).to.equal('Invalid random bits count');
  27. done();
  28. });
  29. });
  30. describe('fixedTimeComparison()', function () {
  31. var a = Cryptiles.randomString(50000);
  32. var b = Cryptiles.randomString(150000);
  33. it('should take the same amount of time comparing different string sizes', function (done) {
  34. var now = Date.now();
  35. Cryptiles.fixedTimeComparison(b, a);
  36. var t1 = Date.now() - now;
  37. now = Date.now();
  38. Cryptiles.fixedTimeComparison(b, b);
  39. var t2 = Date.now() - now;
  40. expect(t2 - t1).to.be.within(-20, 20);
  41. done();
  42. });
  43. it('should return true for equal strings', function (done) {
  44. expect(Cryptiles.fixedTimeComparison(a, a)).to.equal(true);
  45. done();
  46. });
  47. it('should return false for different strings (size, a < b)', function (done) {
  48. expect(Cryptiles.fixedTimeComparison(a, a + 'x')).to.equal(false);
  49. done();
  50. });
  51. it('should return false for different strings (size, a > b)', function (done) {
  52. expect(Cryptiles.fixedTimeComparison(a + 'x', a)).to.equal(false);
  53. done();
  54. });
  55. it('should return false for different strings (size, a = b)', function (done) {
  56. expect(Cryptiles.fixedTimeComparison(a + 'x', a + 'y')).to.equal(false);
  57. done();
  58. });
  59. it('should return false when not a string', function (done) {
  60. expect(Cryptiles.fixedTimeComparison('x', null)).to.equal(false);
  61. done();
  62. });
  63. it('should return false when not a string (left)', function (done) {
  64. expect(Cryptiles.fixedTimeComparison(null, 'x')).to.equal(false);
  65. done();
  66. });
  67. });