• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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');
26const URL = require('url').URL;
27
28function check(async, sync) {
29  const argsSync = Array.prototype.slice.call(arguments, 2);
30  const argsAsync = argsSync.concat(common.mustNotCall());
31
32  if (sync) {
33    assert.throws(
34      () => {
35        sync.apply(null, argsSync);
36      },
37      {
38        code: 'ERR_INVALID_ARG_VALUE',
39        name: 'TypeError',
40      });
41  }
42
43  if (async) {
44    assert.throws(
45      () => {
46        async.apply(null, argsAsync);
47      },
48      {
49        code: 'ERR_INVALID_ARG_VALUE',
50        name: 'TypeError'
51      });
52  }
53}
54
55check(fs.access, fs.accessSync, 'foo\u0000bar');
56check(fs.access, fs.accessSync, 'foo\u0000bar', fs.F_OK);
57check(fs.appendFile, fs.appendFileSync, 'foo\u0000bar', 'abc');
58check(fs.chmod, fs.chmodSync, 'foo\u0000bar', '0644');
59check(fs.chown, fs.chownSync, 'foo\u0000bar', 12, 34);
60check(fs.copyFile, fs.copyFileSync, 'foo\u0000bar', 'abc');
61check(fs.copyFile, fs.copyFileSync, 'abc', 'foo\u0000bar');
62check(fs.lchown, fs.lchownSync, 'foo\u0000bar', 12, 34);
63check(fs.link, fs.linkSync, 'foo\u0000bar', 'foobar');
64check(fs.link, fs.linkSync, 'foobar', 'foo\u0000bar');
65check(fs.lstat, fs.lstatSync, 'foo\u0000bar');
66check(fs.mkdir, fs.mkdirSync, 'foo\u0000bar', '0755');
67check(fs.open, fs.openSync, 'foo\u0000bar', 'r');
68check(fs.readFile, fs.readFileSync, 'foo\u0000bar');
69check(fs.readdir, fs.readdirSync, 'foo\u0000bar');
70check(fs.readlink, fs.readlinkSync, 'foo\u0000bar');
71check(fs.realpath, fs.realpathSync, 'foo\u0000bar');
72check(fs.rename, fs.renameSync, 'foo\u0000bar', 'foobar');
73check(fs.rename, fs.renameSync, 'foobar', 'foo\u0000bar');
74check(fs.rmdir, fs.rmdirSync, 'foo\u0000bar');
75check(fs.stat, fs.statSync, 'foo\u0000bar');
76check(fs.symlink, fs.symlinkSync, 'foo\u0000bar', 'foobar');
77check(fs.symlink, fs.symlinkSync, 'foobar', 'foo\u0000bar');
78check(fs.truncate, fs.truncateSync, 'foo\u0000bar');
79check(fs.unlink, fs.unlinkSync, 'foo\u0000bar');
80check(null, fs.unwatchFile, 'foo\u0000bar', common.mustNotCall());
81check(fs.utimes, fs.utimesSync, 'foo\u0000bar', 0, 0);
82check(null, fs.watch, 'foo\u0000bar', common.mustNotCall());
83check(null, fs.watchFile, 'foo\u0000bar', common.mustNotCall());
84check(fs.writeFile, fs.writeFileSync, 'foo\u0000bar', 'abc');
85
86const fileUrl = new URL('file:///C:/foo\u0000bar');
87const fileUrl2 = new URL('file:///C:/foo%00bar');
88
89check(fs.access, fs.accessSync, fileUrl);
90check(fs.access, fs.accessSync, fileUrl, fs.F_OK);
91check(fs.appendFile, fs.appendFileSync, fileUrl, 'abc');
92check(fs.chmod, fs.chmodSync, fileUrl, '0644');
93check(fs.chown, fs.chownSync, fileUrl, 12, 34);
94check(fs.copyFile, fs.copyFileSync, fileUrl, 'abc');
95check(fs.copyFile, fs.copyFileSync, 'abc', fileUrl);
96check(fs.lchown, fs.lchownSync, fileUrl, 12, 34);
97check(fs.link, fs.linkSync, fileUrl, 'foobar');
98check(fs.link, fs.linkSync, 'foobar', fileUrl);
99check(fs.lstat, fs.lstatSync, fileUrl);
100check(fs.mkdir, fs.mkdirSync, fileUrl, '0755');
101check(fs.open, fs.openSync, fileUrl, 'r');
102check(fs.readFile, fs.readFileSync, fileUrl);
103check(fs.readdir, fs.readdirSync, fileUrl);
104check(fs.readlink, fs.readlinkSync, fileUrl);
105check(fs.realpath, fs.realpathSync, fileUrl);
106check(fs.rename, fs.renameSync, fileUrl, 'foobar');
107check(fs.rename, fs.renameSync, 'foobar', fileUrl);
108check(fs.rmdir, fs.rmdirSync, fileUrl);
109check(fs.stat, fs.statSync, fileUrl);
110check(fs.symlink, fs.symlinkSync, fileUrl, 'foobar');
111check(fs.symlink, fs.symlinkSync, 'foobar', fileUrl);
112check(fs.truncate, fs.truncateSync, fileUrl);
113check(fs.unlink, fs.unlinkSync, fileUrl);
114check(null, fs.unwatchFile, fileUrl, assert.fail);
115check(fs.utimes, fs.utimesSync, fileUrl, 0, 0);
116check(null, fs.watch, fileUrl, assert.fail);
117check(null, fs.watchFile, fileUrl, assert.fail);
118check(fs.writeFile, fs.writeFileSync, fileUrl, 'abc');
119
120check(fs.access, fs.accessSync, fileUrl2);
121check(fs.access, fs.accessSync, fileUrl2, fs.F_OK);
122check(fs.appendFile, fs.appendFileSync, fileUrl2, 'abc');
123check(fs.chmod, fs.chmodSync, fileUrl2, '0644');
124check(fs.chown, fs.chownSync, fileUrl2, 12, 34);
125check(fs.copyFile, fs.copyFileSync, fileUrl2, 'abc');
126check(fs.copyFile, fs.copyFileSync, 'abc', fileUrl2);
127check(fs.lchown, fs.lchownSync, fileUrl2, 12, 34);
128check(fs.link, fs.linkSync, fileUrl2, 'foobar');
129check(fs.link, fs.linkSync, 'foobar', fileUrl2);
130check(fs.lstat, fs.lstatSync, fileUrl2);
131check(fs.mkdir, fs.mkdirSync, fileUrl2, '0755');
132check(fs.open, fs.openSync, fileUrl2, 'r');
133check(fs.readFile, fs.readFileSync, fileUrl2);
134check(fs.readdir, fs.readdirSync, fileUrl2);
135check(fs.readlink, fs.readlinkSync, fileUrl2);
136check(fs.realpath, fs.realpathSync, fileUrl2);
137check(fs.rename, fs.renameSync, fileUrl2, 'foobar');
138check(fs.rename, fs.renameSync, 'foobar', fileUrl2);
139check(fs.rmdir, fs.rmdirSync, fileUrl2);
140check(fs.stat, fs.statSync, fileUrl2);
141check(fs.symlink, fs.symlinkSync, fileUrl2, 'foobar');
142check(fs.symlink, fs.symlinkSync, 'foobar', fileUrl2);
143check(fs.truncate, fs.truncateSync, fileUrl2);
144check(fs.unlink, fs.unlinkSync, fileUrl2);
145check(null, fs.unwatchFile, fileUrl2, assert.fail);
146check(fs.utimes, fs.utimesSync, fileUrl2, 0, 0);
147check(null, fs.watch, fileUrl2, assert.fail);
148check(null, fs.watchFile, fileUrl2, assert.fail);
149check(fs.writeFile, fs.writeFileSync, fileUrl2, 'abc');
150
151// An 'error' for exists means that it doesn't exist.
152// One of many reasons why this file is the absolute worst.
153fs.exists('foo\u0000bar', common.mustCall((exists) => {
154  assert(!exists);
155}));
156assert(!fs.existsSync('foo\u0000bar'));
157