api-utility-process-spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import { expect } from 'chai';
  2. import * as childProcess from 'child_process';
  3. import * as path from 'path';
  4. import { BrowserWindow, MessageChannelMain, utilityProcess, app } from 'electron/main';
  5. import { ifit } from './lib/spec-helpers';
  6. import { closeWindow } from './lib/window-helpers';
  7. import { once } from 'events';
  8. import { setImmediate } from 'timers/promises';
  9. const fixturesPath = path.resolve(__dirname, 'fixtures', 'api', 'utility-process');
  10. const isWindowsOnArm = process.platform === 'win32' && process.arch === 'arm64';
  11. const isWindows32Bit = process.platform === 'win32' && process.arch === 'ia32';
  12. describe('utilityProcess module', () => {
  13. describe('UtilityProcess constructor', () => {
  14. it('throws when empty script path is provided', async () => {
  15. expect(() => {
  16. utilityProcess.fork('');
  17. }).to.throw();
  18. });
  19. it('throws when options.stdio is not valid', async () => {
  20. expect(() => {
  21. utilityProcess.fork(path.join(fixturesPath, 'empty.js'), [], {
  22. execArgv: ['--test', '--test2'],
  23. serviceName: 'test',
  24. stdio: 'ipc'
  25. });
  26. }).to.throw(/stdio must be of the following values: inherit, pipe, ignore/);
  27. expect(() => {
  28. utilityProcess.fork(path.join(fixturesPath, 'empty.js'), [], {
  29. execArgv: ['--test', '--test2'],
  30. serviceName: 'test',
  31. stdio: ['ignore', 'ignore']
  32. });
  33. }).to.throw(/configuration missing for stdin, stdout or stderr/);
  34. expect(() => {
  35. utilityProcess.fork(path.join(fixturesPath, 'empty.js'), [], {
  36. execArgv: ['--test', '--test2'],
  37. serviceName: 'test',
  38. stdio: ['pipe', 'inherit', 'inherit']
  39. });
  40. }).to.throw(/stdin value other than ignore is not supported/);
  41. });
  42. });
  43. describe('lifecycle events', () => {
  44. it('emits \'spawn\' when child process successfully launches', async () => {
  45. const child = utilityProcess.fork(path.join(fixturesPath, 'empty.js'));
  46. await once(child, 'spawn');
  47. });
  48. it('emits \'exit\' when child process exits gracefully', async () => {
  49. const child = utilityProcess.fork(path.join(fixturesPath, 'empty.js'));
  50. const [code] = await once(child, 'exit');
  51. expect(code).to.equal(0);
  52. });
  53. ifit(!isWindows32Bit)('emits \'exit\' when child process crashes', async () => {
  54. const child = utilityProcess.fork(path.join(fixturesPath, 'crash.js'));
  55. // Do not check for exit code in this case,
  56. // SIGSEGV code can be 139 or 11 across our different CI pipeline.
  57. await once(child, 'exit');
  58. });
  59. ifit(!isWindows32Bit)('emits \'exit\' corresponding to the child process', async () => {
  60. const child1 = utilityProcess.fork(path.join(fixturesPath, 'endless.js'));
  61. await once(child1, 'spawn');
  62. const child2 = utilityProcess.fork(path.join(fixturesPath, 'crash.js'));
  63. await once(child2, 'exit');
  64. expect(child1.kill()).to.be.true();
  65. await once(child1, 'exit');
  66. });
  67. it('emits \'exit\' when there is uncaught exception', async () => {
  68. const child = utilityProcess.fork(path.join(fixturesPath, 'exception.js'));
  69. const [code] = await once(child, 'exit');
  70. expect(code).to.equal(1);
  71. });
  72. it('emits \'exit\' when process.exit is called', async () => {
  73. const exitCode = 2;
  74. const child = utilityProcess.fork(path.join(fixturesPath, 'custom-exit.js'), [`--exitCode=${exitCode}`]);
  75. const [code] = await once(child, 'exit');
  76. expect(code).to.equal(exitCode);
  77. });
  78. });
  79. describe('app \'child-process-gone\' event', () => {
  80. ifit(!isWindows32Bit)('with default serviceName', async () => {
  81. utilityProcess.fork(path.join(fixturesPath, 'crash.js'));
  82. const [, details] = await once(app, 'child-process-gone') as [any, Electron.Details];
  83. expect(details.type).to.equal('Utility');
  84. expect(details.serviceName).to.equal('node.mojom.NodeService');
  85. expect(details.name).to.equal('Node Utility Process');
  86. expect(details.reason).to.be.oneOf(['crashed', 'abnormal-exit']);
  87. });
  88. ifit(!isWindows32Bit)('with custom serviceName', async () => {
  89. utilityProcess.fork(path.join(fixturesPath, 'crash.js'), [], { serviceName: 'Hello World!' });
  90. const [, details] = await once(app, 'child-process-gone') as [any, Electron.Details];
  91. expect(details.type).to.equal('Utility');
  92. expect(details.serviceName).to.equal('node.mojom.NodeService');
  93. expect(details.name).to.equal('Hello World!');
  94. expect(details.reason).to.be.oneOf(['crashed', 'abnormal-exit']);
  95. });
  96. });
  97. describe('app.getAppMetrics()', () => {
  98. it('with default serviceName', async () => {
  99. const child = utilityProcess.fork(path.join(fixturesPath, 'endless.js'));
  100. await once(child, 'spawn');
  101. expect(child.pid).to.not.be.null();
  102. await setImmediate();
  103. const details = app.getAppMetrics().find(item => item.pid === child.pid)!;
  104. expect(details).to.be.an('object');
  105. expect(details.type).to.equal('Utility');
  106. expect(details.serviceName).to.to.equal('node.mojom.NodeService');
  107. expect(details.name).to.equal('Node Utility Process');
  108. });
  109. it('with custom serviceName', async () => {
  110. const child = utilityProcess.fork(path.join(fixturesPath, 'endless.js'), [], { serviceName: 'Hello World!' });
  111. await once(child, 'spawn');
  112. expect(child.pid).to.not.be.null();
  113. await setImmediate();
  114. const details = app.getAppMetrics().find(item => item.pid === child.pid)!;
  115. expect(details).to.be.an('object');
  116. expect(details.type).to.equal('Utility');
  117. expect(details.serviceName).to.to.equal('node.mojom.NodeService');
  118. expect(details.name).to.equal('Hello World!');
  119. });
  120. });
  121. describe('kill() API', () => {
  122. it('terminates the child process gracefully', async () => {
  123. const child = utilityProcess.fork(path.join(fixturesPath, 'endless.js'), [], {
  124. serviceName: 'endless'
  125. });
  126. await once(child, 'spawn');
  127. expect(child.kill()).to.be.true();
  128. await once(child, 'exit');
  129. });
  130. });
  131. describe('pid property', () => {
  132. it('is valid when child process launches successfully', async () => {
  133. const child = utilityProcess.fork(path.join(fixturesPath, 'empty.js'));
  134. await once(child, 'spawn');
  135. expect(child.pid).to.not.be.null();
  136. });
  137. it('is undefined when child process fails to launch', async () => {
  138. const child = utilityProcess.fork(path.join(fixturesPath, 'does-not-exist.js'));
  139. expect(child.pid).to.be.undefined();
  140. });
  141. });
  142. describe('stdout property', () => {
  143. it('is null when child process launches with default stdio', async () => {
  144. const child = utilityProcess.fork(path.join(fixturesPath, 'log.js'));
  145. await once(child, 'spawn');
  146. expect(child.stdout).to.be.null();
  147. expect(child.stderr).to.be.null();
  148. await once(child, 'exit');
  149. });
  150. it('is null when child process launches with ignore stdio configuration', async () => {
  151. const child = utilityProcess.fork(path.join(fixturesPath, 'log.js'), [], {
  152. stdio: 'ignore'
  153. });
  154. await once(child, 'spawn');
  155. expect(child.stdout).to.be.null();
  156. expect(child.stderr).to.be.null();
  157. await once(child, 'exit');
  158. });
  159. it('is valid when child process launches with pipe stdio configuration', async () => {
  160. const child = utilityProcess.fork(path.join(fixturesPath, 'log.js'), [], {
  161. stdio: 'pipe'
  162. });
  163. await once(child, 'spawn');
  164. expect(child.stdout).to.not.be.null();
  165. let log = '';
  166. child.stdout!.on('data', (chunk) => {
  167. log += chunk.toString('utf8');
  168. });
  169. await once(child, 'exit');
  170. expect(log).to.equal('hello\n');
  171. });
  172. });
  173. describe('stderr property', () => {
  174. it('is null when child process launches with default stdio', async () => {
  175. const child = utilityProcess.fork(path.join(fixturesPath, 'log.js'));
  176. await once(child, 'spawn');
  177. expect(child.stdout).to.be.null();
  178. expect(child.stderr).to.be.null();
  179. await once(child, 'exit');
  180. });
  181. it('is null when child process launches with ignore stdio configuration', async () => {
  182. const child = utilityProcess.fork(path.join(fixturesPath, 'log.js'), [], {
  183. stdio: 'ignore'
  184. });
  185. await once(child, 'spawn');
  186. expect(child.stderr).to.be.null();
  187. await once(child, 'exit');
  188. });
  189. ifit(!isWindowsOnArm)('is valid when child process launches with pipe stdio configuration', async () => {
  190. const child = utilityProcess.fork(path.join(fixturesPath, 'log.js'), [], {
  191. stdio: ['ignore', 'pipe', 'pipe']
  192. });
  193. await once(child, 'spawn');
  194. expect(child.stderr).to.not.be.null();
  195. let log = '';
  196. child.stderr!.on('data', (chunk) => {
  197. log += chunk.toString('utf8');
  198. });
  199. await once(child, 'exit');
  200. expect(log).to.equal('world');
  201. });
  202. });
  203. describe('postMessage() API', () => {
  204. it('establishes a default ipc channel with the child process', async () => {
  205. const result = 'I will be echoed.';
  206. const child = utilityProcess.fork(path.join(fixturesPath, 'post-message.js'));
  207. await once(child, 'spawn');
  208. child.postMessage(result);
  209. const [data] = await once(child, 'message');
  210. expect(data).to.equal(result);
  211. const exit = once(child, 'exit');
  212. expect(child.kill()).to.be.true();
  213. await exit;
  214. });
  215. it('supports queuing messages on the receiving end', async () => {
  216. const child = utilityProcess.fork(path.join(fixturesPath, 'post-message-queue.js'));
  217. const p = once(child, 'spawn');
  218. child.postMessage('This message');
  219. child.postMessage(' is');
  220. child.postMessage(' queued');
  221. await p;
  222. const [data] = await once(child, 'message');
  223. expect(data).to.equal('This message is queued');
  224. const exit = once(child, 'exit');
  225. expect(child.kill()).to.be.true();
  226. await exit;
  227. });
  228. });
  229. describe('behavior', () => {
  230. it('supports starting the v8 inspector with --inspect-brk', (done) => {
  231. const child = utilityProcess.fork(path.join(fixturesPath, 'log.js'), [], {
  232. stdio: 'pipe',
  233. execArgv: ['--inspect-brk']
  234. });
  235. let output = '';
  236. const cleanup = () => {
  237. child.stderr!.removeListener('data', listener);
  238. child.stdout!.removeListener('data', listener);
  239. child.once('exit', () => { done(); });
  240. child.kill();
  241. };
  242. const listener = (data: Buffer) => {
  243. output += data;
  244. if (/Debugger listening on ws:/m.test(output)) {
  245. cleanup();
  246. }
  247. };
  248. child.stderr!.on('data', listener);
  249. child.stdout!.on('data', listener);
  250. });
  251. it('supports starting the v8 inspector with --inspect and a provided port', (done) => {
  252. const child = utilityProcess.fork(path.join(fixturesPath, 'log.js'), [], {
  253. stdio: 'pipe',
  254. execArgv: ['--inspect=17364']
  255. });
  256. let output = '';
  257. const cleanup = () => {
  258. child.stderr!.removeListener('data', listener);
  259. child.stdout!.removeListener('data', listener);
  260. child.once('exit', () => { done(); });
  261. child.kill();
  262. };
  263. const listener = (data: Buffer) => {
  264. output += data;
  265. if (/Debugger listening on ws:/m.test(output)) {
  266. expect(output.trim()).to.contain(':17364', 'should be listening on port 17364');
  267. cleanup();
  268. }
  269. };
  270. child.stderr!.on('data', listener);
  271. child.stdout!.on('data', listener);
  272. });
  273. it('supports changing dns verbatim with --dns-result-order', (done) => {
  274. const child = utilityProcess.fork(path.join(fixturesPath, 'dns-result-order.js'), [], {
  275. stdio: 'pipe',
  276. execArgv: ['--dns-result-order=ipv4first']
  277. });
  278. let output = '';
  279. const cleanup = () => {
  280. child.stderr!.removeListener('data', listener);
  281. child.stdout!.removeListener('data', listener);
  282. child.once('exit', () => { done(); });
  283. child.kill();
  284. };
  285. const listener = (data: Buffer) => {
  286. output += data;
  287. expect(output.trim()).to.contain('ipv4first', 'default verbatim should be ipv4first');
  288. cleanup();
  289. };
  290. child.stderr!.on('data', listener);
  291. child.stdout!.on('data', listener);
  292. });
  293. ifit(process.platform !== 'win32')('supports redirecting stdout to parent process', async () => {
  294. const result = 'Output from utility process';
  295. const appProcess = childProcess.spawn(process.execPath, [path.join(fixturesPath, 'inherit-stdout'), `--payload=${result}`]);
  296. let output = '';
  297. appProcess.stdout.on('data', (data: Buffer) => { output += data; });
  298. await once(appProcess, 'exit');
  299. expect(output).to.equal(result);
  300. });
  301. ifit(process.platform !== 'win32')('supports redirecting stderr to parent process', async () => {
  302. const result = 'Error from utility process';
  303. const appProcess = childProcess.spawn(process.execPath, [path.join(fixturesPath, 'inherit-stderr'), `--payload=${result}`]);
  304. let output = '';
  305. appProcess.stderr.on('data', (data: Buffer) => { output += data; });
  306. await once(appProcess, 'exit');
  307. expect(output).to.include(result);
  308. });
  309. it('can establish communication channel with sandboxed renderer', async () => {
  310. const result = 'Message from sandboxed renderer';
  311. const w = new BrowserWindow({
  312. show: false,
  313. webPreferences: {
  314. preload: path.join(fixturesPath, 'preload.js')
  315. }
  316. });
  317. await w.loadFile(path.join(__dirname, 'fixtures', 'blank.html'));
  318. // Create Message port pair for Renderer <-> Utility Process.
  319. const { port1: rendererPort, port2: childPort1 } = new MessageChannelMain();
  320. w.webContents.postMessage('port', result, [rendererPort]);
  321. // Send renderer and main channel port to utility process.
  322. const child = utilityProcess.fork(path.join(fixturesPath, 'receive-message.js'));
  323. await once(child, 'spawn');
  324. child.postMessage('', [childPort1]);
  325. const [data] = await once(child, 'message');
  326. expect(data).to.equal(result);
  327. // Cleanup.
  328. const exit = once(child, 'exit');
  329. expect(child.kill()).to.be.true();
  330. await exit;
  331. await closeWindow(w);
  332. });
  333. ifit(process.platform === 'linux')('allows executing a setuid binary with child_process', async () => {
  334. const child = utilityProcess.fork(path.join(fixturesPath, 'suid.js'));
  335. await once(child, 'spawn');
  336. const [data] = await once(child, 'message');
  337. expect(data).to.not.be.empty();
  338. const exit = once(child, 'exit');
  339. expect(child.kill()).to.be.true();
  340. await exit;
  341. });
  342. it('inherits parent env as default', async () => {
  343. const appProcess = childProcess.spawn(process.execPath, [path.join(fixturesPath, 'env-app')], {
  344. env: {
  345. FROM: 'parent',
  346. ...process.env
  347. }
  348. });
  349. let output = '';
  350. appProcess.stdout.on('data', (data: Buffer) => { output += data; });
  351. await once(appProcess.stdout, 'end');
  352. const result = process.platform === 'win32' ? '\r\nparent' : 'parent';
  353. expect(output).to.equal(result);
  354. });
  355. it('does not inherit parent env when custom env is provided', async () => {
  356. const appProcess = childProcess.spawn(process.execPath, [path.join(fixturesPath, 'env-app'), '--create-custom-env'], {
  357. env: {
  358. FROM: 'parent',
  359. ...process.env
  360. }
  361. });
  362. let output = '';
  363. appProcess.stdout.on('data', (data: Buffer) => { output += data; });
  364. await once(appProcess.stdout, 'end');
  365. const result = process.platform === 'win32' ? '\r\nchild' : 'child';
  366. expect(output).to.equal(result);
  367. });
  368. it('changes working directory with cwd', async () => {
  369. const child = utilityProcess.fork('./log.js', [], {
  370. cwd: fixturesPath,
  371. stdio: ['ignore', 'pipe', 'ignore']
  372. });
  373. await once(child, 'spawn');
  374. expect(child.stdout).to.not.be.null();
  375. let log = '';
  376. child.stdout!.on('data', (chunk) => {
  377. log += chunk.toString('utf8');
  378. });
  379. await once(child, 'exit');
  380. expect(log).to.equal('hello\n');
  381. });
  382. it('does not crash when running eval', async () => {
  383. const child = utilityProcess.fork('./eval.js', [], {
  384. cwd: fixturesPath,
  385. stdio: 'ignore'
  386. });
  387. await once(child, 'spawn');
  388. const [data] = await once(child, 'message');
  389. expect(data).to.equal(42);
  390. // Cleanup.
  391. const exit = once(child, 'exit');
  392. expect(child.kill()).to.be.true();
  393. await exit;
  394. });
  395. });
  396. });