• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const fixtures = require('../common/fixtures');
4
5// Test directly created TLS sockets and options.
6
7const assert = require('assert');
8const {
9  connect, keys, tls
10} = require(fixtures.path('tls-connect'));
11
12test(undefined, (err) => {
13  assert.strictEqual(err.message, 'unable to verify the first certificate');
14});
15
16test({}, (err) => {
17  assert.strictEqual(err.message, 'unable to verify the first certificate');
18});
19
20test(
21  { secureContext: tls.createSecureContext({ ca: keys.agent1.ca }) },
22  (err) => { assert.ifError(err); });
23
24test(
25  { ca: keys.agent1.ca },
26  (err) => { assert.ifError(err); });
27
28// Secure context options, like ca, are ignored if a sec ctx is explicitly
29// provided.
30test(
31  { secureContext: tls.createSecureContext(), ca: keys.agent1.ca },
32  (err) => {
33    assert.strictEqual(err.message,
34                       'unable to verify the first certificate');
35  });
36
37function test(client, callback) {
38  callback = common.mustCall(callback);
39  connect({
40    server: {
41      key: keys.agent1.key,
42      cert: keys.agent1.cert,
43    },
44  }, function(err, pair, cleanup) {
45    assert.strictEqual(err.message, 'unable to verify the first certificate');
46    let recv = '';
47    pair.server.server.once('secureConnection', common.mustCall((conn) => {
48      conn.on('data', (data) => recv += data);
49      conn.on('end', common.mustCall(() => {
50        // Server sees nothing wrong with connection, even though the client's
51        // authentication of the server cert failed.
52        assert.strictEqual(recv, 'hello');
53        cleanup();
54      }));
55    }));
56
57    // `new TLSSocket` doesn't support the 'secureConnect' event on client side,
58    // and doesn't error if authentication failed. Caller must explicitly check
59    // for failure.
60    (new tls.TLSSocket(null, client)).connect(pair.server.server.address().port)
61      .on('connect', common.mustCall(function() {
62        this.end('hello');
63      }))
64      .on('secure', common.mustCall(function() {
65        callback(this.ssl.verifyError());
66      }));
67  });
68}
69