Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / cryptiles / test / index.js
1 // Load modules
2
3 var Code = require('code');
4 var Cryptiles = require('..');
5 var Lab = require('lab');
6
7
8 // Declare internals
9
10 var internals = {};
11
12
13 // Test shortcuts
14
15 var lab = exports.lab = Lab.script();
16 var describe = lab.describe;
17 var it = lab.it;
18 var expect = Code.expect;
19
20
21 describe('randomString()', function () {
22
23     it('should generate the right length string', function (done) {
24
25         for (var i = 1; i <= 1000; ++i) {
26             expect(Cryptiles.randomString(i).length).to.equal(i);
27         }
28
29         done();
30     });
31
32     it('returns an error on invalid bits size', function (done) {
33
34         expect(Cryptiles.randomString(99999999999999999999).message).to.match(/Failed generating random bits/);
35         done();
36     });
37 });
38
39 describe('randomBits()', function () {
40
41     it('returns an error on invalid input', function (done) {
42
43         expect(Cryptiles.randomBits(0).message).to.equal('Invalid random bits count');
44         done();
45     });
46 });
47
48 describe('fixedTimeComparison()', function () {
49
50     var a = Cryptiles.randomString(50000);
51     var b = Cryptiles.randomString(150000);
52
53     it('should take the same amount of time comparing different string sizes', function (done) {
54
55         var now = Date.now();
56         Cryptiles.fixedTimeComparison(b, a);
57         var t1 = Date.now() - now;
58
59         now = Date.now();
60         Cryptiles.fixedTimeComparison(b, b);
61         var t2 = Date.now() - now;
62
63         expect(t2 - t1).to.be.within(-20, 20);
64         done();
65     });
66
67     it('should return true for equal strings', function (done) {
68
69         expect(Cryptiles.fixedTimeComparison(a, a)).to.equal(true);
70         done();
71     });
72
73     it('should return false for different strings (size, a < b)', function (done) {
74
75         expect(Cryptiles.fixedTimeComparison(a, a + 'x')).to.equal(false);
76         done();
77     });
78
79     it('should return false for different strings (size, a > b)', function (done) {
80
81         expect(Cryptiles.fixedTimeComparison(a + 'x', a)).to.equal(false);
82         done();
83     });
84
85     it('should return false for different strings (size, a = b)', function (done) {
86
87         expect(Cryptiles.fixedTimeComparison(a + 'x', a + 'y')).to.equal(false);
88         done();
89     });
90
91     it('should return false when not a string', function (done) {
92
93         expect(Cryptiles.fixedTimeComparison('x', null)).to.equal(false);
94         done();
95     });
96
97     it('should return false when not a string (left)', function (done) {
98
99         expect(Cryptiles.fixedTimeComparison(null, 'x')).to.equal(false);
100         done();
101     });
102 });