summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/unit/test_nsIProcess.js
blob: 582d10440c3e7ba08463311f48b74ed3be901e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// nsIProcess unit test
const TEST_ARGS = [
  "mozilla",
  "firefox",
  "thunderbird",
  "seamonkey",
  "foo",
  "bar",
  "argument with spaces",
  '"argument with quotes"',
];

const TEST_UNICODE_ARGS = [
  "M\u00F8z\u00EEll\u00E5",
  "\u041C\u043E\u0437\u0438\u043B\u043B\u0430",
  "\u09AE\u09CB\u099C\u09BF\u09B2\u09BE",
  "\uD808\uDE2C\uD808\uDF63\uD808\uDDB7",
];

// test if a process can be started, polled for its running status
// and then killed
function test_kill() {
  var file = get_test_program("TestBlockingProcess");

  var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  process.init(file);

  Assert.ok(!process.isRunning);

  try {
    process.kill();
    do_throw("Attempting to kill a not-running process should throw");
  } catch (e) {}

  process.run(false, [], 0);

  Assert.ok(process.isRunning);

  process.kill();

  Assert.ok(!process.isRunning);

  try {
    process.kill();
    do_throw("Attempting to kill a not-running process should throw");
  } catch (e) {}
}

// test if we can get an exit value from an application that is
// guaranteed to return an exit value of 42
function test_quick() {
  var file = get_test_program("TestQuickReturn");

  var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  process.init(file);

  // to get an exit value it must be a blocking process
  process.run(true, [], 0);

  Assert.equal(process.exitValue, 42);
}

function test_args(file, args, argsAreASCII) {
  var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  process.init(file);

  if (argsAreASCII) {
    process.run(true, args, args.length);
  } else {
    process.runw(true, args, args.length);
  }

  Assert.equal(process.exitValue, 0);
}

// test if an argument can be successfully passed to an application
// that will return 0 if "mozilla" is the only argument
function test_arguments() {
  test_args(get_test_program("TestArguments"), TEST_ARGS, true);
}

// test if Unicode arguments can be successfully passed to an application
function test_unicode_arguments() {
  test_args(get_test_program("TestUnicodeArguments"), TEST_UNICODE_ARGS, false);
}

function rename_and_test(asciiName, unicodeName, args, argsAreASCII) {
  var asciiFile = get_test_program(asciiName);
  var asciiLeaf = asciiFile.leafName;
  var unicodeLeaf = asciiLeaf.replace(asciiName, unicodeName);

  asciiFile.moveTo(null, unicodeLeaf);

  var unicodeFile = get_test_program(unicodeName);

  test_args(unicodeFile, args, argsAreASCII);

  unicodeFile.moveTo(null, asciiLeaf);
}

// test passing ASCII and Unicode arguments to an application with a Unicode name
function test_unicode_app() {
  rename_and_test(
    "TestArguments",
    // "Unicode" in Tamil
    "\u0BAF\u0BC1\u0BA9\u0BBF\u0B95\u0BCB\u0B9F\u0BCD",
    TEST_ARGS,
    true
  );

  rename_and_test(
    "TestUnicodeArguments",
    // "Unicode" in Thai
    "\u0E22\u0E39\u0E19\u0E34\u0E42\u0E04\u0E14",
    TEST_UNICODE_ARGS,
    false
  );
}

// test if we get notified about a blocking process
function test_notify_blocking() {
  var file = get_test_program("TestQuickReturn");

  var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  process.init(file);

  process.runAsync([], 0, {
    observe(subject, topic, data) {
      process = subject.QueryInterface(Ci.nsIProcess);
      Assert.equal(topic, "process-failed");
      Assert.equal(process.exitValue, 42);
      test_notify_nonblocking();
    },
  });
}

// test if we get notified about a non-blocking process
function test_notify_nonblocking() {
  var file = get_test_program("TestArguments");

  var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  process.init(file);

  process.runAsync(TEST_ARGS, TEST_ARGS.length, {
    observe(subject, topic, data) {
      process = subject.QueryInterface(Ci.nsIProcess);
      Assert.equal(topic, "process-finished");
      Assert.equal(process.exitValue, 0);
      test_notify_killed();
    },
  });
}

// test if we get notified about a killed process
function test_notify_killed() {
  var file = get_test_program("TestBlockingProcess");

  var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  process.init(file);

  process.runAsync([], 0, {
    observe(subject, topic, data) {
      process = subject.QueryInterface(Ci.nsIProcess);
      Assert.equal(topic, "process-failed");
      do_test_finished();
    },
  });

  process.kill();
}

function run_test() {
  set_process_running_environment();
  test_kill();
  test_quick();
  test_arguments();
  test_unicode_arguments();
  test_unicode_app();
  do_test_pending();
  test_notify_blocking();
}