1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25 26// Test variants of pid 27// 28// null: TypeError 29// undefined: TypeError 30// 31// 'SIGTERM': TypeError 32// 33// String(process.pid): TypeError 34// 35// Nan, Infinity, -Infinity: TypeError 36// 37// 0, String(0): our group process 38// 39// process.pid, String(process.pid): ourself 40 41['SIGTERM', null, undefined, NaN, Infinity, -Infinity].forEach((val) => { 42 assert.throws(() => process.kill(val), { 43 code: 'ERR_INVALID_ARG_TYPE', 44 name: 'TypeError', 45 message: 'The "pid" argument must be of type number.' + 46 common.invalidArgTypeHelper(val) 47 }); 48}); 49 50// Test that kill throws an error for unknown signal names 51assert.throws(() => process.kill(0, 'test'), { 52 code: 'ERR_UNKNOWN_SIGNAL', 53 name: 'TypeError', 54 message: 'Unknown signal: test' 55}); 56 57// Test that kill throws an error for invalid signal numbers 58assert.throws(() => process.kill(0, 987), { 59 code: 'EINVAL', 60 name: 'Error', 61 message: 'kill EINVAL' 62}); 63 64// Test kill argument processing in valid cases. 65// 66// Monkey patch _kill so that we don't actually send any signals, particularly 67// that we don't kill our process group, or try to actually send ANY signals on 68// windows, which doesn't support them. 69function kill(tryPid, trySig, expectPid, expectSig) { 70 let getPid; 71 let getSig; 72 const origKill = process._kill; 73 process._kill = function(pid, sig) { 74 getPid = pid; 75 getSig = sig; 76 77 // un-monkey patch process._kill 78 process._kill = origKill; 79 }; 80 81 process.kill(tryPid, trySig); 82 83 assert.strictEqual(getPid.toString(), expectPid.toString()); 84 assert.strictEqual(getSig, expectSig); 85} 86 87// Note that SIGHUP and SIGTERM map to 1 and 15 respectively, even on Windows 88// (for Windows, libuv maps 1 and 15 to the correct behavior). 89 90kill(0, 'SIGHUP', 0, 1); 91kill(0, undefined, 0, 15); 92kill('0', 'SIGHUP', 0, 1); 93kill('0', undefined, 0, 15); 94 95// Confirm that numeric signal arguments are supported 96 97kill(0, 1, 0, 1); 98kill(0, 15, 0, 15); 99 100// Negative numbers are meaningful on unix 101kill(-1, 'SIGHUP', -1, 1); 102kill(-1, undefined, -1, 15); 103kill('-1', 'SIGHUP', -1, 1); 104kill('-1', undefined, -1, 15); 105 106kill(process.pid, 'SIGHUP', process.pid, 1); 107kill(process.pid, undefined, process.pid, 15); 108kill(String(process.pid), 'SIGHUP', process.pid, 1); 109kill(String(process.pid), undefined, process.pid, 15); 110