|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common');
|
| 3 | + |
| 4 | +// Test a directly created TLS socket supports no options, and empty options. |
| 5 | + |
3 | 6 | const assert = require('assert');
|
| 7 | +const join = require('path').join; |
| 8 | +const { |
| 9 | + connect, keys, tls |
| 10 | +} = require(join(common.fixturesDir, 'tls-connect')); |
4 | 11 |
|
5 | 12 | if (!common.hasCrypto) {
|
6 | 13 | common.skip('missing crypto');
|
7 | 14 | return;
|
8 | 15 | }
|
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 |
| -]; |
47 | 16 |
|
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 | + }); |
53 | 59 | }
|
54 |
| - |
55 |
| -runTests(); |
|
0 commit comments