Files
node/test/parallel/test-dtls-errors.mjs
T
James M Snell bd2b98f549 lib,src: apply multiple updates to dtls implementation
Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-by: Opecode
PR-URL: https://github.com/nodejs/node/pull/65511
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
2026-09-16 00:39:35 +00:00

59 lines
2.1 KiB
JavaScript

// Flags: --experimental-dtls --no-warnings --expose-internals
// Test: DTLS error handling for invalid certificate/key material and endpoint
// state.
import { hasCrypto, skip, mustNotCall } from '../common/index.mjs';
import assert from 'node:assert';
import * as fixtures from '../common/fixtures.mjs';
if (!hasCrypto) {
skip('missing crypto');
}
if (!process.features.dtls) {
skip('DTLS is not enabled');
}
const { listen, DTLSEndpoint } = await import('node:dtls');
// bind() is module plumbing rather than API; every argument reaches a
// CHECK() in the binding, so it is symbol-keyed and not callable from
// user code.
const { kBind } = (await import('internal/dtls/symbols')).default;
const cert = fixtures.readKey('agent1-cert.pem').toString();
const key = fixtures.readKey('agent1-key.pem').toString();
const mismatchedKey = fixtures.readKey('agent2-key.pem').toString();
// Bad credentials report the reason OpenSSL gave, rather than a single
// ERR_CRYPTO_OPERATION_FAILED for every kind of failure. These codes come
// from OpenSSL and could shift if the bundled version changes; the point
// being pinned is that the three cases are distinguishable.
// A malformed certificate PEM is rejected.
assert.throws(() => listen(mustNotCall(), {
cert: 'not a certificate', key, port: 0,
}), { code: 'ERR_OSSL_PEM_NO_START_LINE' });
// A malformed private key PEM is rejected.
assert.throws(() => listen(mustNotCall(), {
cert, key: 'not a key', port: 0,
}), { code: 'ERR_OSSL_UNSUPPORTED' });
// A private key that does not match the certificate is rejected.
assert.throws(() => listen(mustNotCall(), {
cert, key: mismatchedKey, port: 0,
}), { code: 'ERR_OSSL_X509_KEY_VALUES_MISMATCH' });
// Binding the same endpoint twice fails.
{
const endpoint = new DTLSEndpoint();
endpoint[kBind]('127.0.0.1', 0);
// Reported with libuv's own code for rebinding a bound handle, rather than
// a generic ERR_INVALID_STATE that says nothing about which failure it was.
assert.throws(() => endpoint[kBind]('127.0.0.1', 0),
{ code: 'EALREADY', syscall: 'bind' });
await endpoint.close();
}