Skip to content

Commit 150c0f1

Browse files
git-srinivastargos
authored andcommitted
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. PR-URL: #40351 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
1 parent 2979c58 commit 150c0f1

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

test/parallel/test-blob.js

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ assert.throws(() => new Blob({}), {
8787
}));
8888
}
8989

90+
{
91+
const b = new Blob(['hello', new Uint8Array([0xed, 0xa0, 0x88])]);
92+
assert.strictEqual(b.size, 8);
93+
b.text().then(common.mustCall((text) => {
94+
assert.strictEqual(text, 'hello\ufffd\ufffd\ufffd');
95+
assert.strictEqual(text.length, 8);
96+
}));
97+
}
98+
9099
{
91100
const b = new Blob(
92101
[

test/parallel/test-stream-consumers.js

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
} = require('stream/consumers');
1414

1515
const {
16+
Readable,
1617
PassThrough
1718
} = require('stream');
1819

@@ -73,6 +74,19 @@ const kArrayBuffer =
7374
setTimeout(() => passthrough.end('there'), 10);
7475
}
7576

77+
{
78+
const readable = new Readable({
79+
read() {}
80+
});
81+
82+
text(readable).then((data) => {
83+
assert.strictEqual(data, 'foo\ufffd\ufffd\ufffd');
84+
});
85+
86+
readable.push(new Uint8Array([0x66, 0x6f, 0x6f, 0xed, 0xa0, 0x80]));
87+
readable.push(null);
88+
}
89+
7690
{
7791
const passthrough = new PassThrough();
7892

test/parallel/test-whatwg-encoding-custom-textdecoder.js

+8
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,11 @@ if (common.hasIntl) {
191191
}
192192
);
193193
}
194+
195+
// Test TextDecoder for incomplete UTF-8 byte sequence.
196+
{
197+
const decoder = new TextDecoder();
198+
const chunk = new Uint8Array([0x66, 0x6f, 0x6f, 0xed]);
199+
const str = decoder.decode(chunk);
200+
assert.strictEqual(str, 'foo\ufffd');
201+
}

0 commit comments

Comments
 (0)