2017-07-26 16:08:07 +03:00
|
|
|
/* eslint-disable mozilla/no-arbitrary-setTimeout */
|
2016-05-30 03:15:47 +03:00
|
|
|
"use strict";
|
|
|
|
|
2016-07-29 02:27:25 +03:00
|
|
|
Cu.import("resource://gre/modules/AppConstants.jsm");
|
2016-05-30 03:15:47 +03:00
|
|
|
Cu.import("resource://gre/modules/Timer.jsm");
|
|
|
|
|
|
|
|
|
|
|
|
const env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
|
|
|
|
|
2016-07-29 02:27:25 +03:00
|
|
|
const MAX_ROUND_TRIP_TIME_MS = AppConstants.DEBUG || AppConstants.ASAN ? 18 : 9;
|
2016-08-03 01:37:34 +03:00
|
|
|
const MAX_RETRIES = 5;
|
2016-07-29 02:27:25 +03:00
|
|
|
|
2016-05-30 03:15:47 +03:00
|
|
|
let PYTHON;
|
|
|
|
let PYTHON_BIN;
|
|
|
|
let PYTHON_DIR;
|
|
|
|
|
|
|
|
const TEST_SCRIPT = do_get_file("data_test_script.py").path;
|
|
|
|
|
|
|
|
let read = pipe => {
|
|
|
|
return pipe.readUint32().then(count => {
|
|
|
|
return pipe.readString(count);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let readAll = async function(pipe) {
|
2016-05-30 03:15:47 +03:00
|
|
|
let result = [];
|
|
|
|
let string;
|
2017-05-12 15:42:39 +03:00
|
|
|
while ((string = await pipe.readString())) {
|
2016-05-30 03:15:47 +03:00
|
|
|
result.push(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.join("");
|
2017-05-12 15:42:39 +03:00
|
|
|
};
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function setup() {
|
|
|
|
PYTHON = await Subprocess.pathSearch(env.get("PYTHON"));
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
PYTHON_BIN = OS.Path.basename(PYTHON);
|
|
|
|
PYTHON_DIR = OS.Path.dirname(PYTHON);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_io() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
Assert.throws(() => { proc.stdout.read(-1); },
|
|
|
|
/non-negative integer/);
|
|
|
|
Assert.throws(() => { proc.stdout.read(1.1); },
|
|
|
|
/non-negative integer/);
|
|
|
|
|
|
|
|
Assert.throws(() => { proc.stdout.read(Infinity); },
|
|
|
|
/non-negative integer/);
|
|
|
|
Assert.throws(() => { proc.stdout.read(NaN); },
|
|
|
|
/non-negative integer/);
|
|
|
|
|
|
|
|
Assert.throws(() => { proc.stdout.readString(-1); },
|
|
|
|
/non-negative integer/);
|
|
|
|
Assert.throws(() => { proc.stdout.readString(1.1); },
|
|
|
|
/non-negative integer/);
|
|
|
|
|
|
|
|
Assert.throws(() => { proc.stdout.readJSON(-1); },
|
|
|
|
/positive integer/);
|
|
|
|
Assert.throws(() => { proc.stdout.readJSON(0); },
|
|
|
|
/positive integer/);
|
|
|
|
Assert.throws(() => { proc.stdout.readJSON(1.1); },
|
|
|
|
/positive integer/);
|
|
|
|
|
|
|
|
|
|
|
|
const LINE1 = "I'm a leaf on the wind.\n";
|
|
|
|
const LINE2 = "Watch how I soar.\n";
|
|
|
|
|
|
|
|
|
|
|
|
let outputPromise = read(proc.stdout);
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let [output] = await Promise.all([
|
2016-05-30 03:15:47 +03:00
|
|
|
outputPromise,
|
|
|
|
proc.stdin.write(LINE1),
|
|
|
|
]);
|
|
|
|
|
|
|
|
equal(output, LINE1, "Got expected output");
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure it succeeds whether the write comes before or after the
|
|
|
|
// read.
|
|
|
|
let inputPromise = proc.stdin.write(LINE2);
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
[output] = await Promise.all([
|
2016-05-30 03:15:47 +03:00
|
|
|
read(proc.stdout),
|
|
|
|
inputPromise,
|
|
|
|
]);
|
|
|
|
|
|
|
|
equal(output, LINE2, "Got expected output");
|
|
|
|
|
|
|
|
|
|
|
|
let JSON_BLOB = {foo: {bar: "baz"}};
|
|
|
|
|
|
|
|
inputPromise = proc.stdin.write(JSON.stringify(JSON_BLOB) + "\n");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
output = await proc.stdout.readUint32().then(count => {
|
2016-05-30 03:15:47 +03:00
|
|
|
return proc.stdout.readJSON(count);
|
|
|
|
});
|
|
|
|
|
|
|
|
Assert.deepEqual(output, JSON_BLOB, "Got expected JSON output");
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await proc.stdin.close();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_large_io() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
const LINE = "I'm a leaf on the wind.\n";
|
|
|
|
const BUFFER_SIZE = 4096;
|
|
|
|
|
|
|
|
// Create a message that's ~3/4 the input buffer size.
|
|
|
|
let msg = Array(BUFFER_SIZE * .75 / 16 | 0).fill("0123456789abcdef").join("") + "\n";
|
|
|
|
|
|
|
|
// This sequence of writes and reads crosses several buffer size
|
|
|
|
// boundaries, and causes some branches of the read buffer code to be
|
|
|
|
// exercised which are not exercised by other tests.
|
|
|
|
proc.stdin.write(msg);
|
|
|
|
proc.stdin.write(msg);
|
|
|
|
proc.stdin.write(LINE);
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let output = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(output, msg, "Got the expected output");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
output = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(output, msg, "Got the expected output");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
output = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(output, LINE, "Got the expected output");
|
|
|
|
|
|
|
|
proc.stdin.close();
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_huge() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
// This should be large enough to fill most pipe input/output buffers.
|
|
|
|
const MESSAGE_SIZE = 1024 * 16;
|
|
|
|
|
|
|
|
let msg = Array(MESSAGE_SIZE).fill("0123456789abcdef").join("") + "\n";
|
|
|
|
|
|
|
|
proc.stdin.write(msg);
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let output = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(output, msg, "Got the expected output");
|
|
|
|
|
|
|
|
proc.stdin.close();
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_round_trip_perf() {
|
2016-08-03 01:37:34 +03:00
|
|
|
let roundTripTime = Infinity;
|
|
|
|
for (let i = 0; i < MAX_RETRIES && roundTripTime > MAX_ROUND_TRIP_TIME_MS; i++) {
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2016-08-03 01:37:34 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const LINE = "I'm a leaf on the wind.\n";
|
|
|
|
|
|
|
|
let now = Date.now();
|
|
|
|
const COUNT = 1000;
|
2016-11-08 06:34:25 +03:00
|
|
|
for (let j = 0; j < COUNT; j++) {
|
2017-05-12 15:42:39 +03:00
|
|
|
let [output] = await Promise.all([
|
2016-08-03 01:37:34 +03:00
|
|
|
read(proc.stdout),
|
|
|
|
proc.stdin.write(LINE),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// We don't want to log this for every iteration, but we still need
|
|
|
|
// to fail if it goes wrong.
|
|
|
|
if (output !== LINE) {
|
|
|
|
equal(output, LINE, "Got expected output");
|
|
|
|
}
|
|
|
|
}
|
2016-07-29 02:27:25 +03:00
|
|
|
|
2016-08-03 01:37:34 +03:00
|
|
|
roundTripTime = (Date.now() - now) / COUNT;
|
2016-07-29 02:27:25 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await proc.stdin.close();
|
2016-07-29 02:27:25 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-08-03 01:37:34 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
2016-07-29 02:27:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ok(roundTripTime <= MAX_ROUND_TRIP_TIME_MS,
|
|
|
|
`Expected round trip time (${roundTripTime}ms) to be less than ${MAX_ROUND_TRIP_TIME_MS}ms`);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_stderr_default() {
|
2016-05-30 03:15:47 +03:00
|
|
|
const LINE1 = "I'm a leaf on the wind.\n";
|
|
|
|
const LINE2 = "Watch how I soar.\n";
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "print", LINE1, LINE2],
|
|
|
|
});
|
|
|
|
|
|
|
|
equal(proc.stderr, undefined, "There should be no stderr pipe by default");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let stdout = await readAll(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(stdout, LINE1, "Got the expected stdout output");
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_stderr_pipe() {
|
2016-05-30 03:15:47 +03:00
|
|
|
const LINE1 = "I'm a leaf on the wind.\n";
|
|
|
|
const LINE2 = "Watch how I soar.\n";
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "print", LINE1, LINE2],
|
|
|
|
stderr: "pipe",
|
|
|
|
});
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let [stdout, stderr] = await Promise.all([
|
2016-05-30 03:15:47 +03:00
|
|
|
readAll(proc.stdout),
|
|
|
|
readAll(proc.stderr),
|
|
|
|
]);
|
|
|
|
|
|
|
|
equal(stdout, LINE1, "Got the expected stdout output");
|
|
|
|
equal(stderr, LINE2, "Got the expected stderr output");
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_stderr_merged() {
|
2016-05-30 03:15:47 +03:00
|
|
|
const LINE1 = "I'm a leaf on the wind.\n";
|
|
|
|
const LINE2 = "Watch how I soar.\n";
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "print", LINE1, LINE2],
|
|
|
|
stderr: "stdout",
|
|
|
|
});
|
|
|
|
|
|
|
|
equal(proc.stderr, undefined, "There should be no stderr pipe by default");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let stdout = await readAll(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(stdout, LINE1 + LINE2, "Got the expected merged stdout output");
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_read_after_exit() {
|
2016-05-30 03:15:47 +03:00
|
|
|
const LINE1 = "I'm a leaf on the wind.\n";
|
|
|
|
const LINE2 = "Watch how I soar.\n";
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "print", LINE1, LINE2],
|
|
|
|
stderr: "pipe",
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(exitCode, 0, "Process exited with expected code");
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let [stdout, stderr] = await Promise.all([
|
2016-05-30 03:15:47 +03:00
|
|
|
readAll(proc.stdout),
|
|
|
|
readAll(proc.stderr),
|
|
|
|
]);
|
|
|
|
|
|
|
|
equal(stdout, LINE1, "Got the expected stdout output");
|
|
|
|
equal(stderr, LINE2, "Got the expected stderr output");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_lazy_close_output() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
const LINE1 = "I'm a leaf on the wind.\n";
|
|
|
|
const LINE2 = "Watch how I soar.\n";
|
|
|
|
|
|
|
|
let writePromises = [
|
|
|
|
proc.stdin.write(LINE1),
|
|
|
|
proc.stdin.write(LINE2),
|
|
|
|
];
|
|
|
|
let closedPromise = proc.stdin.close();
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let output1 = await read(proc.stdout);
|
|
|
|
let output2 = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await Promise.all([...writePromises, closedPromise]);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(output1, LINE1, "Got expected output");
|
|
|
|
equal(output2, LINE2, "Got expected output");
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_lazy_close_input() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
let readPromise = proc.stdout.readUint32();
|
|
|
|
let closedPromise = proc.stdout.close();
|
|
|
|
|
|
|
|
|
|
|
|
const LINE = "I'm a leaf on the wind.\n";
|
|
|
|
|
|
|
|
proc.stdin.write(LINE);
|
|
|
|
proc.stdin.close();
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let len = await readPromise;
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(len, LINE.length);
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await closedPromise;
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
|
2016-07-29 02:27:25 +03:00
|
|
|
// Don't test for a successful exit here. The process may exit with a
|
|
|
|
// write error if we close the pipe after it's written the message
|
|
|
|
// size but before it's written the message.
|
2017-05-12 15:42:39 +03:00
|
|
|
await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_force_close() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
let readPromise = proc.stdout.readUint32();
|
|
|
|
let closedPromise = proc.stdout.close(true);
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await Assert.rejects(
|
2016-05-30 03:15:47 +03:00
|
|
|
readPromise,
|
|
|
|
function(e) {
|
|
|
|
equal(e.errorCode, Subprocess.ERROR_END_OF_FILE,
|
|
|
|
"Got the expected error code");
|
|
|
|
return /File closed/.test(e.message);
|
|
|
|
},
|
|
|
|
"Promise should be rejected when file is closed");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await closedPromise;
|
|
|
|
await proc.stdin.close();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_eof() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
let readPromise = proc.stdout.readUint32();
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await proc.stdin.close();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await Assert.rejects(
|
2016-05-30 03:15:47 +03:00
|
|
|
readPromise,
|
|
|
|
function(e) {
|
|
|
|
equal(e.errorCode, Subprocess.ERROR_END_OF_FILE,
|
|
|
|
"Got the expected error code");
|
|
|
|
return /File closed/.test(e.message);
|
|
|
|
},
|
|
|
|
"Promise should be rejected on EOF");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_invalid_json() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
const LINE = "I'm a leaf on the wind.\n";
|
|
|
|
|
|
|
|
proc.stdin.write(LINE);
|
|
|
|
proc.stdin.close();
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let count = await proc.stdout.readUint32();
|
2016-05-30 03:15:47 +03:00
|
|
|
let readPromise = proc.stdout.readJSON(count);
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await Assert.rejects(
|
2016-05-30 03:15:47 +03:00
|
|
|
readPromise,
|
|
|
|
function(e) {
|
|
|
|
equal(e.errorCode, Subprocess.ERROR_INVALID_JSON,
|
|
|
|
"Got the expected error code");
|
|
|
|
return /SyntaxError/.test(e);
|
|
|
|
},
|
|
|
|
"Promise should be rejected on EOF");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-05-28 22:28:30 +03:00
|
|
|
if (AppConstants.isPlatformAndVersionAtLeast("win", "6")) {
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_inherited_descriptors() {
|
2017-01-09 13:12:26 +03:00
|
|
|
let {ctypes, libc, win32} = Cu.import("resource://gre/modules/subprocess/subprocess_win.jsm", {});
|
2016-05-28 22:28:30 +03:00
|
|
|
|
|
|
|
let secAttr = new win32.SECURITY_ATTRIBUTES();
|
|
|
|
secAttr.nLength = win32.SECURITY_ATTRIBUTES.size;
|
|
|
|
secAttr.bInheritHandle = true;
|
|
|
|
|
|
|
|
let handles = win32.createPipe(secAttr, 0);
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2016-05-28 22:28:30 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Close the output end of the pipe.
|
|
|
|
// Ours should be the only copy, so reads should fail after this.
|
|
|
|
handles[1].dispose();
|
|
|
|
|
|
|
|
let buffer = new ArrayBuffer(1);
|
|
|
|
let succeeded = libc.ReadFile(handles[0], buffer, buffer.byteLength,
|
|
|
|
null, null);
|
|
|
|
|
|
|
|
ok(!succeeded, "ReadFile should fail on broken pipe");
|
|
|
|
equal(ctypes.winLastError, win32.ERROR_BROKEN_PIPE, "Read should fail with ERROR_BROKEN_PIPE");
|
|
|
|
|
|
|
|
|
|
|
|
proc.stdin.close();
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-28 22:28:30 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_wait() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "exit", "42"],
|
|
|
|
});
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 42, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_pathSearch() {
|
2016-05-30 03:15:47 +03:00
|
|
|
let promise = Subprocess.call({
|
|
|
|
command: PYTHON_BIN,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "exit", "13"],
|
|
|
|
environment: {
|
|
|
|
PATH: PYTHON_DIR,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await Assert.rejects(
|
2016-05-30 03:15:47 +03:00
|
|
|
promise,
|
|
|
|
function(error) {
|
|
|
|
return error.errorCode == Subprocess.ERROR_BAD_EXECUTABLE;
|
|
|
|
},
|
|
|
|
"Subprocess.call should fail for a bad executable");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_workdir() {
|
|
|
|
let procDir = await OS.File.getCurrentDirectory();
|
2017-06-08 03:40:58 +03:00
|
|
|
let tmpDirFile = Components.classes["@mozilla.org/file/local;1"]
|
2017-08-04 11:49:22 +03:00
|
|
|
.createInstance(Components.interfaces.nsIFile);
|
2017-06-08 03:40:58 +03:00
|
|
|
tmpDirFile.initWithPath(OS.Constants.Path.tmpDir);
|
|
|
|
tmpDirFile.normalize();
|
|
|
|
let tmpDir = tmpDirFile.path;
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
notEqual(procDir, tmpDir,
|
|
|
|
"Current process directory must not be the current temp directory");
|
|
|
|
|
2017-05-12 15:45:01 +03:00
|
|
|
async function pwd(options) {
|
|
|
|
let proc = await Subprocess.call(Object.assign({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "pwd"],
|
|
|
|
}, options));
|
|
|
|
|
2016-11-08 06:34:25 +03:00
|
|
|
let pwdOutput = read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:45:01 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
|
2016-11-08 06:34:25 +03:00
|
|
|
return pwdOutput;
|
2016-05-30 03:15:47 +03:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let dir = await pwd({});
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(dir, procDir, "Process should normally launch in current process directory");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
dir = await pwd({workdir: tmpDir});
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(dir, tmpDir, "Process should launch in the directory specified in `workdir`");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
dir = await OS.File.getCurrentDirectory();
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(dir, procDir, "`workdir` should not change the working directory of the current process");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_term() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Windows does not support killing processes gracefully, so they will
|
|
|
|
// always exit with -9 there.
|
|
|
|
let retVal = AppConstants.platform == "win" ? -9 : -15;
|
|
|
|
|
|
|
|
// Kill gracefully with the default timeout of 300ms.
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.kill();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, retVal, "Got expected exit code");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
({exitCode} = await proc.wait());
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, retVal, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_kill() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "echo"],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Force kill with no gracefull termination timeout.
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.kill(0);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, -9, "Got expected exit code");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
({exitCode} = await proc.wait());
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, -9, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_kill_timeout() {
|
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "ignore_sigterm"],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for the process to set up its signal handler and tell us it's
|
|
|
|
// ready.
|
2017-05-12 15:42:39 +03:00
|
|
|
let msg = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(msg, "Ready", "Process is ready");
|
|
|
|
|
|
|
|
// Kill gracefully with the default timeout of 300ms.
|
|
|
|
// Expect a force kill after 300ms, since the process traps SIGTERM.
|
|
|
|
const TIMEOUT = 300;
|
|
|
|
let startTime = Date.now();
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.kill(TIMEOUT);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
// Graceful termination is not supported on Windows, so don't bother
|
|
|
|
// testing the timeout there.
|
|
|
|
if (AppConstants.platform != "win") {
|
|
|
|
let diff = Date.now() - startTime;
|
|
|
|
ok(diff >= TIMEOUT, `Process was killed after ${diff}ms (expected ~${TIMEOUT}ms)`);
|
|
|
|
}
|
|
|
|
|
|
|
|
equal(exitCode, -9, "Got expected exit code");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
({exitCode} = await proc.wait());
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, -9, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_arguments() {
|
2016-05-30 03:15:47 +03:00
|
|
|
let args = [
|
|
|
|
String.raw`C:\Program Files\Company\Program.exe`,
|
|
|
|
String.raw`\\NETWORK SHARE\Foo Directory${"\\"}`,
|
|
|
|
String.raw`foo bar baz`,
|
|
|
|
String.raw`"foo bar baz"`,
|
|
|
|
String.raw`foo " bar`,
|
|
|
|
String.raw`Thing \" with "" "\" \\\" \\\\" quotes\\" \\`,
|
|
|
|
];
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "print_args", ...args],
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let [i, arg] of args.entries()) {
|
2017-05-12 15:42:39 +03:00
|
|
|
let val = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
equal(val, arg, `Got correct value for args[${i}]`);
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-06-29 21:50:39 +03:00
|
|
|
add_task(async function test_subprocess_environment() {
|
|
|
|
let environment = {
|
|
|
|
FOO: "BAR",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Our Windows environment can't handle launching python without
|
|
|
|
// PATH variables.
|
|
|
|
if (AppConstants.platform == "win") {
|
|
|
|
Object.assign(environment, {
|
|
|
|
PATH: env.get("PATH"),
|
|
|
|
PATHEXT: env.get("PATHEXT"),
|
2016-05-30 03:15:47 +03:00
|
|
|
});
|
2017-06-29 21:50:39 +03:00
|
|
|
}
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-06-29 21:50:39 +03:00
|
|
|
env.set("BAR", "BAZ");
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-06-29 21:50:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "env", "FOO", "BAR"],
|
|
|
|
environment,
|
|
|
|
});
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-06-29 21:50:39 +03:00
|
|
|
let foo = await read(proc.stdout);
|
|
|
|
let bar = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-06-29 21:50:39 +03:00
|
|
|
equal(foo, "BAR", "Got expected $FOO value");
|
|
|
|
equal(bar, "", "Got expected $BAR value");
|
|
|
|
|
|
|
|
let {exitCode} = await proc.wait();
|
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_environmentAppend() {
|
2017-07-17 11:57:19 +03:00
|
|
|
env.set("VALUE_FROM_BASE_ENV", "untouched");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
2017-07-17 11:57:19 +03:00
|
|
|
arguments: ["-u", TEST_SCRIPT, "env", "VALUE_FROM_BASE_ENV", "VALUE_APPENDED_ONCE"],
|
2016-05-30 03:15:47 +03:00
|
|
|
environmentAppend: true,
|
|
|
|
environment: {
|
2017-07-17 11:57:19 +03:00
|
|
|
VALUE_APPENDED_ONCE: "soon empty",
|
2016-05-30 03:15:47 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-07-17 11:57:19 +03:00
|
|
|
let valueFromBaseEnv = await read(proc.stdout);
|
|
|
|
let valueAppendedOnce = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-07-17 11:57:19 +03:00
|
|
|
equal(valueFromBaseEnv, "untouched", "Got expected $VALUE_FROM_BASE_ENV value");
|
|
|
|
equal(valueAppendedOnce, "soon empty", "Got expected $VALUE_APPENDED_ONCE value");
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
proc = await Subprocess.call({
|
2016-05-30 03:15:47 +03:00
|
|
|
command: PYTHON,
|
2017-07-17 11:57:19 +03:00
|
|
|
arguments: ["-u", TEST_SCRIPT, "env", "VALUE_FROM_BASE_ENV", "VALUE_APPENDED_ONCE"],
|
2016-05-30 03:15:47 +03:00
|
|
|
environmentAppend: true,
|
|
|
|
});
|
|
|
|
|
2017-07-17 11:57:19 +03:00
|
|
|
valueFromBaseEnv = await read(proc.stdout);
|
|
|
|
valueAppendedOnce = await read(proc.stdout);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-07-17 11:57:19 +03:00
|
|
|
equal(valueFromBaseEnv, "untouched", "Got expected $VALUE_FROM_BASE_ENV value");
|
|
|
|
equal(valueAppendedOnce, "", "Got expected $VALUE_APPENDED_ONCE value");
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
({exitCode} = await proc.wait());
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
|
2017-04-03 04:46:10 +03:00
|
|
|
if (AppConstants.platform !== "win") {
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_subprocess_nonASCII() {
|
2017-04-03 04:46:10 +03:00
|
|
|
const {libc} = Cu.import("resource://gre/modules/subprocess/subprocess_unix.jsm", {});
|
|
|
|
|
|
|
|
// Use TextDecoder rather than a string with a \xff escape, since
|
|
|
|
// the latter will automatically be normalized to valid UTF-8.
|
|
|
|
let val = new TextDecoder().decode(Uint8Array.of(1, 255));
|
|
|
|
|
|
|
|
libc.setenv("FOO", Uint8Array.from(val + "\0", c => c.charCodeAt(0)), 1);
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let proc = await Subprocess.call({
|
2017-04-03 04:46:10 +03:00
|
|
|
command: PYTHON,
|
|
|
|
arguments: ["-u", TEST_SCRIPT, "env", "FOO"],
|
|
|
|
});
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let foo = await read(proc.stdout);
|
2017-04-03 04:46:10 +03:00
|
|
|
|
|
|
|
equal(foo, val, "Got expected $FOO value");
|
|
|
|
|
|
|
|
env.set("FOO", "");
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let {exitCode} = await proc.wait();
|
2017-04-03 04:46:10 +03:00
|
|
|
|
|
|
|
equal(exitCode, 0, "Got expected exit code");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-30 03:15:47 +03:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_bad_executable() {
|
2016-05-30 03:15:47 +03:00
|
|
|
// Test with a non-executable file.
|
|
|
|
|
|
|
|
let textFile = do_get_file("data_text_file.txt").path;
|
|
|
|
|
|
|
|
let promise = Subprocess.call({
|
|
|
|
command: textFile,
|
|
|
|
arguments: [],
|
|
|
|
});
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await Assert.rejects(
|
2016-05-30 03:15:47 +03:00
|
|
|
promise,
|
|
|
|
function(error) {
|
|
|
|
if (AppConstants.platform == "win") {
|
|
|
|
return /Failed to create process/.test(error.message);
|
|
|
|
}
|
|
|
|
return error.errorCode == Subprocess.ERROR_BAD_EXECUTABLE;
|
|
|
|
},
|
|
|
|
"Subprocess.call should fail for a bad executable");
|
|
|
|
|
|
|
|
// Test with a nonexistent file.
|
|
|
|
promise = Subprocess.call({
|
|
|
|
command: textFile + ".doesNotExist",
|
|
|
|
arguments: [],
|
|
|
|
});
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await Assert.rejects(
|
2016-05-30 03:15:47 +03:00
|
|
|
promise,
|
|
|
|
function(error) {
|
|
|
|
return error.errorCode == Subprocess.ERROR_BAD_EXECUTABLE;
|
|
|
|
},
|
|
|
|
"Subprocess.call should fail for a bad executable");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test_cleanup() {
|
2017-01-09 13:12:26 +03:00
|
|
|
let {SubprocessImpl} = Cu.import("resource://gre/modules/Subprocess.jsm", {});
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
let worker = SubprocessImpl.Process.getWorker();
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
let openFiles = await worker.call("getOpenFiles", []);
|
|
|
|
let processes = await worker.call("getProcesses", []);
|
2016-05-30 03:15:47 +03:00
|
|
|
|
|
|
|
equal(openFiles.size, 0, "No remaining open files");
|
|
|
|
equal(processes.size, 0, "No remaining processes");
|
|
|
|
});
|