Skip to content

Commit 6c78a4e

Browse files
committed
test: more encoding mistakes tests
1 parent d9b2fed commit 6c78a4e

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

tests/encoding/mistakes.test.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ describe('Common implementation mistakes', () => {
121121

122122
describe('specific encodings', () => {
123123
describe('utf-16le', () => {
124+
// https://github.com/facebook/hermes/pull/1855#issuecomment-3639872455
124125
test('does not produce more chars than truncated', (t) => {
125126
const d = new TextDecoder('utf-16le')
126127
t.assert.strictEqual(d.decode(u(0, 0, 0)), '\0\uFFFD') // two character, 0 was valid
127128
t.assert.strictEqual(d.decode(u(42, 0, 0)), '*\uFFFD') // two characters, * was valid
128129
t.assert.strictEqual(d.decode(u(0, 0xd8, 0)), '\uFFFD') // single character
130+
t.assert.strictEqual(d.decode(u(0, 0xd8, 0xd8)), '\uFFFD') // single character
129131
})
130132
})
131133

@@ -456,6 +458,15 @@ describe('Common implementation mistakes', () => {
456458

457459
check([0xef], { stream: true }, '')
458460
check([0xbb, 0xbf, 42, 43], {}, '*+') // close
461+
462+
// https://github.com/facebook/hermes/pull/1855#issuecomment-3633217171
463+
{
464+
const d = new TextDecoder()
465+
t.assert.strictEqual(d.decode(u(0, 0), { stream: true }), '\0\0')
466+
t.assert.strictEqual(d.decode(u(0)), '\0')
467+
t.assert.strictEqual(d.decode(u(0xef, 0xbb), { stream: true }), '') // empty string
468+
t.assert.strictEqual(d.decode(u(0xbf)), '') // empty string
469+
}
459470
})
460471

461472
// Bun fails at this
@@ -511,6 +522,12 @@ describe('Common implementation mistakes', () => {
511522
d.decode(),
512523
]
513524
t.assert.strictEqual(chunks.join(''), str)
525+
526+
// https://github.com/facebook/hermes/pull/1855#issuecomment-3630446958
527+
const r = '\uFFFD'
528+
t.assert.strictEqual(new TextDecoder().decode(u(0xc0), { stream: true }), r)
529+
t.assert.strictEqual(new TextDecoder().decode(u(0xff), { stream: true }), r)
530+
t.assert.strictEqual(new TextDecoder().decode(u(0xed, 0xbf), { stream: true }), `${r}${r}`)
514531
})
515532

516533
// Deno and Servo are incorrect
@@ -582,6 +599,7 @@ describe('Common implementation mistakes', () => {
582599
}
583600
})
584601

602+
// https://github.com/facebook/hermes/pull/1855#issuecomment-3632349129
585603
for (const encoding of ['utf-16le', 'utf-16be']) {
586604
test(encoding, (t) => {
587605
const d = new TextDecoder(encoding, { fatal: true })
@@ -647,10 +665,16 @@ describe('Common implementation mistakes', () => {
647665
}
648666
})
649667

650-
describe('invalid labels', () => {
651-
test('non-ascii', (t) => {
668+
describe('labels', () => {
669+
test('invalid non-ascii', (t) => {
652670
const bad = ['\u212Aoi8-r', '\u212Aoi8-u', 'euc-\u212Ar']
653671
for (const label of bad) t.assert.throws(() => new TextDecoder(label))
654672
})
673+
674+
// https://github.com/facebook/hermes/pull/1855#issuecomment-3632092843
675+
test('transformed', (t) => {
676+
t.assert.strictEqual(new TextDecoder('UTF-8').encoding, 'utf-8')
677+
t.assert.strictEqual(new TextDecoder('UTF-8'.toLowerCase()).encoding, 'utf-8') // Do not remove .toLowerCase() from test
678+
})
655679
})
656680
})

0 commit comments

Comments
 (0)