2020-05-24 09:10:01 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
|
|
|
import { encode, decode, decodeString } from "./base64.ts";
|
|
|
|
|
|
|
|
const testsetString = [
|
|
|
|
["", ""],
|
|
|
|
["f", "Zg=="],
|
|
|
|
["fo", "Zm8="],
|
|
|
|
["foo", "Zm9v"],
|
|
|
|
["foob", "Zm9vYg=="],
|
|
|
|
["fooba", "Zm9vYmE="],
|
|
|
|
["foobar", "Zm9vYmFy"],
|
|
|
|
];
|
|
|
|
|
|
|
|
const testsetBinary = [
|
|
|
|
[new TextEncoder().encode("\x00"), "AA=="],
|
|
|
|
[new TextEncoder().encode("\x00\x00"), "AAA="],
|
|
|
|
[new TextEncoder().encode("\x00\x00\x00"), "AAAA"],
|
|
|
|
[new TextEncoder().encode("\x00\x00\x00\x00"), "AAAAAA=="],
|
|
|
|
];
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("[encoding/base64] testBase64EncodeString", () => {
|
2020-05-24 09:10:01 -04:00
|
|
|
for (const [input, output] of testsetString) {
|
|
|
|
assertEquals(encode(input), output);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("[encoding/base64] testBase64DecodeString", () => {
|
2020-05-24 09:10:01 -04:00
|
|
|
for (const [input, output] of testsetString) {
|
|
|
|
assertEquals(decodeString(output), input);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("[encoding/base64] testBase64EncodeBinary", () => {
|
2020-05-24 09:10:01 -04:00
|
|
|
for (const [input, output] of testsetBinary) {
|
|
|
|
assertEquals(encode(input), output);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("[encoding/base64] testBase64DecodeBinary", () => {
|
2020-05-24 09:10:01 -04:00
|
|
|
for (const [input, output] of testsetBinary) {
|
|
|
|
const outputBinary = new Uint8Array(decode(output as string));
|
|
|
|
assertEquals(outputBinary, input as Uint8Array);
|
|
|
|
}
|
|
|
|
});
|