11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst tls = require('tls');
81cb0ef41Sopenharmony_ciconst stream = require('stream');
91cb0ef41Sopenharmony_ciconst net = require('net');
101cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst options = { key: fixtures.readKey('rsa_private.pem'),
131cb0ef41Sopenharmony_ci                  cert: fixtures.readKey('rsa_cert.crt'),
141cb0ef41Sopenharmony_ci                  ca: [ fixtures.readKey('rsa_ca.crt') ],
151cb0ef41Sopenharmony_ci                  ciphers: 'AES256-GCM-SHA384' };
161cb0ef41Sopenharmony_ciconst content = 'hello world';
171cb0ef41Sopenharmony_ciconst recv_bufs = [];
181cb0ef41Sopenharmony_cilet send_data = '';
191cb0ef41Sopenharmony_ciconst server = tls.createServer(options, function(s) {
201cb0ef41Sopenharmony_ci  s.on('data', function(c) {
211cb0ef41Sopenharmony_ci    recv_bufs.push(c);
221cb0ef41Sopenharmony_ci  });
231cb0ef41Sopenharmony_ci});
241cb0ef41Sopenharmony_ciserver.listen(0, function() {
251cb0ef41Sopenharmony_ci  const raw = net.connect(this.address().port);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  let pending = false;
281cb0ef41Sopenharmony_ci  raw.on('readable', function() {
291cb0ef41Sopenharmony_ci    if (pending)
301cb0ef41Sopenharmony_ci      p._read();
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  const p = new stream.Duplex({
341cb0ef41Sopenharmony_ci    read: function read() {
351cb0ef41Sopenharmony_ci      pending = false;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci      const chunk = raw.read();
381cb0ef41Sopenharmony_ci      if (chunk) {
391cb0ef41Sopenharmony_ci        this.push(chunk);
401cb0ef41Sopenharmony_ci      } else {
411cb0ef41Sopenharmony_ci        pending = true;
421cb0ef41Sopenharmony_ci      }
431cb0ef41Sopenharmony_ci    },
441cb0ef41Sopenharmony_ci    write: function write(data, enc, cb) {
451cb0ef41Sopenharmony_ci      raw.write(data, enc, cb);
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  const socket = tls.connect({
501cb0ef41Sopenharmony_ci    socket: p,
511cb0ef41Sopenharmony_ci    rejectUnauthorized: false
521cb0ef41Sopenharmony_ci  }, function() {
531cb0ef41Sopenharmony_ci    for (let i = 0; i < 50; ++i) {
541cb0ef41Sopenharmony_ci      socket.write(content);
551cb0ef41Sopenharmony_ci      send_data += content;
561cb0ef41Sopenharmony_ci    }
571cb0ef41Sopenharmony_ci    socket.end();
581cb0ef41Sopenharmony_ci    server.close();
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci});
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciprocess.on('exit', function() {
631cb0ef41Sopenharmony_ci  const recv_data = (Buffer.concat(recv_bufs)).toString();
641cb0ef41Sopenharmony_ci  assert.strictEqual(send_data, recv_data);
651cb0ef41Sopenharmony_ci});
66