-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
test.js
136 lines (116 loc) · 6.27 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
import {webcrypto} from 'node:crypto';
import {hasProperty, setProperty} from 'dot-prop';
import test from 'ava';
import browserCryptoRandomString, {cryptoRandomStringAsync as browserCryptoRandomStringAsync} from './browser.js';
import nodeCryptoRandomString, {cryptoRandomStringAsync as nodeCryptoRandomStringAsync} from './index.js';
if (!hasProperty(globalThis, 'crypto')) {
setProperty(globalThis, 'crypto', webcrypto);
}
// Probabilistic, result is always less than or equal to actual set size, chance it is less is below 1e-256 for sizes up to 32656.
const generatedCharacterSetSize = (cryptoRandomString, options, targetSize) => {
const set = new Set();
const length = targetSize * 640;
const string = cryptoRandomString({...options, length});
for (let index = 0; index < length; index++) {
set.add(string[index]);
}
return set.size;
};
function runTest(title, macro) {
test(`Node.js: ${title}`, macro, {
cryptoRandomString: nodeCryptoRandomString,
cryptoRandomStringAsync: nodeCryptoRandomStringAsync,
});
test(`Browser: ${title}`, macro, {
cryptoRandomString: browserCryptoRandomString,
cryptoRandomStringAsync: browserCryptoRandomStringAsync,
});
}
runTest('main', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0}).length, 0);
t.is(cryptoRandomString({length: 10}).length, 10);
t.is(cryptoRandomString({length: 100}).length, 100);
t.regex(cryptoRandomString({length: 100}), /^[a-f\d]*$/); // Sanity check, probabilistic
t.is(generatedCharacterSetSize(cryptoRandomString, {}, 16), 16);
}));
runTest('async', test.macro(async (t, {cryptoRandomStringAsync}) => {
/* eslint-disable unicorn/no-await-expression-member */
t.is((await cryptoRandomStringAsync({length: 0})).length, 0);
t.is((await cryptoRandomStringAsync({length: 10})).length, 10);
t.is((await cryptoRandomStringAsync({length: 100})).length, 100);
/* eslint-enable unicorn/no-await-expression-member */
t.regex(await cryptoRandomStringAsync({length: 100}), /^[a-f\d]*$/); // Sanity check, probabilistic
}));
runTest('hex', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0, type: 'hex'}).length, 0);
t.is(cryptoRandomString({length: 10, type: 'hex'}).length, 10);
t.is(cryptoRandomString({length: 100, type: 'hex'}).length, 100);
t.regex(cryptoRandomString({length: 100, type: 'hex'}), /^[a-f\d]*$/); // Sanity check, probabilistic
t.is(generatedCharacterSetSize(cryptoRandomString, {type: 'hex'}, 16), 16);
}));
runTest('base64', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0, type: 'base64'}).length, 0);
t.is(cryptoRandomString({length: 10, type: 'base64'}).length, 10);
t.is(cryptoRandomString({length: 100, type: 'base64'}).length, 100);
t.regex(cryptoRandomString({length: 100, type: 'base64'}), /^[a-zA-Z\d/+]*$/); // Sanity check, probabilistic
t.is(generatedCharacterSetSize(cryptoRandomString, {type: 'base64'}, 64), 64);
}));
runTest('url-safe', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0, type: 'url-safe'}).length, 0);
t.is(cryptoRandomString({length: 10, type: 'url-safe'}).length, 10);
t.is(cryptoRandomString({length: 100, type: 'url-safe'}).length, 100);
t.regex(cryptoRandomString({length: 100, type: 'url-safe'}), /^[\w.~-]*$/); // Sanity check, probabilistic
t.is(generatedCharacterSetSize(cryptoRandomString, {type: 'url-safe'}, 66), 66);
}));
runTest('numeric', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0, type: 'numeric'}).length, 0);
t.is(cryptoRandomString({length: 10, type: 'numeric'}).length, 10);
t.is(cryptoRandomString({length: 100, type: 'numeric'}).length, 100);
t.regex(cryptoRandomString({length: 100, type: 'numeric'}), /^\d*$/); // Sanity check, probabilistic
t.is(generatedCharacterSetSize(cryptoRandomString, {type: 'numeric'}, 10), 10);
}));
runTest('distinguishable', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0, type: 'distinguishable'}).length, 0);
t.is(cryptoRandomString({length: 10, type: 'distinguishable'}).length, 10);
t.is(cryptoRandomString({length: 100, type: 'distinguishable'}).length, 100);
t.regex(cryptoRandomString({length: 100, type: 'distinguishable'}), /^[CDEHKMPRTUWXY012458]*$/); // Sanity check, probabilistic
t.is(generatedCharacterSetSize(cryptoRandomString, {type: 'distinguishable'}, 19), 19);
}));
runTest('ascii-printable', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0, type: 'ascii-printable'}).length, 0);
t.is(cryptoRandomString({length: 10, type: 'ascii-printable'}).length, 10);
t.is(cryptoRandomString({length: 100, type: 'ascii-printable'}).length, 100);
t.regex(cryptoRandomString({length: 100, type: 'ascii-printable'}), /^[!"#$%&'()*+,-./\w:;<=>?@[\\\]^`{|}~]*$/); // Sanity check, probabilistic
}));
runTest('alphanumeric', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0, type: 'alphanumeric'}).length, 0);
t.is(cryptoRandomString({length: 10, type: 'alphanumeric'}).length, 10);
t.is(cryptoRandomString({length: 100, type: 'alphanumeric'}).length, 100);
t.regex(cryptoRandomString({length: 100, type: 'alphanumeric'}), /^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789]*$/); // Sanity check, probabilistic
t.is(generatedCharacterSetSize(cryptoRandomString, {type: 'alphanumeric'}, 19), 62);
}));
runTest('characters', test.macro((t, {cryptoRandomString}) => {
t.is(cryptoRandomString({length: 0, characters: '1234'}).length, 0);
t.is(cryptoRandomString({length: 10, characters: '1234'}).length, 10);
t.is(cryptoRandomString({length: 100, characters: '1234'}).length, 100);
t.regex(cryptoRandomString({length: 100, characters: '1234'}), /^[1-4]*$/); // Sanity check, probabilistic
t.is(generatedCharacterSetSize(cryptoRandomString, {characters: '1234'}, 4), 4);
t.is(generatedCharacterSetSize(cryptoRandomString, {characters: '0123456789'}, 10), 10);
}));
runTest('argument errors', test.macro((t, {cryptoRandomString}) => {
t.throws(() => {
cryptoRandomString({length: Number.POSITIVE_INFINITY});
});
t.throws(() => {
cryptoRandomString({length: -1});
});
t.throws(() => {
cryptoRandomString({length: 0, type: 'hex', characters: '1234'});
});
t.throws(() => {
cryptoRandomString({length: 0, characters: 42});
});
t.throws(() => {
cryptoRandomString({length: 0, type: 'unknown'});
});
}));