test: rewrite ocsp test to run in parallel

Run tests in parallel and use common.mustCall() and mustNotCall()
instead of process.exit() to ensure test assertions are run.

PR-URL: https://github.com/nodejs/node/pull/26460
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Sam Roberts 2019-03-05 14:17:15 -08:00 committed by Ruben Bridgewater
parent e7f58868b5
commit 78913393fa
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -36,21 +36,20 @@ const assert = require('assert');
const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET; const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;
const pfx = fixtures.readKey('agent1.pfx'); const pfx = fixtures.readKey('agent1.pfx');
const key = fixtures.readKey('agent1-key.pem');
const cert = fixtures.readKey('agent1-cert.pem');
const ca = fixtures.readKey('ca1-cert.pem');
function test(testOptions, cb) { function test(testOptions, cb) {
const key = fixtures.readKey('agent1-key.pem');
const cert = fixtures.readKey('agent1-cert.pem');
const ca = fixtures.readKey('ca1-cert.pem');
const options = { const options = {
key, key,
cert, cert,
ca: [ca] ca: [ca]
}; };
let requestCount = 0; const requestCount = testOptions.response ? 0 : 1;
let clientSecure = 0;
let ocspCount = 0; if (!testOptions.ocsp)
let ocspResponse; assert.strictEqual(testOptions.response, undefined);
if (testOptions.pfx) { if (testOptions.pfx) {
delete options.key; delete options.key;
@ -59,7 +58,7 @@ function test(testOptions, cb) {
options.passphrase = testOptions.passphrase; options.passphrase = testOptions.passphrase;
} }
const server = tls.createServer(options, function(cleartext) { const server = tls.createServer(options, common.mustCall((cleartext) => {
cleartext.on('error', function(er) { cleartext.on('error', function(er) {
// We're ok with getting ECONNRESET in this test, but it's // We're ok with getting ECONNRESET in this test, but it's
// timing-dependent, and thus unreliable. Any other errors // timing-dependent, and thus unreliable. Any other errors
@ -67,74 +66,48 @@ function test(testOptions, cb) {
if (er.code !== 'ECONNRESET') if (er.code !== 'ECONNRESET')
throw er; throw er;
}); });
++requestCount;
cleartext.end(); cleartext.end();
}); }, requestCount));
server.on('OCSPRequest', function(cert, issuer, callback) {
++ocspCount; if (!testOptions.ocsp)
assert.ok(Buffer.isBuffer(cert)); server.on('OCSPRequest', common.mustNotCall());
assert.ok(Buffer.isBuffer(issuer)); else
server.on('OCSPRequest', common.mustCall((cert, issuer, callback) => {
assert.ok(Buffer.isBuffer(cert));
assert.ok(Buffer.isBuffer(issuer));
// Callback a little later to ensure that async really works.
return setTimeout(callback, 100, null, testOptions.response ?
Buffer.from(testOptions.response) : null);
}));
// Just to check that async really works there
setTimeout(function() {
callback(null,
testOptions.response ? Buffer.from(testOptions.response) : null);
}, 100);
});
server.listen(0, function() { server.listen(0, function() {
const client = tls.connect({ const client = tls.connect({
port: this.address().port, port: this.address().port,
requestOCSP: testOptions.ocsp !== false, requestOCSP: testOptions.ocsp,
secureOptions: testOptions.ocsp === false ? secureOptions: testOptions.ocsp ? 0 : SSL_OP_NO_TICKET,
SSL_OP_NO_TICKET : 0,
rejectUnauthorized: false rejectUnauthorized: false
}, function() { }, common.mustCall(() => { }, requestCount));
clientSecure++;
}); client.on('OCSPResponse', common.mustCall((resp) => {
client.on('OCSPResponse', function(resp) { if (testOptions.response) {
ocspResponse = resp; assert.strictEqual(resp.toString(), testOptions.response);
if (resp)
client.destroy(); client.destroy();
}); } else {
client.on('close', function() { assert.strictEqual(resp, null);
}
}, testOptions.ocsp === false ? 0 : 1));
client.on('close', common.mustCall(() => {
server.close(cb); server.close(cb);
}); }));
});
process.on('exit', function() {
if (testOptions.ocsp === false) {
assert.strictEqual(requestCount, clientSecure);
assert.strictEqual(requestCount, 1);
return;
}
if (testOptions.response) {
assert.strictEqual(ocspResponse.toString(), testOptions.response);
} else {
assert.strictEqual(ocspResponse, null);
}
assert.strictEqual(requestCount, testOptions.response ? 0 : 1);
assert.strictEqual(clientSecure, requestCount);
assert.strictEqual(ocspCount, 1);
}); });
} }
const tests = [ test({ ocsp: true, response: false });
{ response: false }, test({ ocsp: true, response: 'hello world' });
{ response: 'hello world' }, test({ ocsp: false });
{ ocsp: false }
];
if (!common.hasFipsCrypto) { if (!common.hasFipsCrypto) {
tests.push({ pfx: pfx, passphrase: 'sample', response: 'hello pfx' }); test({ ocsp: true, response: 'hello pfx', pfx: pfx, passphrase: 'sample' });
} }
function runTests(i) {
if (i === tests.length) return;
test(tests[i], common.mustCall(function() {
runTests(i + 1);
}));
}
runTests(0);