react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / hawk / node_modules / cryptiles / test / index.js
81155 views// Load modules12var Lab = require('lab');3var Cryptiles = require('../lib');456// Declare internals78var internals = {};91011// Test shortcuts1213var lab = exports.lab = Lab.script();14var before = lab.before;15var after = lab.after;16var describe = lab.experiment;17var it = lab.test;18var expect = Lab.expect;192021describe('Cryptiles', function () {2223describe('#randomString', function () {2425it('should generate the right length string', function (done) {2627for (var i = 1; i <= 1000; ++i) {28expect(Cryptiles.randomString(i).length).to.equal(i);29}3031done();32});3334it('returns an error on invalid bits size', function (done) {3536expect(Cryptiles.randomString(99999999999999999999).message).to.equal('Failed generating random bits: Argument #1 must be number > 0');37done();38});39});4041describe('#randomBits', function () {4243it('returns an error on invalid input', function (done) {4445expect(Cryptiles.randomBits(0).message).to.equal('Invalid random bits count');46done();47});48});4950describe('#fixedTimeComparison', function () {5152var a = Cryptiles.randomString(50000);53var b = Cryptiles.randomString(150000);5455it('should take the same amount of time comparing different string sizes', function (done) {5657var now = Date.now();58Cryptiles.fixedTimeComparison(b, a);59var t1 = Date.now() - now;6061now = Date.now();62Cryptiles.fixedTimeComparison(b, b);63var t2 = Date.now() - now;6465expect(t2 - t1).to.be.within(-20, 20);66done();67});6869it('should return true for equal strings', function (done) {7071expect(Cryptiles.fixedTimeComparison(a, a)).to.equal(true);72done();73});7475it('should return false for different strings (size, a < b)', function (done) {7677expect(Cryptiles.fixedTimeComparison(a, a + 'x')).to.equal(false);78done();79});8081it('should return false for different strings (size, a > b)', function (done) {8283expect(Cryptiles.fixedTimeComparison(a + 'x', a)).to.equal(false);84done();85});8687it('should return false for different strings (size, a = b)', function (done) {8889expect(Cryptiles.fixedTimeComparison(a + 'x', a + 'y')).to.equal(false);90done();91});9293it('should return false when not a string', function (done) {9495expect(Cryptiles.fixedTimeComparison('x', null)).to.equal(false);96done();97});9899it('should return false when not a string (left)', function (done) {100101expect(Cryptiles.fixedTimeComparison(null, 'x')).to.equal(false);102done();103});104});105});106107108109110