diff options
author | Sakthipriyan Vairamani (thefourtheye) <thechargingvolcano@gmail.com> | 2016-10-09 19:37:59 +0530 |
---|---|---|
committer | Sakthipriyan Vairamani (thefourtheye) <thechargingvolcano@gmail.com> | 2016-10-09 19:37:59 +0530 |
commit | 7542bddddaccd2453c72a67119e7badc4f2a3933 (patch) | |
tree | ba8ff635b5c565441f9a883f0db1fcbacb467f4d /test/parallel/test-fs-options-immutable.js | |
parent | 7084b9c5a179098edc8952c3348fc66c61ddcfc6 (diff) | |
download | android-node-v8-7542bddddaccd2453c72a67119e7badc4f2a3933.tar.gz android-node-v8-7542bddddaccd2453c72a67119e7badc4f2a3933.tar.bz2 android-node-v8-7542bddddaccd2453c72a67119e7badc4f2a3933.zip |
fs: don't alter user provided `options` object
This patch makes a copy of the `options` object before the fs module
functions alter it.
PR-URL: https://github.com/nodejs/node/pull/7831
Fixes: https://github.com/nodejs/node/pull/7655
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Nicu Micleușanu <micnic90@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
Diffstat (limited to 'test/parallel/test-fs-options-immutable.js')
-rw-r--r-- | test/parallel/test-fs-options-immutable.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/test/parallel/test-fs-options-immutable.js b/test/parallel/test-fs-options-immutable.js new file mode 100644 index 0000000000..47796a94a5 --- /dev/null +++ b/test/parallel/test-fs-options-immutable.js @@ -0,0 +1,98 @@ +'use strict'; + +/* + * These tests make sure that the `options` object passed to these functions are + * never altered. + * + * Refer: https://github.com/nodejs/node/issues/7655 + */ + +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); +const fs = require('fs'); +const errHandler = (e) => assert.ifError(e); +const options = Object.freeze({}); +common.refreshTmpDir(); + +{ + assert.doesNotThrow(() => + fs.readFile(__filename, options, common.mustCall(errHandler)) + ); + assert.doesNotThrow(() => fs.readFileSync(__filename, options)); +} + +{ + assert.doesNotThrow(() => + fs.readdir(__dirname, options, common.mustCall(errHandler)) + ); + assert.doesNotThrow(() => fs.readdirSync(__dirname, options)); +} + +{ + const sourceFile = path.resolve(common.tmpDir, 'test-readlink'); + const linkFile = path.resolve(common.tmpDir, 'test-readlink-link'); + + fs.writeFileSync(sourceFile, ''); + fs.symlinkSync(sourceFile, linkFile); + + assert.doesNotThrow(() => + fs.readlink(linkFile, options, common.mustCall(errHandler)) + ); + assert.doesNotThrow(() => fs.readlinkSync(linkFile, options)); +} + +{ + const fileName = path.resolve(common.tmpDir, 'writeFile'); + assert.doesNotThrow(() => fs.writeFileSync(fileName, 'ABCD', options)); + assert.doesNotThrow(() => + fs.writeFile(fileName, 'ABCD', options, common.mustCall(errHandler)) + ); +} + +{ + const fileName = path.resolve(common.tmpDir, 'appendFile'); + assert.doesNotThrow(() => fs.appendFileSync(fileName, 'ABCD', options)); + assert.doesNotThrow(() => + fs.appendFile(fileName, 'ABCD', options, common.mustCall(errHandler)) + ); +} + +if (!common.isAix) { + // TODO(thefourtheye) Remove this guard once + // https://github.com/nodejs/node/issues/5085 is fixed + { + let watch; + assert.doesNotThrow(() => watch = fs.watch(__filename, options, () => {})); + watch.close(); + } + + { + assert.doesNotThrow(() => fs.watchFile(__filename, options, () => {})); + fs.unwatchFile(__filename); + } +} + +{ + assert.doesNotThrow(() => fs.realpathSync(__filename, options)); + assert.doesNotThrow(() => + fs.realpath(__filename, options, common.mustCall(errHandler)) + ); +} + +{ + const tempFileName = path.resolve(common.tmpDir, 'mkdtemp-'); + assert.doesNotThrow(() => fs.mkdtempSync(tempFileName, options)); + assert.doesNotThrow(() => + fs.mkdtemp(tempFileName, options, common.mustCall(errHandler)) + ); +} + +{ + const fileName = path.resolve(common.tmpDir, 'streams'); + assert.doesNotThrow(() => { + fs.WriteStream(fileName, options).once('open', () => { + assert.doesNotThrow(() => fs.ReadStream(fileName, options)); + }); + }); +} |