From 75c69afe4eb62554dd475d1c94a2e92512e91b47 Mon Sep 17 00:00:00 2001 From: git-srinivas Date: Sat, 23 Oct 2021 08:14:40 +0530 Subject: [PATCH 1/2] test: add tests for invalid UTF-8 Verify that `Blob.prototype.text()`, `streamConsumers.text()` and `TextDecoder.prototype.decode()` work as expected with invalid UTF-8. Fixes: https://github.com/nodejs/node/issues/39804 --- test/parallel/test-blob.js | 9 +++++++++ test/parallel/test-stream-consumers.js | 14 ++++++++++++++ .../test-whatwg-encoding-custom-textdecoder.js | 8 ++++++++ 3 files changed, 31 insertions(+) diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 2ac91ac99c9bcc..39301e85c9c9b6 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -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( [ diff --git a/test/parallel/test-stream-consumers.js b/test/parallel/test-stream-consumers.js index 1a3b6ce00200d4..aba1864565d61a 100644 --- a/test/parallel/test-stream-consumers.js +++ b/test/parallel/test-stream-consumers.js @@ -13,6 +13,7 @@ const { } = require('stream/consumers'); const { + Readable, PassThrough } = require('stream'); @@ -73,6 +74,19 @@ const kArrayBuffer = setTimeout(() => passthrough.end('there'), 10); } +{ + const readable = new Readable({ + read() {} + }); + + text(readable).then((data) => { + assert.deepStrictEqual(data, 'foo\ufffd\ufffd\ufffd'); + }); + + readable.push(new Uint8Array([0x66, 0x6f, 0x6f, 0xed, 0xa0, 0x80])); + readable.push(null); +} + { const passthrough = new PassThrough(); diff --git a/test/parallel/test-whatwg-encoding-custom-textdecoder.js b/test/parallel/test-whatwg-encoding-custom-textdecoder.js index 877cd43734451b..1fa65164c70678 100644 --- a/test/parallel/test-whatwg-encoding-custom-textdecoder.js +++ b/test/parallel/test-whatwg-encoding-custom-textdecoder.js @@ -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'); +} From b99d501cf63f835e8e7a325574636b215c03b0e8 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sat, 6 Nov 2021 07:04:15 +0100 Subject: [PATCH 2/2] fixup! assert.deepStrictEqual() -> assert.strictEqual() --- test/parallel/test-stream-consumers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-stream-consumers.js b/test/parallel/test-stream-consumers.js index aba1864565d61a..bdcc6fc6ff1b21 100644 --- a/test/parallel/test-stream-consumers.js +++ b/test/parallel/test-stream-consumers.js @@ -80,7 +80,7 @@ const kArrayBuffer = }); text(readable).then((data) => { - assert.deepStrictEqual(data, 'foo\ufffd\ufffd\ufffd'); + assert.strictEqual(data, 'foo\ufffd\ufffd\ufffd'); }); readable.push(new Uint8Array([0x66, 0x6f, 0x6f, 0xed, 0xa0, 0x80]));