Skip to content

Commit

Permalink
test: rewrite ocsp test to run in parallel
Browse files Browse the repository at this point in the history
Run tests in parallel and use common.mustCall() and mustNotCall()
instead of process.exit() to ensure test assertions are run.

PR-URL: #26460
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
sam-github authored and BridgeAR committed Mar 14, 2019
1 parent 3e2e779 commit d590a45
Showing 1 changed file with 40 additions and 67 deletions.
107 changes: 40 additions & 67 deletions test/parallel/test-tls-ocsp-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,20 @@ const assert = require('assert');
const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;

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) {

const key = fixtures.readKey('agent1-key.pem');
const cert = fixtures.readKey('agent1-cert.pem');
const ca = fixtures.readKey('ca1-cert.pem');
const options = {
key,
cert,
ca: [ca]
};
let requestCount = 0;
let clientSecure = 0;
let ocspCount = 0;
let ocspResponse;
const requestCount = testOptions.response ? 0 : 1;

if (!testOptions.ocsp)
assert.strictEqual(testOptions.response, undefined);

if (testOptions.pfx) {
delete options.key;
Expand All @@ -59,82 +58,56 @@ function test(testOptions, cb) {
options.passphrase = testOptions.passphrase;
}

const server = tls.createServer(options, function(cleartext) {
const server = tls.createServer(options, common.mustCall((cleartext) => {
cleartext.on('error', function(er) {
// We're ok with getting ECONNRESET in this test, but it's
// timing-dependent, and thus unreliable. Any other errors
// are just failures, though.
if (er.code !== 'ECONNRESET')
throw er;
});
++requestCount;
cleartext.end();
});
server.on('OCSPRequest', function(cert, issuer, callback) {
++ocspCount;
assert.ok(Buffer.isBuffer(cert));
assert.ok(Buffer.isBuffer(issuer));

// Just to check that async really works there
setTimeout(function() {
callback(null,
testOptions.response ? Buffer.from(testOptions.response) : null);
}, 100);
});
}, requestCount));

if (!testOptions.ocsp)
server.on('OCSPRequest', common.mustNotCall());
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);
}));

server.listen(0, function() {
const client = tls.connect({
port: this.address().port,
requestOCSP: testOptions.ocsp !== false,
secureOptions: testOptions.ocsp === false ?
SSL_OP_NO_TICKET : 0,
requestOCSP: testOptions.ocsp,
secureOptions: testOptions.ocsp ? 0 : SSL_OP_NO_TICKET,
rejectUnauthorized: false
}, function() {
clientSecure++;
});
client.on('OCSPResponse', function(resp) {
ocspResponse = resp;
if (resp)
}, common.mustCall(() => { }, requestCount));

client.on('OCSPResponse', common.mustCall((resp) => {
if (testOptions.response) {
assert.strictEqual(resp.toString(), testOptions.response);
client.destroy();
});
client.on('close', function() {
server.close(cb);
});
});
} else {
assert.strictEqual(resp, null);
}
}, testOptions.ocsp === false ? 0 : 1));

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);
client.on('close', common.mustCall(() => {
server.close(cb);
}));
});
}

const tests = [
{ response: false },
{ response: 'hello world' },
{ ocsp: false }
];
test({ ocsp: true, response: false });
test({ ocsp: true, response: 'hello world' });
test({ ocsp: false });

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);

0 comments on commit d590a45

Please sign in to comment.