1 | var bcrypt = require('../bcrypt');
|
2 |
|
3 | var EXPECTED = 2500;
|
4 |
|
5 | module.exports = {
|
6 | test_salt_length: function(assert) {
|
7 | assert.expect(EXPECTED);
|
8 | var n = 0;
|
9 | for (var i = 0; i < EXPECTED; i++) {
|
10 | bcrypt.genSalt(10, function(err, salt) {
|
11 | assert.equals(29, salt.length, "Salt ("+salt+") isn't the correct length. It is: " + salt.length);
|
12 | n++;
|
13 | });
|
14 | }
|
15 |
|
16 | function checkVal() {
|
17 | if (n == EXPECTED) {
|
18 | assert.done();
|
19 | } else {
|
20 | setTimeout(checkVal, 100);
|
21 | }
|
22 | }
|
23 | setTimeout(checkVal, 100);
|
24 | },
|
25 | test_hash_length: function(assert) {
|
26 | assert.expect(EXPECTED);
|
27 | var SALT = '$2a$04$TnjywYklQbbZjdjBgBoA4e';
|
28 | var n = 0;
|
29 | for (var i = 0; i < EXPECTED; i++) {
|
30 | bcrypt.hash('test', SALT, function(err, crypted) {
|
31 | assert.equals(60, crypted.length, "Encrypted ("+crypted+") isn't the correct length. It is: " + crypted.length);
|
32 | n++;
|
33 | });
|
34 | }
|
35 |
|
36 | function checkVal() {
|
37 | if (n == EXPECTED) {
|
38 | assert.done();
|
39 | } else {
|
40 | setTimeout(checkVal, 100);
|
41 | }
|
42 | }
|
43 | setTimeout(checkVal, 100);
|
44 | },
|
45 | test_compare: function(assert) {
|
46 | assert.expect(EXPECTED);
|
47 | var HASH = '$2a$04$TnjywYklQbbZjdjBgBoA4e9G7RJt9blgMgsCvUvus4Iv4TENB5nHy';
|
48 | var n = 0;
|
49 | for (var i = 0; i < EXPECTED; i++) {
|
50 | bcrypt.compare('test', HASH, function(err, match) {
|
51 | assert.equal(true, match, "No match.");
|
52 | n++;
|
53 | });
|
54 | }
|
55 |
|
56 | function checkVal() {
|
57 | if (n == EXPECTED) {
|
58 | assert.done();
|
59 | } else {
|
60 | setTimeout(checkVal, 100);
|
61 | }
|
62 | }
|
63 | setTimeout(checkVal, 100);
|
64 | },
|
65 | test_hash_and_compare: function(assert) {
|
66 | assert.expect((EXPECTED-1)*3);
|
67 | var salt = bcrypt.genSaltSync(4),
|
68 | idx = 0,
|
69 | good_done = false,
|
70 | bad_done = false;
|
71 |
|
72 | function next() {
|
73 | return test('secret' + Math.random());
|
74 | }
|
75 |
|
76 | function test(password) {
|
77 | idx += 1;
|
78 | return bcrypt.hash(password, salt, function(err, hash) {
|
79 | if (err) throw err;
|
80 |
|
81 |
|
82 | assert.ok(hash);
|
83 |
|
84 | bcrypt.compare(password, hash, function(err, res) {
|
85 |
|
86 | assert.ok(res);
|
87 | if (idx >= (EXPECTED-1)) {
|
88 | good_done = true;
|
89 | }
|
90 | });
|
91 |
|
92 | bcrypt.compare('bad' + password, hash, function(err, res) {
|
93 |
|
94 | assert.ok(!res);
|
95 | if (idx >= (EXPECTED-1)) {
|
96 | bad_done = true;
|
97 | }
|
98 | });
|
99 |
|
100 | if (idx < ((EXPECTED)-1)) {
|
101 | next();
|
102 | } else {
|
103 | function checkDone() {
|
104 | if (idx >= (EXPECTED-1) && good_done && bad_done) {
|
105 | assert.done();
|
106 | } else {
|
107 | setTimeout(checkDone, 100);
|
108 | }
|
109 | }
|
110 |
|
111 | setTimeout(checkDone, 100);
|
112 | }
|
113 | });
|
114 | }
|
115 |
|
116 | next();
|
117 | }
|
118 | };
|