forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-tls-socket-close.js
58 lines (47 loc) · 1.52 KB
/
test-tls-socket-close.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const tls = require('tls');
const net = require('net');
const fixtures = require('../common/fixtures');
const key = fixtures.readKey('agent2-key.pem');
const cert = fixtures.readKey('agent2-cert.pem');
let serverTlsSocket;
const tlsServer = tls.createServer({ cert, key }, (socket) => {
serverTlsSocket = socket;
});
// A plain net server, that manually passes connections to the TLS
// server to be upgraded
let netSocket;
const netServer = net.createServer((socket) => {
tlsServer.emit('connection', socket);
netSocket = socket;
}).listen(0, common.mustCall(function() {
connectClient(netServer);
}));
// A client that connects, sends one message, and closes the raw connection:
function connectClient(server) {
const clientTlsSocket = tls.connect({
host: 'localhost',
port: server.address().port,
rejectUnauthorized: false
});
clientTlsSocket.write('foo', 'utf8', common.mustCall(() => {
assert(netSocket);
netSocket.setTimeout(common.platformTimeout(10), common.mustCall(() => {
assert(serverTlsSocket);
netSocket.destroy();
setImmediate(() => {
assert.strictEqual(netSocket.destroyed, true);
assert.strictEqual(clientTlsSocket.destroyed, true);
setImmediate(() => {
assert.strictEqual(serverTlsSocket.destroyed, true);
tlsServer.close();
netServer.close();
});
});
}));
}));
}