11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('node compiled without crypto.');
51cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// This test ensures that TLS does not fail to read a self-signed certificate
81cb0ef41Sopenharmony_ci// and thus throw an `authorizationError`.
91cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/5100
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst tls = require('tls');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst pfx = fixtures.readKey('agent1.pfx');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst server = tls
171cb0ef41Sopenharmony_ci  .createServer(
181cb0ef41Sopenharmony_ci    {
191cb0ef41Sopenharmony_ci      pfx: pfx,
201cb0ef41Sopenharmony_ci      passphrase: 'sample',
211cb0ef41Sopenharmony_ci      requestCert: true,
221cb0ef41Sopenharmony_ci      rejectUnauthorized: false
231cb0ef41Sopenharmony_ci    },
241cb0ef41Sopenharmony_ci    common.mustCall(function(c) {
251cb0ef41Sopenharmony_ci      assert.strictEqual(c.getPeerCertificate().serialNumber,
261cb0ef41Sopenharmony_ci                         '147D36C1C2F74206DE9FAB5F2226D78ADB00A426');
271cb0ef41Sopenharmony_ci      assert.strictEqual(c.authorizationError, null);
281cb0ef41Sopenharmony_ci      c.end();
291cb0ef41Sopenharmony_ci    })
301cb0ef41Sopenharmony_ci  )
311cb0ef41Sopenharmony_ci  .listen(0, function() {
321cb0ef41Sopenharmony_ci    const client = tls.connect(
331cb0ef41Sopenharmony_ci      {
341cb0ef41Sopenharmony_ci        port: this.address().port,
351cb0ef41Sopenharmony_ci        pfx: pfx,
361cb0ef41Sopenharmony_ci        passphrase: 'sample',
371cb0ef41Sopenharmony_ci        rejectUnauthorized: false
381cb0ef41Sopenharmony_ci      },
391cb0ef41Sopenharmony_ci      function() {
401cb0ef41Sopenharmony_ci        for (let i = 0; i < 10; ++i) {
411cb0ef41Sopenharmony_ci          // Calling this repeatedly is a regression test that verifies
421cb0ef41Sopenharmony_ci          // that .getCertificate() does not accidentally decrease the
431cb0ef41Sopenharmony_ci          // reference count of the X509* certificate on the native side.
441cb0ef41Sopenharmony_ci          assert.strictEqual(client.getCertificate().serialNumber,
451cb0ef41Sopenharmony_ci                             '147D36C1C2F74206DE9FAB5F2226D78ADB00A426');
461cb0ef41Sopenharmony_ci        }
471cb0ef41Sopenharmony_ci        client.end();
481cb0ef41Sopenharmony_ci        server.close();
491cb0ef41Sopenharmony_ci      }
501cb0ef41Sopenharmony_ci    );
511cb0ef41Sopenharmony_ci  });
52