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';
23const {
24  mustCall,
25  mustCallAtLeast,
26} = require('../common');
27const assert = require('assert');
28const fork = require('child_process').fork;
29const net = require('net');
30const debug = require('util').debuglog('test');
31
32if (process.argv[2] === 'child') {
33
34  const onSocket = mustCall((msg, socket) => {
35    if (msg.what !== 'socket') return;
36    process.removeListener('message', onSocket);
37    socket.end('echo');
38    debug('CHILD: got socket');
39  });
40
41  process.on('message', onSocket);
42
43  process.send({ what: 'ready' });
44} else {
45
46  const child = fork(process.argv[1], ['child']);
47
48  child.on('exit', mustCall((code, signal) => {
49    const message = `CHILD: died with ${code}, ${signal}`;
50    assert.strictEqual(code, 0, message);
51  }));
52
53  // Send net.Socket to child.
54  function testSocket() {
55
56    // Create a new server and connect to it,
57    // but the socket will be handled by the child.
58    const server = net.createServer();
59    server.on('connection', mustCall((socket) => {
60      // TODO(@jasnell): Close does not seem to actually be called.
61      // It is not clear if it is needed.
62      socket.on('close', () => {
63        debug('CLIENT: socket closed');
64      });
65      child.send({ what: 'socket' }, socket);
66    }));
67    server.on('close', mustCall(() => {
68      debug('PARENT: server closed');
69    }));
70
71    server.listen(0, mustCall(() => {
72      debug('testSocket, listening');
73      const connect = net.connect(server.address().port);
74      let store = '';
75      connect.on('data', mustCallAtLeast((chunk) => {
76        store += chunk;
77        debug('CLIENT: got data');
78      }));
79      connect.on('close', mustCall(() => {
80        debug('CLIENT: closed');
81        assert.strictEqual(store, 'echo');
82        server.close();
83      }));
84    }));
85  }
86
87  const onReady = mustCall((msg) => {
88    if (msg.what !== 'ready') return;
89    child.removeListener('message', onReady);
90
91    testSocket();
92  });
93
94  // Create socket and send it to child.
95  child.on('message', onReady);
96}
97