-
Notifications
You must be signed in to change notification settings - Fork 24
/
test.js
250 lines (198 loc) · 7.09 KB
/
test.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import test from 'ava';
import m from '.';
const rot = function(n) {
const obj = {
hash: password => {
const hash = password.replace(/([A-M])|([N-Z])/gi, (m, p1) =>
String.fromCharCode(m.charCodeAt(0) + (p1 ? n : -n))
);
return Promise.resolve(`$rot${n}$${hash}`);
},
verify: async (hashstr, password) =>
Promise.resolve(hashstr === (await obj.hash(password))),
identifiers: () => [`rot${n}`]
};
return obj;
};
test.serial('should install and uninstall a valid algorithm', t => {
t.deepEqual(m.list(), []);
t.notThrows(() => m.install('rot13', rot(13)));
t.deepEqual(m.list(), ['rot13']);
t.notThrows(() => m.uninstall('rot13'));
t.deepEqual(m.list(), []);
});
test.serial('should install and uninstall multiple algorithms', t => {
t.deepEqual(m.list(), []);
t.notThrows(() => m.install('rot13', rot(13)));
t.notThrows(() => m.install('rot5', rot(5)));
t.deepEqual(m.list(), ['rot13', 'rot5']);
t.notThrows(() => m.uninstall('rot13'));
t.deepEqual(m.list(), ['rot5']);
t.notThrows(() => m.uninstall('rot5'));
t.deepEqual(m.list(), []);
});
test.serial(
'should throw an error if while installing multiple algorithms there is a clash with the identifiers',
t => {
t.deepEqual(m.list(), []);
t.notThrows(() => m.install('rot13', rot(13)));
const err = t.throws(() => m.install('rot5', rot(13)));
t.is(
err.message,
'The identifiers property of the algorithm object clashes with the ones of another algorithm.'
);
t.notThrows(() => m.uninstall('rot13'));
t.deepEqual(m.list(), []);
}
);
test('should trow an error passing an invalid algorithm name to install', t => {
let err;
err = t.throws(() => m.install('', rot(13)));
t.is(err.message, 'The algorithm name must be an non-empty string.');
err = t.throws(() => m.install(undefined, rot(13)));
t.is(err.message, 'The algorithm name must be an non-empty string.');
err = t.throws(() => m.install(null, rot(13)));
t.is(err.message, 'The algorithm name must be an non-empty string.');
});
test('should throw an error if an invalid algorithm object is given to install', async t => {
let err;
err = await t.throws(() => m.install('a', undefined));
t.is(err.message, 'The algorithm object must be an object.');
err = await t.throws(() => m.install('a', null));
t.is(err.message, 'The algorithm object must be an object.');
err = await t.throws(() => m.install('a', []));
t.is(err.message, 'The algorithm object must be an object.');
err = await t.throws(() =>
m.install('a', {verify: () => false, identifiers: () => []})
);
t.is(
err.message,
'The hash property of the algorithm object should be a function.'
);
err = await t.throws(() =>
m.install('a', {hash: () => '', identifiers: () => []})
);
t.is(
err.message,
'The verify property of the algorithm object should be a function.'
);
err = await t.throws(() =>
m.install('a', {hash: () => '', verify: () => false})
);
t.is(
err.message,
'The identifiers property of the algorithm object should be a function.'
);
});
test.serial(
'should thow an error trying to install the same algorithms more than once',
t => {
t.deepEqual(m.list(), []);
t.notThrows(() => m.install('rot13', rot(13)));
const err = t.throws(() => m.install('rot13', rot(13)));
t.regex(err.message, /algorithm is already installed/);
m.uninstall('rot13');
t.deepEqual(m.list(), []);
}
);
test('should trow an error passing an invalid algorithm name to uninstall', t => {
let err;
err = t.throws(() => m.uninstall(''));
t.is(err.message, 'The algorithm name must be an non-empty string.');
err = t.throws(() => m.uninstall(undefined));
t.is(err.message, 'The algorithm name must be an non-empty string.');
err = t.throws(() => m.uninstall(null));
t.is(err.message, 'The algorithm name must be an non-empty string.');
});
test.serial(
'should thow an error trying to uninstall the same algorithms more than once',
t => {
t.deepEqual(m.list(), []);
t.notThrows(() => m.install('rot13', rot(13)));
t.notThrows(() => m.uninstall('rot13'));
t.deepEqual(m.list(), []);
const err = t.throws(() => m.uninstall('rot13'));
t.regex(err.message, /algorithm is not installed/);
}
);
test('should trow an error passing an invalid algorithm name to use', t => {
let err;
err = t.throws(() => m.use(''));
t.is(err.message, 'The algorithm name must be an non-empty string.');
err = t.throws(() => m.use(null));
t.is(err.message, 'The algorithm name must be an non-empty string.');
});
test.serial(
'should thow an error trying to use an algorithms that is not installed',
t => {
t.deepEqual(m.list(), []);
const err = t.throws(() => m.use('rot13'));
t.regex(err.message, /algorithm is not installed/);
}
);
test.serial(
'should thow an error trying to verify without an algorithm installed',
async t => {
t.deepEqual(m.list(), []);
const err = await t.throws(() => m.verify('$rot13$cnffjbeq', 'password'));
t.is(err.message, 'No algorithm installed.');
}
);
test.serial(
'should thow an error trying to hash without an algorithm installed',
async t => {
t.deepEqual(m.list(), []);
const err = await t.throws(() => m.hash('password'));
t.is(err.message, 'No algorithm installed.');
}
);
test.serial('should verify a correct password', async t => {
t.deepEqual(m.list(), []);
m.install('rot13', rot(13));
const pass = 'password';
const hashstr1 = await m.use('rot13').hash(pass);
t.true(typeof hashstr1 === 'string');
t.true(await m.use('rot13').verify(hashstr1, pass));
const hashstr2 = await m.hash(pass);
t.true(typeof hashstr2 === 'string');
t.true(await m.verify(hashstr2, pass));
m.uninstall('rot13');
t.deepEqual(m.list(), []);
});
test.serial('should not verify a wrong password', async t => {
t.deepEqual(m.list(), []);
m.install('rot13', rot(13));
const pass = 'password';
const wrong = 'Password';
const hashstr1 = await m.use('rot13').hash(pass);
t.true(typeof hashstr1 === 'string');
t.false(await m.use('rot13').verify(hashstr1, wrong));
const hashstr2 = await m.hash(pass);
t.true(typeof hashstr2 === 'string');
t.false(await m.verify(hashstr2, wrong));
m.uninstall('rot13');
t.deepEqual(m.list(), []);
});
test.serial(
'should throw an error verifying a wrong formatted hash',
async t => {
t.deepEqual(m.list(), []);
m.install('rot13', rot(13));
let err;
const pass = 'password';
const hashstr = 'rot13$cnffjbeq';
err = await t.throws(() => m.verify(hashstr, pass));
t.is(
err.message,
'The hashstr param provided is not in a supported format.'
);
err = await t.throws(() => m.verify(undefined, pass));
t.is(err.message, 'The hashstr param must be an non-empty string.');
err = await t.throws(() => m.verify(null, pass));
t.is(err.message, 'The hashstr param must be an non-empty string.');
err = await t.throws(() => m.verify('', pass));
t.is(err.message, 'The hashstr param must be an non-empty string.');
m.uninstall('rot13');
t.deepEqual(m.list(), []);
}
);