1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const fs = require('fs'); 26 27function check(async, sync) { 28 const argsSync = Array.prototype.slice.call(arguments, 2); 29 const argsAsync = argsSync.concat(common.mustNotCall()); 30 31 if (sync) { 32 assert.throws( 33 () => { 34 sync.apply(null, argsSync); 35 }, 36 { 37 code: 'ERR_INVALID_ARG_VALUE', 38 name: 'TypeError', 39 }); 40 } 41 42 if (async) { 43 assert.throws( 44 () => { 45 async.apply(null, argsAsync); 46 }, 47 { 48 code: 'ERR_INVALID_ARG_VALUE', 49 name: 'TypeError' 50 }); 51 } 52} 53 54check(fs.access, fs.accessSync, 'foo\u0000bar'); 55check(fs.access, fs.accessSync, 'foo\u0000bar', fs.F_OK); 56check(fs.appendFile, fs.appendFileSync, 'foo\u0000bar', 'abc'); 57check(fs.chmod, fs.chmodSync, 'foo\u0000bar', '0644'); 58check(fs.chown, fs.chownSync, 'foo\u0000bar', 12, 34); 59check(fs.copyFile, fs.copyFileSync, 'foo\u0000bar', 'abc'); 60check(fs.copyFile, fs.copyFileSync, 'abc', 'foo\u0000bar'); 61check(fs.lchown, fs.lchownSync, 'foo\u0000bar', 12, 34); 62check(fs.link, fs.linkSync, 'foo\u0000bar', 'foobar'); 63check(fs.link, fs.linkSync, 'foobar', 'foo\u0000bar'); 64check(fs.lstat, fs.lstatSync, 'foo\u0000bar'); 65check(fs.mkdir, fs.mkdirSync, 'foo\u0000bar', '0755'); 66check(fs.open, fs.openSync, 'foo\u0000bar', 'r'); 67check(fs.readFile, fs.readFileSync, 'foo\u0000bar'); 68check(fs.readdir, fs.readdirSync, 'foo\u0000bar'); 69check(fs.readlink, fs.readlinkSync, 'foo\u0000bar'); 70check(fs.realpath, fs.realpathSync, 'foo\u0000bar'); 71check(fs.rename, fs.renameSync, 'foo\u0000bar', 'foobar'); 72check(fs.rename, fs.renameSync, 'foobar', 'foo\u0000bar'); 73check(fs.rmdir, fs.rmdirSync, 'foo\u0000bar'); 74check(fs.stat, fs.statSync, 'foo\u0000bar'); 75check(fs.symlink, fs.symlinkSync, 'foo\u0000bar', 'foobar'); 76check(fs.symlink, fs.symlinkSync, 'foobar', 'foo\u0000bar'); 77check(fs.truncate, fs.truncateSync, 'foo\u0000bar'); 78check(fs.unlink, fs.unlinkSync, 'foo\u0000bar'); 79check(null, fs.unwatchFile, 'foo\u0000bar', common.mustNotCall()); 80check(fs.utimes, fs.utimesSync, 'foo\u0000bar', 0, 0); 81check(null, fs.watch, 'foo\u0000bar', common.mustNotCall()); 82check(null, fs.watchFile, 'foo\u0000bar', common.mustNotCall()); 83check(fs.writeFile, fs.writeFileSync, 'foo\u0000bar', 'abc'); 84 85const fileUrl = new URL('file:///C:/foo\u0000bar'); 86const fileUrl2 = new URL('file:///C:/foo%00bar'); 87 88check(fs.access, fs.accessSync, fileUrl); 89check(fs.access, fs.accessSync, fileUrl, fs.F_OK); 90check(fs.appendFile, fs.appendFileSync, fileUrl, 'abc'); 91check(fs.chmod, fs.chmodSync, fileUrl, '0644'); 92check(fs.chown, fs.chownSync, fileUrl, 12, 34); 93check(fs.copyFile, fs.copyFileSync, fileUrl, 'abc'); 94check(fs.copyFile, fs.copyFileSync, 'abc', fileUrl); 95check(fs.lchown, fs.lchownSync, fileUrl, 12, 34); 96check(fs.link, fs.linkSync, fileUrl, 'foobar'); 97check(fs.link, fs.linkSync, 'foobar', fileUrl); 98check(fs.lstat, fs.lstatSync, fileUrl); 99check(fs.mkdir, fs.mkdirSync, fileUrl, '0755'); 100check(fs.open, fs.openSync, fileUrl, 'r'); 101check(fs.readFile, fs.readFileSync, fileUrl); 102check(fs.readdir, fs.readdirSync, fileUrl); 103check(fs.readlink, fs.readlinkSync, fileUrl); 104check(fs.realpath, fs.realpathSync, fileUrl); 105check(fs.rename, fs.renameSync, fileUrl, 'foobar'); 106check(fs.rename, fs.renameSync, 'foobar', fileUrl); 107check(fs.rmdir, fs.rmdirSync, fileUrl); 108check(fs.stat, fs.statSync, fileUrl); 109check(fs.symlink, fs.symlinkSync, fileUrl, 'foobar'); 110check(fs.symlink, fs.symlinkSync, 'foobar', fileUrl); 111check(fs.truncate, fs.truncateSync, fileUrl); 112check(fs.unlink, fs.unlinkSync, fileUrl); 113check(null, fs.unwatchFile, fileUrl, assert.fail); 114check(fs.utimes, fs.utimesSync, fileUrl, 0, 0); 115check(null, fs.watch, fileUrl, assert.fail); 116check(null, fs.watchFile, fileUrl, assert.fail); 117check(fs.writeFile, fs.writeFileSync, fileUrl, 'abc'); 118 119check(fs.access, fs.accessSync, fileUrl2); 120check(fs.access, fs.accessSync, fileUrl2, fs.F_OK); 121check(fs.appendFile, fs.appendFileSync, fileUrl2, 'abc'); 122check(fs.chmod, fs.chmodSync, fileUrl2, '0644'); 123check(fs.chown, fs.chownSync, fileUrl2, 12, 34); 124check(fs.copyFile, fs.copyFileSync, fileUrl2, 'abc'); 125check(fs.copyFile, fs.copyFileSync, 'abc', fileUrl2); 126check(fs.lchown, fs.lchownSync, fileUrl2, 12, 34); 127check(fs.link, fs.linkSync, fileUrl2, 'foobar'); 128check(fs.link, fs.linkSync, 'foobar', fileUrl2); 129check(fs.lstat, fs.lstatSync, fileUrl2); 130check(fs.mkdir, fs.mkdirSync, fileUrl2, '0755'); 131check(fs.open, fs.openSync, fileUrl2, 'r'); 132check(fs.readFile, fs.readFileSync, fileUrl2); 133check(fs.readdir, fs.readdirSync, fileUrl2); 134check(fs.readlink, fs.readlinkSync, fileUrl2); 135check(fs.realpath, fs.realpathSync, fileUrl2); 136check(fs.rename, fs.renameSync, fileUrl2, 'foobar'); 137check(fs.rename, fs.renameSync, 'foobar', fileUrl2); 138check(fs.rmdir, fs.rmdirSync, fileUrl2); 139check(fs.stat, fs.statSync, fileUrl2); 140check(fs.symlink, fs.symlinkSync, fileUrl2, 'foobar'); 141check(fs.symlink, fs.symlinkSync, 'foobar', fileUrl2); 142check(fs.truncate, fs.truncateSync, fileUrl2); 143check(fs.unlink, fs.unlinkSync, fileUrl2); 144check(null, fs.unwatchFile, fileUrl2, assert.fail); 145check(fs.utimes, fs.utimesSync, fileUrl2, 0, 0); 146check(null, fs.watch, fileUrl2, assert.fail); 147check(null, fs.watchFile, fileUrl2, assert.fail); 148check(fs.writeFile, fs.writeFileSync, fileUrl2, 'abc'); 149 150// An 'error' for exists means that it doesn't exist. 151// One of many reasons why this file is the absolute worst. 152fs.exists('foo\u0000bar', common.mustCall((exists) => { 153 assert(!exists); 154})); 155assert(!fs.existsSync('foo\u0000bar')); 156