|
| 1 | +import { assertEquals, assertMatch, assertThrows } from "./test_deps.ts"; |
| 2 | +import { cryptoRandomString, cryptoRandomStringAsync } from "./mod.ts"; |
| 3 | + |
| 4 | +// 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 |
| 5 | +const generatedCharacterSetSize = ( |
| 6 | + options: { type?: string; characters?: string }, |
| 7 | + targetSize: number, |
| 8 | +) => { |
| 9 | + const set = new Set(); |
| 10 | + const length = targetSize * 640; |
| 11 | + const string = cryptoRandomString({ ...options, length }); |
| 12 | + |
| 13 | + for (let i = 0; i < length; i++) { |
| 14 | + set.add(string[i]); |
| 15 | + } |
| 16 | + |
| 17 | + return set.size; |
| 18 | +}; |
| 19 | + |
| 20 | +Deno.test("main", () => { |
| 21 | + assertEquals(cryptoRandomString({ length: 0 }).length, 0); |
| 22 | + assertEquals(cryptoRandomString({ length: 10 }).length, 10); |
| 23 | + assertEquals(cryptoRandomString({ length: 100 }).length, 100); |
| 24 | + assertMatch(cryptoRandomString({ length: 100 }), /^[a-f\d]*$/); // Sanity check, probabilistic |
| 25 | + assertEquals(generatedCharacterSetSize({}, 16), 16); |
| 26 | +}); |
| 27 | + |
| 28 | +Deno.test("async", async () => { |
| 29 | + assertEquals((await cryptoRandomStringAsync({ length: 0 })).length, 0); |
| 30 | + assertEquals((await cryptoRandomStringAsync({ length: 10 })).length, 10); |
| 31 | + assertEquals((await cryptoRandomStringAsync({ length: 100 })).length, 100); |
| 32 | + assertMatch(await cryptoRandomStringAsync({ length: 100 }), /^[a-f\d]*$/); |
| 33 | +}); |
| 34 | + |
| 35 | +Deno.test("hex", () => { |
| 36 | + assertEquals(cryptoRandomString({ length: 0, type: "hex" }).length, 0); |
| 37 | + assertEquals(cryptoRandomString({ length: 10, type: "hex" }).length, 10); |
| 38 | + assertEquals(cryptoRandomString({ length: 100, type: "hex" }).length, 100); |
| 39 | + assertMatch(cryptoRandomString({ length: 100, type: "hex" }), /^[a-f\d]*$/); // Sanity check, probabilistic |
| 40 | + assertEquals(generatedCharacterSetSize({ type: "hex" }, 16), 16); |
| 41 | +}); |
| 42 | + |
| 43 | +Deno.test("base64", () => { |
| 44 | + assertEquals(cryptoRandomString({ length: 0, type: "base64" }).length, 0); |
| 45 | + assertEquals(cryptoRandomString({ length: 10, type: "base64" }).length, 10); |
| 46 | + assertEquals(cryptoRandomString({ length: 100, type: "base64" }).length, 100); |
| 47 | + assertMatch( |
| 48 | + cryptoRandomString({ length: 100, type: "base64" }), |
| 49 | + /^[a-zA-Z\d/+]*$/, |
| 50 | + ); // Sanity check, probabilistic |
| 51 | + assertEquals(generatedCharacterSetSize({ type: "base64" }, 64), 64); |
| 52 | +}); |
| 53 | + |
| 54 | +Deno.test("url-safe", () => { |
| 55 | + assertEquals(cryptoRandomString({ length: 0, type: "url-safe" }).length, 0); |
| 56 | + assertEquals(cryptoRandomString({ length: 10, type: "url-safe" }).length, 10); |
| 57 | + assertEquals( |
| 58 | + cryptoRandomString({ length: 100, type: "url-safe" }).length, |
| 59 | + 100, |
| 60 | + ); |
| 61 | + assertMatch( |
| 62 | + cryptoRandomString({ length: 100, type: "url-safe" }), |
| 63 | + /^[a-zA-Z\d._~-]*$/, |
| 64 | + ); // Sanity check, probabilistic |
| 65 | + assertEquals(generatedCharacterSetSize({ type: "url-safe" }, 66), 66); |
| 66 | +}); |
| 67 | + |
| 68 | +Deno.test("numeric", () => { |
| 69 | + assertEquals(cryptoRandomString({ length: 0, type: "numeric" }).length, 0); |
| 70 | + assertEquals(cryptoRandomString({ length: 10, type: "numeric" }).length, 10); |
| 71 | + assertEquals( |
| 72 | + cryptoRandomString({ length: 100, type: "numeric" }).length, |
| 73 | + 100, |
| 74 | + ); |
| 75 | + assertMatch(cryptoRandomString({ length: 100, type: "numeric" }), /^[\d]*$/); // Sanity check, probabilistic |
| 76 | + assertEquals(generatedCharacterSetSize({ type: "numeric" }, 10), 10); |
| 77 | +}); |
| 78 | + |
| 79 | +Deno.test("distinguishable", () => { |
| 80 | + assertEquals( |
| 81 | + cryptoRandomString({ length: 0, type: "distinguishable" }).length, |
| 82 | + 0, |
| 83 | + ); |
| 84 | + assertEquals( |
| 85 | + cryptoRandomString({ length: 10, type: "distinguishable" }).length, |
| 86 | + 10, |
| 87 | + ); |
| 88 | + assertEquals( |
| 89 | + cryptoRandomString({ length: 100, type: "distinguishable" }).length, |
| 90 | + 100, |
| 91 | + ); |
| 92 | + assertMatch( |
| 93 | + cryptoRandomString({ length: 100, type: "distinguishable" }), |
| 94 | + /^[CDEHKMPRTUWXY012458]*$/, |
| 95 | + ); // Sanity check, probabilistic |
| 96 | + assertEquals(generatedCharacterSetSize({ type: "distinguishable" }, 19), 19); |
| 97 | +}); |
| 98 | + |
| 99 | +Deno.test("ascii-printable", () => { |
| 100 | + assertEquals( |
| 101 | + cryptoRandomString({ length: 0, type: "ascii-printable" }).length, |
| 102 | + 0, |
| 103 | + ); |
| 104 | + assertEquals( |
| 105 | + cryptoRandomString({ length: 10, type: "ascii-printable" }).length, |
| 106 | + 10, |
| 107 | + ); |
| 108 | + assertEquals( |
| 109 | + cryptoRandomString({ length: 100, type: "ascii-printable" }).length, |
| 110 | + 100, |
| 111 | + ); |
| 112 | + assertMatch( |
| 113 | + cryptoRandomString({ length: 100, type: "ascii-printable" }), |
| 114 | + /^[!"#$%&'()*+,-./\d:;<=>?@A-Z[\\\]^_`a-z{|}~]*$/, |
| 115 | + ); // Sanity check, probabilistic |
| 116 | +}); |
| 117 | + |
| 118 | +Deno.test("alphanumeric", () => { |
| 119 | + assertEquals( |
| 120 | + cryptoRandomString({ length: 0, type: "alphanumeric" }).length, |
| 121 | + 0, |
| 122 | + ); |
| 123 | + assertEquals( |
| 124 | + cryptoRandomString({ length: 10, type: "alphanumeric" }).length, |
| 125 | + 10, |
| 126 | + ); |
| 127 | + assertEquals( |
| 128 | + cryptoRandomString({ length: 100, type: "alphanumeric" }).length, |
| 129 | + 100, |
| 130 | + ); |
| 131 | + assertMatch( |
| 132 | + cryptoRandomString({ length: 100, type: "alphanumeric" }), |
| 133 | + /^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789]*$/, |
| 134 | + ); // Sanity check, probabilistic |
| 135 | + assertEquals(generatedCharacterSetSize({ type: "alphanumeric" }, 19), 62); |
| 136 | +}); |
| 137 | + |
| 138 | +Deno.test("characters", () => { |
| 139 | + assertEquals(cryptoRandomString({ length: 0, characters: "1234" }).length, 0); |
| 140 | + assertEquals( |
| 141 | + cryptoRandomString({ length: 10, characters: "1234" }).length, |
| 142 | + 10, |
| 143 | + ); |
| 144 | + assertEquals( |
| 145 | + cryptoRandomString({ length: 100, characters: "1234" }).length, |
| 146 | + 100, |
| 147 | + ); |
| 148 | + assertMatch( |
| 149 | + cryptoRandomString({ length: 100, characters: "1234" }), |
| 150 | + /^[1-4]*$/, |
| 151 | + ); // Sanity check, probabilistic |
| 152 | + assertEquals(generatedCharacterSetSize({ characters: "1234" }, 4), 4); |
| 153 | + assertEquals(generatedCharacterSetSize({ characters: "0123456789" }, 10), 10); |
| 154 | +}); |
| 155 | + |
| 156 | +Deno.test("argument errors", () => { |
| 157 | + assertThrows(() => { |
| 158 | + cryptoRandomString({ length: Infinity }); |
| 159 | + }); |
| 160 | + |
| 161 | + assertThrows(() => { |
| 162 | + cryptoRandomString({ length: -1 }); |
| 163 | + }); |
| 164 | + |
| 165 | + assertThrows(() => { |
| 166 | + cryptoRandomString({ length: 0, type: "hex", characters: "1234" }); |
| 167 | + }); |
| 168 | + |
| 169 | + assertThrows(() => { |
| 170 | + cryptoRandomString({ length: 0, type: "unknown" }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments