2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-10-13 01:12:10 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
2023-09-21 09:29:13 +00:00
|
|
|
import { decodeBase64Url, encodeBase64Url } from "./base64url.ts";
|
2020-06-03 13:44:51 +00:00
|
|
|
|
|
|
|
const testsetString = [
|
|
|
|
["", ""],
|
2020-10-13 01:12:10 +00:00
|
|
|
["ß", "w58"],
|
2020-06-03 13:44:51 +00:00
|
|
|
["f", "Zg"],
|
|
|
|
["fo", "Zm8"],
|
|
|
|
["foo", "Zm9v"],
|
|
|
|
["foob", "Zm9vYg"],
|
|
|
|
["fooba", "Zm9vYmE"],
|
|
|
|
["foobar", "Zm9vYmFy"],
|
2020-10-13 01:12:10 +00:00
|
|
|
[">?>d?ß", "Pj8-ZD_Dnw"],
|
2020-06-03 13:44:51 +00:00
|
|
|
];
|
|
|
|
|
2020-10-13 01:12:10 +00:00
|
|
|
const testsetBinary = testsetString.map(([str, b64]) => [
|
|
|
|
new TextEncoder().encode(str),
|
|
|
|
b64,
|
|
|
|
]) as Array<[Uint8Array, string]>;
|
2020-06-03 13:44:51 +00:00
|
|
|
|
2021-07-26 12:08:13 +00:00
|
|
|
const testsetInvalid = [
|
|
|
|
"Pj8/ZD+Dnw",
|
|
|
|
"PDw/Pz8+Pg",
|
|
|
|
"Pj8/ZD+Dnw==",
|
|
|
|
"PDw/Pz8+Pg==",
|
|
|
|
];
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("encodeBase64Url() encodes string", () => {
|
2021-10-07 20:51:43 +00:00
|
|
|
for (const [input, output] of testsetString) {
|
2024-02-24 20:24:08 +00:00
|
|
|
assertEquals(encodeBase64Url(input!), output);
|
2021-10-07 20:51:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("encodeBase64Url() encodes binary", () => {
|
2020-06-03 13:44:51 +00:00
|
|
|
for (const [input, output] of testsetBinary) {
|
2023-09-21 09:29:13 +00:00
|
|
|
assertEquals(encodeBase64Url(input), output);
|
2020-06-03 13:44:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("decodeBase64Url() decodes binary", () => {
|
2020-06-03 13:44:51 +00:00
|
|
|
for (const [input, output] of testsetBinary) {
|
2023-09-21 09:29:13 +00:00
|
|
|
assertEquals(decodeBase64Url(output), input);
|
2020-06-03 13:44:51 +00:00
|
|
|
}
|
|
|
|
});
|
2021-07-26 12:08:13 +00:00
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("decodeBase64Url() throws on invalid input", () => {
|
2021-07-26 12:08:13 +00:00
|
|
|
for (const invalidb64url of testsetInvalid) {
|
|
|
|
assertThrows(
|
2023-09-21 09:29:13 +00:00
|
|
|
() => decodeBase64Url(invalidb64url),
|
2021-07-26 12:08:13 +00:00
|
|
|
TypeError,
|
|
|
|
"invalid character",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2023-12-17 20:16:38 +00:00
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("decodeBase64Url() throws on illegal base64url string", () => {
|
2023-12-17 20:16:38 +00:00
|
|
|
const testsetIllegalBase64url = [
|
|
|
|
"w58De",
|
|
|
|
"Zm9vYmFyy",
|
|
|
|
"DPj8-ZD_DnwEg",
|
|
|
|
"SGVsbG8gV29ybGQ-_",
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const illegalBase64url of testsetIllegalBase64url) {
|
|
|
|
assertThrows(
|
|
|
|
() => decodeBase64Url(illegalBase64url),
|
|
|
|
TypeError,
|
2024-08-23 03:32:15 +00:00
|
|
|
"Illegal base64url string",
|
2023-12-17 20:16:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|