11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// This test verifies that `tls.connect()` honors the `allowHalfOpen` option.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (!common.hasCrypto)
81cb0ef41Sopenharmony_ci  common.skip('missing crypto');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
121cb0ef41Sopenharmony_ciconst tls = require('tls');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci{
151cb0ef41Sopenharmony_ci  const socket = tls.connect({ port: 42, lookup() {} });
161cb0ef41Sopenharmony_ci  assert.strictEqual(socket.allowHalfOpen, false);
171cb0ef41Sopenharmony_ci}
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  const socket = tls.connect({ port: 42, allowHalfOpen: false, lookup() {} });
211cb0ef41Sopenharmony_ci  assert.strictEqual(socket.allowHalfOpen, false);
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst server = tls.createServer({
251cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
261cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem'),
271cb0ef41Sopenharmony_ci}, common.mustCall((socket) => {
281cb0ef41Sopenharmony_ci  server.close();
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  let message = '';
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  socket.setEncoding('utf8');
331cb0ef41Sopenharmony_ci  socket.on('data', (chunk) => {
341cb0ef41Sopenharmony_ci    message += chunk;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    if (message === 'Hello') {
371cb0ef41Sopenharmony_ci      socket.end(message);
381cb0ef41Sopenharmony_ci      message = '';
391cb0ef41Sopenharmony_ci    }
401cb0ef41Sopenharmony_ci  });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  socket.on('end', common.mustCall(() => {
431cb0ef41Sopenharmony_ci    assert.strictEqual(message, 'Bye');
441cb0ef41Sopenharmony_ci  }));
451cb0ef41Sopenharmony_ci}));
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
481cb0ef41Sopenharmony_ci  const socket = tls.connect({
491cb0ef41Sopenharmony_ci    port: server.address().port,
501cb0ef41Sopenharmony_ci    rejectUnauthorized: false,
511cb0ef41Sopenharmony_ci    allowHalfOpen: true,
521cb0ef41Sopenharmony_ci  }, common.mustCall(() => {
531cb0ef41Sopenharmony_ci    let message = '';
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    socket.on('data', (chunk) => {
561cb0ef41Sopenharmony_ci      message += chunk;
571cb0ef41Sopenharmony_ci    });
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    socket.on('end', common.mustCall(() => {
601cb0ef41Sopenharmony_ci      assert.strictEqual(message, 'Hello');
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci      setTimeout(() => {
631cb0ef41Sopenharmony_ci        assert(socket.writable);
641cb0ef41Sopenharmony_ci        assert(socket.write('Bye'));
651cb0ef41Sopenharmony_ci        socket.end();
661cb0ef41Sopenharmony_ci      }, 50);
671cb0ef41Sopenharmony_ci    }));
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    socket.write('Hello');
701cb0ef41Sopenharmony_ci  }));
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  socket.setEncoding('utf8');
731cb0ef41Sopenharmony_ci}));
74