[79a0317] | 1 | import * as assert from 'assert';
|
---|
| 2 | import test, { describe } from 'node:test';
|
---|
| 3 | import md5 from '../md5.js';
|
---|
| 4 | import sha1 from '../sha1.js';
|
---|
| 5 | import v3 from '../v3.js';
|
---|
| 6 | import { stringToBytes } from '../v35.js';
|
---|
| 7 | import v5 from '../v5.js';
|
---|
| 8 | describe('v35', () => {
|
---|
| 9 | const HASH_SAMPLES = [
|
---|
| 10 | {
|
---|
| 11 | input: stringToBytes(''),
|
---|
| 12 | sha1: 'da39a3ee5e6b4b0d3255bfef95601890afd80709',
|
---|
| 13 | md5: 'd41d8cd98f00b204e9800998ecf8427e',
|
---|
| 14 | },
|
---|
| 15 | {
|
---|
| 16 | input: stringToBytes('\t\b\f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6\u00A7\u00A8\u00A9\u00AA\u00AB\u00AC\u00AE\u00AF\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7\u00B8\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D7\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF'),
|
---|
| 17 | sha1: 'ca4a426a3d536f14cfd79011e79e10d64de950a0',
|
---|
| 18 | md5: 'e8098ec21950f841731d28749129d3ee',
|
---|
| 19 | },
|
---|
| 20 | {
|
---|
| 21 | input: stringToBytes('\u00A5\u0104\u018F\u0256\u02B1o\u0315\u038E\u0409\u0500\u0531\u05E1\u05B6\u0920\u0903\u09A4\u0983\u0A20\u0A02\u0AA0\u0A83\u0B06\u0C05\u0C03\u1401\u16A0'),
|
---|
| 22 | sha1: 'f2753ebc390e5f637e333c2a4179644a93ae9f65',
|
---|
| 23 | md5: '231b309e277b6be8bb3d6c688b7f098b',
|
---|
| 24 | },
|
---|
| 25 | ];
|
---|
| 26 | function hashToHex(hash) {
|
---|
| 27 | const chars = new Array(hash.length);
|
---|
| 28 | for (let i = 0; i < hash.length; i++) {
|
---|
| 29 | chars[i] = hash[i].toString(16).padStart(2, '0');
|
---|
| 30 | }
|
---|
| 31 | return chars.join('');
|
---|
| 32 | }
|
---|
| 33 | HASH_SAMPLES.forEach(function (sample, i) {
|
---|
| 34 | test(`sha1(node) HASH_SAMPLES[${i}]`, () => {
|
---|
| 35 | assert.equal(hashToHex(sha1(sample.input)), sample.sha1);
|
---|
| 36 | });
|
---|
| 37 | });
|
---|
| 38 | HASH_SAMPLES.forEach(function (sample, i) {
|
---|
| 39 | test(`md5(node) HASH_SAMPLES[${i}]`, () => {
|
---|
| 40 | assert.equal(hashToHex(md5(sample.input)), sample.md5);
|
---|
| 41 | });
|
---|
| 42 | });
|
---|
| 43 | test('v3', () => {
|
---|
| 44 | assert.strictEqual(v3('hello.example.com', v3.DNS), '9125a8dc-52ee-365b-a5aa-81b0b3681cf6');
|
---|
| 45 | assert.strictEqual(v3('http://example.com/hello', v3.URL), 'c6235813-3ba4-3801-ae84-e0a6ebb7d138');
|
---|
| 46 | assert.strictEqual(v3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'), 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec');
|
---|
| 47 | });
|
---|
| 48 | test('v3 namespace.toUpperCase', () => {
|
---|
| 49 | assert.strictEqual(v3('hello.example.com', v3.DNS.toUpperCase()), '9125a8dc-52ee-365b-a5aa-81b0b3681cf6');
|
---|
| 50 | assert.strictEqual(v3('http://example.com/hello', v3.URL.toUpperCase()), 'c6235813-3ba4-3801-ae84-e0a6ebb7d138');
|
---|
| 51 | assert.strictEqual(v3('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()), 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec');
|
---|
| 52 | });
|
---|
| 53 | test('v3 namespace string validation', () => {
|
---|
| 54 | assert.throws(() => {
|
---|
| 55 | v3('hello.example.com', 'zyxwvuts-rqpo-nmlk-jihg-fedcba000000');
|
---|
| 56 | });
|
---|
| 57 | assert.throws(() => {
|
---|
| 58 | v3('hello.example.com', 'invalid uuid value');
|
---|
| 59 | });
|
---|
| 60 | assert.ok(v3('hello.example.com', '00000000-0000-0000-0000-000000000000'));
|
---|
| 61 | });
|
---|
| 62 | test('v3 namespace buffer validation', () => {
|
---|
| 63 | assert.throws(() => {
|
---|
| 64 | v3('hello.example.com', new Uint8Array(15));
|
---|
| 65 | });
|
---|
| 66 | assert.throws(() => {
|
---|
| 67 | v3('hello.example.com', new Uint8Array(17));
|
---|
| 68 | });
|
---|
| 69 | assert.ok(v3('hello.example.com', new Uint8Array(16).fill(0)));
|
---|
| 70 | });
|
---|
| 71 | test('v3 fill buffer', () => {
|
---|
| 72 | let buf = new Uint8Array(16);
|
---|
| 73 | const expectedUuid = Uint8Array.of(0x91, 0x25, 0xa8, 0xdc, 0x52, 0xee, 0x36, 0x5b, 0xa5, 0xaa, 0x81, 0xb0, 0xb3, 0x68, 0x1c, 0xf6);
|
---|
| 74 | const result = v3('hello.example.com', v3.DNS, buf);
|
---|
| 75 | assert.deepEqual(buf, expectedUuid);
|
---|
| 76 | assert.strictEqual(result, buf);
|
---|
| 77 | buf = new Uint8Array(19).fill(0xaa);
|
---|
| 78 | const expectedBuf = new Uint8Array(19).fill(0xaa);
|
---|
| 79 | expectedBuf.set(expectedUuid, 3);
|
---|
| 80 | v3('hello.example.com', v3.DNS, buf, 3);
|
---|
| 81 | assert.deepEqual(buf, expectedBuf);
|
---|
| 82 | });
|
---|
| 83 | test('v3 undefined/null', () => {
|
---|
| 84 | assert.throws(() => v3());
|
---|
| 85 | assert.throws(() => v3('hello'));
|
---|
| 86 | assert.throws(() => v3('hello.example.com', undefined));
|
---|
| 87 | assert.throws(() => v3('hello.example.com', null, new Uint8Array(16)));
|
---|
| 88 | });
|
---|
| 89 | test('v5', () => {
|
---|
| 90 | assert.strictEqual(v5('hello.example.com', v5.DNS), 'fdda765f-fc57-5604-a269-52a7df8164ec');
|
---|
| 91 | assert.strictEqual(v5('http://example.com/hello', v5.URL), '3bbcee75-cecc-5b56-8031-b6641c1ed1f1');
|
---|
| 92 | assert.strictEqual(v5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'), '90123e1c-7512-523e-bb28-76fab9f2f73d');
|
---|
| 93 | });
|
---|
| 94 | test('v5 namespace.toUpperCase', () => {
|
---|
| 95 | assert.strictEqual(v5('hello.example.com', v5.DNS.toUpperCase()), 'fdda765f-fc57-5604-a269-52a7df8164ec');
|
---|
| 96 | assert.strictEqual(v5('http://example.com/hello', v5.URL.toUpperCase()), '3bbcee75-cecc-5b56-8031-b6641c1ed1f1');
|
---|
| 97 | assert.strictEqual(v5('hello', '0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()), '90123e1c-7512-523e-bb28-76fab9f2f73d');
|
---|
| 98 | });
|
---|
| 99 | test('v5 namespace string validation', () => {
|
---|
| 100 | assert.throws(() => {
|
---|
| 101 | v5('hello.example.com', 'zyxwvuts-rqpo-nmlk-jihg-fedcba000000');
|
---|
| 102 | });
|
---|
| 103 | assert.throws(() => {
|
---|
| 104 | v5('hello.example.com', 'invalid uuid value');
|
---|
| 105 | });
|
---|
| 106 | assert.ok(v5('hello.example.com', '00000000-0000-0000-0000-000000000000'));
|
---|
| 107 | });
|
---|
| 108 | test('v5 namespace buffer validation', () => {
|
---|
| 109 | assert.throws(() => {
|
---|
| 110 | v5('hello.example.com', new Uint8Array(15));
|
---|
| 111 | });
|
---|
| 112 | assert.throws(() => {
|
---|
| 113 | v5('hello.example.com', new Uint8Array(17));
|
---|
| 114 | });
|
---|
| 115 | assert.ok(v5('hello.example.com', new Uint8Array(16).fill(0)));
|
---|
| 116 | });
|
---|
| 117 | test('v5 fill buffer', () => {
|
---|
| 118 | let buf = new Uint8Array(16);
|
---|
| 119 | const expectedUuid = Uint8Array.of(0xfd, 0xda, 0x76, 0x5f, 0xfc, 0x57, 0x56, 0x04, 0xa2, 0x69, 0x52, 0xa7, 0xdf, 0x81, 0x64, 0xec);
|
---|
| 120 | const result = v5('hello.example.com', v5.DNS, buf);
|
---|
| 121 | assert.deepEqual(buf, expectedUuid);
|
---|
| 122 | assert.strictEqual(result, buf);
|
---|
| 123 | buf = new Uint8Array(19).fill(0xaa);
|
---|
| 124 | const expectedBuf = new Uint8Array(19).fill(0xaa);
|
---|
| 125 | expectedBuf.set(expectedUuid, 3);
|
---|
| 126 | v5('hello.example.com', v5.DNS, buf, 3);
|
---|
| 127 | assert.deepEqual(buf, expectedBuf);
|
---|
| 128 | });
|
---|
| 129 | test('v5 undefined/null', () => {
|
---|
| 130 | assert.throws(() => v5());
|
---|
| 131 | assert.throws(() => v5('hello'));
|
---|
| 132 | assert.throws(() => v5('hello.example.com', undefined));
|
---|
| 133 | assert.throws(() => v5('hello.example.com', null, new Uint8Array(16)));
|
---|
| 134 | });
|
---|
| 135 | test('v3/v5 constants', () => {
|
---|
| 136 | assert.strictEqual(v3.DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
|
---|
| 137 | assert.strictEqual(v3.URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
|
---|
| 138 | assert.strictEqual(v5.DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
|
---|
| 139 | assert.strictEqual(v5.URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
|
---|
| 140 | });
|
---|
| 141 | });
|
---|