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  primary();
35
36function primary() {
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, [
40    '--expose-internals', __filename, 'worker',
41  ], {
42    stdio: ['pipe', 'pipe', 'pipe', 'ipc']
43  });
44  let handle = null;
45  proc.on('exit', () => {
46    handle.close();
47  });
48  proc.stdout.on('data', common.mustCall((data) => {
49    assert.strictEqual(data.toString(), 'ok\r\n');
50    net.createServer(common.mustNotCall()).listen(0, function() {
51      handle = this._handle;
52      proc.send('one');
53      proc.send('two', handle);
54      proc.send('three');
55      proc.stdin.write('ok\r\n');
56    });
57  }));
58  proc.stderr.pipe(process.stderr);
59}
60
61function worker() {
62  const { kChannelHandle } = require('internal/child_process');
63  process[kChannelHandle].readStop();  // Make messages batch up.
64  process.stdout.ref();
65  process.stdout.write('ok\r\n');
66  process.stdin.once('data', common.mustCall((data) => {
67    assert.strictEqual(data.toString(), 'ok\r\n');
68    process[kChannelHandle].readStart();
69  }));
70  let n = 0;
71  process.on('message', common.mustCall((msg, handle) => {
72    n += 1;
73    if (n === 1) {
74      assert.strictEqual(msg, 'one');
75      assert.strictEqual(handle, undefined);
76    } else if (n === 2) {
77      assert.strictEqual(msg, 'two');
78      assert.ok(handle !== null && typeof handle === 'object');
79      handle.close();
80    } else if (n === 3) {
81      assert.strictEqual(msg, 'three');
82      assert.strictEqual(handle, undefined);
83      process.exit();
84    }
85  }, 3));
86}
87