Skip to content

Commit f3d4f2a

Browse files
committed
string_decoder: Some more tests for invalid input
I thought of this while looking at nodejs#7310.
1 parent 52462c6 commit f3d4f2a

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

test/parallel/test-string-decoder.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ test(
2828
'\u02e4\u0064\u12e4\u0030\u3045'
2929
);
3030

31+
function testBits(bits, expected, singleSequence) {
32+
let buf = Buffer.from(bits.split(' ').map(b => parseInt(b, 2)));
33+
let exp = buf.toString('utf-8');
34+
if (expected) assert.strictEqual(exp, expected);
35+
test('utf-8', buf, exp, singleSequence);
36+
}
37+
testBits('10111000 01000001', '\ufffdA');
38+
//fails: testBits('11101100 01000001', '\ufffdA');
39+
testBits('11101100 10111000 01000001', '\ufffd\ufffdA'); // why two U+FFFD?
40+
testBits('11000001 10000001 01000001', '\ufffd\ufffdA');
41+
test('utf-8', Buffer.from('F09D908D', 'hex'), '\ud835\udc0d');
42+
//CESU-8 not supported: test('utf-8', Buffer.from('EDA0B5EDB08D', 'hex'), '\ud835\udc0d');
43+
3144
// UCS-2
3245
test('ucs2', Buffer.from('ababc', 'ucs2'), 'ababc');
3346

@@ -98,6 +111,7 @@ function test(encoding, input, expected, singleSequence) {
98111
sequence.forEach(function(write) {
99112
output += decoder.write(input.slice(write[0], write[1]));
100113
});
114+
output += decoder.end();
101115
process.stdout.write('.');
102116
if (output !== expected) {
103117
var message =

0 commit comments

Comments
 (0)