11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciif (!common.hasCrypto) common.skip('missing crypto'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 71cb0ef41Sopenharmony_ciconst makeDuplexPair = require('../common/duplexpair'); 81cb0ef41Sopenharmony_ciconst net = require('net'); 91cb0ef41Sopenharmony_ciconst assert = require('assert'); 101cb0ef41Sopenharmony_ciconst tls = require('tls'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_citls.DEFAULT_MAX_VERSION = 'TLSv1.3'; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci// This test ensures that an instance of StreamWrap should emit "end" and 151cb0ef41Sopenharmony_ci// "close" when the socket on the other side call `destroy()` instead of 161cb0ef41Sopenharmony_ci// `end()`. 171cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/14605 181cb0ef41Sopenharmony_ciconst CONTENT = 'Hello World'; 191cb0ef41Sopenharmony_ciconst tlsServer = tls.createServer( 201cb0ef41Sopenharmony_ci { 211cb0ef41Sopenharmony_ci key: fixtures.readKey('rsa_private.pem'), 221cb0ef41Sopenharmony_ci cert: fixtures.readKey('rsa_cert.crt'), 231cb0ef41Sopenharmony_ci ca: [fixtures.readKey('rsa_ca.crt')], 241cb0ef41Sopenharmony_ci }, 251cb0ef41Sopenharmony_ci (socket) => { 261cb0ef41Sopenharmony_ci socket.on('close', common.mustCall()); 271cb0ef41Sopenharmony_ci socket.write(CONTENT); 281cb0ef41Sopenharmony_ci socket.destroy(); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci socket.on('error', (err) => { 311cb0ef41Sopenharmony_ci // destroy() is sync, write() is async, whether write completes depends 321cb0ef41Sopenharmony_ci // on the protocol, it is not guaranteed by stream API. 331cb0ef41Sopenharmony_ci if (err.code === 'ERR_STREAM_DESTROYED') 341cb0ef41Sopenharmony_ci return; 351cb0ef41Sopenharmony_ci assert.ifError(err); 361cb0ef41Sopenharmony_ci }); 371cb0ef41Sopenharmony_ci }, 381cb0ef41Sopenharmony_ci); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciconst server = net.createServer((conn) => { 411cb0ef41Sopenharmony_ci conn.on('error', common.mustNotCall()); 421cb0ef41Sopenharmony_ci // Assume that we want to use data to determine what to do with connections. 431cb0ef41Sopenharmony_ci conn.once('data', common.mustCall((chunk) => { 441cb0ef41Sopenharmony_ci const { clientSide, serverSide } = makeDuplexPair(); 451cb0ef41Sopenharmony_ci serverSide.on('close', common.mustCall(() => { 461cb0ef41Sopenharmony_ci conn.destroy(); 471cb0ef41Sopenharmony_ci })); 481cb0ef41Sopenharmony_ci clientSide.pipe(conn); 491cb0ef41Sopenharmony_ci conn.pipe(clientSide); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci conn.on('close', common.mustCall(() => { 521cb0ef41Sopenharmony_ci clientSide.destroy(); 531cb0ef41Sopenharmony_ci })); 541cb0ef41Sopenharmony_ci clientSide.on('close', common.mustCall(() => { 551cb0ef41Sopenharmony_ci conn.destroy(); 561cb0ef41Sopenharmony_ci })); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci process.nextTick(() => { 591cb0ef41Sopenharmony_ci conn.unshift(chunk); 601cb0ef41Sopenharmony_ci }); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci tlsServer.emit('connection', serverSide); 631cb0ef41Sopenharmony_ci })); 641cb0ef41Sopenharmony_ci}); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciserver.listen(0, () => { 671cb0ef41Sopenharmony_ci const port = server.address().port; 681cb0ef41Sopenharmony_ci const conn = tls.connect({ port, rejectUnauthorized: false }, () => { 691cb0ef41Sopenharmony_ci // Whether the server's write() completed before its destroy() is 701cb0ef41Sopenharmony_ci // indeterminate, but if data was written, we should receive it correctly. 711cb0ef41Sopenharmony_ci conn.on('data', (data) => { 721cb0ef41Sopenharmony_ci assert.strictEqual(data.toString('utf8'), CONTENT); 731cb0ef41Sopenharmony_ci }); 741cb0ef41Sopenharmony_ci conn.on('error', common.mustNotCall()); 751cb0ef41Sopenharmony_ci conn.on('close', common.mustCall(() => server.close())); 761cb0ef41Sopenharmony_ci }); 771cb0ef41Sopenharmony_ci}); 78