11cb0ef41Sopenharmony_ci// Check that exceeding RLIMIT_FSIZE fails with EFBIG
21cb0ef41Sopenharmony_ci// rather than terminating the process with SIGXFSZ.
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst child_process = require('child_process');
91cb0ef41Sopenharmony_ciconst fs = require('fs');
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciif (common.isWindows)
131cb0ef41Sopenharmony_ci  common.skip('no RLIMIT_FSIZE on Windows');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciif (process.config.variables.node_shared)
161cb0ef41Sopenharmony_ci  common.skip('SIGXFSZ signal handler not installed in shared library mode');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
191cb0ef41Sopenharmony_ci  const filename = path.join(tmpdir.path, 'efbig.txt');
201cb0ef41Sopenharmony_ci  tmpdir.refresh();
211cb0ef41Sopenharmony_ci  fs.writeFileSync(filename, '.'.repeat(1 << 16));  // Exceeds RLIMIT_FSIZE.
221cb0ef41Sopenharmony_ci} else {
231cb0ef41Sopenharmony_ci  const cmd = `ulimit -f 1 && '${process.execPath}' '${__filename}' child`;
241cb0ef41Sopenharmony_ci  const result = child_process.spawnSync('/bin/sh', ['-c', cmd]);
251cb0ef41Sopenharmony_ci  const haystack = result.stderr.toString();
261cb0ef41Sopenharmony_ci  const needle = 'Error: EFBIG: file too large, write';
271cb0ef41Sopenharmony_ci  const ok = haystack.includes(needle);
281cb0ef41Sopenharmony_ci  if (!ok) console.error(haystack);
291cb0ef41Sopenharmony_ci  assert(ok);
301cb0ef41Sopenharmony_ci  assert.strictEqual(result.status, 1);
311cb0ef41Sopenharmony_ci  assert.strictEqual(result.stdout.toString(), '');
321cb0ef41Sopenharmony_ci}
33