1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "MPL"); you may not use this file
6
* except in compliance with the MPL. You may obtain a copy of
7
* the MPL at http://www.mozilla.org/MPL/
9
* Software distributed under the MPL is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the MPL for the specific language governing
12
* rights and limitations under the MPL.
14
* The Original Code is ipc-pipe.
16
* The Initial Developer of the Original Code is Patrick Brunschwig.
17
* Portions created by Patrick Brunschwig <patrick@mozilla-enigmail.org> are
18
* Copyright (C) 2010 Patrick Brunschwig. All Rights Reserved.
22
* Alternatively, the contents of this file may be used under the terms of
23
* either the GNU General Public License Version 2 or later (the "GPL"), or
24
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25
* in which case the provisions of the GPL or the LGPL are applicable instead
26
* of those above. If you wish to allow use of your version of this file only
27
* under the terms of either the GPL or the LGPL, and not to allow others to
28
* use your version of this file under the terms of the MPL, indicate your
29
* decision by deleting the provisions above and replace them with the notice
30
* and other provisions required by the GPL or the LGPL. If you do not delete
31
* the provisions above, a recipient may use your version of this file under
32
* the terms of any one of the MPL, the GPL or the LGPL.
33
* ***** END LICENSE BLOCK ***** */
36
* This file tests the implementation of subprocess.jsm
39
Components.utils.import("resource://gre/modules/subprocess.jsm");
41
const Cc = Components.classes;
42
const Ci = Components.interfaces;
49
var isWindows = ("@mozilla.org/windows-registry-key;1" in Components.classes);
50
var dataFile = do_get_file("ipc-data.txt" , true);
52
var processDir = do_get_cwd();
53
var cmd = processDir.clone();
54
cmd.append("IpcCat" + (isWindows ? ".exe" : ""));
57
do_throw("Could not locate the IpcCat helper executable\n");
59
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
60
getService(Ci.nsIProperties).
61
QueryInterface(Ci.nsIDirectoryService);
62
var greDir = dirSvc.get("GreD", Ci.nsIFile);
66
"DYLD_LIBRARY_PATH="+greDir.path, // for Mac
67
"LD_LIBRARY_PATH="+greDir.path // for Linux
70
var eol = isWindows ? "\r\n" : "\n";
71
gTestLines = [ "Writing example data"+eol,
72
"Writing something more"+eol,
73
"And yet some more text"+eol ];
75
/////////////////////////////////////////////////////////////////
76
// Test standard scenario
77
/////////////////////////////////////////////////////////////////
80
var p = subprocess.call({
82
arguments: [ 'dump' ],
84
stdin: subprocess.WritablePipe(function() {
85
for (var i=0; i < gTestLines.length; i++) {
86
this.write(gTestLines[i]);
90
stdout: subprocess.ReadablePipe(function (data) {
93
stderr: subprocess.ReadablePipe(function(data) {
94
let len = gTestLines.join("").length;
95
if (isWindows) len -= gTestLines.length;
96
do_check_eq("Starting dump\nDumped "+len+" bytes\n",
97
data.replace(/\r\n/g, "\n"));
99
onFinished: subprocess.Terminate(function() {
100
do_check_eq(0, this.exitCode);
106
do_check_eq(gTestLines.join(""), gResultData);
109
/////////////////////////////////////////////////////////////////
110
// Test mergeStderr=true & stdin as string
111
/////////////////////////////////////////////////////////////////
114
p = subprocess.call({
116
arguments: [ 'dump' ],
117
environment: envList,
118
stdin: gTestLines.join(""),
119
stdout: subprocess.ReadablePipe(function (data) {
122
stderr: subprocess.ReadablePipe(function(data) {
123
do_throw("Got unexpected data '"+data+"' on stderr\n");
125
onFinished: subprocess.Terminate(function() {
126
do_check_eq(0, this.exitCode);
132
do_check_eq(gTestLines.join("").length + (isWindows ? 32 : 30), gResultData.length);
135
/////////////////////////////////////////////////////////////////
136
// Test with cwd & no stderr
137
/////////////////////////////////////////////////////////////////
140
var p = subprocess.call({
142
arguments: [ 'dump' ],
143
environment: envList,
144
cwd: do_get_file(".", true),
145
stdin: subprocess.WritablePipe(function() {
146
for (var i=0; i < gTestLines.length; i++) {
147
this.write(gTestLines[i]);
150
onFinished: subprocess.Terminate(function() {
151
gResultData = this.stdoutData;
152
do_check_eq(0, this.exitCode);
158
do_check_eq(gTestLines.join(""), gResultData);
160
/////////////////////////////////////////////////////////////////
161
// Test exit code != 0
162
/////////////////////////////////////////////////////////////////
165
var p = subprocess.call({
167
arguments: [ 'wrong', 'arguments' ],
168
environment: envList,
170
stdout: subprocess.ReadablePipe(function (data) {
173
stderr: subprocess.ReadablePipe(function(data) {
174
do_check_eq(0, data.length);
176
onFinished: subprocess.Terminate(function() {
177
do_check_eq(4, this.exitCode);
183
do_check_eq("", gResultData);
186
/////////////////////////////////////////////////////////////////
187
// Test minimal scenario with stdout only
188
/////////////////////////////////////////////////////////////////
191
var p = subprocess.call({
193
arguments: [ 'write', dataFile.path ],
194
stdin: gTestLines.join("")
199
var p = subprocess.call({
201
arguments: [ 'read', dataFile.path ],
202
environment: envList,
203
stdout: subprocess.ReadablePipe(function (data) {
209
do_check_eq(gTestLines.join(""), gResultData);
211
/////////////////////////////////////////////////////////////////
212
// Test minimal scenario with onFinished only
213
/////////////////////////////////////////////////////////////////
215
var p = subprocess.call({
217
arguments: [ 'read', dataFile.path ],
218
environment: envList,
219
onFinished: subprocess.Terminate(function() {
220
gResultData = this.stdoutData;
221
do_check_eq(0, this.exitCode);
226
do_check_eq(gTestLines.join(""), gResultData);
229
/////////////////////////////////////////////////////////////////
230
// Test environment variables
231
/////////////////////////////////////////////////////////////////
233
gTestLines= [ "This is test variable" ];
234
envList.push("TESTVAR="+gTestLines[0]);
237
var p = subprocess.call({
239
arguments: [ 'getenv', 'TESTVAR' ],
240
cwd: do_get_file(".", true),
241
environment: envList,
242
onFinished: subprocess.Terminate(function() {
243
gResultData = this.stdoutData;
244
do_check_eq(0, this.exitCode);
250
do_check_eq(gTestLines.join(""), gResultData);