UNPKG

6.92 kBJavaScriptView Raw
1var bcrypt = require('../bcrypt');
2
3module.exports = {
4 test_salt_length: function(assert) {
5 assert.expect(1);
6 bcrypt.genSalt(10, function(err, salt) {
7 assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
8 assert.done();
9 });
10 },
11 test_salt_only_cb: function(assert) {
12 assert.doesNotThrow(function() {bcrypt.genSalt(function(err, salt) {});}, "Should not throw an Error. Rounds and seed length are optional.");
13 assert.done();
14 },
15 test_salt_rounds_is_string_number: function(assert) {
16 bcrypt.genSalt('10', void 0, function (err, salt) {
17 assert.ok((err instanceof Error), "Should be an Error. genSalt requires round to be of type number.");
18 assert.done();
19 });
20 },
21 test_salt_rounds_is_string_non_number: function(assert) {
22 bcrypt.genSalt('z', function (err, salt) {
23 assert.ok((err instanceof Error), "Should throw an Error. genSalt requires rounds to of type number.");
24 assert.done();
25 });
26 },
27 test_salt_minor: function(assert) {
28 assert.expect(3);
29 bcrypt.genSalt(10, 'a', function(err, salt) {
30 assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
31 var split_salt = salt.split('$');
32 assert.strictEqual(split_salt[1], '2a');
33 assert.strictEqual(split_salt[2], '10');
34 assert.done();
35 });
36 },
37 test_salt_minor_b: function(assert) {
38 assert.expect(3);
39 bcrypt.genSalt(10, 'b', function(err, salt) {
40 assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
41 var split_salt = salt.split('$');
42 assert.strictEqual(split_salt[1], '2b');
43 assert.strictEqual(split_salt[2], '10');
44 assert.done();
45 });
46 },
47 test_hash: function(assert) {
48 assert.expect(1);
49 bcrypt.genSalt(10, function(err, salt) {
50 bcrypt.hash('password', salt, function(err, res) {
51 assert.ok(res, "Res should be defined.");
52 assert.done();
53 });
54 });
55 },
56 test_hash_rounds: function(assert) {
57 assert.expect(1);
58 bcrypt.hash('bacon', 8, function(err, hash) {
59 assert.strictEqual(bcrypt.getRounds(hash), 8, "Number of rounds should be that specified in the function call.");
60 assert.done();
61 });
62 },
63 test_hash_empty_strings: function(assert) {
64 assert.expect(2);
65 bcrypt.genSalt(10, function(err, salt) {
66 bcrypt.hash('', salt, function(err, res) {
67 assert.ok(res, "Res should be defined even with an empty pw.");
68 bcrypt.hash('', '', function(err, res) {
69 if (err) {
70 assert.ok(err);
71 } else {
72 assert.fail();
73 }
74
75 assert.done();
76 });
77 });
78 });
79 },
80 test_hash_no_params: function(assert) {
81 bcrypt.hash(function (err, hash) {
82 assert.ok(err, "Should be an error. No params.");
83 assert.done();
84 });
85 },
86 test_hash_one_param: function(assert) {
87 bcrypt.hash('password', function (err, hash) {
88 assert.ok(err, "Should be an Error. No salt.");
89 assert.done();
90 });
91 },
92 test_hash_salt_validity: function(assert) {
93 assert.expect(3);
94 bcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse', function(err, enc) {
95 assert.strictEqual(err, undefined);
96 bcrypt.hash('password', 'some$value', function(err, enc) {
97 assert.notEqual(err, undefined);
98 assert.strictEqual(err.message, "Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
99 assert.done();
100 });
101 });
102 },
103 test_verify_salt: function(assert) {
104 assert.expect(2);
105 bcrypt.genSalt(10, function(err, salt) {
106 var split_salt = salt.split('$');
107 assert.strictEqual(split_salt[1], '2b');
108 assert.strictEqual(split_salt[2], '10');
109 assert.done();
110 });
111 },
112 test_verify_salt_min_rounds: function(assert) {
113 assert.expect(2);
114 bcrypt.genSalt(1, function(err, salt) {
115 var split_salt = salt.split('$');
116 assert.strictEqual(split_salt[1], '2b');
117 assert.strictEqual(split_salt[2], '04');
118 assert.done();
119 });
120 },
121 test_verify_salt_max_rounds: function(assert) {
122 assert.expect(2);
123 bcrypt.genSalt(100, function(err, salt) {
124 var split_salt = salt.split('$');
125 assert.strictEqual(split_salt[1], '2b');
126 assert.strictEqual(split_salt[2], '31');
127 assert.done();
128 });
129 },
130 test_hash_compare: function(assert) {
131 assert.expect(3);
132 bcrypt.genSalt(10, function(err, salt) {
133 assert.strictEqual(29, salt.length, "Salt isn't the correct length.");
134 bcrypt.hash("test", salt, function(err, hash) {
135 bcrypt.compare("test", hash, function(err, res) {
136 assert.strictEqual(res, true, "These hashes should be equal.");
137 bcrypt.compare("blah", hash, function(err, res) {
138 assert.strictEqual(res, false, "These hashes should not be equal.");
139 assert.done();
140 });
141 });
142 });
143 });
144 },
145 test_hash_compare_empty_strings: function(assert) {
146 assert.expect(2);
147 var hash = bcrypt.hashSync("test", bcrypt.genSaltSync(10));
148
149 bcrypt.compare("", hash, function(err, res) {
150 assert.strictEqual(res, false, "These hashes should not be equal.");
151 bcrypt.compare("", "", function(err, res) {
152 assert.strictEqual(res, false, "These hashes should not be equal.");
153 assert.done();
154 });
155 });
156 },
157 test_hash_compare_invalid_strings: function(assert) {
158 var fullString = 'envy1362987212538';
159 var hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
160 var wut = ':';
161 bcrypt.compare(fullString, hash, function(err, res) {
162 assert.ok(res);
163 bcrypt.compare(fullString, wut, function(err, res) {
164 assert.ok(!res);
165 assert.done();
166 });
167 });
168 },
169 test_compare_no_params: function(assert) {
170 bcrypt.compare(function(err, hash) {
171 assert.ok(err, 'Should be an error. No params.');
172 assert.done();
173 });
174 },
175 test_hash_compare_one_param: function(assert) {
176 bcrypt.compare('password', function(err, hash) {
177 assert.ok(err, 'Should be an Error. No hash.');
178 assert.done();
179 });
180 }
181};