node/test/parallel/test-tls-set-default-ca-certificates-error.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

41 lines
1.7 KiB
JavaScript

'use strict';
// This tests input validation of tls.setDefaultCACertificates().
const common = require('../common');
if (!common.hasCrypto) common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const tls = require('tls');
const { assertEqualCerts } = require('../common/tls');
const defaultCerts = tls.getCACertificates('default');
const fixtureCert = fixtures.readKey('fake-startcom-root-cert.pem');
for (const invalid of [null, undefined, 'string', 42, {}, true]) {
// Test input validation - should throw when not passed an array
assert.throws(() => tls.setDefaultCACertificates(invalid), {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "certs" argument must be an instance of Array/
});
// Verify that default certificates remain unchanged after error.
assertEqualCerts(tls.getCACertificates('default'), defaultCerts);
}
for (const invalid of [null, undefined, 42, {}, true]) {
// Test input validation - should throw when passed an array with invalid elements
assert.throws(() => tls.setDefaultCACertificates([invalid]), {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "certs\[0\]" argument must be of type string or an instance of ArrayBufferView/
});
// Verify that default certificates remain unchanged after error.
assertEqualCerts(tls.getCACertificates('default'), defaultCerts);
assert.throws(() => tls.setDefaultCACertificates([fixtureCert, invalid]), {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "certs\[1\]" argument must be of type string or an instance of ArrayBufferView/
});
// Verify that default certificates remain unchanged after error.
assertEqualCerts(tls.getCACertificates('default'), defaultCerts);
}