1'use strict'; 2const common = require('../common'); 3if (!common.hasCrypto) 4 common.skip('missing crypto'); 5 6const tls = require('tls'); 7const assert = require('assert'); 8const fixtures = require('../common/fixtures'); 9const { getEventListeners, once } = require('events'); 10 11const serverOptions = { 12 key: fixtures.readKey('agent1-key.pem'), 13 cert: fixtures.readKey('agent1-cert.pem') 14}; 15const server = tls.createServer(serverOptions); 16server.listen(0, common.mustCall(async () => { 17 const port = server.address().port; 18 const host = 'localhost'; 19 const connectOptions = (signal) => ({ 20 port, 21 host, 22 signal, 23 rejectUnauthorized: false, 24 }); 25 26 const assertAbort = async (socket, testName) => { 27 try { 28 await once(socket, 'close'); 29 assert.fail(`close ${testName} should have thrown`); 30 } catch (err) { 31 assert.strictEqual(err.name, 'AbortError'); 32 } 33 }; 34 35 async function postAbort() { 36 const ac = new AbortController(); 37 const { signal } = ac; 38 const socket = tls.connect(connectOptions(signal)); 39 assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 40 ac.abort(); 41 await assertAbort(socket, 'postAbort'); 42 } 43 44 async function preAbort() { 45 const ac = new AbortController(); 46 const { signal } = ac; 47 ac.abort(); 48 const socket = tls.connect(connectOptions(signal)); 49 assert.strictEqual(getEventListeners(signal, 'abort').length, 0); 50 await assertAbort(socket, 'preAbort'); 51 } 52 53 async function tickAbort() { 54 const ac = new AbortController(); 55 const { signal } = ac; 56 const socket = tls.connect(connectOptions(signal)); 57 setImmediate(() => ac.abort()); 58 assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 59 await assertAbort(socket, 'tickAbort'); 60 } 61 62 async function testConstructor() { 63 const ac = new AbortController(); 64 const { signal } = ac; 65 ac.abort(); 66 const socket = new tls.TLSSocket(undefined, connectOptions(signal)); 67 assert.strictEqual(getEventListeners(signal, 'abort').length, 0); 68 await assertAbort(socket, 'testConstructor'); 69 } 70 71 async function testConstructorPost() { 72 const ac = new AbortController(); 73 const { signal } = ac; 74 const socket = new tls.TLSSocket(undefined, connectOptions(signal)); 75 assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 76 ac.abort(); 77 await assertAbort(socket, 'testConstructorPost'); 78 } 79 80 async function testConstructorPostTick() { 81 const ac = new AbortController(); 82 const { signal } = ac; 83 const socket = new tls.TLSSocket(undefined, connectOptions(signal)); 84 setImmediate(() => ac.abort()); 85 assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 86 await assertAbort(socket, 'testConstructorPostTick'); 87 } 88 89 await postAbort(); 90 await preAbort(); 91 await tickAbort(); 92 await testConstructor(); 93 await testConstructorPost(); 94 await testConstructorPostTick(); 95 96 server.close(common.mustCall()); 97})); 98