]> Lady’s Gitweb - Pisces/blob - binary.test.js
Make base32 handling less forgiving
[Pisces] / binary.test.js
1 // ♓🌟 Piscēs ∷ binary.test.js
2 // ====================================================================
3 //
4 // Copyright © 2020–2023 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import {
11 assert,
12 assertEquals,
13 assertStrictEquals,
14 assertThrows,
15 describe,
16 it,
17 } from "./dev-deps.js";
18 import {
19 base16Binary,
20 base16String,
21 base32Binary,
22 base32String,
23 base64Binary,
24 base64String,
25 filenameSafeBase64Binary,
26 filenameSafeBase64String,
27 isBase16,
28 isBase32,
29 isBase64,
30 isFilenameSafeBase64,
31 isWRMGBase32,
32 wrmgBase32Binary,
33 wrmgBase32String,
34 } from "./binary.js";
35
36 // These tests assume a LITTLE‐ENDIAN environment.
37 const data = new Map([
38 ["", {
39 base16: "",
40 base32: "",
41 base64: "",
42 wrmg: "",
43 }],
44 ["base64", {
45 base16: "006200610073006500360034",
46 base32: "ABRAAYIAOMAGKABWAA2A====",
47 base64: "AGIAYQBzAGUANgA0",
48 wrmg: "01H00R80EC06A01P00T0",
49 }],
50 [new Uint16Array([62535]), {
51 base16: "47F4",
52 base32: "I72A====",
53 base64: "R/Q=",
54 wrmg: "8ZT0",
55 }],
56 [new Uint8ClampedArray([75, 73, 66, 73]).buffer, {
57 base16: "4B494249",
58 base32: "JNEUESI=",
59 base64: "S0lCSQ==",
60 wrmg: "9D4M4J8",
61 }],
62 [new DataView(new Uint8Array([98, 97, 115, 101, 54, 52]).buffer), {
63 base16: "626173653634",
64 base32: "MJQXGZJWGQ======",
65 base64: "YmFzZTY0",
66 wrmg: "C9GQ6S9P6G",
67 }],
68
69 // The following three examples are from RFC 3548.
70 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]), {
71 base16: "14FB9C03D97E",
72 base32: "CT5ZYA6ZPY======",
73 base64: "FPucA9l+",
74 wrmg: "2KXSR0YSFR",
75 }],
76 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]), {
77 base16: "14FB9C03D9",
78 base32: "CT5ZYA6Z",
79 base64: "FPucA9k=",
80 wrmg: "2KXSR0YS",
81 }],
82 [new Uint8Array([0x14, 0xFB, 0x9C, 0x03]), {
83 base16: "14FB9C03",
84 base32: "CT5ZYAY=",
85 base64: "FPucAw==",
86 wrmg: "2KXSR0R",
87 }],
88
89 // The following examples are from the Ruby base32 gem.
90 [new Uint8Array([0x28]), {
91 base16: "28",
92 base32: "FA======",
93 base64: "KA==",
94 wrmg: "50",
95 }],
96 [new Uint8Array([0xD6]), {
97 base16: "D6",
98 base32: "2Y======",
99 base64: "1g==",
100 wrmg: "TR",
101 }],
102 [new Uint16Array([0xF8D6]), {
103 base16: "D6F8",
104 base32: "234A====",
105 base64: "1vg=",
106 wrmg: "TVW0",
107 }],
108 [new Uint8Array([0xD6, 0xF8, 0x00]), {
109 base16: "D6F800",
110 base32: "234AA===",
111 base64: "1vgA",
112 wrmg: "TVW00",
113 }],
114 [new Uint8Array([0xD6, 0xF8, 0x10]), {
115 base16: "D6F810",
116 base32: "234BA===",
117 base64: "1vgQ",
118 wrmg: "TVW10",
119 }],
120 [new Uint32Array([0x0C11F8D6]), {
121 base16: "D6F8110C",
122 base32: "234BCDA=",
123 base64: "1vgRDA==",
124 wrmg: "TVW1230",
125 }],
126 [new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]), {
127 base16: "D6F8110C80",
128 base32: "234BCDEA",
129 base64: "1vgRDIA=",
130 wrmg: "TVW12340",
131 }],
132 [new Uint16Array([0xF8D6, 0x0C11, 0x3085]), {
133 base16: "D6F8110C8530",
134 base32: "234BCDEFGA======",
135 base64: "1vgRDIUw",
136 wrmg: "TVW1234560",
137 }],
138 ]);
139
140 describe("base16Binary", () => {
141 it("[[Call]] returns the correct data", () => {
142 assertEquals(
143 new Uint8Array(base16Binary("")),
144 new Uint8Array([]),
145 "<empty>",
146 );
147 assertEquals(
148 new Uint8Array(base16Binary("006200610073006500360034")),
149 Uint8Array.from(
150 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
151 ($) => $.charCodeAt(0),
152 ),
153 "006200610073006500360034",
154 );
155 assertEquals(
156 new Uint16Array(base16Binary("47F4")),
157 new Uint16Array([62535]),
158 "47F4",
159 );
160 assertEquals(
161 new Uint8ClampedArray(base16Binary("4B494249")),
162 new Uint8ClampedArray([75, 73, 66, 73]),
163 "4B494249",
164 );
165 assertEquals(
166 new Uint8Array(base16Binary("626173653634")),
167 new Uint8Array([98, 97, 115, 101, 54, 52]),
168 "626173653634",
169 );
170 assertEquals(
171 new Uint8Array(base16Binary("14FB9C03D97E")),
172 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
173 "14FB9C03D97E",
174 );
175 assertEquals(
176 new Uint8Array(base16Binary("14FB9C03D9")),
177 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
178 "14FB9C03D9",
179 );
180 assertEquals(
181 new Uint8Array(base16Binary("14FB9C03")),
182 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
183 "14FB9C03",
184 );
185 assertEquals(
186 new Uint8Array(base16Binary("28")),
187 new Uint8Array([0x28]),
188 "28",
189 );
190 assertEquals(
191 new Uint8Array(base16Binary("D6")),
192 new Uint8Array([0xD6]),
193 "D6",
194 );
195 assertEquals(
196 new Uint16Array(base16Binary("D6F8")),
197 new Uint16Array([0xF8D6]),
198 "D6F8",
199 );
200 assertEquals(
201 new Uint8Array(base16Binary("D6F800")),
202 new Uint8Array([0xD6, 0xF8, 0x00]),
203 "D6F800",
204 );
205 assertEquals(
206 new Uint8Array(base16Binary("D6F810")),
207 new Uint8Array([0xD6, 0xF8, 0x10]),
208 "D6F810",
209 );
210 assertEquals(
211 new Uint32Array(base16Binary("D6F8110C")),
212 new Uint32Array([0x0C11F8D6]),
213 "D6F8110C",
214 );
215 assertEquals(
216 new Uint8Array(base16Binary("D6F8110C80")),
217 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
218 "D6F8110C80",
219 );
220 assertEquals(
221 new Uint16Array(base16Binary("D6F8110C8530")),
222 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
223 "D6F8110C8530",
224 );
225 });
226
227 it("[[Call]] is case‐insensitive", () => {
228 assertEquals(
229 new Uint8Array(base16Binary("d6f8110C80")),
230 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
231 "d6f8110C80",
232 );
233 });
234
235 it("[[Call]] throws when provided with an invalid character", () => {
236 assertThrows(() => base16Binary("ABCG"));
237 });
238
239 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 2", () => {
240 assertThrows(() => base16Binary("A"));
241 assertThrows(() => base16Binary("ABC"));
242 });
243 });
244
245 describe("base16String", () => {
246 it("[[Call]] returns the correct string", () => {
247 for (const [source, { base16 }] of data) {
248 assertStrictEquals(base16String(source), base16);
249 }
250 });
251 });
252
253 describe("base32Binary", () => {
254 it("[[Call]] returns the correct data", () => {
255 assertEquals(
256 new Uint8Array(base32Binary("")),
257 new Uint8Array([]),
258 "<empty>",
259 );
260 assertEquals(
261 new Uint8Array(base32Binary("ABRAAYIAOMAGKABWAA2A====")),
262 Uint8Array.from(
263 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
264 ($) => $.charCodeAt(0),
265 ),
266 "ABRAAYIAOMAGKABWAA2A====",
267 );
268 assertEquals(
269 new Uint16Array(base32Binary("I72A====")),
270 new Uint16Array([62535]),
271 "I72A====",
272 );
273 assertEquals(
274 new Uint8ClampedArray(base32Binary("JNEUESI=")),
275 new Uint8ClampedArray([75, 73, 66, 73]),
276 "JNEUESI=",
277 );
278 assertEquals(
279 new Uint8Array(base32Binary("MJQXGZJWGQ======")),
280 new Uint8Array([98, 97, 115, 101, 54, 52]),
281 "MJQXGZJWGQ======",
282 );
283 assertEquals(
284 new Uint8Array(base32Binary("CT5ZYA6ZPY======")),
285 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
286 "CT5ZYA6ZPY======",
287 );
288 assertEquals(
289 new Uint8Array(base32Binary("CT5ZYA6Z")),
290 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
291 "CT5ZYA6Z",
292 );
293 assertEquals(
294 new Uint8Array(base32Binary("CT5ZYAY=")),
295 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
296 "CT5ZYAY=",
297 );
298 assertEquals(
299 new Uint8Array(base32Binary("FA======")),
300 new Uint8Array([0x28]),
301 "FA======",
302 );
303 assertEquals(
304 new Uint8Array(base32Binary("2Y======")),
305 new Uint8Array([0xD6]),
306 "2Y======",
307 );
308 assertEquals(
309 new Uint16Array(base32Binary("234A====")),
310 new Uint16Array([0xF8D6]),
311 "234A====",
312 );
313 assertEquals(
314 new Uint8Array(base32Binary("234AA===")),
315 new Uint8Array([0xD6, 0xF8, 0x00]),
316 "234AA===",
317 );
318 assertEquals(
319 new Uint8Array(base32Binary("234BA===")),
320 new Uint8Array([0xD6, 0xF8, 0x10]),
321 "234BA===",
322 );
323 assertEquals(
324 new Uint32Array(base32Binary("234BCDA=")),
325 new Uint32Array([0x0C11F8D6]),
326 "234BCDA=",
327 );
328 assertEquals(
329 new Uint8Array(base32Binary("234BCDEA")),
330 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
331 "234BCDEA",
332 );
333 assertEquals(
334 new Uint16Array(base32Binary("234BCDEFGA======")),
335 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
336 "234BCDEFGA======",
337 );
338 });
339
340 it("[[Call]] is case‐insensitive", () => {
341 assertEquals(
342 new Uint8Array(base32Binary("234bcdEA")),
343 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
344 "234bcdEA",
345 );
346 });
347
348 it("[[Call]] throws when provided with an invalid character", () => {
349 assertThrows(() => base32Binary("ABC0"));
350 assertThrows(() => base32Binary("ABC1"));
351 assertThrows(() => base32Binary("ABC8"));
352 assertThrows(() => base32Binary("ABC9"));
353 });
354
355 it("[[Call]] throws when provided with an invalid equals", () => {
356 assertThrows(() => base32Binary("CT3ZYAY=="));
357 });
358
359 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
360 assertThrows(() => base32Binary("234BCDEAA"));
361 assertThrows(() => base32Binary("234BCDEAA======="));
362 assertThrows(() => base32Binary("UHI"));
363 assertThrows(() => base32Binary("UHIUJD"));
364 });
365 });
366
367 describe("base32String", () => {
368 it("[[Call]] returns the correct string", () => {
369 for (const [source, { base32 }] of data) {
370 assertStrictEquals(base32String(source), base32);
371 }
372 });
373 });
374
375 describe("base64Binary", () => {
376 it("[[Call]] returns the correct data", () => {
377 assertEquals(
378 new Uint8Array(base64Binary("")),
379 new Uint8Array([]),
380 "<empty>",
381 );
382 assertEquals(
383 new Uint8Array(base64Binary("AGIAYQBzAGUANgA0")),
384 Uint8Array.from(
385 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
386 ($) => $.charCodeAt(0),
387 ),
388 "AGIAYQBzAGUANgA0",
389 );
390 assertEquals(
391 new Uint16Array(base64Binary("R/Q=")),
392 new Uint16Array([62535]),
393 "R/Q=",
394 );
395 assertEquals(
396 new Uint8ClampedArray(base64Binary("S0lCSQ==")),
397 new Uint8ClampedArray([75, 73, 66, 73]),
398 "S0lCSQ==",
399 );
400 assertEquals(
401 new Uint8Array(base64Binary("YmFzZTY0")),
402 new Uint8Array([98, 97, 115, 101, 54, 52]),
403 "YmFzZTY0",
404 );
405 assertEquals(
406 new Uint8Array(base64Binary("FPucA9l+")),
407 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
408 "FPucA9l+",
409 );
410 assertEquals(
411 new Uint8Array(base64Binary("FPucA9k=")),
412 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
413 "FPucA9k=",
414 );
415 assertEquals(
416 new Uint8Array(base64Binary("FPucAw==")),
417 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
418 "FPucAw==",
419 );
420 assertEquals(
421 new Uint8Array(base64Binary("KA==")),
422 new Uint8Array([0x28]),
423 "KA==",
424 );
425 assertEquals(
426 new Uint8Array(base64Binary("1g==")),
427 new Uint8Array([0xD6]),
428 "1g==",
429 );
430 assertEquals(
431 new Uint16Array(base64Binary("1vg")),
432 new Uint16Array([0xF8D6]),
433 "1vg",
434 );
435 assertEquals(
436 new Uint8Array(base64Binary("1vgA")),
437 new Uint8Array([0xD6, 0xF8, 0x00]),
438 "1vgA",
439 );
440 assertEquals(
441 new Uint8Array(base64Binary("1vgQ")),
442 new Uint8Array([0xD6, 0xF8, 0x10]),
443 "1vgQ",
444 );
445 assertEquals(
446 new Uint32Array(base64Binary("1vgRDA==")),
447 new Uint32Array([0x0C11F8D6]),
448 "1vgRDA==",
449 );
450 assertEquals(
451 new Uint8Array(base64Binary("1vgRDIA=")),
452 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
453 "1vgRDIA=",
454 );
455 assertEquals(
456 new Uint16Array(base64Binary("1vgRDIUw")),
457 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
458 "1vgRDIUw",
459 );
460 });
461
462 it("[[Call]] throws when provided with an invalid character", () => {
463 assertThrows(() => base64Binary("abc_"));
464 });
465
466 it("[[Call]] throws when provided with an invalid equals", () => {
467 assertThrows(() => base64Binary("abc=="));
468 });
469
470 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
471 assertThrows(() => base64Binary("abcde"));
472 assertThrows(() => base64Binary("abcde==="));
473 });
474 });
475
476 describe("base64String", () => {
477 it("[[Call]] returns the correct string", () => {
478 for (const [source, { base64 }] of data) {
479 assertStrictEquals(base64String(source), base64);
480 }
481 });
482 });
483
484 describe("filenameSafeBase64Binary", () => {
485 it("[[Call]] returns the correct data", () => {
486 assertEquals(
487 new Uint8Array(filenameSafeBase64Binary("")),
488 new Uint8Array([]),
489 "<empty>",
490 );
491 assertEquals(
492 new Uint8Array(filenameSafeBase64Binary("AGIAYQBzAGUANgA0")),
493 Uint8Array.from(
494 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
495 ($) => $.charCodeAt(0),
496 ),
497 "AGIAYQBzAGUANgA0",
498 );
499 assertEquals(
500 new Uint16Array(filenameSafeBase64Binary("R_Q=")),
501 new Uint16Array([62535]),
502 "R/Q=",
503 );
504 assertEquals(
505 new Uint8ClampedArray(filenameSafeBase64Binary("S0lCSQ==")),
506 new Uint8ClampedArray([75, 73, 66, 73]),
507 "S0lCSQ==",
508 );
509 assertEquals(
510 new Uint8Array(filenameSafeBase64Binary("YmFzZTY0")),
511 new Uint8Array([98, 97, 115, 101, 54, 52]),
512 "YmFzZTY0",
513 );
514 assertEquals(
515 new Uint8Array(filenameSafeBase64Binary("KA==")),
516 new Uint8Array([0x28]),
517 "KA==",
518 );
519 assertEquals(
520 new Uint8Array(filenameSafeBase64Binary("1g==")),
521 new Uint8Array([0xD6]),
522 "1g==",
523 );
524 assertEquals(
525 new Uint16Array(filenameSafeBase64Binary("1vg")),
526 new Uint16Array([0xF8D6]),
527 "1vg",
528 );
529 assertEquals(
530 new Uint8Array(filenameSafeBase64Binary("1vgA")),
531 new Uint8Array([0xD6, 0xF8, 0x00]),
532 "1vgA",
533 );
534 assertEquals(
535 new Uint8Array(filenameSafeBase64Binary("1vgQ")),
536 new Uint8Array([0xD6, 0xF8, 0x10]),
537 "1vgQ",
538 );
539 assertEquals(
540 new Uint32Array(filenameSafeBase64Binary("1vgQDA==")),
541 new Uint32Array([0x0C10F8D6]),
542 "1vgQDA==",
543 );
544 assertEquals(
545 new Uint8Array(filenameSafeBase64Binary("1vgQDIA=")),
546 new Uint8Array([0xD6, 0xF8, 0x10, 0x0C, 0x80]),
547 "1vgQDIA=",
548 );
549 assertEquals(
550 new Uint16Array(filenameSafeBase64Binary("1vgQDIUw")),
551 new Uint16Array([0xF8D6, 0x0C10, 0x3085]),
552 "1vgQDIUw",
553 );
554 });
555
556 it("[[Call]] throws when provided with an invalid character", () => {
557 assertThrows(() => filenameSafeBase64Binary("abc/"));
558 });
559
560 it("[[Call]] throws when provided with an invalid equals", () => {
561 assertThrows(() => filenameSafeBase64Binary("abc=="));
562 });
563
564 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
565 assertThrows(() => filenameSafeBase64Binary("abcde"));
566 assertThrows(() => filenameSafeBase64Binary("abcde==="));
567 });
568 });
569
570 describe("filenameSafeBase64String", () => {
571 it("[[Call]] returns the correct string", () => {
572 for (const [source, { base64 }] of data) {
573 assertStrictEquals(
574 filenameSafeBase64String(source),
575 base64.replace("+", "-").replace("/", "_"),
576 );
577 }
578 });
579 });
580
581 describe("isBase16", () => {
582 it("[[Call]] returns true for base64 strings", () => {
583 for (const { base16 } of data.values()) {
584 assert(isBase16(base16));
585 assert(isBase16(base16.toLowerCase()));
586 }
587 });
588
589 it("[[Call]] returns false for others", () => {
590 [
591 undefined,
592 null,
593 true,
594 Symbol(),
595 27,
596 98n,
597 {},
598 [],
599 () => {},
600 new Proxy({}, {}),
601 "abc_",
602 "a",
603 "abc",
604 "abcg",
605 ].forEach((value) => assert(!isBase16(value)));
606 });
607 });
608
609 describe("isBase32", () => {
610 it("[[Call]] returns true for base32 strings", () => {
611 for (const { base32 } of data.values()) {
612 assert(isBase32(base32));
613 assert(isBase32(base32.toLowerCase()));
614 }
615 });
616
617 it("[[Call]] returns false for others", () => {
618 [
619 undefined,
620 null,
621 true,
622 Symbol(),
623 27,
624 98n,
625 {},
626 [],
627 () => {},
628 new Proxy({}, {}),
629 "ABC1",
630 "A=======",
631 "ABCDEFGHI",
632 ].forEach((value) => assert(!isBase32(value)));
633 });
634 });
635
636 describe("isBase64", () => {
637 it("[[Call]] returns true for base64 strings", () => {
638 for (const { base64 } of data.values()) {
639 assert(isBase64(base64));
640 }
641 });
642
643 it("[[Call]] returns false for others", () => {
644 [
645 undefined,
646 null,
647 true,
648 Symbol(),
649 27,
650 98n,
651 {},
652 [],
653 () => {},
654 new Proxy({}, {}),
655 "abc_",
656 "a",
657 "abc==",
658 ].forEach((value) => assert(!isBase64(value)));
659 });
660 });
661
662 describe("isFilenameSafeBase64", () => {
663 it("[[Call]] returns true for filename‐safe base64 strings", () => {
664 for (const { base64 } of data.values()) {
665 assert(
666 isFilenameSafeBase64(
667 base64.replace("+", "-").replace("/", "_"),
668 ),
669 );
670 }
671 });
672
673 it("[[Call]] returns false for others", () => {
674 [
675 undefined,
676 null,
677 true,
678 Symbol(),
679 27,
680 98n,
681 {},
682 [],
683 () => {},
684 new Proxy({}, {}),
685 "abc/",
686 "a",
687 "abc==",
688 ].forEach((value) => assert(!isFilenameSafeBase64(value)));
689 });
690 });
691
692 describe("isWRMGBase32", () => {
693 it("[[Call]] returns true for W·R·M·G base32 strings", () => {
694 for (const { wrmg } of data.values()) {
695 assert(isWRMGBase32(wrmg));
696 assert(isWRMGBase32(wrmg.toLowerCase()));
697 assert(isWRMGBase32(`--${wrmg}--`));
698 assert(isWRMGBase32(wrmg.replaceAll(/1/gu, "I")));
699 assert(isWRMGBase32(wrmg.replaceAll(/1/gu, "L")));
700 assert(isWRMGBase32(wrmg.replaceAll(/0/gu, "O")));
701 assert(isWRMGBase32(wrmg.replaceAll(/1/gu, "i")));
702 assert(isWRMGBase32(wrmg.replaceAll(/1/gu, "l")));
703 assert(isWRMGBase32(wrmg.replaceAll(/0/gu, "o")));
704 assert(isWRMGBase32(wrmg.replaceAll(/./gu, ($) => {
705 const rand = Math.random();
706 return rand < 0.25
707 ? $
708 : rand < 0.5
709 ? `-${$}`
710 : rand < 0.75
711 ? `${$}-`
712 : `-${$}-`;
713 })));
714 }
715 });
716
717 it("[[Call]] returns false for others", () => {
718 [
719 undefined,
720 null,
721 true,
722 Symbol(),
723 27,
724 98n,
725 {},
726 [],
727 () => {},
728 new Proxy({}, {}),
729 "ABCU",
730 "A",
731 "ABCDEFGH1",
732 ].forEach((value) => assert(!isWRMGBase32(value)));
733 });
734 });
735
736 describe("wrmgBase32Binary", () => {
737 it("[[Call]] returns the correct data", () => {
738 assertEquals(
739 new Uint8Array(wrmgBase32Binary("")),
740 new Uint8Array([]),
741 "<empty>",
742 );
743 assertEquals(
744 new Uint8Array(wrmgBase32Binary("01H00R80EC06A01P00T0")),
745 Uint8Array.from(
746 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
747 ($) => $.charCodeAt(0),
748 ),
749 "01H00R80EC06A01P00T0",
750 );
751 assertEquals(
752 new Uint16Array(wrmgBase32Binary("8ZT0")),
753 new Uint16Array([62535]),
754 "8ZT0",
755 );
756 assertEquals(
757 new Uint8ClampedArray(wrmgBase32Binary("9D4M4J8")),
758 new Uint8ClampedArray([75, 73, 66, 73]),
759 "9D4M4J8",
760 );
761 assertEquals(
762 new Uint8Array(wrmgBase32Binary("C9GQ6S9P6G")),
763 new Uint8Array([98, 97, 115, 101, 54, 52]),
764 "C9GQ6S9P6G",
765 );
766 assertEquals(
767 new Uint8Array(wrmgBase32Binary("2KXSR0YSFR")),
768 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9, 0x7E]),
769 "2KXSR0YSFR",
770 );
771 assertEquals(
772 new Uint8Array(wrmgBase32Binary("2KXSR0YS")),
773 new Uint8Array([0x14, 0xFB, 0x9C, 0x03, 0xD9]),
774 "2KXSR0YS",
775 );
776 assertEquals(
777 new Uint8Array(wrmgBase32Binary("2KXSR0R")),
778 new Uint8Array([0x14, 0xFB, 0x9C, 0x03]),
779 "2KXSR0R",
780 );
781 assertEquals(
782 new Uint8Array(wrmgBase32Binary("50")),
783 new Uint8Array([0x28]),
784 "50",
785 );
786 assertEquals(
787 new Uint8Array(wrmgBase32Binary("TR")),
788 new Uint8Array([0xD6]),
789 "TR",
790 );
791 assertEquals(
792 new Uint16Array(wrmgBase32Binary("TVW0")),
793 new Uint16Array([0xF8D6]),
794 "TVW0",
795 );
796 assertEquals(
797 new Uint8Array(wrmgBase32Binary("TVW00")),
798 new Uint8Array([0xD6, 0xF8, 0x00]),
799 "TVW00",
800 );
801 assertEquals(
802 new Uint8Array(wrmgBase32Binary("TVW10")),
803 new Uint8Array([0xD6, 0xF8, 0x10]),
804 "TVW10",
805 );
806 assertEquals(
807 new Uint32Array(wrmgBase32Binary("TVW1230")),
808 new Uint32Array([0x0C11F8D6]),
809 "TVW1230",
810 );
811 assertEquals(
812 new Uint8Array(wrmgBase32Binary("TVW12340")),
813 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
814 "TVW12340",
815 );
816 assertEquals(
817 new Uint16Array(wrmgBase32Binary("TVW1234560")),
818 new Uint16Array([0xF8D6, 0x0C11, 0x3085]),
819 "TVW1234560",
820 );
821 });
822
823 it("[[Call]] is case‐insensitive", () => {
824 assertEquals(
825 new Uint8Array(wrmgBase32Binary("tVw12340")),
826 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
827 "tVw12340",
828 );
829 });
830
831 it("[[Call]] casts I, L & O", () => {
832 assertEquals(
833 new Uint8Array(wrmgBase32Binary("TVWI234O")),
834 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
835 "TVWI234O",
836 );
837 assertEquals(
838 new Uint8Array(wrmgBase32Binary("TVWi234o")),
839 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
840 "TVWi234o",
841 );
842 assertEquals(
843 new Uint8Array(wrmgBase32Binary("TVWL234O")),
844 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
845 "TVWL234O",
846 );
847 assertEquals(
848 new Uint8Array(wrmgBase32Binary("TVWl234o")),
849 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
850 "TVWl234o",
851 );
852 });
853
854 it("[[Call]] ignores hyphens", () => {
855 assertEquals(
856 new Uint8Array(wrmgBase32Binary("--TVW---123-40-----")),
857 new Uint8Array([0xD6, 0xF8, 0x11, 0x0C, 0x80]),
858 "--TVW---123-40-----",
859 );
860 });
861
862 it("[[Call]] throws when provided with an invalid character", () => {
863 assertThrows(() => wrmgBase32Binary("ABCu"));
864 assertThrows(() => wrmgBase32Binary("ABCU"));
865 assertThrows(() => wrmgBase32Binary("ABC="));
866 });
867
868 it("[[Call]] throws when provided with a length with a remainder of 1, 3 ∣ 6 when divided by 8", () => {
869 assertThrows(() => base32Binary("TVW123400"));
870 assertThrows(() => base32Binary("TVW123400======="));
871 assertThrows(() => base32Binary("M78"));
872 assertThrows(() => base32Binary("M78M93"));
873 });
874
875 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
876 assertThrows(() => wrmgBase32Binary("234BCDEAA"));
877 assertThrows(() => wrmgBase32Binary("2-34-B--CD-EA-A-"));
878 });
879 });
880
881 describe("wrmgBase32String", () => {
882 it("[[Call]] returns the correct string", () => {
883 for (const [source, { wrmg }] of data) {
884 assertStrictEquals(wrmgBase32String(source), wrmg);
885 }
886 });
887 });
This page took 0.184371 seconds and 5 git commands to generate.