-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
test-tls-getprotocol.js
45 lines (40 loc) · 1.36 KB
/
test-tls-getprotocol.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
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
// This test ensures that `getProtocol` returns the right protocol
// from a TLS connection
const assert = require('assert');
const tls = require('tls');
const fixtures = require('../common/fixtures');
const clientConfigs = [
{ secureProtocol: 'TLSv1_method', version: 'TLSv1' },
{ secureProtocol: 'TLSv1_1_method', version: 'TLSv1.1' },
{ secureProtocol: 'TLSv1_2_method', version: 'TLSv1.2' },
];
const serverConfig = {
secureProtocol: 'TLS_method',
ciphers: 'RSA@SECLEVEL=0',
key: fixtures.readKey('agent2-key.pem'),
cert: fixtures.readKey('agent2-cert.pem')
};
const server = tls.createServer(serverConfig, common.mustCall(clientConfigs.length))
.listen(0, common.localhostIPv4, function() {
let connected = 0;
clientConfigs.forEach(function(v) {
tls.connect({
host: common.localhostIPv4,
port: server.address().port,
rejectUnauthorized: false,
secureProtocol: v.secureProtocol
}, common.mustCall(function() {
assert.strictEqual(this.getProtocol(), v.version);
this.on('end', common.mustCall());
this.on('close', common.mustCall(function() {
assert.strictEqual(this.getProtocol(), null);
})).end();
if (++connected === clientConfigs.length)
server.close();
}));
});
});