repetitions.js
2.88 KB
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
109
110
111
112
113
114
115
116
117
118
var bcrypt = require('../bcrypt');
var EXPECTED = 2500; //number of times to iterate these tests...
module.exports = {
test_salt_length: function(assert) {
assert.expect(EXPECTED);
var n = 0;
for (var i = 0; i < EXPECTED; i++) {
bcrypt.genSalt(10, function(err, salt) {
assert.equals(29, salt.length, "Salt ("+salt+") isn't the correct length. It is: " + salt.length);
n++;
});
}
function checkVal() {
if (n == EXPECTED) {
assert.done();
} else {
setTimeout(checkVal, 100);
}
}
setTimeout(checkVal, 100);
},
test_hash_length: function(assert) {
assert.expect(EXPECTED);
var SALT = '$2a$04$TnjywYklQbbZjdjBgBoA4e';
var n = 0;
for (var i = 0; i < EXPECTED; i++) {
bcrypt.hash('test', SALT, function(err, crypted) {
assert.equals(60, crypted.length, "Encrypted ("+crypted+") isn't the correct length. It is: " + crypted.length);
n++;
});
}
function checkVal() {
if (n == EXPECTED) {
assert.done();
} else {
setTimeout(checkVal, 100);
}
}
setTimeout(checkVal, 100);
},
test_compare: function(assert) {
assert.expect(EXPECTED);
var HASH = '$2a$04$TnjywYklQbbZjdjBgBoA4e9G7RJt9blgMgsCvUvus4Iv4TENB5nHy';
var n = 0;
for (var i = 0; i < EXPECTED; i++) {
bcrypt.compare('test', HASH, function(err, match) {
assert.equal(true, match, "No match.");
n++;
});
}
function checkVal() {
if (n == EXPECTED) {
assert.done();
} else {
setTimeout(checkVal, 100);
}
}
setTimeout(checkVal, 100);
},
test_hash_and_compare: function(assert) {
assert.expect((EXPECTED-1)*3);
var salt = bcrypt.genSaltSync(4),
idx = 0,
good_done = false,
bad_done = false;
function next() {
return test('secret' + Math.random());
}
function test(password) {
idx += 1;
return bcrypt.hash(password, salt, function(err, hash) {
if (err) throw err;
//console.log('\nbcrypt iter ' + idx);
assert.ok(hash);
bcrypt.compare(password, hash, function(err, res) {
//if (err) throw err;
assert.ok(res);
if (idx >= (EXPECTED-1)) {
good_done = true;
}
});
bcrypt.compare('bad' + password, hash, function(err, res) {
//if (err) throw err;
assert.ok(!res);
if (idx >= (EXPECTED-1)) {
bad_done = true;
}
});
if (idx < ((EXPECTED)-1)) {
next();
} else {
function checkDone() {
if (idx >= (EXPECTED-1) && good_done && bad_done) {
assert.done();
} else {
setTimeout(checkDone, 100);
}
}
setTimeout(checkDone, 100);
}
});
}
next();
}
};