• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23// Test that a Linux specific quirk in the handle passing protocol is handled
24// correctly. See https://github.com/joyent/node/issues/5330 for details.
25
26const common = require('../common');
27const assert = require('assert');
28const net = require('net');
29const spawn = require('child_process').spawn;
30
31if (process.argv[2] === 'worker')
32  worker();
33else
34  master();
35
36function master() {
37  // spawn() can only create one IPC channel so we use stdin/stdout as an
38  // ad-hoc command channel.
39  const proc = spawn(process.execPath, [__filename, 'worker'], {
40    stdio: ['pipe', 'pipe', 'pipe', 'ipc']
41  });
42  let handle = null;
43  proc.on('exit', () => {
44    handle.close();
45  });
46  proc.stdout.on('data', common.mustCall((data) => {
47    assert.strictEqual(data.toString(), 'ok\r\n');
48    net.createServer(common.mustNotCall()).listen(0, function() {
49      handle = this._handle;
50      proc.send('one');
51      proc.send('two', handle);
52      proc.send('three');
53      proc.stdin.write('ok\r\n');
54    });
55  }));
56  proc.stderr.pipe(process.stderr);
57}
58
59function worker() {
60  process.channel.readStop();  // Make messages batch up.
61  process.stdout.ref();
62  process.stdout.write('ok\r\n');
63  process.stdin.once('data', common.mustCall((data) => {
64    assert.strictEqual(data.toString(), 'ok\r\n');
65    process.channel.readStart();
66  }));
67  let n = 0;
68  process.on('message', common.mustCall((msg, handle) => {
69    n += 1;
70    if (n === 1) {
71      assert.strictEqual(msg, 'one');
72      assert.strictEqual(handle, undefined);
73    } else if (n === 2) {
74      assert.strictEqual(msg, 'two');
75      assert.ok(handle !== null && typeof handle === 'object');
76      handle.close();
77    } else if (n === 3) {
78      assert.strictEqual(msg, 'three');
79      assert.strictEqual(handle, undefined);
80      process.exit();
81    }
82  }, 3));
83}
84