Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index.js « test « cryptiles « node_modules - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3340141a2c60eda69397a4ebb136d776167b42ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Load modules

var Lab = require('lab');
var Cryptiles = require('../lib');


// Declare internals

var internals = {};


// Test shortcuts

var lab = exports.lab = Lab.script();
var before = lab.before;
var after = lab.after;
var describe = lab.experiment;
var it = lab.test;
var expect = Lab.expect;


describe('Cryptiles', function () {

    describe('#randomString', function () {

        it('should generate the right length string', function (done) {

            for (var i = 1; i <= 1000; ++i) {
                expect(Cryptiles.randomString(i).length).to.equal(i);
            }

            done();
        });

        it('returns an error on invalid bits size', function (done) {

            expect(Cryptiles.randomString(99999999999999999999).message).to.equal('Failed generating random bits: Argument #1 must be number > 0');
            done();
        });
    });

    describe('#randomBits', function () {

        it('returns an error on invalid input', function (done) {

            expect(Cryptiles.randomBits(0).message).to.equal('Invalid random bits count');
            done();
        });
    });

    describe('#fixedTimeComparison', function () {

        var a = Cryptiles.randomString(50000);
        var b = Cryptiles.randomString(150000);

        it('should take the same amount of time comparing different string sizes', function (done) {

            var now = Date.now();
            Cryptiles.fixedTimeComparison(b, a);
            var t1 = Date.now() - now;

            now = Date.now();
            Cryptiles.fixedTimeComparison(b, b);
            var t2 = Date.now() - now;

            expect(t2 - t1).to.be.within(-20, 20);
            done();
        });

        it('should return true for equal strings', function (done) {

            expect(Cryptiles.fixedTimeComparison(a, a)).to.equal(true);
            done();
        });

        it('should return false for different strings (size, a < b)', function (done) {

            expect(Cryptiles.fixedTimeComparison(a, a + 'x')).to.equal(false);
            done();
        });

        it('should return false for different strings (size, a > b)', function (done) {

            expect(Cryptiles.fixedTimeComparison(a + 'x', a)).to.equal(false);
            done();
        });

        it('should return false for different strings (size, a = b)', function (done) {

            expect(Cryptiles.fixedTimeComparison(a + 'x', a + 'y')).to.equal(false);
            done();
        });

        it('should return false when not a string', function (done) {

            expect(Cryptiles.fixedTimeComparison('x', null)).to.equal(false);
            done();
        });

        it('should return false when not a string (left)', function (done) {

            expect(Cryptiles.fixedTimeComparison(null, 'x')).to.equal(false);
            done();
        });
    });
});