11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Verify that stdout is never read from.
61cb0ef41Sopenharmony_ciconst net = require('net');
71cb0ef41Sopenharmony_ciconst read = net.Socket.prototype.read;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cinet.Socket.prototype.read = function() {
101cb0ef41Sopenharmony_ci  if (this.fd === 1)
111cb0ef41Sopenharmony_ci    throw new Error('reading from stdout!');
121cb0ef41Sopenharmony_ci  if (this.fd === 2)
131cb0ef41Sopenharmony_ci    throw new Error('reading from stderr!');
141cb0ef41Sopenharmony_ci  return read.apply(this, arguments);
151cb0ef41Sopenharmony_ci};
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciif (process.argv[2] === 'child')
181cb0ef41Sopenharmony_ci  child();
191cb0ef41Sopenharmony_cielse
201cb0ef41Sopenharmony_ci  parent();
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cifunction parent() {
231cb0ef41Sopenharmony_ci  const spawn = require('child_process').spawn;
241cb0ef41Sopenharmony_ci  const node = process.execPath;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const c1 = spawn(node, [__filename, 'child']);
271cb0ef41Sopenharmony_ci  let c1out = '';
281cb0ef41Sopenharmony_ci  c1.stdout.setEncoding('utf8');
291cb0ef41Sopenharmony_ci  c1.stdout.on('data', function(chunk) {
301cb0ef41Sopenharmony_ci    c1out += chunk;
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci  c1.stderr.setEncoding('utf8');
331cb0ef41Sopenharmony_ci  c1.stderr.on('data', function(chunk) {
341cb0ef41Sopenharmony_ci    console.error(`c1err: ${chunk.split('\n').join('\nc1err: ')}`);
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci  c1.on('close', common.mustCall(function(code, signal) {
371cb0ef41Sopenharmony_ci    assert(!code);
381cb0ef41Sopenharmony_ci    assert(!signal);
391cb0ef41Sopenharmony_ci    assert.strictEqual(c1out, 'ok\n');
401cb0ef41Sopenharmony_ci    console.log('ok');
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  const c2 = spawn(node, ['-e', 'console.log("ok")']);
441cb0ef41Sopenharmony_ci  let c2out = '';
451cb0ef41Sopenharmony_ci  c2.stdout.setEncoding('utf8');
461cb0ef41Sopenharmony_ci  c2.stdout.on('data', function(chunk) {
471cb0ef41Sopenharmony_ci    c2out += chunk;
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci  c1.stderr.setEncoding('utf8');
501cb0ef41Sopenharmony_ci  c1.stderr.on('data', function(chunk) {
511cb0ef41Sopenharmony_ci    console.error(`c1err: ${chunk.split('\n').join('\nc1err: ')}`);
521cb0ef41Sopenharmony_ci  });
531cb0ef41Sopenharmony_ci  c2.on('close', common.mustCall(function(code, signal) {
541cb0ef41Sopenharmony_ci    assert(!code);
551cb0ef41Sopenharmony_ci    assert(!signal);
561cb0ef41Sopenharmony_ci    assert.strictEqual(c2out, 'ok\n');
571cb0ef41Sopenharmony_ci    console.log('ok');
581cb0ef41Sopenharmony_ci  }));
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cifunction child() {
621cb0ef41Sopenharmony_ci  // Should not be reading *ever* in here.
631cb0ef41Sopenharmony_ci  net.Socket.prototype.read = function() {
641cb0ef41Sopenharmony_ci    throw new Error('no reading allowed in child');
651cb0ef41Sopenharmony_ci  };
661cb0ef41Sopenharmony_ci  console.log('ok');
671cb0ef41Sopenharmony_ci}
68