11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst net = require('net');
101cb0ef41Sopenharmony_ciconst stream = require('stream');
111cb0ef41Sopenharmony_ciconst tls = require('tls');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst server = tls.createServer({
141cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
151cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
161cb0ef41Sopenharmony_ci}, common.mustCall(function(c) {
171cb0ef41Sopenharmony_ci  console.log('new client');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  c.resume();
201cb0ef41Sopenharmony_ci  c.end('ohai');
211cb0ef41Sopenharmony_ci})).listen(0, common.mustCall(function() {
221cb0ef41Sopenharmony_ci  const raw = net.connect(this.address().port);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  let pending = false;
251cb0ef41Sopenharmony_ci  raw.on('readable', function() {
261cb0ef41Sopenharmony_ci    if (pending)
271cb0ef41Sopenharmony_ci      p._read();
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  raw.on('end', function() {
311cb0ef41Sopenharmony_ci    p.push(null);
321cb0ef41Sopenharmony_ci  });
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  const p = new stream.Duplex({
351cb0ef41Sopenharmony_ci    read: function read() {
361cb0ef41Sopenharmony_ci      pending = false;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci      const chunk = raw.read();
391cb0ef41Sopenharmony_ci      if (chunk) {
401cb0ef41Sopenharmony_ci        console.log('read', chunk);
411cb0ef41Sopenharmony_ci        this.push(chunk);
421cb0ef41Sopenharmony_ci      } else {
431cb0ef41Sopenharmony_ci        pending = true;
441cb0ef41Sopenharmony_ci      }
451cb0ef41Sopenharmony_ci    },
461cb0ef41Sopenharmony_ci    write: function write(data, enc, cb) {
471cb0ef41Sopenharmony_ci      console.log('write', data, enc);
481cb0ef41Sopenharmony_ci      raw.write(data, enc, cb);
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci  });
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  const socket = tls.connect({
531cb0ef41Sopenharmony_ci    socket: p,
541cb0ef41Sopenharmony_ci    rejectUnauthorized: false
551cb0ef41Sopenharmony_ci  }, common.mustCall(function() {
561cb0ef41Sopenharmony_ci    console.log('client secure');
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    socket.resume();
591cb0ef41Sopenharmony_ci    socket.end('hello');
601cb0ef41Sopenharmony_ci  }));
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  socket.once('close', function() {
631cb0ef41Sopenharmony_ci    console.log('client close');
641cb0ef41Sopenharmony_ci    server.close();
651cb0ef41Sopenharmony_ci  });
661cb0ef41Sopenharmony_ci}));
67