Commit 822e8ef3ce3 for nodejs
commit 822e8ef3ce3f234d7d41f104360b26e32cfb0805
Author: Filip Skokan <panva.ip@gmail.com>
Date: Wed Sep 23 19:00:48 2026 +0200
test: fix fs write encoding coverage
Pass an encoding rather than a string length to FileHandle.write.
Cover invalid encodings in fs.write, fs.writeSync, and FileHandle.write,
along with valid encodings and unchanged files after rejected writes.
Rejecting previously accepted invalid encodings is an intentional
semver-major bug fix shared with the crypto validation changes.
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
Assisted-by: Codex
PR-URL: https://github.com/nodejs/node/pull/66247
Fixes: https://github.com/nodejs/node/issues/45189
Refs: https://github.com/nodejs/node/pull/45990
Reviewed-By: Xuguang Mei <meixuguang@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/test/parallel/test-fs-promises-file-handle-write.js b/test/parallel/test-fs-promises-file-handle-write.js
index 7f3d12d4817..efa477dc501 100644
--- a/test/parallel/test-fs-promises-file-handle-write.js
+++ b/test/parallel/test-fs-promises-file-handle-write.js
@@ -41,11 +41,11 @@ async function validateEmptyWrite() {
async function validateNonUint8ArrayWrite() {
const filePathForHandle = path.resolve(tmpDir, 'tmp-data-write.txt');
const fileHandle = await open(filePathForHandle, 'w+');
- const buffer = Buffer.from('Hello world', 'utf8').toString('base64');
+ const string = Buffer.from('Hello world', 'utf8').toString('base64');
- await fileHandle.write(buffer, 0, buffer.length);
+ await fileHandle.write(string, 0, 'utf8');
const readFileData = fs.readFileSync(filePathForHandle);
- assert.deepStrictEqual(Buffer.from(buffer, 'utf8'), readFileData);
+ assert.deepStrictEqual(Buffer.from(string, 'utf8'), readFileData);
await fileHandle.close();
}
diff --git a/test/parallel/test-fs-write-invalid-encoding.js b/test/parallel/test-fs-write-invalid-encoding.js
new file mode 100644
index 00000000000..64142a57cc2
--- /dev/null
+++ b/test/parallel/test-fs-write-invalid-encoding.js
@@ -0,0 +1,56 @@
+'use strict';
+
+const common = require('../common');
+const assert = require('assert');
+const fs = require('fs');
+const path = require('path');
+const tmpdir = require('../common/tmpdir');
+
+tmpdir.refresh();
+
+async function main() {
+ const file = path.join(tmpdir.path, 'write-invalid-encoding');
+ const handle = await fs.promises.open(file, 'w+');
+ try {
+ for (const encoding of ['bad', 16]) {
+ const error = {
+ code: 'ERR_UNKNOWN_ENCODING',
+ message: `Unknown encoding: ${encoding}`,
+ };
+ assert.throws(() => fs.writeSync(handle.fd, '6162', 0, encoding), error);
+ assert.throws(
+ () => fs.write(handle.fd, '6162', 0, encoding, common.mustNotCall()),
+ error);
+ await assert.rejects(handle.write('6162', 0, encoding), error);
+ }
+ // Rejected writes must not change the file.
+ assert.strictEqual((await handle.stat()).size, 0);
+
+ for (const encoding of [undefined, null, '', 'utf8', 'UTF-8', 'hex']) {
+ const expected = encoding === 'hex' ? 'ab' : '6162';
+ await handle.truncate(0);
+ assert.strictEqual(fs.writeSync(handle.fd, '6162', 0, encoding),
+ expected.length);
+ assert.strictEqual(fs.readFileSync(file, 'utf8'), expected);
+
+ await handle.truncate(0);
+ await new Promise((resolve, reject) => {
+ fs.write(handle.fd, '6162', 0, encoding, common.mustCall((err, written) => {
+ if (err) return reject(err);
+ assert.strictEqual(written, expected.length);
+ resolve();
+ }));
+ });
+ assert.strictEqual(fs.readFileSync(file, 'utf8'), expected);
+
+ await handle.truncate(0);
+ const { bytesWritten } = await handle.write('6162', 0, encoding);
+ assert.strictEqual(bytesWritten, expected.length);
+ assert.strictEqual(fs.readFileSync(file, 'utf8'), expected);
+ }
+ } finally {
+ await handle.close();
+ }
+}
+
+main().then(common.mustCall());