node/test/addons/openssl-get-ssl-ctx/test.js
Tim Perry 2263b4d6f1
crypto: add crypto::GetSSLCtx API for addon access to OpenSSL contexts
This intended to replace usage of the unsupported _external field,
offering an official API for native addons to access OpenSSL directly
while reducing the JS API and internal field exposure.

PR-URL: https://github.com/nodejs/node/pull/62254
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2026-03-23 11:27:22 +00:00

49 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const tls = require('tls');
const binding = require(`./build/${common.buildType}/binding`);
// Test 1: Pass a SecureContext to getSSLCtx.
{
const ctx = tls.createSecureContext();
assert.strictEqual(binding.getSSLCtx(ctx), true);
}
// Test 2: Passing a non-SecureContext should return nullptr.
{
assert.strictEqual(binding.getSSLCtxInvalid({}), true);
}
// Test 3: Passing a number should return nullptr.
{
assert.strictEqual(binding.getSSLCtxInvalid(42), true);
}
// Test 4: Passing undefined should return nullptr.
{
assert.strictEqual(binding.getSSLCtxInvalid(undefined), true);
}
// Test 5: Passing null should return nullptr.
{
assert.strictEqual(binding.getSSLCtxInvalid(null), true);
}
// Test 6: An object with a non-SecureContext .context property should return
// nullptr.
{
assert.strictEqual(binding.getSSLCtxInvalid({ context: 'not a context' }), true);
}
// Test 7: An object with a throwing .context getter should return nullptr
// without propagating the exception.
{
const obj = {
get context() { throw new Error('getter threw'); },
};
assert.strictEqual(binding.getSSLCtxInvalid(obj), true);
}