Skip to content

Commit 25cab7e

Browse files
tniessendanielleadams
authored andcommitted
test: improve control flow in test-tls-dhe
If this test fails, e.g., if the s_client output does not match the expectation, the previous implementation would not produce any helpful error messages. Rework the control flow to be more idiomatic. Avoid callback chaining and stream operations. Also, the TLS server 'close' event does not pass an error to the event handler, so remove the respective assertion. PR-URL: #46751 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5353110 commit 25cab7e

File tree

1 file changed

+21
-54
lines changed

1 file changed

+21
-54
lines changed

test/parallel/test-tls-dhe.js

+21-54
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ if (!common.opensslCli)
2929
common.skip('missing openssl-cli');
3030

3131
const assert = require('assert');
32+
const { once } = require('events');
3233
const tls = require('tls');
33-
const spawn = require('child_process').spawn;
34+
const { execFile } = require('child_process');
3435
const fixtures = require('../common/fixtures');
3536

3637
const key = fixtures.readKey('agent2-key.pem');
3738
const cert = fixtures.readKey('agent2-cert.pem');
38-
let nsuccess = 0;
39-
let ntests = 0;
4039
const ciphers = 'DHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
4140

4241
// Test will emit a warning because the DH parameter size is < 2048 bits
@@ -48,7 +47,7 @@ function loadDHParam(n) {
4847
return fixtures.readKey(keyname);
4948
}
5049

51-
function test(keylen, expectedCipher, cb) {
50+
function test(keylen, expectedCipher) {
5251
const options = {
5352
key: key,
5453
cert: cert,
@@ -57,61 +56,29 @@ function test(keylen, expectedCipher, cb) {
5756
maxVersion: 'TLSv1.2',
5857
};
5958

60-
const server = tls.createServer(options, function(conn) {
61-
conn.end();
62-
});
59+
const server = tls.createServer(options, (conn) => conn.end());
6360

64-
server.on('close', function(err) {
65-
assert.ifError(err);
66-
if (cb) cb();
67-
});
68-
69-
server.listen(0, '127.0.0.1', function() {
70-
const args = ['s_client', '-connect', `127.0.0.1:${this.address().port}`,
61+
server.listen(0, '127.0.0.1', common.mustCall(() => {
62+
const args = ['s_client', '-connect', `127.0.0.1:${server.address().port}`,
7163
'-cipher', ciphers];
7264

73-
const client = spawn(common.opensslCli, args);
74-
let out = '';
75-
client.stdout.setEncoding('utf8');
76-
client.stdout.on('data', function(d) {
77-
out += d;
78-
});
79-
client.stdout.on('end', function() {
65+
execFile(common.opensslCli, args, common.mustSucceed((stdout) => {
8066
assert(keylen === 'error' ||
81-
out.includes(`Server Temp Key: DH, ${keylen} bits`));
82-
const reg = new RegExp(`Cipher : ${expectedCipher}`);
83-
if (reg.test(out)) {
84-
nsuccess++;
85-
server.close();
86-
}
87-
});
88-
});
89-
}
90-
91-
function test512() {
92-
assert.throws(function() {
93-
test(512, 'DHE-RSA-AES128-SHA256', null);
94-
}, /DH parameter is less than 1024 bits/);
95-
}
67+
stdout.includes(`Server Temp Key: DH, ${keylen} bits`));
68+
assert(stdout.includes(`Cipher : ${expectedCipher}`));
69+
server.close();
70+
}));
71+
}));
9672

97-
function test1024() {
98-
test(1024, 'DHE-RSA-AES128-SHA256', test2048);
99-
ntests++;
73+
return once(server, 'close');
10074
}
10175

102-
function test2048() {
103-
test(2048, 'DHE-RSA-AES128-SHA256', testError);
104-
ntests++;
105-
}
106-
107-
function testError() {
108-
test('error', 'ECDHE-RSA-AES128-SHA256', test512);
109-
ntests++;
110-
}
111-
112-
test1024();
76+
(async () => {
77+
assert.throws(() => {
78+
test(512, 'DHE-RSA-AES128-SHA256');
79+
}, /DH parameter is less than 1024 bits/);
11380

114-
process.on('exit', function() {
115-
assert.strictEqual(ntests, nsuccess);
116-
assert.strictEqual(ntests, 3);
117-
});
81+
await test(1024, 'DHE-RSA-AES128-SHA256');
82+
await test(2048, 'DHE-RSA-AES128-SHA256');
83+
await test('error', 'ECDHE-RSA-AES128-SHA256');
84+
})().then(common.mustCall());

0 commit comments

Comments
 (0)