2020-02-02 21:49:41 +00:00
|
|
|
// Test cases copied from https://github.com/LinusU/base32-encode/blob/master/test.js
|
|
|
|
// Copyright (c) 2016-2017 Linus Unnebäck. MIT license.
|
2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-08-09 09:57:21 +00:00
|
|
|
import { assertEquals, assertExists, assertThrows } from "@std/assert";
|
2024-01-12 05:06:49 +00:00
|
|
|
import { decodeBase32, encodeBase32 } from "./base32.ts";
|
2020-02-02 21:49:41 +00:00
|
|
|
|
2024-08-10 18:41:08 +00:00
|
|
|
// Test vectors from https://www.rfc-editor.org/rfc/rfc4648.html#section-10
|
2020-02-02 21:49:41 +00:00
|
|
|
const testCases = [
|
2024-08-10 18:41:08 +00:00
|
|
|
["", ""],
|
|
|
|
["f", "MY======"],
|
|
|
|
["fo", "MZXQ===="],
|
|
|
|
["foo", "MZXW6==="],
|
|
|
|
["foob", "MZXW6YQ="],
|
|
|
|
["fooba", "MZXW6YTB"],
|
|
|
|
["foobar", "MZXW6YTBOI======"],
|
2024-02-24 20:24:08 +00:00
|
|
|
] as const;
|
2020-02-02 21:49:41 +00:00
|
|
|
|
2020-02-11 16:24:27 +00:00
|
|
|
Deno.test({
|
2023-12-28 03:03:43 +00:00
|
|
|
name: "encodeBase32()",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2020-02-02 21:49:41 +00:00
|
|
|
for (const [bin, b32] of testCases) {
|
2024-08-10 18:41:08 +00:00
|
|
|
assertEquals(encodeBase32(bin), b32);
|
2020-02-02 21:49:41 +00:00
|
|
|
}
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2020-02-02 21:49:41 +00:00
|
|
|
});
|
|
|
|
|
2020-02-11 16:24:27 +00:00
|
|
|
Deno.test({
|
2023-12-28 03:03:43 +00:00
|
|
|
name: "decodeBase32()",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2020-02-02 21:49:41 +00:00
|
|
|
for (const [bin, b32] of testCases) {
|
2024-08-10 18:41:08 +00:00
|
|
|
assertEquals(decodeBase32(b32), new TextEncoder().encode(bin));
|
2020-02-02 21:49:41 +00:00
|
|
|
}
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2020-02-02 21:49:41 +00:00
|
|
|
});
|
|
|
|
|
2020-02-11 16:24:27 +00:00
|
|
|
Deno.test({
|
2023-12-28 03:03:43 +00:00
|
|
|
name: "decodeBase32() throws on bad length",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2023-12-28 03:03:43 +00:00
|
|
|
assertThrows(
|
|
|
|
() => decodeBase32("OOOO=="),
|
2024-05-15 22:58:53 +00:00
|
|
|
Error,
|
2024-08-23 03:32:15 +00:00
|
|
|
"Cannot decode base32 string as the length must be a multiple of 8: received length 6",
|
2023-12-28 03:03:43 +00:00
|
|
|
);
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2020-02-02 21:49:41 +00:00
|
|
|
});
|
|
|
|
|
2020-02-11 16:24:27 +00:00
|
|
|
Deno.test({
|
2023-12-28 03:03:43 +00:00
|
|
|
name: "decodeBase32() throws on bad padding",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2023-12-28 03:03:43 +00:00
|
|
|
assertThrows(
|
2024-05-15 22:58:53 +00:00
|
|
|
() => decodeBase32("5HXR334AQYAAAA=="),
|
|
|
|
Error,
|
2023-12-28 03:03:43 +00:00
|
|
|
"Invalid pad length",
|
|
|
|
);
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2020-02-02 21:49:41 +00:00
|
|
|
});
|
2023-12-17 20:16:38 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2023-12-28 03:03:43 +00:00
|
|
|
name: "encodeBase32() encodes very long text",
|
2023-12-17 20:16:38 +00:00
|
|
|
fn() {
|
|
|
|
const data = "a".repeat(16400);
|
|
|
|
assertExists(encodeBase32(data));
|
|
|
|
},
|
|
|
|
});
|