Skip to content

Commit ecb3a7e

Browse files
sam-githubitaloacasas
authored andcommitted
test: make tls-socket-default-options tests run
Because of a poorly constructed test, only one of the two test vectors ran. The test also failed to cover the authentication error that occurs when the server's certificate is not trusted. Both issues are fixed. Fix: #10538 PR-URL: #11005 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f5b4849 commit ecb3a7e

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

test/parallel/test-tls-socket-default-options.js

+49-45
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11
'use strict';
22
const common = require('../common');
3+
4+
// Test a directly created TLS socket supports no options, and empty options.
5+
36
const assert = require('assert');
7+
const join = require('path').join;
8+
const {
9+
connect, keys, tls
10+
} = require(join(common.fixturesDir, 'tls-connect'));
411

512
if (!common.hasCrypto) {
613
common.skip('missing crypto');
714
return;
815
}
9-
const tls = require('tls');
10-
11-
const fs = require('fs');
12-
13-
const sent = 'hello world';
14-
15-
const serverOptions = {
16-
isServer: true,
17-
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
18-
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
19-
};
20-
21-
function testSocketOptions(socket, socketOptions) {
22-
let received = '';
23-
const server = tls.createServer(serverOptions, function(s) {
24-
s.on('data', function(chunk) {
25-
received += chunk;
26-
});
27-
28-
s.on('end', function() {
29-
server.close();
30-
s.destroy();
31-
assert.strictEqual(received, sent);
32-
setImmediate(runTests);
33-
});
34-
}).listen(0, function() {
35-
const c = new tls.TLSSocket(socket, socketOptions);
36-
c.connect(this.address().port, function() {
37-
c.end(sent);
38-
});
39-
});
40-
41-
}
42-
43-
const testArgs = [
44-
[],
45-
[undefined, {}]
46-
];
4716

48-
let n = 0;
49-
function runTests() {
50-
if (n++ < testArgs.length) {
51-
testSocketOptions.apply(null, testArgs[n]);
52-
}
17+
test(undefined, (err) => {
18+
assert.strictEqual(err.message, 'unable to verify the first certificate');
19+
});
20+
21+
test({}, (err) => {
22+
assert.strictEqual(err.message, 'unable to verify the first certificate');
23+
});
24+
25+
test({secureContext: tls.createSecureContext({ca: keys.agent1.ca})}, (err) => {
26+
assert.ifError(err);
27+
});
28+
29+
function test(client, callback) {
30+
callback = common.mustCall(callback);
31+
connect({
32+
server: {
33+
key: keys.agent1.key,
34+
cert: keys.agent1.cert,
35+
},
36+
}, function(err, pair, cleanup) {
37+
assert.strictEqual(err.message, 'unable to verify the first certificate');
38+
let recv = '';
39+
pair.server.server.once('secureConnection', common.mustCall((conn) => {
40+
conn.on('data', (data) => recv += data);
41+
conn.on('end', common.mustCall(() => {
42+
// Server sees nothing wrong with connection, even though the client's
43+
// authentication of the server cert failed.
44+
assert.strictEqual(recv, 'hello');
45+
cleanup();
46+
}));
47+
}));
48+
49+
// Client doesn't support the 'secureConnect' event, and doesn't error if
50+
// authentication failed. Caller must explicitly check for failure.
51+
(new tls.TLSSocket(null, client)).connect(pair.server.server.address().port)
52+
.on('connect', common.mustCall(function() {
53+
this.end('hello');
54+
}))
55+
.on('secure', common.mustCall(function() {
56+
callback(this.ssl.verifyError());
57+
}));
58+
});
5359
}
54-
55-
runTests();

0 commit comments

Comments
 (0)