11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Verifies that the REPL history file is created with mode 0600
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Flags: --expose-internals
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciif (common.isWindows) {
101cb0ef41Sopenharmony_ci  common.skip('Win32 uses ACLs for file permissions, ' +
111cb0ef41Sopenharmony_ci              'modes are always 0666 and says nothing about group/other ' +
121cb0ef41Sopenharmony_ci              'read access.');
131cb0ef41Sopenharmony_ci}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst assert = require('assert');
161cb0ef41Sopenharmony_ciconst path = require('path');
171cb0ef41Sopenharmony_ciconst fs = require('fs');
181cb0ef41Sopenharmony_ciconst repl = require('internal/repl');
191cb0ef41Sopenharmony_ciconst Duplex = require('stream').Duplex;
201cb0ef41Sopenharmony_ci// Invoking the REPL should create a repl history file at the specified path
211cb0ef41Sopenharmony_ci// and mode 600.
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst stream = new Duplex();
241cb0ef41Sopenharmony_cistream.pause = stream.resume = () => {};
251cb0ef41Sopenharmony_ci// ends immediately
261cb0ef41Sopenharmony_cistream._read = function() {
271cb0ef41Sopenharmony_ci  this.push(null);
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_cistream._write = function(c, e, cb) {
301cb0ef41Sopenharmony_ci  cb();
311cb0ef41Sopenharmony_ci};
321cb0ef41Sopenharmony_cistream.readable = stream.writable = true;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
351cb0ef41Sopenharmony_citmpdir.refresh();
361cb0ef41Sopenharmony_ciconst replHistoryPath = path.join(tmpdir.path, '.node_repl_history');
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst checkResults = common.mustSucceed((r) => {
391cb0ef41Sopenharmony_ci  const stat = fs.statSync(replHistoryPath);
401cb0ef41Sopenharmony_ci  const fileMode = stat.mode & 0o777;
411cb0ef41Sopenharmony_ci  assert.strictEqual(
421cb0ef41Sopenharmony_ci    fileMode, 0o600,
431cb0ef41Sopenharmony_ci    `REPL history file should be mode 0600 but was 0${fileMode.toString(8)}`);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  // Close the REPL
461cb0ef41Sopenharmony_ci  r.input.emit('keypress', '', { ctrl: true, name: 'd' });
471cb0ef41Sopenharmony_ci  r.input.end();
481cb0ef41Sopenharmony_ci});
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_cirepl.createInternalRepl(
511cb0ef41Sopenharmony_ci  { NODE_REPL_HISTORY: replHistoryPath },
521cb0ef41Sopenharmony_ci  {
531cb0ef41Sopenharmony_ci    terminal: true,
541cb0ef41Sopenharmony_ci    input: stream,
551cb0ef41Sopenharmony_ci    output: stream
561cb0ef41Sopenharmony_ci  },
571cb0ef41Sopenharmony_ci  checkResults
581cb0ef41Sopenharmony_ci);
59