|
| 1 | +// Copyright (c) 2026 Cloudflare, Inc. |
| 2 | +// Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | +// https://opensource.org/licenses/Apache-2.0 |
| 4 | + |
| 5 | +// Constructor arguments: algorithm names (string or {name} object, matched |
| 6 | +// case-insensitively over the whole name) and the options bag. |
| 7 | + |
| 8 | +import { strictEqual, deepStrictEqual, throws } from 'node:assert'; |
| 9 | +import { digestOf } from 'digest-vectors'; |
| 10 | + |
| 11 | +export const algorithmNamesAreCaseInsensitive = { |
| 12 | + async test() { |
| 13 | + const variants = { |
| 14 | + crc32: ['crc32', 'CRC32', 'Crc32', 'cRc32'], |
| 15 | + crc32c: ['crc32c', 'CRC32C', 'Crc32c', 'crc32C', 'cRc32C'], |
| 16 | + crc64nvme: ['crc64nvme', 'CRC64NVME', 'Crc64Nvme', 'crc64NVME'], |
| 17 | + md5: ['md5', 'MD5', 'Md5'], |
| 18 | + 'sha-256': ['sha-256', 'SHA-256', 'Sha-256'], |
| 19 | + }; |
| 20 | + for (const [canonical, names] of Object.entries(variants)) { |
| 21 | + const expected = await digestOf(canonical, 'hello'); |
| 22 | + for (const name of names) { |
| 23 | + deepStrictEqual( |
| 24 | + await digestOf(name, 'hello'), |
| 25 | + expected, |
| 26 | + `${name} should select the same algorithm as ${canonical}` |
| 27 | + ); |
| 28 | + } |
| 29 | + } |
| 30 | + }, |
| 31 | +}; |
| 32 | + |
| 33 | +export const crcNamesStillRequireAnExactSpelling = { |
| 34 | + test() { |
| 35 | + // Case-insensitivity must not turn a nonsense name into a match: the |
| 36 | + // comparison is over the whole name. |
| 37 | + for (const name of [ |
| 38 | + 'crc', |
| 39 | + 'crc3', |
| 40 | + 'crc322', |
| 41 | + 'crc32d', |
| 42 | + ' crc32', |
| 43 | + 'crc32 ', |
| 44 | + 'crc-32', |
| 45 | + 'crc64', |
| 46 | + 'crc64nvm', |
| 47 | + 'crc64nvmex', |
| 48 | + 'nvme', |
| 49 | + ]) { |
| 50 | + throws( |
| 51 | + () => new crypto.DigestStream(name), |
| 52 | + { name: 'NotSupportedError' }, |
| 53 | + `${JSON.stringify(name)} should not be a valid algorithm` |
| 54 | + ); |
| 55 | + } |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +export const unknownAlgorithmThrowsSynchronously = { |
| 60 | + test() { |
| 61 | + throws(() => new crypto.DigestStream('foo')); |
| 62 | + throws(() => new crypto.DigestStream('')); |
| 63 | + // A missing name coerces to "undefined" and then fails the lookup. |
| 64 | + throws(() => new crypto.DigestStream({})); |
| 65 | + throws(() => new crypto.DigestStream(null)); |
| 66 | + throws(() => new crypto.DigestStream(undefined)); |
| 67 | + }, |
| 68 | +}; |
| 69 | + |
| 70 | +export const algorithmAsObject = { |
| 71 | + async test() { |
| 72 | + const check = new Uint8Array([ |
| 73 | + 93, 65, 64, 42, 188, 75, 42, 118, 185, 113, 157, 145, 16, 23, 197, 146, |
| 74 | + ]); |
| 75 | + deepStrictEqual(await digestOf({ name: 'md5' }, 'hello'), check); |
| 76 | + }, |
| 77 | +}; |
| 78 | + |
| 79 | +export const allAlgorithms = { |
| 80 | + async test() { |
| 81 | + const sizes = { |
| 82 | + md5: 16, |
| 83 | + 'SHA-1': 20, |
| 84 | + 'SHA-256': 32, |
| 85 | + 'SHA-384': 48, |
| 86 | + 'SHA-512': 64, |
| 87 | + crc32: 4, |
| 88 | + crc32c: 4, |
| 89 | + crc64nvme: 8, |
| 90 | + }; |
| 91 | + for (const [name, size] of Object.entries(sizes)) { |
| 92 | + strictEqual( |
| 93 | + (await digestOf(name, 'abc')).byteLength, |
| 94 | + size, |
| 95 | + `${name} digest size` |
| 96 | + ); |
| 97 | + } |
| 98 | + }, |
| 99 | +}; |
| 100 | + |
| 101 | +// The option bag follows Web IDL dictionary rules: undefined and null are an |
| 102 | +// empty bag, any object is read for its fields, and a primitive is a |
| 103 | +// TypeError. Arrays and functions count as objects. |
| 104 | +export const optionBagAcceptsObjectsAndRejectsPrimitives = { |
| 105 | + test() { |
| 106 | + for (const options of [undefined, null, {}, [], () => {}, new Date()]) { |
| 107 | + const stream = new crypto.DigestStream('md5', options); |
| 108 | + stream[Symbol.dispose](); |
| 109 | + } |
| 110 | + for (const options of [0, 1, '', 'x', true, false, 1n, Symbol.iterator]) { |
| 111 | + throws( |
| 112 | + () => new crypto.DigestStream('md5', options), |
| 113 | + { |
| 114 | + name: 'TypeError', |
| 115 | + message: |
| 116 | + "Failed to construct 'DigestStream': constructor parameter 2 is " + |
| 117 | + "not of type 'Options'.", |
| 118 | + }, |
| 119 | + `should reject primitive option bag: ${String(options)}` |
| 120 | + ); |
| 121 | + } |
| 122 | + }, |
| 123 | +}; |
| 124 | + |
| 125 | +// Argument type-checking happens before the algorithm name is looked up. |
| 126 | +export const argumentTypesAreCheckedBeforeAlgorithmLookup = { |
| 127 | + test() { |
| 128 | + throws(() => new crypto.DigestStream('foo', 0), { |
| 129 | + name: 'TypeError', |
| 130 | + message: |
| 131 | + "Failed to construct 'DigestStream': constructor parameter 2 is not " + |
| 132 | + "of type 'Options'.", |
| 133 | + }); |
| 134 | + throws(() => new crypto.DigestStream('foo', { toWellFormed: true }), { |
| 135 | + name: 'NotSupportedError', |
| 136 | + }); |
| 137 | + }, |
| 138 | +}; |
0 commit comments