|
1 | 1 | import test from 'ava'; |
2 | 2 | import cryptoRandomString from '.'; |
3 | 3 |
|
| 4 | +// Probailistic, result is always less than or equal to actual set size, chance it is less is below 1e-256 for sizes up to 32656 |
| 5 | +const generatedCharacterSetSize = (options, targetSize) => { |
| 6 | + const set = new Set(); |
| 7 | + const length = targetSize * 640; |
| 8 | + const string = cryptoRandomString({...options, length}); |
| 9 | + |
| 10 | + for (let i = 0; i < length; i++) { |
| 11 | + set.add(string[i]); |
| 12 | + } |
| 13 | + |
| 14 | + return set.size; |
| 15 | +}; |
| 16 | + |
4 | 17 | test('main', t => { |
5 | 18 | t.is(cryptoRandomString({length: 0}).length, 0); |
6 | 19 | t.is(cryptoRandomString({length: 10}).length, 10); |
7 | 20 | t.is(cryptoRandomString({length: 100}).length, 100); |
8 | 21 | t.regex(cryptoRandomString({length: 100}), /^[a-f\d]*$/); // Sanity check, probabilistic |
| 22 | + t.is(generatedCharacterSetSize({}, 16), 16); |
9 | 23 | }); |
10 | 24 |
|
11 | 25 | test('hex', t => { |
12 | 26 | t.is(cryptoRandomString({length: 0, type: 'hex'}).length, 0); |
13 | 27 | t.is(cryptoRandomString({length: 10, type: 'hex'}).length, 10); |
14 | 28 | t.is(cryptoRandomString({length: 100, type: 'hex'}).length, 100); |
15 | 29 | t.regex(cryptoRandomString({length: 100, type: 'hex'}), /^[a-f\d]*$/); // Sanity check, probabilistic |
| 30 | + t.is(generatedCharacterSetSize({type: 'hex'}, 16), 16); |
16 | 31 | }); |
17 | 32 |
|
18 | 33 | test('base64', t => { |
19 | 34 | t.is(cryptoRandomString({length: 0, type: 'base64'}).length, 0); |
20 | 35 | t.is(cryptoRandomString({length: 10, type: 'base64'}).length, 10); |
21 | 36 | t.is(cryptoRandomString({length: 100, type: 'base64'}).length, 100); |
22 | 37 | t.regex(cryptoRandomString({length: 100, type: 'base64'}), /^[a-zA-Z\d/+]*$/); // Sanity check, probabilistic |
| 38 | + t.is(generatedCharacterSetSize({type: 'base64'}, 64), 64); |
23 | 39 | }); |
24 | 40 |
|
25 | 41 | test('url-safe', t => { |
26 | 42 | t.is(cryptoRandomString({length: 0, type: 'url-safe'}).length, 0); |
27 | 43 | t.is(cryptoRandomString({length: 10, type: 'url-safe'}).length, 10); |
28 | 44 | t.is(cryptoRandomString({length: 100, type: 'url-safe'}).length, 100); |
29 | 45 | t.regex(cryptoRandomString({length: 100, type: 'url-safe'}), /^[a-zA-Z\d._~-]*$/); // Sanity check, probabilistic |
| 46 | + t.is(generatedCharacterSetSize({type: 'url-safe'}, 66), 66); |
30 | 47 | }); |
31 | 48 |
|
32 | 49 | test('characters', t => { |
33 | 50 | t.is(cryptoRandomString({length: 0, characters: '1234'}).length, 0); |
34 | 51 | t.is(cryptoRandomString({length: 10, characters: '1234'}).length, 10); |
35 | 52 | t.is(cryptoRandomString({length: 100, characters: '1234'}).length, 100); |
36 | 53 | t.regex(cryptoRandomString({length: 100, characters: '1234'}), /^[1-4]*$/); // Sanity check, probabilistic |
| 54 | + t.is(generatedCharacterSetSize({characters: '1234'}, 4), 4); |
| 55 | + t.is(generatedCharacterSetSize({characters: '0123456789'}, 10), 10); |
37 | 56 | }); |
38 | 57 |
|
39 | 58 | test('argument errors', t => { |
|
0 commit comments