11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciif (!common.isMainThread)
261cb0ef41Sopenharmony_ci  common.skip('Setting process.umask is not supported in Workers');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst assert = require('assert');
291cb0ef41Sopenharmony_ciconst path = require('path');
301cb0ef41Sopenharmony_ciconst fs = require('fs');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// On Windows chmod is only able to manipulate read-only bit. Test if creating
331cb0ef41Sopenharmony_ci// the file in read-only mode works.
341cb0ef41Sopenharmony_ciconst mode = common.isWindows ? 0o444 : 0o755;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// Reset the umask for testing
371cb0ef41Sopenharmony_ciprocess.umask(0o000);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
401cb0ef41Sopenharmony_citmpdir.refresh();
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Test writeFileSync
431cb0ef41Sopenharmony_ci{
441cb0ef41Sopenharmony_ci  const file = path.join(tmpdir.path, 'testWriteFileSync.txt');
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  fs.writeFileSync(file, '123', { mode });
471cb0ef41Sopenharmony_ci  const content = fs.readFileSync(file, { encoding: 'utf8' });
481cb0ef41Sopenharmony_ci  assert.strictEqual(content, '123');
491cb0ef41Sopenharmony_ci  assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci// Test appendFileSync
531cb0ef41Sopenharmony_ci{
541cb0ef41Sopenharmony_ci  const file = path.join(tmpdir.path, 'testAppendFileSync.txt');
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  fs.appendFileSync(file, 'abc', { mode });
571cb0ef41Sopenharmony_ci  const content = fs.readFileSync(file, { encoding: 'utf8' });
581cb0ef41Sopenharmony_ci  assert.strictEqual(content, 'abc');
591cb0ef41Sopenharmony_ci  assert.strictEqual(fs.statSync(file).mode & mode, mode);
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci// Test writeFileSync with file descriptor
631cb0ef41Sopenharmony_ci{
641cb0ef41Sopenharmony_ci  // Need to hijack fs.open/close to make sure that things
651cb0ef41Sopenharmony_ci  // get closed once they're opened.
661cb0ef41Sopenharmony_ci  const _openSync = fs.openSync;
671cb0ef41Sopenharmony_ci  const _closeSync = fs.closeSync;
681cb0ef41Sopenharmony_ci  let openCount = 0;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  fs.openSync = (...args) => {
711cb0ef41Sopenharmony_ci    openCount++;
721cb0ef41Sopenharmony_ci    return _openSync(...args);
731cb0ef41Sopenharmony_ci  };
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  fs.closeSync = (...args) => {
761cb0ef41Sopenharmony_ci    openCount--;
771cb0ef41Sopenharmony_ci    return _closeSync(...args);
781cb0ef41Sopenharmony_ci  };
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  const file = path.join(tmpdir.path, 'testWriteFileSyncFd.txt');
811cb0ef41Sopenharmony_ci  const fd = fs.openSync(file, 'w+', mode);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  fs.writeFileSync(fd, '123');
841cb0ef41Sopenharmony_ci  fs.closeSync(fd);
851cb0ef41Sopenharmony_ci  const content = fs.readFileSync(file, { encoding: 'utf8' });
861cb0ef41Sopenharmony_ci  assert.strictEqual(content, '123');
871cb0ef41Sopenharmony_ci  assert.strictEqual(fs.statSync(file).mode & 0o777, mode);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  // Verify that all opened files were closed.
901cb0ef41Sopenharmony_ci  assert.strictEqual(openCount, 0);
911cb0ef41Sopenharmony_ci  fs.openSync = _openSync;
921cb0ef41Sopenharmony_ci  fs.closeSync = _closeSync;
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci// Test writeFileSync with flags
961cb0ef41Sopenharmony_ci{
971cb0ef41Sopenharmony_ci  const file = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt');
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  fs.writeFileSync(file, 'hello ', { encoding: 'utf8', flag: 'a' });
1001cb0ef41Sopenharmony_ci  fs.writeFileSync(file, 'world!', { encoding: 'utf8', flag: 'a' });
1011cb0ef41Sopenharmony_ci  const content = fs.readFileSync(file, { encoding: 'utf8' });
1021cb0ef41Sopenharmony_ci  assert.strictEqual(content, 'hello world!');
1031cb0ef41Sopenharmony_ci}
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci// Test writeFileSync with an object with an own toString function
1061cb0ef41Sopenharmony_ci{
1071cb0ef41Sopenharmony_ci  // Runtime deprecated by DEP0162
1081cb0ef41Sopenharmony_ci  common.expectWarning('DeprecationWarning',
1091cb0ef41Sopenharmony_ci                       'Implicit coercion of objects with own toString property is deprecated.',
1101cb0ef41Sopenharmony_ci                       'DEP0162');
1111cb0ef41Sopenharmony_ci  const file = path.join(tmpdir.path, 'testWriteFileSyncStringify.txt');
1121cb0ef41Sopenharmony_ci  const data = {
1131cb0ef41Sopenharmony_ci    toString() {
1141cb0ef41Sopenharmony_ci      return 'hello world!';
1151cb0ef41Sopenharmony_ci    }
1161cb0ef41Sopenharmony_ci  };
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  fs.writeFileSync(file, data, { encoding: 'utf8', flag: 'a' });
1191cb0ef41Sopenharmony_ci  const content = fs.readFileSync(file, { encoding: 'utf8' });
1201cb0ef41Sopenharmony_ci  assert.strictEqual(content, String(data));
1211cb0ef41Sopenharmony_ci}
122