Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
// Load modules
2
3
var Lab = require('lab');
4
var Cryptiles = require('../lib');
5
6
7
// Declare internals
8
9
var internals = {};
10
11
12
// Test shortcuts
13
14
var lab = exports.lab = Lab.script();
15
var before = lab.before;
16
var after = lab.after;
17
var describe = lab.experiment;
18
var it = lab.test;
19
var expect = Lab.expect;
20
21
22
describe('Cryptiles', function () {
23
24
describe('#randomString', function () {
25
26
it('should generate the right length string', function (done) {
27
28
for (var i = 1; i <= 1000; ++i) {
29
expect(Cryptiles.randomString(i).length).to.equal(i);
30
}
31
32
done();
33
});
34
35
it('returns an error on invalid bits size', function (done) {
36
37
expect(Cryptiles.randomString(99999999999999999999).message).to.equal('Failed generating random bits: Argument #1 must be number > 0');
38
done();
39
});
40
});
41
42
describe('#randomBits', function () {
43
44
it('returns an error on invalid input', function (done) {
45
46
expect(Cryptiles.randomBits(0).message).to.equal('Invalid random bits count');
47
done();
48
});
49
});
50
51
describe('#fixedTimeComparison', function () {
52
53
var a = Cryptiles.randomString(50000);
54
var b = Cryptiles.randomString(150000);
55
56
it('should take the same amount of time comparing different string sizes', function (done) {
57
58
var now = Date.now();
59
Cryptiles.fixedTimeComparison(b, a);
60
var t1 = Date.now() - now;
61
62
now = Date.now();
63
Cryptiles.fixedTimeComparison(b, b);
64
var t2 = Date.now() - now;
65
66
expect(t2 - t1).to.be.within(-20, 20);
67
done();
68
});
69
70
it('should return true for equal strings', function (done) {
71
72
expect(Cryptiles.fixedTimeComparison(a, a)).to.equal(true);
73
done();
74
});
75
76
it('should return false for different strings (size, a < b)', function (done) {
77
78
expect(Cryptiles.fixedTimeComparison(a, a + 'x')).to.equal(false);
79
done();
80
});
81
82
it('should return false for different strings (size, a > b)', function (done) {
83
84
expect(Cryptiles.fixedTimeComparison(a + 'x', a)).to.equal(false);
85
done();
86
});
87
88
it('should return false for different strings (size, a = b)', function (done) {
89
90
expect(Cryptiles.fixedTimeComparison(a + 'x', a + 'y')).to.equal(false);
91
done();
92
});
93
94
it('should return false when not a string', function (done) {
95
96
expect(Cryptiles.fixedTimeComparison('x', null)).to.equal(false);
97
done();
98
});
99
100
it('should return false when not a string (left)', function (done) {
101
102
expect(Cryptiles.fixedTimeComparison(null, 'x')).to.equal(false);
103
done();
104
});
105
});
106
});
107
108
109
110