3 var Code = require('code');
4 var Cryptiles = require('..');
5 var Lab = require('lab');
15 var lab = exports.lab = Lab.script();
16 var describe = lab.describe;
18 var expect = Code.expect;
21 describe('randomString()', function () {
23 it('should generate the right length string', function (done) {
25 for (var i = 1; i <= 1000; ++i) {
26 expect(Cryptiles.randomString(i).length).to.equal(i);
32 it('returns an error on invalid bits size', function (done) {
34 expect(Cryptiles.randomString(99999999999999999999).message).to.match(/Failed generating random bits/);
39 describe('randomBits()', function () {
41 it('returns an error on invalid input', function (done) {
43 expect(Cryptiles.randomBits(0).message).to.equal('Invalid random bits count');
48 describe('fixedTimeComparison()', function () {
50 var a = Cryptiles.randomString(50000);
51 var b = Cryptiles.randomString(150000);
53 it('should take the same amount of time comparing different string sizes', function (done) {
56 Cryptiles.fixedTimeComparison(b, a);
57 var t1 = Date.now() - now;
60 Cryptiles.fixedTimeComparison(b, b);
61 var t2 = Date.now() - now;
63 expect(t2 - t1).to.be.within(-20, 20);
67 it('should return true for equal strings', function (done) {
69 expect(Cryptiles.fixedTimeComparison(a, a)).to.equal(true);
73 it('should return false for different strings (size, a < b)', function (done) {
75 expect(Cryptiles.fixedTimeComparison(a, a + 'x')).to.equal(false);
79 it('should return false for different strings (size, a > b)', function (done) {
81 expect(Cryptiles.fixedTimeComparison(a + 'x', a)).to.equal(false);
85 it('should return false for different strings (size, a = b)', function (done) {
87 expect(Cryptiles.fixedTimeComparison(a + 'x', a + 'y')).to.equal(false);
91 it('should return false when not a string', function (done) {
93 expect(Cryptiles.fixedTimeComparison('x', null)).to.equal(false);
97 it('should return false when not a string (left)', function (done) {
99 expect(Cryptiles.fixedTimeComparison(null, 'x')).to.equal(false);