Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve control flow in test-tls-dhe #46751

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 21 additions & 54 deletions test/parallel/test-tls-dhe.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ if (!common.opensslCli)
common.skip('missing openssl-cli');

const assert = require('assert');
const { once } = require('events');
const tls = require('tls');
const spawn = require('child_process').spawn;
const { execFile } = require('child_process');
const fixtures = require('../common/fixtures');

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

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

function test(keylen, expectedCipher, cb) {
function test(keylen, expectedCipher) {
const options = {
key: key,
cert: cert,
Expand All @@ -57,61 +56,29 @@ function test(keylen, expectedCipher, cb) {
maxVersion: 'TLSv1.2',
};

const server = tls.createServer(options, function(conn) {
conn.end();
});
const server = tls.createServer(options, (conn) => conn.end());

server.on('close', function(err) {
assert.ifError(err);
if (cb) cb();
});

server.listen(0, '127.0.0.1', function() {
const args = ['s_client', '-connect', `127.0.0.1:${this.address().port}`,
server.listen(0, '127.0.0.1', common.mustCall(() => {
const args = ['s_client', '-connect', `127.0.0.1:${server.address().port}`,
'-cipher', ciphers];

const client = spawn(common.opensslCli, args);
let out = '';
client.stdout.setEncoding('utf8');
client.stdout.on('data', function(d) {
out += d;
});
client.stdout.on('end', function() {
execFile(common.opensslCli, args, common.mustSucceed((stdout) => {
assert(keylen === 'error' ||
out.includes(`Server Temp Key: DH, ${keylen} bits`));
const reg = new RegExp(`Cipher : ${expectedCipher}`);
if (reg.test(out)) {
nsuccess++;
server.close();
}
});
});
}

function test512() {
assert.throws(function() {
test(512, 'DHE-RSA-AES128-SHA256', null);
}, /DH parameter is less than 1024 bits/);
}
stdout.includes(`Server Temp Key: DH, ${keylen} bits`));
assert(stdout.includes(`Cipher : ${expectedCipher}`));
server.close();
}));
}));

function test1024() {
test(1024, 'DHE-RSA-AES128-SHA256', test2048);
ntests++;
return once(server, 'close');
}

function test2048() {
test(2048, 'DHE-RSA-AES128-SHA256', testError);
ntests++;
}

function testError() {
test('error', 'ECDHE-RSA-AES128-SHA256', test512);
ntests++;
}

test1024();
(async () => {
assert.throws(() => {
test(512, 'DHE-RSA-AES128-SHA256');
}, /DH parameter is less than 1024 bits/);

process.on('exit', function() {
assert.strictEqual(ntests, nsuccess);
assert.strictEqual(ntests, 3);
});
await test(1024, 'DHE-RSA-AES128-SHA256');
await test(2048, 'DHE-RSA-AES128-SHA256');
await test('error', 'ECDHE-RSA-AES128-SHA256');
})().then(common.mustCall());