node/test/parallel/test-tls-set-secure-context.js
Daniel Bevenius 640fe94354 src,test: support dynamically linking OpenSSL 3.0
This commit enables node to dynamically link against OpenSSL 3.0.

The motivation for opening this PR even though OpenSSL 3.0 has not been
released yet is to allow a nightly CI job to be created. This will
allow us stay on top of changes required for OpenSSL 3.0, and also to
make sure that changes to node crypto do not cause issues when linking
to OpenSSL 3.0.

PR-URL: https://github.com/nodejs/node/pull/37669
Refs: https://github.com/nodejs/node/issues/29817
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
2021-03-16 05:59:25 +01:00

109 lines
2.8 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
// This test verifies the behavior of the tls setSecureContext() method.
// It also verifies that existing connections are not disrupted when the
// secure context is changed.
const assert = require('assert');
const https = require('https');
const fixtures = require('../common/fixtures');
const credentialOptions = [
{
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem'),
ca: fixtures.readKey('ca1-cert.pem')
},
{
key: fixtures.readKey('agent2-key.pem'),
cert: fixtures.readKey('agent2-cert.pem'),
ca: fixtures.readKey('ca2-cert.pem')
}
];
let firstResponse;
const server = https.createServer(credentialOptions[0], (req, res) => {
const id = +req.headers.id;
if (id === 1) {
firstResponse = res;
firstResponse.write('multi-');
return;
} else if (id === 4) {
firstResponse.write('success-');
}
res.end('success');
});
server.listen(0, common.mustCall(() => {
const { port } = server.address();
const firstRequest = makeRequest(port, 1);
async function makeRemainingRequests() {
// Wait until the first request is guaranteed to have been handled.
if (!firstResponse) {
return setImmediate(makeRemainingRequests);
}
assert.strictEqual(await makeRequest(port, 2), 'success');
server.setSecureContext(credentialOptions[1]);
firstResponse.write('request-');
const errorMessageRegex = common.hasOpenSSL3 ?
/^Error: self-signed certificate$/ :
/^Error: self signed certificate$/;
await assert.rejects(async () => {
await makeRequest(port, 3);
}, errorMessageRegex);
server.setSecureContext(credentialOptions[0]);
assert.strictEqual(await makeRequest(port, 4), 'success');
server.setSecureContext(credentialOptions[1]);
firstResponse.end('fun!');
await assert.rejects(async () => {
await makeRequest(port, 5);
}, errorMessageRegex);
assert.strictEqual(await firstRequest, 'multi-request-success-fun!');
server.close();
}
makeRemainingRequests();
}));
function makeRequest(port, id) {
return new Promise((resolve, reject) => {
const options = {
rejectUnauthorized: true,
ca: credentialOptions[0].ca,
servername: 'agent1',
headers: { id }
};
let errored = false;
https.get(`https://localhost:${port}`, options, (res) => {
let response = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
response += chunk;
});
res.on('end', common.mustCall(() => {
resolve(response);
}));
}).on('error', (err) => {
errored = true;
reject(err);
}).on('finish', () => {
assert.strictEqual(errored, false);
});
});
}