node/test/parallel/test-zlib-zstd-pledged-src-size.js
Jan Krems bf12d72faa
Some checks are pending
Coverage Linux (without intl) / coverage-linux-without-intl (push) Waiting to run
Coverage Linux / coverage-linux (push) Waiting to run
Coverage Windows / coverage-windows (push) Waiting to run
Test and upload documentation to artifacts / build-docs (push) Waiting to run
Linters / lint-addon-docs (push) Waiting to run
Linters / lint-cpp (push) Waiting to run
Linters / format-cpp (push) Waiting to run
Linters / lint-js-and-md (push) Waiting to run
Linters / lint-py (push) Waiting to run
Linters / lint-yaml (push) Waiting to run
Linters / lint-sh (push) Waiting to run
Linters / lint-codeowners (push) Waiting to run
Linters / lint-pr-url (push) Waiting to run
Linters / lint-readme (push) Waiting to run
Notify on Push / Notify on Force Push on `main` (push) Waiting to run
Notify on Push / Notify on Push on `main` that lacks metadata (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
zlib: add zstd support
Fixes: https://github.com/nodejs/node/issues/48412
PR-URL: https://github.com/nodejs/node/pull/52100
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-02-08 12:43:53 +00:00

38 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');
function compressWithPledgedSrcSize({ pledgedSrcSize, actualSrcSize }) {
return new Promise((resolve, reject) => {
const compressor = zlib.createZstdCompress({ pledgedSrcSize });
compressor.on('error', (e) => {
reject(e);
});
compressor.on('end', resolve);
compressor.write('x'.repeat(actualSrcSize), () => {
compressor.end();
compressor.resume();
});
}).then(() => {
// Compression should only succeed if sizes match
assert.strictEqual(pledgedSrcSize, actualSrcSize);
}, (error) => {
assert.strictEqual(error.code, 'ZSTD_error_srcSize_wrong');
// Size error should only happen when sizes do not match
assert.notStrictEqual(pledgedSrcSize, actualSrcSize);
}).then(common.mustCall());
}
compressWithPledgedSrcSize({ pledgedSrcSize: 0, actualSrcSize: 0 });
compressWithPledgedSrcSize({ pledgedSrcSize: 0, actualSrcSize: 42 });
compressWithPledgedSrcSize({ pledgedSrcSize: 13, actualSrcSize: 42 });
compressWithPledgedSrcSize({ pledgedSrcSize: 42, actualSrcSize: 0 });
compressWithPledgedSrcSize({ pledgedSrcSize: 42, actualSrcSize: 13 });
compressWithPledgedSrcSize({ pledgedSrcSize: 42, actualSrcSize: 42 });