11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// This test ensures that Node.js doesn't incur a segfault while accessing
81cb0ef41Sopenharmony_ci// TLSWrap fields after the parent handle was destroyed.
91cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/5108
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst tls = require('tls');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst options = {
151cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
161cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
171cb0ef41Sopenharmony_ci};
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst server = tls.createServer(options, function(s) {
201cb0ef41Sopenharmony_ci  s.end('hello');
211cb0ef41Sopenharmony_ci}).listen(0, function() {
221cb0ef41Sopenharmony_ci  const opts = {
231cb0ef41Sopenharmony_ci    port: this.address().port,
241cb0ef41Sopenharmony_ci    rejectUnauthorized: false
251cb0ef41Sopenharmony_ci  };
261cb0ef41Sopenharmony_ci  const client = tls.connect(opts, function() {
271cb0ef41Sopenharmony_ci    putImmediate(client);
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci  client.resume();
301cb0ef41Sopenharmony_ci});
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction putImmediate(client) {
331cb0ef41Sopenharmony_ci  setImmediate(function() {
341cb0ef41Sopenharmony_ci    if (client.ssl) {
351cb0ef41Sopenharmony_ci      const fd = client.ssl.fd;
361cb0ef41Sopenharmony_ci      assert(!!fd);
371cb0ef41Sopenharmony_ci      putImmediate(client);
381cb0ef41Sopenharmony_ci    } else {
391cb0ef41Sopenharmony_ci      server.close();
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci  });
421cb0ef41Sopenharmony_ci}
43