11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../../common');
31cb0ef41Sopenharmony_ciif (common.isWindows)
41cb0ef41Sopenharmony_ci  common.skip('No RegisterSignalHandler() on Windows');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst fs = require('fs');
81cb0ef41Sopenharmony_ciconst path = require('path');
91cb0ef41Sopenharmony_ciconst { signals } = require('os').constants;
101cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst bindingPath = path.resolve(
131cb0ef41Sopenharmony_ci  __dirname, 'build', common.buildType, 'binding.node');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciif (!fs.existsSync(bindingPath))
161cb0ef41Sopenharmony_ci  common.skip('binding not built yet');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst binding = require(bindingPath);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
211cb0ef41Sopenharmony_ci  const signo = +process.argv[3];
221cb0ef41Sopenharmony_ci  const reset = process.argv[4] === 'reset';
231cb0ef41Sopenharmony_ci  const count = +process.argv[5];
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  binding.registerSignalHandler(signo, reset);
261cb0ef41Sopenharmony_ci  for (let i = 0; i < count; i++)
271cb0ef41Sopenharmony_ci    process.kill(process.pid, signo);
281cb0ef41Sopenharmony_ci  return;
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cifor (const raiseSignal of [ 'SIGABRT', 'SIGSEGV' ]) {
321cb0ef41Sopenharmony_ci  const signo = signals[raiseSignal];
331cb0ef41Sopenharmony_ci  for (const { reset, count, stderr, code, signal } of [
341cb0ef41Sopenharmony_ci    { reset: true, count: 1, stderr: [signo], code: 0, signal: null },
351cb0ef41Sopenharmony_ci    { reset: true, count: 2, stderr: [signo], code: null, signal: raiseSignal },
361cb0ef41Sopenharmony_ci    { reset: false, count: 1, stderr: [signo], code: 0, signal: null },
371cb0ef41Sopenharmony_ci    { reset: false, count: 2, stderr: [signo, signo], code: 0, signal: null },
381cb0ef41Sopenharmony_ci  ]) {
391cb0ef41Sopenharmony_ci    // We do not want to generate core files when running this test as an
401cb0ef41Sopenharmony_ci    // addon test. We require this file as an abort test as well, though,
411cb0ef41Sopenharmony_ci    // with ALLOW_CRASHES set.
421cb0ef41Sopenharmony_ci    if (signal !== null && !process.env.ALLOW_CRASHES)
431cb0ef41Sopenharmony_ci      continue;
441cb0ef41Sopenharmony_ci    // reset_handler does not work with SIGSEGV.
451cb0ef41Sopenharmony_ci    if (reset && signo === signals.SIGSEGV)
461cb0ef41Sopenharmony_ci      continue;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    const args = [__filename, 'child', signo, reset ? 'reset' : '', count];
491cb0ef41Sopenharmony_ci    console.log(`Running: node ${args.join(' ')}`);
501cb0ef41Sopenharmony_ci    const result = spawnSync(
511cb0ef41Sopenharmony_ci      process.execPath, args, { stdio: ['inherit', 'pipe', 'inherit'] });
521cb0ef41Sopenharmony_ci    assert.strictEqual(result.status, code);
531cb0ef41Sopenharmony_ci    assert.strictEqual(result.signal, signal);
541cb0ef41Sopenharmony_ci    assert.deepStrictEqual([...result.stdout], stderr);
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci}
57