mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/64716 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Aviv Keller <me@aviv.sh>
88 lines
2.2 KiB
JavaScript
88 lines
2.2 KiB
JavaScript
// Flags: --experimental-dtls --no-warnings
|
|
|
|
// Test: Basic DTLS handshake and bidirectional data exchange.
|
|
|
|
import { hasCrypto, skip, mustCall } 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, connect } = await import('node:dtls');
|
|
|
|
const serverCert = fixtures.readKey('agent1-cert.pem');
|
|
const serverKey = fixtures.readKey('agent1-key.pem');
|
|
const ca = fixtures.readKey('ca1-cert.pem');
|
|
|
|
const serverReceivedData = Promise.withResolvers();
|
|
const clientReceivedData = Promise.withResolvers();
|
|
|
|
let serverHandshakeDone = false;
|
|
let clientHandshakeDone = false;
|
|
|
|
// Start server.
|
|
const endpoint = listen(mustCall((session) => {
|
|
session.onmessage = mustCall((data) => {
|
|
assert.strictEqual(data.toString(), 'hello from client');
|
|
serverReceivedData.resolve();
|
|
|
|
// Send response back to client.
|
|
session.send('hello from server');
|
|
});
|
|
|
|
session.onhandshake = mustCall((protocol) => {
|
|
assert.ok(protocol);
|
|
assert.match(protocol, /DTLS/i);
|
|
serverHandshakeDone = true;
|
|
});
|
|
}), {
|
|
cert: serverCert.toString(),
|
|
key: serverKey.toString(),
|
|
port: 0,
|
|
host: '127.0.0.1',
|
|
});
|
|
|
|
const serverAddress = endpoint.address;
|
|
assert.ok(serverAddress);
|
|
assert.ok(serverAddress.port > 0);
|
|
|
|
// Connect client.
|
|
const clientSession = connect('127.0.0.1', serverAddress.port, {
|
|
ca: [ca.toString()],
|
|
rejectUnauthorized: false,
|
|
});
|
|
|
|
clientSession.onmessage = mustCall((data) => {
|
|
assert.strictEqual(data.toString(), 'hello from server');
|
|
clientReceivedData.resolve();
|
|
});
|
|
|
|
clientSession.onhandshake = mustCall((protocol) => {
|
|
assert.ok(protocol);
|
|
clientHandshakeDone = true;
|
|
});
|
|
|
|
// Wait for handshake.
|
|
const { protocol } = await clientSession.opened;
|
|
assert.match(protocol, /DTLS/i);
|
|
|
|
// Send data.
|
|
clientSession.send('hello from client');
|
|
|
|
// Wait for bidirectional exchange.
|
|
await Promise.all([serverReceivedData.promise, clientReceivedData.promise]);
|
|
|
|
// Verify handshakes completed.
|
|
assert.ok(clientHandshakeDone);
|
|
assert.ok(serverHandshakeDone);
|
|
|
|
// Clean up.
|
|
await clientSession.close();
|
|
await endpoint.close();
|