node/test/parallel/test-stream-iter-transform-errors.js
James M Snell 2fa9c1bc58 test: add tests for experimental stream/iter implementation
Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-By: Claude/Opus 4.6
PR-URL: https://github.com/nodejs/node/pull/62066
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2026-03-27 19:55:17 -07:00

71 lines
1.9 KiB
JavaScript

// Flags: --experimental-stream-iter
'use strict';
const common = require('../common');
const assert = require('assert');
const {
from,
pull,
bytes,
} = require('stream/iter');
const {
decompressGzip,
decompressDeflate,
decompressBrotli,
decompressZstd,
} = require('zlib/iter');
// =============================================================================
// Decompression of corrupt data
// =============================================================================
async function testCorruptGzipData() {
const corrupt = new Uint8Array([0x1F, 0x8B, 0xFF, 0xFF, 0xFF]);
await assert.rejects(
async () => await bytes(pull(from(corrupt), decompressGzip())), {
name: 'Error',
code: 'Z_DATA_ERROR',
});
}
async function testCorruptDeflateData() {
const corrupt = new Uint8Array([0x78, 0xFF, 0xFF, 0xFF]);
await assert.rejects(
async () => await bytes(pull(from(corrupt), decompressDeflate())), {
name: 'Error',
code: 'Z_DATA_ERROR',
});
}
async function testCorruptBrotliData() {
const corrupt = new Uint8Array([0xFF, 0xFF, 0xFF, 0xFF]);
await assert.rejects(
async () => await bytes(pull(from(corrupt), decompressBrotli())), {
name: 'Error',
code: 'ERR__ERROR_FORMAT_PADDING_2',
});
}
async function testCorruptZstdData() {
// Completely invalid data (not even valid magic bytes)
const corrupt = new Uint8Array([0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
await assert.rejects(
async () => await bytes(pull(from(corrupt), decompressZstd())), {
name: 'Error',
code: 'ZSTD_error_prefix_unknown',
});
}
// =============================================================================
// Run all tests
// =============================================================================
(async () => {
await testCorruptGzipData();
await testCorruptDeflateData();
await testCorruptBrotliData();
await testCorruptZstdData();
})().then(common.mustCall());