From 652fbc286e33ad9dcdbdd7f29a82150e23ea250d Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 10 May 2026 11:44:34 -0700 Subject: [PATCH] src,lib: add dtls interop tests PR-URL: https://github.com/nodejs/node/pull/63182 Fixes: https://github.com/nodejs/node/issues/61630 Reviewed-By: Matteo Collina Reviewed-By: Stephen Belanger Reviewed-By: Rafael Gonzaga --- lib/internal/dtls/dtls.js | 6 + lib/internal/dtls/state.js | 6 + lib/internal/dtls/stats.js | 6 + lib/internal/dtls/symbols.js | 6 + test/doctool/test-make-doc.mjs | 3 +- .../test-dtls-interop-openssl-client.mjs | 103 ++++++++++++++++++ .../test-dtls-interop-openssl-server.mjs | 95 ++++++++++++++++ 7 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 test/sequential/test-dtls-interop-openssl-client.mjs create mode 100644 test/sequential/test-dtls-interop-openssl-server.mjs diff --git a/lib/internal/dtls/dtls.js b/lib/internal/dtls/dtls.js index aa4018239d9..c4aab52e6e7 100644 --- a/lib/internal/dtls/dtls.js +++ b/lib/internal/dtls/dtls.js @@ -1,5 +1,9 @@ 'use strict'; +// TODO(@jasnell) Temporarily ignoring c8 coverage for this file while tests +// are still being developed. +/* c8 ignore start */ + const { ArrayIsArray, FunctionPrototypeBind, @@ -648,3 +652,5 @@ module.exports = { DTLSEndpoint, DTLSSession, }; + +/* c8 ignore stop */ diff --git a/lib/internal/dtls/state.js b/lib/internal/dtls/state.js index be827266174..5d86b556f1a 100644 --- a/lib/internal/dtls/state.js +++ b/lib/internal/dtls/state.js @@ -1,5 +1,9 @@ 'use strict'; +// TODO(@jasnell) Temporarily ignoring c8 coverage for this file while tests +// are still being developed. +/* c8 ignore start */ + const { DataView, DataViewPrototypeGetByteLength, @@ -166,3 +170,5 @@ module.exports = { DTLSEndpointState, DTLSSessionState, }; + +/* c8 ignore stop */ diff --git a/lib/internal/dtls/stats.js b/lib/internal/dtls/stats.js index c1fbd7dc16f..b3393fe6b76 100644 --- a/lib/internal/dtls/stats.js +++ b/lib/internal/dtls/stats.js @@ -1,5 +1,9 @@ 'use strict'; +// TODO(@jasnell) Temporarily ignoring c8 coverage for this file while tests +// are still being developed. +/* c8 ignore start */ + const { BigUint64Array, JSONStringify, @@ -333,3 +337,5 @@ module.exports = { DTLSEndpointStats, DTLSSessionStats, }; + +/* c8 ignore stop */ diff --git a/lib/internal/dtls/symbols.js b/lib/internal/dtls/symbols.js index 0fe418d023d..fbeeadc562a 100644 --- a/lib/internal/dtls/symbols.js +++ b/lib/internal/dtls/symbols.js @@ -1,5 +1,9 @@ 'use strict'; +// TODO(@jasnell) Temporarily ignoring c8 coverage for this file while tests +// are still being developed. +/* c8 ignore start */ + const { Symbol, } = primordials; @@ -35,3 +39,5 @@ module.exports = { kSessionClose: Symbol('dtls.session.close'), kSessionKeylog: Symbol('dtls.session.keylog'), }; + +/* c8 ignore stop */ diff --git a/test/doctool/test-make-doc.mjs b/test/doctool/test-make-doc.mjs index 55519322767..59e681707dd 100644 --- a/test/doctool/test-make-doc.mjs +++ b/test/doctool/test-make-doc.mjs @@ -61,7 +61,8 @@ for (const actualDoc of actualDocs) { // Unless the old file is still available pointing to the correct location // 301 redirects are not yet automated. So keeping the old URL is a // reasonable workaround. - if (renamedDocs.includes(actualDoc) || actualDoc === 'apilinks.json') continue; + if (renamedDocs.includes(actualDoc) || skipedDocs.includes(actualDoc) || + actualDoc === 'apilinks.json') continue; assert.ok( expectedDocs.includes(actualDoc), `${actualDoc} does not match TOC`); diff --git a/test/sequential/test-dtls-interop-openssl-client.mjs b/test/sequential/test-dtls-interop-openssl-client.mjs new file mode 100644 index 00000000000..683d725e766 --- /dev/null +++ b/test/sequential/test-dtls-interop-openssl-client.mjs @@ -0,0 +1,103 @@ +// Flags: --experimental-dtls --no-warnings + +// Test: DTLS interop -- Node.js DTLS server with OpenSSL s_client. +// Verifies that an external DTLS client (OpenSSL CLI) can complete a +// handshake with Node's DTLS server and exchange application data. + +import { hasCrypto, skip, mustCall } from '../common/index.mjs'; +import { createRequire } from 'module'; +import assert from 'node:assert'; +import { spawn } from 'node:child_process'; +import { setTimeout } from 'node:timers/promises'; +import * as fixtures from '../common/fixtures.mjs'; + +if (!hasCrypto) { + skip('missing crypto'); +} + +if (!process.features.dtls) { + skip('DTLS is not enabled'); +} + +const require = createRequire(import.meta.url); +const { opensslCli } = require('../common/crypto'); + +if (!opensslCli) { + skip('missing openssl-cli'); +} + +const { listen } = await import('node:dtls'); + +const reply = 'I AM THE WALRUS'; // Something recognizable +const serverReceivedData = Promise.withResolvers(); + +// Start Node.js DTLS server. +const endpoint = listen(mustCall((session) => { + session.onmessage = mustCall((data) => { + assert.strictEqual(data.toString().trim(), 'hello from openssl'); + session.send(reply); + serverReceivedData.resolve(); + }); + session.onhandshake = mustCall(); +}), { + cert: fixtures.readKey('agent1-cert.pem').toString(), + key: fixtures.readKey('agent1-key.pem').toString(), + port: 0, + host: '127.0.0.1', +}); + +const { port } = endpoint.address; + +// Spawn OpenSSL s_client to connect to the Node.js server. +const args = [ + 's_client', + '-dtls', + '-connect', `127.0.0.1:${port}`, + '-CAfile', fixtures.path('keys/ca1-cert.pem'), +]; + +const client = spawn(opensslCli, args, { stdio: 'pipe' }); + +let stdout = ''; +client.stdout.on('data', (data) => { stdout += data; }); + +let stderr = ''; +client.stderr.on('data', (data) => { stderr += data; }); + +const timeout = setTimeout(() => { + client.kill(); + endpoint.close(); + assert.fail('Test timed out'); +}, 10000); + +// Wait for the handshake to start (s_client writes TLS info to stdout), +// then send data. +await new Promise((resolve) => client.stdout.once('data', resolve)); +await setTimeout(500); + +client.stdin.write('hello from openssl\n'); + +// Wait for the server to receive and reply. +await serverReceivedData.promise; +await setTimeout(500); + +// Close stdin so s_client exits. +client.stdin.end(); + +// Wait for s_client to exit. +const code = await new Promise((resolve) => client.on('close', resolve)); +clearTimeout(timeout); + +// s_client should exit cleanly. +assert.strictEqual(code, 0, + `openssl s_client exited with code ${code}\n${stderr}`); + +// Verify the reply from Node's server appeared in s_client's stdout. +assert(stdout.includes(reply), + `Expected stdout to include "${reply}"\n${stdout}`); + +// Verify it was a DTLS connection. +assert(stdout.includes('DTLS'), + `Expected stdout to include "DTLS"\n${stdout}`); + +await endpoint.close(); diff --git a/test/sequential/test-dtls-interop-openssl-server.mjs b/test/sequential/test-dtls-interop-openssl-server.mjs new file mode 100644 index 00000000000..26e66710611 --- /dev/null +++ b/test/sequential/test-dtls-interop-openssl-server.mjs @@ -0,0 +1,95 @@ +// Flags: --experimental-dtls --no-warnings + +// Test: DTLS interop -- OpenSSL s_server with Node.js DTLS client. +// Verifies that Node's DTLS client can complete a handshake with an +// external DTLS server (OpenSSL CLI) and exchange application data. + +import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs'; +import { createRequire } from 'module'; +import assert from 'node:assert'; +import { spawn } from 'node:child_process'; +import * as fixtures from '../common/fixtures.mjs'; + +if (!hasCrypto) { + skip('missing crypto'); +} + +if (!process.features.dtls) { + skip('DTLS is not enabled'); +} + +const require = createRequire(import.meta.url); +const common = require('../common'); +const { opensslCli } = require('../common/crypto'); + +if (!opensslCli) { + skip('missing openssl-cli'); +} + +const { connect } = await import('node:dtls'); + +const reply = 'I AM THE WALRUS'; // Something recognizable + +// Start OpenSSL DTLS server. +const server = spawn(opensslCli, [ + 's_server', + '-dtls1_2', + '-accept', String(common.PORT), + '-cert', fixtures.path('keys/agent1-cert.pem'), + '-key', fixtures.path('keys/agent1-key.pem'), + '-listen', +], { stdio: 'pipe' }); + +let serverOut = ''; +server.stdout.on('data', (data) => { serverOut += data; }); +let serverErr = ''; +server.stderr.on('data', (data) => { serverErr += data; }); +server.on('error', mustNotCall()); + +const timeout = setTimeout(() => { + server.kill(); + assert.fail(`Test timed out\nstdout: ${serverOut}\nstderr: ${serverErr}`); +}, 10000); + +// Wait for "ACCEPT" on stdout -- this means s_server is ready. +await new Promise((resolve) => { + server.stdout.on('data', function onReady() { + if (!serverOut.includes('ACCEPT')) return; + server.stdout.removeListener('data', onReady); + resolve(); + }); +}); + +// Connect Node.js DTLS client. +const session = connect('127.0.0.1', common.PORT, { + ca: [fixtures.readKey('ca1-cert.pem').toString()], + rejectUnauthorized: false, +}); + +const { protocol } = await session.opened; +assert.match(protocol, /DTLS/i); + +// Send data from Node to OpenSSL server. +session.send('hello from node'); + +// Send data from OpenSSL server to Node client via s_server stdin. +// s_server forwards its stdin to the connected client. +server.stdin.write(reply + '\n'); + +// Wait for Node client to receive the message. +const data = await new Promise((resolve) => { + session.onmessage = mustCall(resolve); +}); +assert.strictEqual(data.toString().trim(), reply); + +// Clean up. +await session.close(); +await session.endpoint.close(); +clearTimeout(timeout); +server.kill(); + +// Wait for server to exit. +const [, signal] = await new Promise((resolve) => { + server.on('exit', mustCall((...args) => resolve(args))); +}); +assert.strictEqual(signal, 'SIGTERM');