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_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// Test variants of pid
271cb0ef41Sopenharmony_ci//
281cb0ef41Sopenharmony_ci// null: TypeError
291cb0ef41Sopenharmony_ci// undefined: TypeError
301cb0ef41Sopenharmony_ci//
311cb0ef41Sopenharmony_ci// 'SIGTERM': TypeError
321cb0ef41Sopenharmony_ci//
331cb0ef41Sopenharmony_ci// String(process.pid): TypeError
341cb0ef41Sopenharmony_ci//
351cb0ef41Sopenharmony_ci// Nan, Infinity, -Infinity: TypeError
361cb0ef41Sopenharmony_ci//
371cb0ef41Sopenharmony_ci// 0, String(0): our group process
381cb0ef41Sopenharmony_ci//
391cb0ef41Sopenharmony_ci// process.pid, String(process.pid): ourself
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci['SIGTERM', null, undefined, NaN, Infinity, -Infinity].forEach((val) => {
421cb0ef41Sopenharmony_ci  assert.throws(() => process.kill(val), {
431cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
441cb0ef41Sopenharmony_ci    name: 'TypeError',
451cb0ef41Sopenharmony_ci    message: 'The "pid" argument must be of type number.' +
461cb0ef41Sopenharmony_ci             common.invalidArgTypeHelper(val)
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci});
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci// Test that kill throws an error for unknown signal names
511cb0ef41Sopenharmony_ciassert.throws(() => process.kill(0, 'test'), {
521cb0ef41Sopenharmony_ci  code: 'ERR_UNKNOWN_SIGNAL',
531cb0ef41Sopenharmony_ci  name: 'TypeError',
541cb0ef41Sopenharmony_ci  message: 'Unknown signal: test'
551cb0ef41Sopenharmony_ci});
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci// Test that kill throws an error for invalid signal numbers
581cb0ef41Sopenharmony_ciassert.throws(() => process.kill(0, 987), {
591cb0ef41Sopenharmony_ci  code: 'EINVAL',
601cb0ef41Sopenharmony_ci  name: 'Error',
611cb0ef41Sopenharmony_ci  message: 'kill EINVAL'
621cb0ef41Sopenharmony_ci});
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci// Test kill argument processing in valid cases.
651cb0ef41Sopenharmony_ci//
661cb0ef41Sopenharmony_ci// Monkey patch _kill so that we don't actually send any signals, particularly
671cb0ef41Sopenharmony_ci// that we don't kill our process group, or try to actually send ANY signals on
681cb0ef41Sopenharmony_ci// windows, which doesn't support them.
691cb0ef41Sopenharmony_cifunction kill(tryPid, trySig, expectPid, expectSig) {
701cb0ef41Sopenharmony_ci  let getPid;
711cb0ef41Sopenharmony_ci  let getSig;
721cb0ef41Sopenharmony_ci  const origKill = process._kill;
731cb0ef41Sopenharmony_ci  process._kill = function(pid, sig) {
741cb0ef41Sopenharmony_ci    getPid = pid;
751cb0ef41Sopenharmony_ci    getSig = sig;
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci    // un-monkey patch process._kill
781cb0ef41Sopenharmony_ci    process._kill = origKill;
791cb0ef41Sopenharmony_ci  };
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  process.kill(tryPid, trySig);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  assert.strictEqual(getPid.toString(), expectPid.toString());
841cb0ef41Sopenharmony_ci  assert.strictEqual(getSig, expectSig);
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci// Note that SIGHUP and SIGTERM map to 1 and 15 respectively, even on Windows
881cb0ef41Sopenharmony_ci// (for Windows, libuv maps 1 and 15 to the correct behavior).
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_cikill(0, 'SIGHUP', 0, 1);
911cb0ef41Sopenharmony_cikill(0, undefined, 0, 15);
921cb0ef41Sopenharmony_cikill('0', 'SIGHUP', 0, 1);
931cb0ef41Sopenharmony_cikill('0', undefined, 0, 15);
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci// Confirm that numeric signal arguments are supported
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_cikill(0, 1, 0, 1);
981cb0ef41Sopenharmony_cikill(0, 15, 0, 15);
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci// Negative numbers are meaningful on unix
1011cb0ef41Sopenharmony_cikill(-1, 'SIGHUP', -1, 1);
1021cb0ef41Sopenharmony_cikill(-1, undefined, -1, 15);
1031cb0ef41Sopenharmony_cikill('-1', 'SIGHUP', -1, 1);
1041cb0ef41Sopenharmony_cikill('-1', undefined, -1, 15);
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_cikill(process.pid, 'SIGHUP', process.pid, 1);
1071cb0ef41Sopenharmony_cikill(process.pid, undefined, process.pid, 15);
1081cb0ef41Sopenharmony_cikill(String(process.pid), 'SIGHUP', process.pid, 1);
1091cb0ef41Sopenharmony_cikill(String(process.pid), undefined, process.pid, 15);
110