Skip to content

Commit d590a45

Browse files
sam-githubBridgeAR
authored andcommitted
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: #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]>
1 parent 3e2e779 commit d590a45

File tree

1 file changed

+40
-67
lines changed

1 file changed

+40
-67
lines changed

Diff for: test/parallel/test-tls-ocsp-callback.js

+40-67
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,20 @@ const assert = require('assert');
3636
const SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;
3737

3838
const pfx = fixtures.readKey('agent1.pfx');
39+
const key = fixtures.readKey('agent1-key.pem');
40+
const cert = fixtures.readKey('agent1-cert.pem');
41+
const ca = fixtures.readKey('ca1-cert.pem');
3942

4043
function test(testOptions, cb) {
41-
42-
const key = fixtures.readKey('agent1-key.pem');
43-
const cert = fixtures.readKey('agent1-cert.pem');
44-
const ca = fixtures.readKey('ca1-cert.pem');
4544
const options = {
4645
key,
4746
cert,
4847
ca: [ca]
4948
};
50-
let requestCount = 0;
51-
let clientSecure = 0;
52-
let ocspCount = 0;
53-
let ocspResponse;
49+
const requestCount = testOptions.response ? 0 : 1;
50+
51+
if (!testOptions.ocsp)
52+
assert.strictEqual(testOptions.response, undefined);
5453

5554
if (testOptions.pfx) {
5655
delete options.key;
@@ -59,82 +58,56 @@ function test(testOptions, cb) {
5958
options.passphrase = testOptions.passphrase;
6059
}
6160

62-
const server = tls.createServer(options, function(cleartext) {
61+
const server = tls.createServer(options, common.mustCall((cleartext) => {
6362
cleartext.on('error', function(er) {
6463
// We're ok with getting ECONNRESET in this test, but it's
6564
// timing-dependent, and thus unreliable. Any other errors
6665
// are just failures, though.
6766
if (er.code !== 'ECONNRESET')
6867
throw er;
6968
});
70-
++requestCount;
7169
cleartext.end();
72-
});
73-
server.on('OCSPRequest', function(cert, issuer, callback) {
74-
++ocspCount;
75-
assert.ok(Buffer.isBuffer(cert));
76-
assert.ok(Buffer.isBuffer(issuer));
77-
78-
// Just to check that async really works there
79-
setTimeout(function() {
80-
callback(null,
81-
testOptions.response ? Buffer.from(testOptions.response) : null);
82-
}, 100);
83-
});
70+
}, requestCount));
71+
72+
if (!testOptions.ocsp)
73+
server.on('OCSPRequest', common.mustNotCall());
74+
else
75+
server.on('OCSPRequest', common.mustCall((cert, issuer, callback) => {
76+
assert.ok(Buffer.isBuffer(cert));
77+
assert.ok(Buffer.isBuffer(issuer));
78+
79+
// Callback a little later to ensure that async really works.
80+
return setTimeout(callback, 100, null, testOptions.response ?
81+
Buffer.from(testOptions.response) : null);
82+
}));
83+
8484
server.listen(0, function() {
8585
const client = tls.connect({
8686
port: this.address().port,
87-
requestOCSP: testOptions.ocsp !== false,
88-
secureOptions: testOptions.ocsp === false ?
89-
SSL_OP_NO_TICKET : 0,
87+
requestOCSP: testOptions.ocsp,
88+
secureOptions: testOptions.ocsp ? 0 : SSL_OP_NO_TICKET,
9089
rejectUnauthorized: false
91-
}, function() {
92-
clientSecure++;
93-
});
94-
client.on('OCSPResponse', function(resp) {
95-
ocspResponse = resp;
96-
if (resp)
90+
}, common.mustCall(() => { }, requestCount));
91+
92+
client.on('OCSPResponse', common.mustCall((resp) => {
93+
if (testOptions.response) {
94+
assert.strictEqual(resp.toString(), testOptions.response);
9795
client.destroy();
98-
});
99-
client.on('close', function() {
100-
server.close(cb);
101-
});
102-
});
96+
} else {
97+
assert.strictEqual(resp, null);
98+
}
99+
}, testOptions.ocsp === false ? 0 : 1));
103100

104-
process.on('exit', function() {
105-
if (testOptions.ocsp === false) {
106-
assert.strictEqual(requestCount, clientSecure);
107-
assert.strictEqual(requestCount, 1);
108-
return;
109-
}
110-
111-
if (testOptions.response) {
112-
assert.strictEqual(ocspResponse.toString(), testOptions.response);
113-
} else {
114-
assert.strictEqual(ocspResponse, null);
115-
}
116-
assert.strictEqual(requestCount, testOptions.response ? 0 : 1);
117-
assert.strictEqual(clientSecure, requestCount);
118-
assert.strictEqual(ocspCount, 1);
101+
client.on('close', common.mustCall(() => {
102+
server.close(cb);
103+
}));
119104
});
120105
}
121106

122-
const tests = [
123-
{ response: false },
124-
{ response: 'hello world' },
125-
{ ocsp: false }
126-
];
107+
test({ ocsp: true, response: false });
108+
test({ ocsp: true, response: 'hello world' });
109+
test({ ocsp: false });
127110

128111
if (!common.hasFipsCrypto) {
129-
tests.push({ pfx: pfx, passphrase: 'sample', response: 'hello pfx' });
112+
test({ ocsp: true, response: 'hello pfx', pfx: pfx, passphrase: 'sample' });
130113
}
131-
132-
function runTests(i) {
133-
if (i === tests.length) return;
134-
135-
test(tests[i], common.mustCall(function() {
136-
runTests(i + 1);
137-
}));
138-
}
139-
140-
runTests(0);

0 commit comments

Comments
 (0)