11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ciconst makeDuplexPair = require('../common/duplexpair');
81cb0ef41Sopenharmony_ciconst tls = require('tls');
91cb0ef41Sopenharmony_ciconst net = require('net');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// This test ensures that `bufferSize` also works for those tlsSockets
121cb0ef41Sopenharmony_ci// created from `socket` of `Duplex`, with which, TLSSocket will wrap
131cb0ef41Sopenharmony_ci// sockets in `StreamWrap`.
141cb0ef41Sopenharmony_ci{
151cb0ef41Sopenharmony_ci  const iter = 10;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  function createDuplex(port) {
181cb0ef41Sopenharmony_ci    const { clientSide, serverSide } = makeDuplexPair();
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    return new Promise((resolve, reject) => {
211cb0ef41Sopenharmony_ci      const socket = net.connect({
221cb0ef41Sopenharmony_ci        port,
231cb0ef41Sopenharmony_ci      }, common.mustCall(() => {
241cb0ef41Sopenharmony_ci        clientSide.pipe(socket);
251cb0ef41Sopenharmony_ci        socket.pipe(clientSide);
261cb0ef41Sopenharmony_ci        clientSide.on('close', common.mustCall(() => socket.destroy()));
271cb0ef41Sopenharmony_ci        socket.on('close', common.mustCall(() => clientSide.destroy()));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci        resolve(serverSide);
301cb0ef41Sopenharmony_ci      }));
311cb0ef41Sopenharmony_ci    });
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  const server = tls.createServer({
351cb0ef41Sopenharmony_ci    key: fixtures.readKey('agent2-key.pem'),
361cb0ef41Sopenharmony_ci    cert: fixtures.readKey('agent2-cert.pem')
371cb0ef41Sopenharmony_ci  }, common.mustCall((socket) => {
381cb0ef41Sopenharmony_ci    let str = '';
391cb0ef41Sopenharmony_ci    socket.setEncoding('utf-8');
401cb0ef41Sopenharmony_ci    socket.on('data', (chunk) => { str += chunk; });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    socket.on('end', common.mustCall(() => {
431cb0ef41Sopenharmony_ci      assert.strictEqual(str, 'a'.repeat(iter - 1));
441cb0ef41Sopenharmony_ci      server.close();
451cb0ef41Sopenharmony_ci    }));
461cb0ef41Sopenharmony_ci  }));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
491cb0ef41Sopenharmony_ci    const { port } = server.address();
501cb0ef41Sopenharmony_ci    createDuplex(port).then((socket) => {
511cb0ef41Sopenharmony_ci      const client = tls.connect({
521cb0ef41Sopenharmony_ci        socket,
531cb0ef41Sopenharmony_ci        rejectUnauthorized: false,
541cb0ef41Sopenharmony_ci      }, common.mustCall(() => {
551cb0ef41Sopenharmony_ci        assert.strictEqual(client.bufferSize, 0);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci        for (let i = 1; i < iter; i++) {
581cb0ef41Sopenharmony_ci          client.write('a');
591cb0ef41Sopenharmony_ci          assert.strictEqual(client.bufferSize, i);
601cb0ef41Sopenharmony_ci        }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci        client.on('end', common.mustCall());
631cb0ef41Sopenharmony_ci        client.on('close', common.mustCall(() => {
641cb0ef41Sopenharmony_ci          assert.strictEqual(client.bufferSize, undefined);
651cb0ef41Sopenharmony_ci        }));
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci        client.end();
681cb0ef41Sopenharmony_ci      }));
691cb0ef41Sopenharmony_ci    });
701cb0ef41Sopenharmony_ci  }));
711cb0ef41Sopenharmony_ci}
72