Skip to content

Commit a92be13

Browse files
tniessentargos
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 <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 81592ff commit a92be13

1 file changed

Lines changed: 21 additions & 54 deletions

File tree

‎test/parallel/test-tls-dhe.js‎

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

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

3637
constkey=fixtures.readKey('agent2-key.pem');
3738
constcert=fixtures.readKey('agent2-cert.pem');
38-
letnsuccess=0;
39-
letntests=0;
4039
constciphers='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
returnfixtures.readKey(keyname);
4948
}
5049

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

60-
constserver=tls.createServer(options,function(conn){
61-
conn.end();
62-
});
59+
constserver=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-
constargs=['s_client','-connect',`127.0.0.1:${this.address().port}`,
61+
server.listen(0,'127.0.0.1',common.mustCall(()=>{
62+
constargs=['s_client','-connect',`127.0.0.1:${server.address().port}`,
7163
'-cipher',ciphers];
7264

73-
constclient=spawn(common.opensslCli,args);
74-
letout='';
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-
constreg=newRegExp(`Cipher : ${expectedCipher}`);
83-
if(reg.test(out)){
84-
nsuccess++;
85-
server.close();
86-
}
87-
});
88-
});
89-
}
90-
91-
functiontest512(){
92-
assert.throws(function(){
93-
test(512,'DHE-RSA-AES128-SHA256',null);
94-
},/DHparameterislessthan1024bits/);
95-
}
67+
stdout.includes(`Server Temp Key: DH, ${keylen} bits`));
68+
assert(stdout.includes(`Cipher : ${expectedCipher}`));
69+
server.close();
70+
}));
71+
}));
9672

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

102-
functiontest2048(){
103-
test(2048,'DHE-RSA-AES128-SHA256',testError);
104-
ntests++;
105-
}
106-
107-
functiontestError(){
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+
},/DHparameterislessthan1024bits/);
11380

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

0 commit comments

Comments
 (0)