Skip to content

Commit 8b88004

Browse files
committed
test: minor work on fatal streams
1 parent ebbd2ba commit 8b88004

1 file changed

Lines changed: 29 additions & 20 deletions

File tree

tests/encoding/stream.test.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const seeds = []
1212
for (let i = 1; i <= 128; i++) seeds.push(fixedPRG.randomBytes(23 * i))
1313
const rand = () => fixedPRG.randomBytes(1)[0] / 256 // or Math.random()
1414

15-
function streamTest(t, label, testFatal) {
15+
const isConsistenFatalEncoding = (label) => legacySingleByte.includes(label) || label === 'utf-8' // see explanation below
16+
17+
function streamChecks(t, label, testFatal) {
1618
for (const u8i of seeds) {
1719
const u8 = Uint8Array.from(u8i)
1820
const loose = new TextDecoder(label)
@@ -41,12 +43,15 @@ function streamTest(t, label, testFatal) {
4143
// Can differ on multi-byte as the previous chunk might have caused a second error in next one
4244
// In fatal mode the queue from the previous invalid chunk is ignored, while in replacement it causes another error
4345
// It can also go the other way around: previous state causing next input to be valid for replacement but not for fatal
44-
// Continiuing streaming after fatal error can also completely switch the behavior for e.g. utf-32
45-
if (legacySingleByte.includes(label) || (label === 'utf-8' && previousValidBytes >= 3)) {
46-
if (ok) {
47-
t.assert.strictEqual(b, a)
48-
} else {
49-
t.assert.ok(a.includes('\uFFFD'))
46+
// Continuing streaming after fatal error can also completely switch the behavior for e.g. utf-16
47+
if (isConsistenFatalEncoding(label)) {
48+
if (ok) t.assert.ok(a.includes(b))
49+
if (legacySingleByte.includes(label) || (label === 'utf-8' && previousValidBytes >= 3)) {
50+
if (ok) {
51+
t.assert.strictEqual(b, a)
52+
} else {
53+
t.assert.ok(a.includes('\uFFFD'))
54+
}
5055
}
5156
}
5257
}
@@ -62,11 +67,14 @@ function streamTest(t, label, testFatal) {
6267
okf = true
6368
} catch {}
6469

65-
if (legacySingleByte.includes(label) || (label === 'utf-8' && continiousValidBytes >= 3)) {
66-
if (okf) {
67-
t.assert.strictEqual(bf, af)
68-
} else {
69-
t.assert.ok(af.includes('\uFFFD'))
70+
if (isConsistenFatalEncoding(label)) {
71+
if (okf) t.assert.ok(af.includes(bf))
72+
if (legacySingleByte.includes(label) || (label === 'utf-8' && continiousValidBytes >= 3)) {
73+
if (okf) {
74+
t.assert.strictEqual(bf, af)
75+
} else {
76+
t.assert.ok(af.includes('\uFFFD'))
77+
}
7078
}
7179
}
7280
}
@@ -78,31 +86,32 @@ function streamTest(t, label, testFatal) {
7886
}
7987
}
8088

89+
function streamTest(label, fatal) {
90+
const skip = fatal && !isConsistenFatalEncoding(label) // see explanation in fatal path above
91+
test(label, { skip }, (t) => streamChecks(t, label, fatal))
92+
}
93+
8194
describe('stream=true in small chunks', () => {
8295
describe('Unicode', () => {
8396
for (const fatal of [false, true]) {
8497
describe(fatal ? 'fatal' : 'loose', () => {
85-
for (const label of unicode) {
86-
if (label !== 'utf-8' && fatal) continue // see comment above, utf-16 is hard to test here
87-
test(label, (t) => streamTest(t, label, fatal))
88-
}
98+
for (const label of unicode) streamTest(label, fatal)
8999
})
90100
}
91101
})
92102

93103
describe('1-byte encodings', () => {
94104
for (const fatal of [false, true]) {
95105
describe(fatal ? 'fatal' : 'loose', () => {
96-
for (const label of legacySingleByte) test(label, (t) => streamTest(t, label, fatal))
106+
for (const label of legacySingleByte) streamTest(label, fatal)
97107
})
98108
}
99109
})
100110

101111
describe('legacy multi-byte encodings', () => {
102-
// See comment above, fatal streaming is hard to test blindly for these
103-
for (const fatal of [false]) {
112+
for (const fatal of [false, true]) {
104113
describe(fatal ? 'fatal' : 'loose', () => {
105-
for (const label of legacyMultiByte) test(label, (t) => streamTest(t, label, fatal))
114+
for (const label of legacyMultiByte) streamTest(label, fatal)
106115
})
107116
}
108117
})

0 commit comments

Comments
 (0)