Skip to content

Commit

Permalink
test: add tests for invalid UTF-8
Browse files Browse the repository at this point in the history
Verify that `Blob.prototype.text()`, `streamConsumers.text()` and
`TextDecoder.prototype.decode()` work as expected with invalid UTF-8.

PR-URL: #40351
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
  • Loading branch information
git-srinivas authored and danielleadams committed Feb 1, 2022
1 parent e60841b commit c002930
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ assert.throws(() => new Blob({}), {
}));
}

{
const b = new Blob(['hello', new Uint8Array([0xed, 0xa0, 0x88])]);
assert.strictEqual(b.size, 8);
b.text().then(common.mustCall((text) => {
assert.strictEqual(text, 'hello\ufffd\ufffd\ufffd');
assert.strictEqual(text.length, 8);
}));
}

{
const b = new Blob(
[
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-stream-consumers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
} = require('stream/consumers');

const {
Readable,
PassThrough
} = require('stream');

Expand Down Expand Up @@ -73,6 +74,19 @@ const kArrayBuffer =
setTimeout(() => passthrough.end('there'), 10);
}

{
const readable = new Readable({
read() {}
});

text(readable).then((data) => {
assert.strictEqual(data, 'foo\ufffd\ufffd\ufffd');
});

readable.push(new Uint8Array([0x66, 0x6f, 0x6f, 0xed, 0xa0, 0x80]));
readable.push(null);
}

{
const passthrough = new PassThrough();

Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,11 @@ if (common.hasIntl) {
}
);
}

// Test TextDecoder for incomplete UTF-8 byte sequence.
{
const decoder = new TextDecoder();
const chunk = new Uint8Array([0x66, 0x6f, 0x6f, 0xed]);
const str = decoder.decode(chunk);
assert.strictEqual(str, 'foo\ufffd');
}

0 comments on commit c002930

Please sign in to comment.