michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: const { spawn, exec, execFile, fork } = require('sdk/system/child_process'); michael@0: const { env, platform, pathFor } = require('sdk/system'); michael@0: const { isNumber } = require('sdk/lang/type'); michael@0: const { after } = require('sdk/test/utils'); michael@0: const { emit } = require('sdk/event/core'); michael@0: const PROFILE_DIR= pathFor('ProfD'); michael@0: const isWindows = platform.toLowerCase().indexOf('win') === 0; michael@0: const { getScript, cleanUp } = require('./fixtures/child-process-scripts'); michael@0: michael@0: // We use direct paths to these utilities as we currently cannot michael@0: // call non-absolute paths to utilities in subprocess.jsm michael@0: const CAT_PATH = isWindows ? 'C:\\Windows\\System32\\more.com' : '/bin/cat'; michael@0: michael@0: exports.testExecCallbackSuccess = function (assert, done) { michael@0: exec(isWindows ? 'DIR /A-D' : 'ls -al', { michael@0: cwd: PROFILE_DIR michael@0: }, function (err, stdout, stderr) { michael@0: assert.ok(!err, 'no errors found'); michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: assert.ok(/extensions\.ini/.test(stdout), 'stdout output of `ls -al` finds files'); michael@0: michael@0: if (isWindows) { michael@0: // `DIR /A-D` does not display directories on WIN michael@0: assert.ok(!//.test(stdout), michael@0: 'passing arguments in `exec` works'); michael@0: } michael@0: else { michael@0: // `ls -al` should list all the priviledge information on Unix michael@0: assert.ok(/d(r[-|w][-|x]){3}/.test(stdout), michael@0: 'passing arguments in `exec` works'); michael@0: } michael@0: done(); michael@0: }); michael@0: }; michael@0: michael@0: exports.testExecCallbackError = function (assert, done) { michael@0: exec('not-real-command', { cwd: PROFILE_DIR }, function (err, stdout, stderr) { michael@0: assert.ok(/not-real-command/.test(err.toString()), michael@0: 'error contains error message'); michael@0: assert.ok(err.lineNumber >= 0, 'error contains lineNumber'); michael@0: assert.ok(/resource:\/\//.test(err.fileName), 'error contains fileName'); michael@0: assert.ok(err.code && isNumber(err.code), 'non-zero error code property on error'); michael@0: assert.equal(err.signal, null, michael@0: 'null signal property when not manually terminated'); michael@0: assert.equal(stdout, '', 'stdout is empty'); michael@0: assert.ok(/not-real-command/.test(stderr), 'stderr contains error message'); michael@0: done(); michael@0: }); michael@0: }; michael@0: michael@0: exports.testExecOptionsEnvironment = function (assert, done) { michael@0: getScript('check-env').then(envScript => { michael@0: exec(envScript, { michael@0: cwd: PROFILE_DIR, michael@0: env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' } michael@0: }, function (err, stdout, stderr) { michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: assert.ok(!err, 'received `cwd` option'); michael@0: assert.ok(/my-value-test/.test(stdout), michael@0: 'receives environment option'); michael@0: done(); michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports.testExecOptionsTimeout = function (assert, done) { michael@0: let count = 0; michael@0: getScript('wait').then(script => { michael@0: let child = exec(script, { timeout: 100 }, (err, stdout, stderr) => { michael@0: assert.equal(err.killed, true, 'error has `killed` property as true'); michael@0: assert.equal(err.code, null, 'error has `code` as null'); michael@0: assert.equal(err.signal, 'SIGTERM', michael@0: 'error has `signal` as SIGTERM by default'); michael@0: assert.equal(stdout, '', 'stdout is empty'); michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: if (++count === 3) complete(); michael@0: }); michael@0: michael@0: function exitHandler (code, signal) { michael@0: assert.equal(code, null, 'error has `code` as null'); michael@0: assert.equal(signal, 'SIGTERM', michael@0: 'error has `signal` as SIGTERM by default'); michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function closeHandler (code, signal) { michael@0: assert.equal(code, null, 'error has `code` as null'); michael@0: assert.equal(signal, 'SIGTERM', michael@0: 'error has `signal` as SIGTERM by default'); michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: child.on('exit', exitHandler); michael@0: child.on('close', closeHandler); michael@0: michael@0: function complete () { michael@0: child.off('exit', exitHandler); michael@0: child.off('close', closeHandler); michael@0: done(); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports.testExecFileCallbackSuccess = function (assert, done) { michael@0: getScript('args').then(script => { michael@0: execFile(script, ['--myargs', '-j', '-s'], { cwd: PROFILE_DIR }, function (err, stdout, stderr) { michael@0: assert.ok(!err, 'no errors found'); michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: // Trim output since different systems have different new line output michael@0: assert.equal(stdout.trim(), '--myargs -j -s'.trim(), 'passes in correct arguments'); michael@0: done(); michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports.testExecFileCallbackError = function (assert, done) { michael@0: execFile('not-real-command', { cwd: PROFILE_DIR }, function (err, stdout, stderr) { michael@0: assert.ok(/NS_ERROR_FILE_UNRECOGNIZED_PATH/.test(err.message), michael@0: 'error contains error message'); michael@0: assert.ok(err.lineNumber >= 0, 'error contains lineNumber'); michael@0: assert.ok(/resource:\/\//.test(err.fileName), 'error contains fileName'); michael@0: assert.equal(stdout, '', 'stdout is empty'); michael@0: assert.equal(stderr, '', 'stdout is empty'); michael@0: done(); michael@0: }); michael@0: }; michael@0: michael@0: exports.testExecFileOptionsEnvironment = function (assert, done) { michael@0: getScript('check-env').then(script => { michael@0: execFile(script, { michael@0: cwd: PROFILE_DIR, michael@0: env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' } michael@0: }, function (err, stdout, stderr) { michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: assert.ok(!err, 'received `cwd` option'); michael@0: assert.ok(/my-value-test/.test(stdout), michael@0: 'receives environment option'); michael@0: done(); michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports.testExecFileOptionsTimeout = function (assert, done) { michael@0: let count = 0; michael@0: getScript('wait').then(script => { michael@0: let child = execFile(script, { timeout: 100 }, (err, stdout, stderr) => { michael@0: assert.equal(err.killed, true, 'error has `killed` property as true'); michael@0: assert.equal(err.code, null, 'error has `code` as null'); michael@0: assert.equal(err.signal, 'SIGTERM', michael@0: 'error has `signal` as SIGTERM by default'); michael@0: assert.equal(stdout, '', 'stdout is empty'); michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: if (++count === 3) complete(); michael@0: }); michael@0: michael@0: function exitHandler (code, signal) { michael@0: assert.equal(code, null, 'error has `code` as null'); michael@0: assert.equal(signal, 'SIGTERM', michael@0: 'error has `signal` as SIGTERM by default'); michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function closeHandler (code, signal) { michael@0: assert.equal(code, null, 'error has `code` as null'); michael@0: assert.equal(signal, 'SIGTERM', michael@0: 'error has `signal` as SIGTERM by default'); michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: child.on('exit', exitHandler); michael@0: child.on('close', closeHandler); michael@0: michael@0: function complete () { michael@0: child.off('exit', exitHandler); michael@0: child.off('close', closeHandler); michael@0: done(); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: /** michael@0: * Not necessary to test for both `exec` and `execFile`, but michael@0: * it is necessary to test both when the buffer is larger michael@0: * and smaller than buffer size used by the subprocess library (1024) michael@0: */ michael@0: exports.testExecFileOptionsMaxBufferLargeStdOut = function (assert, done) { michael@0: let count = 0; michael@0: let stdoutChild; michael@0: michael@0: // Creates a buffer of 2000 to stdout, greater than 1024 michael@0: getScript('large-out').then(script => { michael@0: stdoutChild = execFile(script, ['10000'], { maxBuffer: 50 }, (err, stdout, stderr) => { michael@0: assert.ok(/stdout maxBuffer exceeded/.test(err.toString()), michael@0: 'error contains stdout maxBuffer exceeded message'); michael@0: assert.ok(stdout.length >= 50, 'stdout has full buffer'); michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: if (++count === 3) complete(); michael@0: }); michael@0: stdoutChild.on('exit', exitHandler); michael@0: stdoutChild.on('close', closeHandler); michael@0: }); michael@0: michael@0: function exitHandler (code, signal) { michael@0: assert.equal(code, null, 'Exit code is null in exit handler'); michael@0: assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in exit handler'); michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function closeHandler (code, signal) { michael@0: assert.equal(code, null, 'Exit code is null in close handler'); michael@0: assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in close handler'); michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function complete () { michael@0: stdoutChild.off('exit', exitHandler); michael@0: stdoutChild.off('close', closeHandler); michael@0: done(); michael@0: } michael@0: }; michael@0: michael@0: exports.testExecFileOptionsMaxBufferLargeStdOErr = function (assert, done) { michael@0: let count = 0; michael@0: let stderrChild; michael@0: // Creates a buffer of 2000 to stderr, greater than 1024 michael@0: getScript('large-err').then(script => { michael@0: stderrChild = execFile(script, ['10000'], { maxBuffer: 50 }, (err, stdout, stderr) => { michael@0: assert.ok(/stderr maxBuffer exceeded/.test(err.toString()), michael@0: 'error contains stderr maxBuffer exceeded message'); michael@0: assert.ok(stderr.length >= 50, 'stderr has full buffer'); michael@0: assert.equal(stdout, '', 'stdout is empty'); michael@0: if (++count === 3) complete(); michael@0: }); michael@0: stderrChild.on('exit', exitHandler); michael@0: stderrChild.on('close', closeHandler); michael@0: }); michael@0: michael@0: function exitHandler (code, signal) { michael@0: assert.equal(code, null, 'Exit code is null in exit handler'); michael@0: assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in exit handler'); michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function closeHandler (code, signal) { michael@0: assert.equal(code, null, 'Exit code is null in close handler'); michael@0: assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in close handler'); michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function complete () { michael@0: stderrChild.off('exit', exitHandler); michael@0: stderrChild.off('close', closeHandler); michael@0: done(); michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * When total buffer is < process buffer (1024), the process will exit michael@0: * and not get a chance to be killed for violating the maxBuffer, michael@0: * although the error will still be sent through (node behaviour) michael@0: */ michael@0: exports.testExecFileOptionsMaxBufferSmallStdOut = function (assert, done) { michael@0: let count = 0; michael@0: let stdoutChild; michael@0: michael@0: // Creates a buffer of 60 to stdout, less than 1024 michael@0: getScript('large-out').then(script => { michael@0: stdoutChild = execFile(script, ['60'], { maxBuffer: 50 }, (err, stdout, stderr) => { michael@0: assert.ok(/stdout maxBuffer exceeded/.test(err.toString()), michael@0: 'error contains stdout maxBuffer exceeded message'); michael@0: assert.ok(stdout.length >= 50, 'stdout has full buffer'); michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: if (++count === 3) complete(); michael@0: }); michael@0: stdoutChild.on('exit', exitHandler); michael@0: stdoutChild.on('close', closeHandler); michael@0: }); michael@0: michael@0: function exitHandler (code, signal) { michael@0: // Sometimes the buffer limit is hit before the process closes successfully michael@0: // on both OSX/Windows michael@0: if (code === null) { michael@0: assert.equal(code, null, 'Exit code is null in exit handler'); michael@0: assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in exit handler'); michael@0: } michael@0: else { michael@0: assert.equal(code, 0, 'Exit code is 0 in exit handler'); michael@0: assert.equal(signal, null, 'Signal is null in exit handler'); michael@0: } michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function closeHandler (code, signal) { michael@0: // Sometimes the buffer limit is hit before the process closes successfully michael@0: // on both OSX/Windows michael@0: if (code === null) { michael@0: assert.equal(code, null, 'Exit code is null in close handler'); michael@0: assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in close handler'); michael@0: } michael@0: else { michael@0: assert.equal(code, 0, 'Exit code is 0 in close handler'); michael@0: assert.equal(signal, null, 'Signal is null in close handler'); michael@0: } michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function complete () { michael@0: stdoutChild.off('exit', exitHandler); michael@0: stdoutChild.off('close', closeHandler); michael@0: done(); michael@0: } michael@0: }; michael@0: michael@0: exports.testExecFileOptionsMaxBufferSmallStdErr = function (assert, done) { michael@0: let count = 0; michael@0: let stderrChild; michael@0: // Creates a buffer of 60 to stderr, less than 1024 michael@0: getScript('large-err').then(script => { michael@0: stderrChild = execFile(script, ['60'], { maxBuffer: 50 }, (err, stdout, stderr) => { michael@0: assert.ok(/stderr maxBuffer exceeded/.test(err.toString()), michael@0: 'error contains stderr maxBuffer exceeded message'); michael@0: assert.ok(stderr.length >= 50, 'stderr has full buffer'); michael@0: assert.equal(stdout, '', 'stdout is empty'); michael@0: if (++count === 3) complete(); michael@0: }); michael@0: stderrChild.on('exit', exitHandler); michael@0: stderrChild.on('close', closeHandler); michael@0: }); michael@0: michael@0: function exitHandler (code, signal) { michael@0: // Sometimes the buffer limit is hit before the process closes successfully michael@0: // on both OSX/Windows michael@0: if (code === null) { michael@0: assert.equal(code, null, 'Exit code is null in exit handler'); michael@0: assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in exit handler'); michael@0: } michael@0: else { michael@0: assert.equal(code, 0, 'Exit code is 0 in exit handler'); michael@0: assert.equal(signal, null, 'Signal is null in exit handler'); michael@0: } michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function closeHandler (code, signal) { michael@0: // Sometimes the buffer limit is hit before the process closes successfully michael@0: // on both OSX/Windows michael@0: if (code === null) { michael@0: assert.equal(code, null, 'Exit code is null in close handler'); michael@0: assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in close handler'); michael@0: } michael@0: else { michael@0: assert.equal(code, 0, 'Exit code is 0 in close handler'); michael@0: assert.equal(signal, null, 'Signal is null in close handler'); michael@0: } michael@0: if (++count === 3) complete(); michael@0: } michael@0: michael@0: function complete () { michael@0: stderrChild.off('exit', exitHandler); michael@0: stderrChild.off('close', closeHandler); michael@0: done(); michael@0: } michael@0: }; michael@0: michael@0: exports.testChildExecFileKillSignal = function (assert, done) { michael@0: getScript('wait').then(script => { michael@0: execFile(script, { michael@0: killSignal: 'beepbeep', michael@0: timeout: 10 michael@0: }, function (err, stdout, stderr) { michael@0: assert.equal(err.signal, 'beepbeep', 'correctly used custom killSignal'); michael@0: done(); michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: exports.testChildProperties = function (assert, done) { michael@0: getScript('check-env').then(script => { michael@0: let child = spawn(script, { michael@0: env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' } michael@0: }); michael@0: michael@0: if (isWindows) michael@0: assert.ok(true, 'Windows environment does not have `pid`'); michael@0: else michael@0: assert.ok(child.pid > 0, 'Child has a pid'); michael@0: done(); michael@0: }); michael@0: }; michael@0: michael@0: exports.testChildStdinStreamLarge = function (assert, done) { michael@0: let REPEAT = 2000; michael@0: let allData = ''; michael@0: // Use direct paths to more/cat, as we do not currently support calling non-files michael@0: // from subprocess.jsm michael@0: let child = spawn(CAT_PATH); michael@0: michael@0: child.stdout.on('data', onData); michael@0: child.on('close', onClose); michael@0: michael@0: for (let i = 0; i < REPEAT; i++) michael@0: emit(child.stdin, 'data', '12345\n'); michael@0: michael@0: emit(child.stdin, 'end'); michael@0: michael@0: function onData (data) { michael@0: allData += data; michael@0: } michael@0: michael@0: function onClose (code, signal) { michael@0: child.stdout.off('data', onData); michael@0: child.off('close', onClose); michael@0: assert.equal(code, 0, 'exited succesfully'); michael@0: assert.equal(signal, null, 'no kill signal given'); michael@0: assert.equal(allData.replace(/\W/g, '').length, '12345'.length * REPEAT, michael@0: 'all data processed from stdin'); michael@0: done(); michael@0: } michael@0: }; michael@0: michael@0: exports.testChildStdinStreamSmall = function (assert, done) { michael@0: let allData = ''; michael@0: let child = spawn(CAT_PATH); michael@0: child.stdout.on('data', onData); michael@0: child.on('close', onClose); michael@0: michael@0: emit(child.stdin, 'data', '12345'); michael@0: emit(child.stdin, 'end'); michael@0: michael@0: function onData (data) { michael@0: allData += data; michael@0: } michael@0: michael@0: function onClose (code, signal) { michael@0: child.stdout.off('data', onData); michael@0: child.off('close', onClose); michael@0: assert.equal(code, 0, 'exited succesfully'); michael@0: assert.equal(signal, null, 'no kill signal given'); michael@0: assert.equal(allData.trim(), '12345', 'all data processed from stdin'); michael@0: done(); michael@0: } michael@0: }; michael@0: /* michael@0: * This tests failures when an error is thrown attempting to michael@0: * spawn the process, like an invalid command michael@0: */ michael@0: exports.testChildEventsSpawningError= function (assert, done) { michael@0: let handlersCalled = 0; michael@0: let child = execFile('i-do-not-exist', (err, stdout, stderr) => { michael@0: assert.ok(err, 'error was passed into callback'); michael@0: assert.equal(stdout, '', 'stdout is empty') michael@0: assert.equal(stderr, '', 'stderr is empty'); michael@0: if (++handlersCalled === 3) complete(); michael@0: }); michael@0: michael@0: child.on('error', handleError); michael@0: child.on('exit', handleExit); michael@0: child.on('close', handleClose); michael@0: michael@0: function handleError (e) { michael@0: assert.ok(e, 'error passed into error handler'); michael@0: if (++handlersCalled === 3) complete(); michael@0: } michael@0: michael@0: function handleClose (code, signal) { michael@0: assert.equal(code, -1, michael@0: 'process was never spawned, therefore exit code is -1'); michael@0: assert.equal(signal, null, 'signal should be null'); michael@0: if (++handlersCalled === 3) complete(); michael@0: } michael@0: michael@0: function handleExit (code, signal) { michael@0: assert.fail('Close event should not be called on init failure'); michael@0: } michael@0: michael@0: function complete () { michael@0: child.off('error', handleError); michael@0: child.off('exit', handleExit); michael@0: child.off('close', handleClose); michael@0: done(); michael@0: } michael@0: }; michael@0: michael@0: exports.testSpawnOptions = function (assert, done) { michael@0: let count = 0; michael@0: let envStdout = ''; michael@0: let cwdStdout = ''; michael@0: let checkEnv, checkPwd, envChild, cwdChild; michael@0: getScript('check-env').then(script => { michael@0: checkEnv = script; michael@0: return getScript('check-pwd'); michael@0: }).then(script => { michael@0: checkPwd = script; michael@0: michael@0: envChild = spawn(checkEnv, { michael@0: env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' } michael@0: }); michael@0: cwdChild = spawn(checkPwd, { cwd: PROFILE_DIR }); michael@0: michael@0: // Do these need to be unbound? michael@0: envChild.stdout.on('data', data => envStdout += data); michael@0: cwdChild.stdout.on('data', data => cwdStdout += data); michael@0: michael@0: envChild.on('close', envClose); michael@0: cwdChild.on('close', cwdClose); michael@0: }); michael@0: michael@0: function envClose () { michael@0: assert.equal(envStdout.trim(), 'my-value-test', 'spawn correctly passed in ENV'); michael@0: if (++count === 2) complete(); michael@0: } michael@0: michael@0: function cwdClose () { michael@0: // Check for PROFILE_DIR in the output because michael@0: // some systems resolve symbolic links, and on OSX michael@0: // /var -> /private/var michael@0: let isCorrectPath = ~cwdStdout.trim().indexOf(PROFILE_DIR); michael@0: assert.ok(isCorrectPath, 'spawn correctly passed in cwd'); michael@0: if (++count === 2) complete(); michael@0: } michael@0: michael@0: function complete () { michael@0: envChild.off('close', envClose); michael@0: cwdChild.off('close', cwdClose); michael@0: done(); michael@0: } michael@0: }; michael@0: michael@0: exports.testFork = function (assert) { michael@0: assert.throws(function () { michael@0: fork(); michael@0: }, /not currently supported/, 'fork() correctly throws an unsupported error'); michael@0: }; michael@0: michael@0: after(exports, cleanUp); michael@0: michael@0: require("test").run(exports); michael@0: michael@0: // Test disabled because of bug 979675 michael@0: module.exports = {};