2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-07-14 14:30:03 -04:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2020-11-09 09:25:13 -05:00
|
|
|
import { Buffer } from "./buffer.ts";
|
2020-07-14 14:30:03 -04:00
|
|
|
import { StringDecoder } from "./string_decoder.ts";
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "String decoder is encoding utf8 correctly",
|
|
|
|
fn() {
|
|
|
|
let decoder;
|
|
|
|
|
|
|
|
decoder = new StringDecoder("utf8");
|
|
|
|
assertEquals(decoder.write(Buffer.from("E1", "hex")), "");
|
|
|
|
assertEquals(decoder.end(), "\ufffd");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("utf8");
|
|
|
|
assertEquals(decoder.write(Buffer.from("E18B", "hex")), "");
|
|
|
|
assertEquals(decoder.end(), "\ufffd");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("utf8");
|
|
|
|
assertEquals(decoder.write(Buffer.from("\ufffd")), "\ufffd");
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("utf8");
|
|
|
|
assertEquals(
|
|
|
|
decoder.write(Buffer.from("\ufffd\ufffd\ufffd")),
|
2020-07-14 15:24:17 -04:00
|
|
|
"\ufffd\ufffd\ufffd",
|
2020-07-14 14:30:03 -04:00
|
|
|
);
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("utf8");
|
|
|
|
assertEquals(decoder.write(Buffer.from("EFBFBDE2", "hex")), "\ufffd");
|
|
|
|
assertEquals(decoder.end(), "\ufffd");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("utf8");
|
|
|
|
assertEquals(decoder.write(Buffer.from("F1", "hex")), "");
|
|
|
|
assertEquals(decoder.write(Buffer.from("41F2", "hex")), "\ufffdA");
|
|
|
|
assertEquals(decoder.end(), "\ufffd");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("utf8");
|
|
|
|
assertEquals(decoder.text(Buffer.from([0x41]), 2), "");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "String decoder is encoding base64 correctly",
|
|
|
|
fn() {
|
|
|
|
let decoder;
|
|
|
|
|
|
|
|
decoder = new StringDecoder("base64");
|
|
|
|
assertEquals(decoder.write(Buffer.from("E1", "hex")), "4Q==");
|
|
|
|
assertEquals(decoder.end(), "4QAA");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("base64");
|
|
|
|
assertEquals(decoder.write(Buffer.from("E18B", "hex")), "4Ys=");
|
|
|
|
assertEquals(decoder.end(), "4YsA");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("base64");
|
|
|
|
assertEquals(decoder.write(Buffer.from("\ufffd")), "77+9");
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("base64");
|
|
|
|
assertEquals(
|
|
|
|
decoder.write(Buffer.from("\ufffd\ufffd\ufffd")),
|
2020-07-14 15:24:17 -04:00
|
|
|
"77+977+977+9",
|
2020-07-14 14:30:03 -04:00
|
|
|
);
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("base64");
|
|
|
|
assertEquals(decoder.write(Buffer.from("EFBFBDE2", "hex")), "77+94g==");
|
|
|
|
assertEquals(decoder.end(), "4gAA");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("base64");
|
|
|
|
assertEquals(decoder.write(Buffer.from("F1", "hex")), "8Q==");
|
|
|
|
assertEquals(decoder.write(Buffer.from("41F2", "hex")), "8UHy");
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("base64");
|
|
|
|
assertEquals(decoder.text(Buffer.from([0x41]), 2), "QQ==");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "String decoder is encoding hex correctly",
|
|
|
|
fn() {
|
|
|
|
let decoder;
|
|
|
|
|
|
|
|
decoder = new StringDecoder("hex");
|
|
|
|
assertEquals(decoder.write(Buffer.from("E1", "hex")), "e1");
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("hex");
|
|
|
|
assertEquals(decoder.write(Buffer.from("E18B", "hex")), "e18b");
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("hex");
|
|
|
|
assertEquals(decoder.write(Buffer.from("\ufffd")), "efbfbd");
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("hex");
|
|
|
|
assertEquals(
|
|
|
|
decoder.write(Buffer.from("\ufffd\ufffd\ufffd")),
|
2020-07-14 15:24:17 -04:00
|
|
|
"efbfbdefbfbdefbfbd",
|
2020-07-14 14:30:03 -04:00
|
|
|
);
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("hex");
|
|
|
|
assertEquals(decoder.write(Buffer.from("EFBFBDE2", "hex")), "efbfbde2");
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("hex");
|
|
|
|
assertEquals(decoder.write(Buffer.from("F1", "hex")), "f1");
|
|
|
|
assertEquals(decoder.write(Buffer.from("41F2", "hex")), "41f2");
|
|
|
|
assertEquals(decoder.end(), "");
|
|
|
|
|
|
|
|
decoder = new StringDecoder("hex");
|
|
|
|
assertEquals(decoder.text(Buffer.from([0x41]), 2), "");
|
|
|
|
},
|
|
|
|
});
|