node/test/parallel/test-tls-set-default-ca-certificates-mixed-types.js
Joyee Cheung edd66d0130
crypto: add tls.setDefaultCACertificates()
This API allows dynamically configuring CA certificates that
will be used by the Node.js TLS clients by default.

Once called, the provided certificates will become the default CA
certificate list returned by `tls.getCACertificates('default')` and
used by TLS connections that don't specify their own CA certificates.

This function only affects the current Node.js thread.

PR-URL: https://github.com/nodejs/node/pull/58822
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
2025-07-18 19:57:53 +00:00

46 lines
1.6 KiB
JavaScript

'use strict';
// This tests mixed input types for tls.setDefaultCACertificates().
const common = require('../common');
if (!common.hasCrypto) common.skip('missing crypto');
const tls = require('tls');
const { assertEqualCerts } = require('../common/tls');
const bundledCerts = tls.getCACertificates('bundled');
if (bundledCerts.length < 4) {
common.skip('Not enough bundled CA certificates available');
}
const encoder = new TextEncoder();
// Test mixed array with string and Buffer.
{
tls.setDefaultCACertificates([bundledCerts[0], Buffer.from(bundledCerts[1], 'utf8')]);
const result = tls.getCACertificates('default');
assertEqualCerts(result, [bundledCerts[0], bundledCerts[1]]);
}
// Test mixed array with string and Uint8Array.
{
tls.setDefaultCACertificates([bundledCerts[1], encoder.encode(bundledCerts[2])]);
const result = tls.getCACertificates('default');
assertEqualCerts(result, [bundledCerts[1], bundledCerts[2]]);
}
// Test mixed array with string and DataView.
{
const uint8Cert = encoder.encode(bundledCerts[3]);
const dataViewCert = new DataView(uint8Cert.buffer, uint8Cert.byteOffset, uint8Cert.byteLength);
tls.setDefaultCACertificates([bundledCerts[1], dataViewCert]);
const result = tls.getCACertificates('default');
assertEqualCerts(result, [bundledCerts[1], bundledCerts[3]]);
}
// Test mixed array with Buffer and Uint8Array.
{
tls.setDefaultCACertificates([Buffer.from(bundledCerts[0], 'utf8'), encoder.encode(bundledCerts[2])]);
const result = tls.getCACertificates('default');
assertEqualCerts(result, [bundledCerts[0], bundledCerts[2]]);
}