mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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 <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
1 parent
0c24639a5a
commit
652fbc286e
7 files changed
+224
-1
No files matched your search
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
@@ -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`);
|
||||
|
||||
|
||||
@@ -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();
|
||||
@@ -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');
|
||||
Reference in new issue
Block a user