1 | const bcrypt = require('../bcrypt');
|
2 |
|
3 | test('salt_length', done => {
|
4 | expect.assertions(1);
|
5 | bcrypt.genSalt(10, function (err, salt) {
|
6 | expect(salt).toHaveLength(29);
|
7 | done();
|
8 | });
|
9 | })
|
10 |
|
11 | test('salt_only_cb', () => {
|
12 | expect.assertions(1);
|
13 | expect(() => {
|
14 | bcrypt.genSalt((err, salt) => {
|
15 | });
|
16 | }).not.toThrow();
|
17 | })
|
18 |
|
19 | test('salt_rounds_is_string_number', done => {
|
20 | expect.assertions(2);
|
21 | bcrypt.genSalt('10', void 0, function (err, salt) {
|
22 | expect(err instanceof Error).toBe(true)
|
23 | expect(err.message).toBe('rounds must be a number')
|
24 | done();
|
25 | });
|
26 | })
|
27 |
|
28 | test('salt_rounds_is_string_non_number', done => {
|
29 | expect.assertions(2);
|
30 | bcrypt.genSalt('z', function (err, salt) {
|
31 | expect(err instanceof Error).toBe(true)
|
32 | expect(err.message).toBe('rounds must be a number')
|
33 | done();
|
34 | });
|
35 | })
|
36 |
|
37 | test('salt_minor', done => {
|
38 | expect.assertions(3);
|
39 | bcrypt.genSalt(10, 'a', function (err, value) {
|
40 | expect(value).toHaveLength(29);
|
41 | const [_, minor, salt] = value.split('$');
|
42 | expect(minor).toEqual('2a');
|
43 | expect(salt).toEqual('10');
|
44 | done();
|
45 | });
|
46 | })
|
47 |
|
48 | test('salt_minor_b', done => {
|
49 | expect.assertions(3);
|
50 | bcrypt.genSalt(10, 'b', function (err, value) {
|
51 | expect(value).toHaveLength(29);
|
52 | const [_, minor, salt] = value.split('$');
|
53 | expect(minor).toEqual('2b');
|
54 | expect(salt).toEqual('10');
|
55 | done();
|
56 | });
|
57 | })
|
58 |
|
59 | test('hash', done => {
|
60 | expect.assertions(2);
|
61 | bcrypt.genSalt(10, function (err, salt) {
|
62 | bcrypt.hash('password', salt, function (err, res) {
|
63 | expect(res).toBeDefined();
|
64 | expect(err).toBeUndefined();
|
65 | done();
|
66 | });
|
67 | });
|
68 | })
|
69 |
|
70 | test('hash_rounds', done => {
|
71 | expect.assertions(1);
|
72 | bcrypt.hash('bacon', 8, function (err, hash) {
|
73 | expect(bcrypt.getRounds(hash)).toEqual(8);
|
74 | done();
|
75 | });
|
76 | })
|
77 |
|
78 | test('hash_empty_strings', done => {
|
79 | expect.assertions(1);
|
80 | bcrypt.genSalt(10, function (err, salt) {
|
81 | bcrypt.hash('', salt, function (err, res) {
|
82 | expect(res).toBeDefined();
|
83 | done();
|
84 | });
|
85 | });
|
86 | })
|
87 |
|
88 | test('hash_fails_with_empty_salt', done => {
|
89 | expect.assertions(1);
|
90 | bcrypt.hash('', '', function (err, res) {
|
91 | expect(err.message).toBe('Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue')
|
92 | done();
|
93 | });
|
94 | })
|
95 |
|
96 | test('hash_no_params', done => {
|
97 | expect.assertions(1);
|
98 | bcrypt.hash(function (err, hash) {
|
99 | expect(err.message).toBe('data must be a string or Buffer and salt must either be a salt string or a number of rounds')
|
100 | done();
|
101 | });
|
102 | })
|
103 |
|
104 | test('hash_one_param', done => {
|
105 | expect.assertions(1);
|
106 | bcrypt.hash('password', function (err, hash) {
|
107 | expect(err.message).toBe('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
|
108 | done();
|
109 | });
|
110 | })
|
111 |
|
112 | test('hash_salt_validity', done => {
|
113 | expect.assertions(2);
|
114 | bcrypt.hash('password', '$2a$10$somesaltyvaluertsetrse', function (err, enc) {
|
115 | expect(err).toBeUndefined();
|
116 | bcrypt.hash('password', 'some$value', function (err, enc) {
|
117 | expect(err.message).toBe("Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue");
|
118 | done();
|
119 | });
|
120 | });
|
121 | })
|
122 |
|
123 | test('verify_salt', done => {
|
124 | expect.assertions(2);
|
125 | bcrypt.genSalt(10, function (err, value) {
|
126 | const [_, version, rounds] = value.split('$');
|
127 | expect(version).toEqual('2b');
|
128 | expect(rounds).toEqual('10');
|
129 | done();
|
130 | });
|
131 | })
|
132 |
|
133 | test('verify_salt_min_rounds', done => {
|
134 | expect.assertions(2);
|
135 | bcrypt.genSalt(1, function (err, value) {
|
136 | const [_, version, rounds] = value.split('$');
|
137 | expect(version).toEqual('2b');
|
138 | expect(rounds).toEqual('04');
|
139 | done();
|
140 | });
|
141 | })
|
142 |
|
143 | test('verify_salt_max_rounds', done => {
|
144 | expect.assertions(2);
|
145 | bcrypt.genSalt(100, function (err, value) {
|
146 | const [_, version, rounds] = value.split('$');
|
147 | expect(version).toEqual('2b');
|
148 | expect(rounds).toEqual('31');
|
149 | done();
|
150 | });
|
151 | })
|
152 |
|
153 | test('hash_compare', done => {
|
154 | expect.assertions(2);
|
155 | bcrypt.genSalt(10, function (err, salt) {
|
156 | bcrypt.hash("test", salt, function (err, hash) {
|
157 | bcrypt.compare("test", hash, function (err, res) {
|
158 | expect(hash).toBeDefined();
|
159 | bcrypt.compare("blah", hash, function (err, res) {
|
160 | expect(res).toBe(false);
|
161 | done();
|
162 | });
|
163 | });
|
164 | });
|
165 | });
|
166 | })
|
167 |
|
168 | test('hash_compare_empty_strings', done => {
|
169 | expect.assertions(2);
|
170 | const hash = bcrypt.hashSync("test", bcrypt.genSaltSync(10));
|
171 |
|
172 | bcrypt.compare("", hash, function (err, res) {
|
173 | expect(res).toEqual(false)
|
174 | bcrypt.compare("", "", function (err, res) {
|
175 | expect(res).toEqual(false);
|
176 | done();
|
177 | });
|
178 | });
|
179 | })
|
180 |
|
181 | test('hash_compare_invalid_strings', done => {
|
182 | expect.assertions(2);
|
183 | const fullString = 'envy1362987212538';
|
184 | const hash = '$2a$10$XOPbrlUPQdwdJUpSrIF6X.LbE14qsMmKGhM1A8W9iqaG3vv1BD7WC';
|
185 | const wut = ':';
|
186 | bcrypt.compare(fullString, hash, function (err, res) {
|
187 | expect(res).toBe(true);
|
188 | bcrypt.compare(fullString, wut, function (err, res) {
|
189 | expect(res).toBe(false);
|
190 | done();
|
191 | });
|
192 | });
|
193 | })
|
194 |
|
195 | test('compare_no_params', done => {
|
196 | expect.assertions(1);
|
197 | bcrypt.compare(function (err, hash) {
|
198 | expect(err.message).toBe('data and hash arguments required');
|
199 | done();
|
200 | });
|
201 | })
|
202 |
|
203 | test('hash_compare_one_param', done => {
|
204 | expect.assertions(1);
|
205 | bcrypt.compare('password', function (err, hash) {
|
206 | expect(err.message).toBe('data and hash arguments required');
|
207 | done();
|
208 | });
|
209 | })
|