11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst tls = require('tls');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
91cb0ef41Sopenharmony_ciconst { getEventListeners, once } = require('events');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst serverOptions = {
121cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
131cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
141cb0ef41Sopenharmony_ci};
151cb0ef41Sopenharmony_ciconst server = tls.createServer(serverOptions);
161cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(async () => {
171cb0ef41Sopenharmony_ci  const port = server.address().port;
181cb0ef41Sopenharmony_ci  const host = 'localhost';
191cb0ef41Sopenharmony_ci  const connectOptions = (signal) => ({
201cb0ef41Sopenharmony_ci    port,
211cb0ef41Sopenharmony_ci    host,
221cb0ef41Sopenharmony_ci    signal,
231cb0ef41Sopenharmony_ci    rejectUnauthorized: false,
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const assertAbort = async (socket, testName) => {
271cb0ef41Sopenharmony_ci    try {
281cb0ef41Sopenharmony_ci      await once(socket, 'close');
291cb0ef41Sopenharmony_ci      assert.fail(`close ${testName} should have thrown`);
301cb0ef41Sopenharmony_ci    } catch (err) {
311cb0ef41Sopenharmony_ci      assert.strictEqual(err.name, 'AbortError');
321cb0ef41Sopenharmony_ci    }
331cb0ef41Sopenharmony_ci  };
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  async function postAbort() {
361cb0ef41Sopenharmony_ci    const ac = new AbortController();
371cb0ef41Sopenharmony_ci    const { signal } = ac;
381cb0ef41Sopenharmony_ci    const socket = tls.connect(connectOptions(signal));
391cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
401cb0ef41Sopenharmony_ci    ac.abort();
411cb0ef41Sopenharmony_ci    await assertAbort(socket, 'postAbort');
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  async function preAbort() {
451cb0ef41Sopenharmony_ci    const ac = new AbortController();
461cb0ef41Sopenharmony_ci    const { signal } = ac;
471cb0ef41Sopenharmony_ci    ac.abort();
481cb0ef41Sopenharmony_ci    const socket = tls.connect(connectOptions(signal));
491cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
501cb0ef41Sopenharmony_ci    await assertAbort(socket, 'preAbort');
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  async function tickAbort() {
541cb0ef41Sopenharmony_ci    const ac = new AbortController();
551cb0ef41Sopenharmony_ci    const { signal } = ac;
561cb0ef41Sopenharmony_ci    const socket = tls.connect(connectOptions(signal));
571cb0ef41Sopenharmony_ci    setImmediate(() => ac.abort());
581cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
591cb0ef41Sopenharmony_ci    await assertAbort(socket, 'tickAbort');
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  async function testConstructor() {
631cb0ef41Sopenharmony_ci    const ac = new AbortController();
641cb0ef41Sopenharmony_ci    const { signal } = ac;
651cb0ef41Sopenharmony_ci    ac.abort();
661cb0ef41Sopenharmony_ci    const socket = new tls.TLSSocket(undefined, connectOptions(signal));
671cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
681cb0ef41Sopenharmony_ci    await assertAbort(socket, 'testConstructor');
691cb0ef41Sopenharmony_ci  }
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  async function testConstructorPost() {
721cb0ef41Sopenharmony_ci    const ac = new AbortController();
731cb0ef41Sopenharmony_ci    const { signal } = ac;
741cb0ef41Sopenharmony_ci    const socket = new tls.TLSSocket(undefined, connectOptions(signal));
751cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
761cb0ef41Sopenharmony_ci    ac.abort();
771cb0ef41Sopenharmony_ci    await assertAbort(socket, 'testConstructorPost');
781cb0ef41Sopenharmony_ci  }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  async function testConstructorPostTick() {
811cb0ef41Sopenharmony_ci    const ac = new AbortController();
821cb0ef41Sopenharmony_ci    const { signal } = ac;
831cb0ef41Sopenharmony_ci    const socket = new tls.TLSSocket(undefined, connectOptions(signal));
841cb0ef41Sopenharmony_ci    setImmediate(() => ac.abort());
851cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
861cb0ef41Sopenharmony_ci    await assertAbort(socket, 'testConstructorPostTick');
871cb0ef41Sopenharmony_ci  }
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  await postAbort();
901cb0ef41Sopenharmony_ci  await preAbort();
911cb0ef41Sopenharmony_ci  await tickAbort();
921cb0ef41Sopenharmony_ci  await testConstructor();
931cb0ef41Sopenharmony_ci  await testConstructorPost();
941cb0ef41Sopenharmony_ci  await testConstructorPostTick();
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  server.close(common.mustCall());
971cb0ef41Sopenharmony_ci}));
98