test: simplify test-tls-alert

Avoid the process 'exit' event handler and use execFile instead of
manual stream operations.

Refs: https://github.com/nodejs/node/pull/46751
PR-URL: https://github.com/nodejs/node/pull/46805
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Tobias Nießen 2023-02-25 19:19:19 +01:00 committed by GitHub
parent 4881770bb3
commit 78b911ddaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,12 +28,10 @@ if (!common.opensslCli)
common.skip('node compiled without OpenSSL CLI.');
const assert = require('assert');
const { spawn } = require('child_process');
const { execFile } = require('child_process');
const tls = require('tls');
const fixtures = require('../common/fixtures');
let success = false;
function loadPEM(n) {
return fixtures.readKey(`${n}.pem`);
}
@ -42,21 +40,13 @@ const server = tls.Server({
secureProtocol: 'TLSv1_2_server_method',
key: loadPEM('agent2-key'),
cert: loadPEM('agent2-cert')
}, null).listen(0, function() {
}, null).listen(0, common.mustCall(() => {
const args = ['s_client', '-quiet', '-tls1_1',
'-connect', `127.0.0.1:${this.address().port}`];
'-connect', `127.0.0.1:${server.address().port}`];
const client = spawn(common.opensslCli, args);
let out = '';
client.stderr.setEncoding('utf8');
client.stderr.on('data', function(d) {
out += d;
if (/SSL alert number 70/.test(out)) {
success = true;
server.close();
}
});
});
process.on('exit', function() {
assert(success);
});
execFile(common.opensslCli, args, common.mustCall((err, _, stderr) => {
assert.strictEqual(err.code, 1);
assert.match(stderr, /SSL alert number 70/);
server.close();
}));
}));