mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
test: fix lint in dtls tests
a7d16a8cc6 introduced destructured uses of assert and fixtures, which
the test lint rules forbid, plus bare strictEqual/throws calls that
were never imported and threw ReferenceError at runtime. Use the
assert and fixtures namespaces directly.
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/64902
Reviewed-By: Mike McCready <66998419+MikeMcC399@users.noreply.github.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
81cd6d7b78
commit
9aa8489a01
23 files changed
+216
-281
No files matched your search
@@ -9,9 +9,6 @@ import {
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -22,9 +19,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
const gotServerSession = Promise.withResolvers();
|
||||
|
||||
@@ -34,26 +31,26 @@ const server = listen(mustCall((session) => {
|
||||
|
||||
// --- Endpoint state after listen(): bound and listening. ---
|
||||
const es = server.state;
|
||||
strictEqual(es.bound, true);
|
||||
strictEqual(es.listening, true);
|
||||
strictEqual(es.closing, false);
|
||||
strictEqual(es.destroyed, false);
|
||||
strictEqual(es.sessionCount, 0);
|
||||
assert.strictEqual(es.bound, true);
|
||||
assert.strictEqual(es.listening, true);
|
||||
assert.strictEqual(es.closing, false);
|
||||
assert.strictEqual(es.destroyed, false);
|
||||
assert.strictEqual(es.sessionCount, 0);
|
||||
|
||||
// The busy property is settable via the endpoint and reflected in the state view.
|
||||
strictEqual(server.busy, false);
|
||||
strictEqual(es.busy, false);
|
||||
assert.strictEqual(server.busy, false);
|
||||
assert.strictEqual(es.busy, false);
|
||||
server.busy = true;
|
||||
strictEqual(server.busy, true);
|
||||
strictEqual(es.busy, true);
|
||||
assert.strictEqual(server.busy, true);
|
||||
assert.strictEqual(es.busy, true);
|
||||
server.busy = false;
|
||||
strictEqual(es.busy, false);
|
||||
assert.strictEqual(es.busy, false);
|
||||
|
||||
// --- Endpoint onerror accessor. ---
|
||||
strictEqual(server.onerror, undefined);
|
||||
assert.strictEqual(server.onerror, undefined);
|
||||
const onEndpointError = mustNotCall();
|
||||
server.onerror = onEndpointError;
|
||||
strictEqual(server.onerror, onEndpointError);
|
||||
assert.strictEqual(server.onerror, onEndpointError);
|
||||
|
||||
const client = connect('127.0.0.1', server.address.port, {
|
||||
ca: [ca],
|
||||
@@ -62,43 +59,43 @@ const client = connect('127.0.0.1', server.address.port, {
|
||||
|
||||
// --- Session state during the handshake. ---
|
||||
const cs = client.state;
|
||||
strictEqual(cs.handshaking, true);
|
||||
strictEqual(cs.open, false);
|
||||
strictEqual(cs.closing, false);
|
||||
strictEqual(cs.destroyed, false);
|
||||
strictEqual(cs.hasMessageListener, false);
|
||||
assert.strictEqual(cs.handshaking, true);
|
||||
assert.strictEqual(cs.open, false);
|
||||
assert.strictEqual(cs.closing, false);
|
||||
assert.strictEqual(cs.destroyed, false);
|
||||
assert.strictEqual(cs.hasMessageListener, false);
|
||||
|
||||
// --- Session callback accessors: unset, then set. ---
|
||||
strictEqual(client.onmessage, undefined);
|
||||
strictEqual(client.onerror, undefined);
|
||||
strictEqual(client.onhandshake, undefined);
|
||||
strictEqual(client.onkeylog, undefined);
|
||||
assert.strictEqual(client.onmessage, undefined);
|
||||
assert.strictEqual(client.onerror, undefined);
|
||||
assert.strictEqual(client.onhandshake, undefined);
|
||||
assert.strictEqual(client.onkeylog, undefined);
|
||||
// A connect() session owns its internal endpoint.
|
||||
strictEqual(client.ownsEndpoint, true);
|
||||
assert.strictEqual(client.ownsEndpoint, true);
|
||||
|
||||
client.onmessage = mustNotCall();
|
||||
strictEqual(typeof client.onmessage, 'function');
|
||||
assert.strictEqual(typeof client.onmessage, 'function');
|
||||
// Attaching a message listener flips the shared flag.
|
||||
strictEqual(cs.hasMessageListener, true);
|
||||
assert.strictEqual(cs.hasMessageListener, true);
|
||||
|
||||
client.onerror = mustNotCall();
|
||||
strictEqual(typeof client.onerror, 'function');
|
||||
assert.strictEqual(typeof client.onerror, 'function');
|
||||
|
||||
client.onhandshake = mustCall();
|
||||
strictEqual(typeof client.onhandshake, 'function');
|
||||
assert.strictEqual(typeof client.onhandshake, 'function');
|
||||
|
||||
client.onkeylog = mustCallAtLeast();
|
||||
strictEqual(typeof client.onkeylog, 'function');
|
||||
assert.strictEqual(typeof client.onkeylog, 'function');
|
||||
|
||||
await client.opened;
|
||||
|
||||
// --- Session state after the handshake completes. ---
|
||||
strictEqual(cs.handshaking, false);
|
||||
strictEqual(cs.open, true);
|
||||
assert.strictEqual(cs.handshaking, false);
|
||||
assert.strictEqual(cs.open, true);
|
||||
|
||||
const serverSession = await gotServerSession.promise;
|
||||
await serverSession.opened;
|
||||
strictEqual(es.sessionCount, 1);
|
||||
assert.strictEqual(es.sessionCount, 1);
|
||||
|
||||
await client.close();
|
||||
await server.close();
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,16 +16,16 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const serverCert = readKey('agent1-cert.pem');
|
||||
const serverKey = readKey('agent1-key.pem');
|
||||
const ca = readKey('ca1-cert.pem');
|
||||
const serverCert = fixtures.readKey('agent1-cert.pem');
|
||||
const serverKey = fixtures.readKey('agent1-key.pem');
|
||||
const ca = fixtures.readKey('ca1-cert.pem');
|
||||
|
||||
const serverAlpnChecked = Promise.withResolvers();
|
||||
|
||||
const endpoint = listen(mustCall(async (session) => {
|
||||
await session.opened;
|
||||
// Server should see the negotiated ALPN protocol.
|
||||
strictEqual(session.alpnProtocol, 'coap');
|
||||
assert.strictEqual(session.alpnProtocol, 'coap');
|
||||
serverAlpnChecked.resolve();
|
||||
}), {
|
||||
cert: serverCert.toString(),
|
||||
@@ -47,7 +44,7 @@ const session = connect('127.0.0.1', endpoint.address.port, {
|
||||
await session.opened;
|
||||
|
||||
// Client should see the negotiated protocol.
|
||||
strictEqual(session.alpnProtocol, 'coap');
|
||||
assert.strictEqual(session.alpnProtocol, 'coap');
|
||||
|
||||
await serverAlpnChecked.promise;
|
||||
|
||||
@@ -77,8 +74,8 @@ await endpoint.close();
|
||||
const serverSession = await gotServerSession.promise;
|
||||
await serverSession.opened;
|
||||
|
||||
strictEqual(client.alpnProtocol, undefined);
|
||||
strictEqual(serverSession.alpnProtocol, undefined);
|
||||
assert.strictEqual(client.alpnProtocol, undefined);
|
||||
assert.strictEqual(serverSession.alpnProtocol, undefined);
|
||||
|
||||
await client.close();
|
||||
await server.close();
|
||||
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const serverCert = readKey('agent1-cert.pem');
|
||||
const serverKey = readKey('agent1-key.pem');
|
||||
const ca = readKey('ca1-cert.pem');
|
||||
const serverCert = fixtures.readKey('agent1-cert.pem');
|
||||
const serverKey = fixtures.readKey('agent1-key.pem');
|
||||
const ca = fixtures.readKey('ca1-cert.pem');
|
||||
|
||||
const endpoint = listen(mustCall((session) => {
|
||||
session.onmessage = mustNotCall();
|
||||
@@ -40,8 +37,8 @@ const session = connect('127.0.0.1', endpoint.address.port, {
|
||||
await session.opened;
|
||||
|
||||
// Test that Symbol.asyncDispose exists.
|
||||
strictEqual(typeof session[Symbol.asyncDispose], 'function');
|
||||
strictEqual(typeof endpoint[Symbol.asyncDispose], 'function');
|
||||
assert.strictEqual(typeof session[Symbol.asyncDispose], 'function');
|
||||
assert.strictEqual(typeof endpoint[Symbol.asyncDispose], 'function');
|
||||
|
||||
// Dispose the session.
|
||||
await session[Symbol.asyncDispose]();
|
||||
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { ok, strictEqual, match } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const serverCert = readKey('agent1-cert.pem');
|
||||
const serverKey = readKey('agent1-key.pem');
|
||||
const ca = readKey('ca1-cert.pem');
|
||||
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();
|
||||
@@ -32,7 +29,7 @@ let clientHandshakeDone = false;
|
||||
// Start server.
|
||||
const endpoint = listen(mustCall((session) => {
|
||||
session.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), 'hello from client');
|
||||
assert.strictEqual(data.toString(), 'hello from client');
|
||||
serverReceivedData.resolve();
|
||||
|
||||
// Send response back to client.
|
||||
@@ -40,8 +37,8 @@ const endpoint = listen(mustCall((session) => {
|
||||
});
|
||||
|
||||
session.onhandshake = mustCall((protocol) => {
|
||||
ok(protocol);
|
||||
match(protocol, /DTLS/i);
|
||||
assert.ok(protocol);
|
||||
assert.match(protocol, /DTLS/i);
|
||||
serverHandshakeDone = true;
|
||||
});
|
||||
}), {
|
||||
@@ -52,8 +49,8 @@ const endpoint = listen(mustCall((session) => {
|
||||
});
|
||||
|
||||
const serverAddress = endpoint.address;
|
||||
ok(serverAddress);
|
||||
ok(serverAddress.port > 0);
|
||||
assert.ok(serverAddress);
|
||||
assert.ok(serverAddress.port > 0);
|
||||
|
||||
// Connect client.
|
||||
const clientSession = connect('127.0.0.1', serverAddress.port, {
|
||||
@@ -62,18 +59,18 @@ const clientSession = connect('127.0.0.1', serverAddress.port, {
|
||||
});
|
||||
|
||||
clientSession.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), 'hello from server');
|
||||
assert.strictEqual(data.toString(), 'hello from server');
|
||||
clientReceivedData.resolve();
|
||||
});
|
||||
|
||||
clientSession.onhandshake = mustCall((protocol) => {
|
||||
ok(protocol);
|
||||
assert.ok(protocol);
|
||||
clientHandshakeDone = true;
|
||||
});
|
||||
|
||||
// Wait for handshake.
|
||||
const { protocol } = await clientSession.opened;
|
||||
match(protocol, /DTLS/i);
|
||||
assert.match(protocol, /DTLS/i);
|
||||
|
||||
// Send data.
|
||||
clientSession.send('hello from client');
|
||||
@@ -82,8 +79,8 @@ clientSession.send('hello from client');
|
||||
await Promise.all([serverReceivedData.promise, clientReceivedData.promise]);
|
||||
|
||||
// Verify handshakes completed.
|
||||
ok(clientHandshakeDone);
|
||||
ok(serverHandshakeDone);
|
||||
assert.ok(clientHandshakeDone);
|
||||
assert.ok(serverHandshakeDone);
|
||||
|
||||
// Clean up.
|
||||
await clientSession.close();
|
||||
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual, throws } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
const CIPHER = 'ECDHE-RSA-AES128-GCM-SHA256';
|
||||
|
||||
@@ -43,15 +40,15 @@ const CIPHER = 'ECDHE-RSA-AES128-GCM-SHA256';
|
||||
const serverSession = await gotServerSession.promise;
|
||||
await serverSession.opened;
|
||||
|
||||
strictEqual(client.cipher.name, CIPHER);
|
||||
strictEqual(serverSession.cipher.name, CIPHER);
|
||||
assert.strictEqual(client.cipher.name, CIPHER);
|
||||
assert.strictEqual(serverSession.cipher.name, CIPHER);
|
||||
|
||||
await client.close();
|
||||
await server.close();
|
||||
}
|
||||
|
||||
// Case 2: an invalid cipher list is rejected.
|
||||
throws(() => listen(mustNotCall(), {
|
||||
assert.throws(() => listen(mustNotCall(), {
|
||||
cert, key, port: 0, host: '127.0.0.1', ciphers: 'THIS-IS-NOT-A-CIPHER',
|
||||
}), { code: 'ERR_CRYPTO_OPERATION_FAILED' });
|
||||
|
||||
@@ -74,6 +71,6 @@ throws(() => listen(mustNotCall(), {
|
||||
}
|
||||
|
||||
// Case 4: an invalid ECDH curve is rejected.
|
||||
throws(() => listen(mustNotCall(), {
|
||||
assert.throws(() => listen(mustNotCall(), {
|
||||
cert, key, port: 0, host: '127.0.0.1', ecdhCurve: 'not-a-curve',
|
||||
}), { code: 'ERR_CRYPTO_OPERATION_FAILED' });
|
||||
@@ -7,9 +7,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { ok, rejects } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -20,9 +17,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
// Case 1: the client presents a certificate the server can verify.
|
||||
{
|
||||
@@ -44,8 +41,8 @@ const ca = readKey('ca1-cert.pem').toString();
|
||||
|
||||
// The server received and verified the client's certificate.
|
||||
const clientCert = serverSession.peerCertificate;
|
||||
ok(clientCert);
|
||||
ok(clientCert.includes('BEGIN CERTIFICATE'));
|
||||
assert.ok(clientCert);
|
||||
assert.ok(clientCert.includes('BEGIN CERTIFICATE'));
|
||||
|
||||
await client.close();
|
||||
await server.close();
|
||||
@@ -63,7 +60,7 @@ const ca = readKey('ca1-cert.pem').toString();
|
||||
});
|
||||
|
||||
// The exact alert text varies, so assert only that the handshake is rejected.
|
||||
await rejects(client.opened, {
|
||||
await assert.rejects(client.opened, {
|
||||
message: /handshake failure/
|
||||
});
|
||||
|
||||
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { ok, throws } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const serverCert = readKey('agent1-cert.pem');
|
||||
const serverKey = readKey('agent1-key.pem');
|
||||
const ca = readKey('ca1-cert.pem');
|
||||
const serverCert = fixtures.readKey('agent1-cert.pem');
|
||||
const serverKey = fixtures.readKey('agent1-key.pem');
|
||||
const ca = fixtures.readKey('ca1-cert.pem');
|
||||
|
||||
// Test 1: Graceful close from client side.
|
||||
{
|
||||
@@ -48,7 +45,7 @@ const ca = readKey('ca1-cert.pem');
|
||||
|
||||
// Graceful close.
|
||||
const closedPromise = session.close();
|
||||
ok(closedPromise instanceof Promise);
|
||||
assert.ok(closedPromise instanceof Promise);
|
||||
await closedPromise;
|
||||
|
||||
// Wait for server to see the close.
|
||||
@@ -80,7 +77,7 @@ const ca = readKey('ca1-cert.pem');
|
||||
session.destroy();
|
||||
|
||||
// After destroy, send should fail.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
session.send('should fail');
|
||||
}, /destroyed/i);
|
||||
|
||||
|
||||
@@ -8,9 +8,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { rejects } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -21,9 +18,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
// The client rejects the certificate mid-handshake, so this server session
|
||||
// never opens; its opened rejection is handled internally by the library.
|
||||
@@ -37,7 +34,7 @@ const session = connect('127.0.0.1', server.address.port, {
|
||||
servername: 'wrong.example.com',
|
||||
});
|
||||
|
||||
await rejects(session.opened, /certificate verify failed/i);
|
||||
await assert.rejects(session.opened, /certificate verify failed/i);
|
||||
|
||||
// The failed connect must have closed its internally-owned endpoint. Without
|
||||
// that, this await never settles and the test times out.
|
||||
|
||||
@@ -9,8 +9,6 @@
|
||||
import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -21,9 +19,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Case 1: destroy the (server) session from inside onmessage. The datagram
|
||||
|
||||
@@ -7,9 +7,6 @@ import { hasCrypto, skip, mustNotCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { throws } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -20,22 +17,22 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, DTLSEndpoint } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const mismatchedKey = readKey('agent2-key.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const mismatchedKey = fixtures.readKey('agent2-key.pem').toString();
|
||||
|
||||
// A malformed certificate PEM is rejected.
|
||||
throws(() => listen(mustNotCall(), {
|
||||
assert.throws(() => listen(mustNotCall(), {
|
||||
cert: 'not a certificate', key, port: 0,
|
||||
}), { code: 'ERR_CRYPTO_OPERATION_FAILED' });
|
||||
|
||||
// A malformed private key PEM is rejected.
|
||||
throws(() => listen(mustNotCall(), {
|
||||
assert.throws(() => listen(mustNotCall(), {
|
||||
cert, key: 'not a key', port: 0,
|
||||
}), { code: 'ERR_CRYPTO_OPERATION_FAILED' });
|
||||
|
||||
// A private key that does not match the certificate is rejected.
|
||||
throws(() => listen(mustNotCall(), {
|
||||
assert.throws(() => listen(mustNotCall(), {
|
||||
cert, key: mismatchedKey, port: 0,
|
||||
}), { code: 'ERR_CRYPTO_OPERATION_FAILED' });
|
||||
|
||||
@@ -43,6 +40,6 @@ throws(() => listen(mustNotCall(), {
|
||||
{
|
||||
const endpoint = new DTLSEndpoint();
|
||||
endpoint.bind('127.0.0.1', 0);
|
||||
throws(() => endpoint.bind('127.0.0.1', 0), { code: 'ERR_INVALID_STATE' });
|
||||
assert.throws(() => endpoint.bind('127.0.0.1', 0), { code: 'ERR_INVALID_STATE' });
|
||||
await endpoint.close();
|
||||
}
|
||||
@@ -7,9 +7,6 @@ import { hasCrypto, skip, mustCall, mustCallAtLeast } from '../common/index.mjs'
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual, match } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -20,9 +17,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
const gotKeylog = Promise.withResolvers();
|
||||
|
||||
@@ -37,8 +34,8 @@ const client = connect('127.0.0.1', server.address.port, {
|
||||
|
||||
// A keylog line is "<LABEL> <hex> <hex>" (e.g. "CLIENT_RANDOM ...").
|
||||
client.onkeylog = mustCallAtLeast((line) => {
|
||||
strictEqual(typeof line, 'string');
|
||||
match(line, /^\S+ [0-9a-f]+ [0-9a-f]+$/i);
|
||||
assert.strictEqual(typeof line, 'string');
|
||||
assert.match(line, /^\S+ [0-9a-f]+ [0-9a-f]+$/i);
|
||||
gotKeylog.resolve();
|
||||
});
|
||||
|
||||
|
||||
@@ -7,9 +7,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual, throws } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -20,13 +17,13 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect, DTLSEndpoint } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
// MTU must be within [256, 65535].
|
||||
throws(() => new DTLSEndpoint({ mtu: 255 }), { code: 'ERR_OUT_OF_RANGE' });
|
||||
throws(() => new DTLSEndpoint({ mtu: 65536 }), { code: 'ERR_OUT_OF_RANGE' });
|
||||
assert.throws(() => new DTLSEndpoint({ mtu: 255 }), { code: 'ERR_OUT_OF_RANGE' });
|
||||
assert.throws(() => new DTLSEndpoint({ mtu: 65536 }), { code: 'ERR_OUT_OF_RANGE' });
|
||||
|
||||
// A valid boundary MTU is accepted.
|
||||
{
|
||||
@@ -41,7 +38,7 @@ throws(() => new DTLSEndpoint({ mtu: 65536 }), { code: 'ERR_OUT_OF_RANGE' });
|
||||
|
||||
const server = listen(mustCall((session) => {
|
||||
session.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), 'hello');
|
||||
assert.strictEqual(data.toString(), 'hello');
|
||||
session.send('world');
|
||||
});
|
||||
}), {
|
||||
@@ -55,7 +52,7 @@ throws(() => new DTLSEndpoint({ mtu: 65536 }), { code: 'ERR_OUT_OF_RANGE' });
|
||||
});
|
||||
|
||||
client.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), 'world');
|
||||
assert.strictEqual(data.toString(), 'world');
|
||||
received.resolve();
|
||||
});
|
||||
|
||||
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const serverCert = readKey('agent1-cert.pem');
|
||||
const serverKey = readKey('agent1-key.pem');
|
||||
const ca = readKey('ca1-cert.pem');
|
||||
const serverCert = fixtures.readKey('agent1-cert.pem');
|
||||
const serverKey = fixtures.readKey('agent1-key.pem');
|
||||
const ca = fixtures.readKey('ca1-cert.pem');
|
||||
|
||||
const NUM_CLIENTS = 3;
|
||||
let sessionsAccepted = 0;
|
||||
@@ -57,7 +54,7 @@ for (let i = 0; i < NUM_CLIENTS; i++) {
|
||||
});
|
||||
|
||||
session.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), `echo:client${i}`);
|
||||
assert.strictEqual(data.toString(), `echo:client${i}`);
|
||||
received.resolve();
|
||||
});
|
||||
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
import { hasCrypto, skip, mustNotCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
|
||||
const { throws } = assert;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -18,50 +16,50 @@ if (!process.features.dtls) {
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
// Test: listen() requires a callback.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
listen(undefined, { cert: 'x', key: 'y', port: 0 });
|
||||
}, { code: 'ERR_INVALID_ARG_TYPE' });
|
||||
|
||||
// Test: listen() requires cert.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
listen(mustNotCall(), { key: 'y', port: 0 });
|
||||
}, { code: 'ERR_MISSING_ARGS' });
|
||||
|
||||
// Test: listen() requires key.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
listen(mustNotCall(), { cert: 'x', port: 0 });
|
||||
}, { code: 'ERR_MISSING_ARGS' });
|
||||
|
||||
// Test: listen() requires port.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
listen(mustNotCall(), { cert: 'x', key: 'y' });
|
||||
}, { code: 'ERR_MISSING_ARGS' });
|
||||
|
||||
// Test: connect() requires valid host.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
connect(123, 4433);
|
||||
}, { code: 'ERR_INVALID_ARG_TYPE' });
|
||||
|
||||
// Test: connect() requires valid port.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
connect('localhost', 'invalid');
|
||||
}, { code: 'ERR_INVALID_ARG_TYPE' });
|
||||
|
||||
// Test: connect() rejects out-of-range port.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
connect('localhost', 99999);
|
||||
}, { code: 'ERR_OUT_OF_RANGE' });
|
||||
|
||||
// Test: mtu must be an integer within [256, 65535].
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
connect('127.0.0.1', 4433, { mtu: 100 });
|
||||
}, { code: 'ERR_OUT_OF_RANGE' });
|
||||
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
connect('127.0.0.1', 4433, { mtu: 70000 });
|
||||
}, { code: 'ERR_OUT_OF_RANGE' });
|
||||
|
||||
// Test: alpn must be a string array or Buffer.
|
||||
throws(() => {
|
||||
assert.throws(() => {
|
||||
connect('127.0.0.1', 4433, { alpn: 123 });
|
||||
}, { code: 'ERR_INVALID_ARG_TYPE' });
|
||||
@@ -8,9 +8,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { match, rejects } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -21,8 +18,8 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
|
||||
// Case 1: with rejectUnauthorized, a certificate that does not chain to a
|
||||
// trusted CA fails verification. The servername matches the certificate, so
|
||||
@@ -39,7 +36,7 @@ const key = readKey('agent1-key.pem').toString();
|
||||
servername: 'agent1',
|
||||
});
|
||||
|
||||
await rejects(client.opened, /certificate verify failed/i);
|
||||
await assert.rejects(client.opened, /certificate verify failed/i);
|
||||
|
||||
await client.endpoint.closed;
|
||||
await server.close();
|
||||
@@ -56,7 +53,7 @@ const key = readKey('agent1-key.pem').toString();
|
||||
});
|
||||
|
||||
const { protocol } = await client.opened;
|
||||
match(protocol, /DTLS/i);
|
||||
assert.match(protocol, /DTLS/i);
|
||||
|
||||
await client.close();
|
||||
await server.close();
|
||||
|
||||
@@ -7,8 +7,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import dgram from 'node:dgram';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +17,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
const server = listen(mustCall((session) => {
|
||||
session.onhandshake = mustCall();
|
||||
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
// The largest application payload that fits in a single DTLS record (2^14).
|
||||
const MAX_RECORD = 16384;
|
||||
@@ -40,12 +37,12 @@ const MAX_RECORD = 16384;
|
||||
rejectUnauthorized: false,
|
||||
});
|
||||
|
||||
strictEqual(client.send('before handshake'), -1);
|
||||
assert.strictEqual(client.send('before handshake'), -1);
|
||||
|
||||
await client.opened;
|
||||
|
||||
strictEqual(client.send(Buffer.alloc(MAX_RECORD, 0x61)), MAX_RECORD);
|
||||
strictEqual(await received.promise, MAX_RECORD);
|
||||
assert.strictEqual(client.send(Buffer.alloc(MAX_RECORD, 0x61)), MAX_RECORD);
|
||||
assert.strictEqual(await received.promise, MAX_RECORD);
|
||||
|
||||
await client.close();
|
||||
await server.close();
|
||||
@@ -63,7 +60,7 @@ const MAX_RECORD = 16384;
|
||||
});
|
||||
|
||||
await client.opened;
|
||||
strictEqual(client.send(Buffer.alloc(MAX_RECORD + 1, 0x61)), -1);
|
||||
assert.strictEqual(client.send(Buffer.alloc(MAX_RECORD + 1, 0x61)), -1);
|
||||
|
||||
await client.close();
|
||||
await server.close();
|
||||
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { ok, strictEqual, match } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const serverCert = readKey('agent1-cert.pem');
|
||||
const serverKey = readKey('agent1-key.pem');
|
||||
const ca = readKey('ca1-cert.pem');
|
||||
const serverCert = fixtures.readKey('agent1-cert.pem');
|
||||
const serverKey = fixtures.readKey('agent1-key.pem');
|
||||
const ca = fixtures.readKey('ca1-cert.pem');
|
||||
|
||||
const endpoint = listen(mustCall((session) => {
|
||||
session.onmessage = mustNotCall();
|
||||
@@ -40,25 +37,25 @@ const session = connect('127.0.0.1', endpoint.address.port, {
|
||||
await session.opened;
|
||||
|
||||
// Protocol should be DTLSv1.2.
|
||||
match(session.protocol, /DTLS/i);
|
||||
assert.match(session.protocol, /DTLS/i);
|
||||
|
||||
// Cipher should be an object with name, standardName, version.
|
||||
const cipher = session.cipher;
|
||||
strictEqual(typeof cipher?.name, 'string');
|
||||
strictEqual(typeof cipher?.standardName, 'string');
|
||||
strictEqual(typeof cipher?.version, 'string');
|
||||
assert.strictEqual(typeof cipher?.name, 'string');
|
||||
assert.strictEqual(typeof cipher?.standardName, 'string');
|
||||
assert.strictEqual(typeof cipher?.version, 'string');
|
||||
|
||||
// Remote address should be defined.
|
||||
const addr = session.remoteAddress;
|
||||
ok(addr);
|
||||
assert.ok(addr);
|
||||
|
||||
// Peer certificate should be available (PEM string).
|
||||
const peerCert = session.peerCertificate;
|
||||
ok(peerCert);
|
||||
ok(peerCert.includes('BEGIN CERTIFICATE'));
|
||||
assert.ok(peerCert);
|
||||
assert.ok(peerCert.includes('BEGIN CERTIFICATE'));
|
||||
|
||||
// State should reflect open connection.
|
||||
ok(session.state);
|
||||
assert.ok(session.state);
|
||||
|
||||
await session.close();
|
||||
await endpoint.close();
|
||||
@@ -8,9 +8,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -21,9 +18,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Case 1: the server closes its own session -> removed from the table.
|
||||
@@ -42,9 +39,9 @@ const ca = readKey('ca1-cert.pem').toString();
|
||||
await client.opened;
|
||||
const session = await gotSession.promise;
|
||||
|
||||
strictEqual(server.state.sessionCount, 1);
|
||||
assert.strictEqual(server.state.sessionCount, 1);
|
||||
await session.close();
|
||||
strictEqual(server.state.sessionCount, 0);
|
||||
assert.strictEqual(server.state.sessionCount, 0);
|
||||
|
||||
await client.close();
|
||||
await server.close();
|
||||
@@ -68,10 +65,10 @@ const ca = readKey('ca1-cert.pem').toString();
|
||||
await client.opened;
|
||||
const session = await gotSession.promise;
|
||||
|
||||
strictEqual(server.state.sessionCount, 1);
|
||||
assert.strictEqual(server.state.sessionCount, 1);
|
||||
await client.close(); // Sends close_notify to the server
|
||||
await session.closed; // Server processes it, detaches, and emits its close
|
||||
strictEqual(server.state.sessionCount, 0);
|
||||
assert.strictEqual(server.state.sessionCount, 0);
|
||||
|
||||
await server.close();
|
||||
}
|
||||
@@ -8,9 +8,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { strictEqual, deepStrictEqual, notDeepStrictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -21,9 +18,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
const SRTP_PROFILE = 'SRTP_AEAD_AES_128_GCM';
|
||||
|
||||
@@ -50,26 +47,26 @@ const serverSession = await gotServerSession.promise;
|
||||
await serverSession.opened;
|
||||
|
||||
// Both peers negotiate the same SRTP profile.
|
||||
strictEqual(client.srtpProfile, SRTP_PROFILE);
|
||||
strictEqual(serverSession.srtpProfile, SRTP_PROFILE);
|
||||
assert.strictEqual(client.srtpProfile, SRTP_PROFILE);
|
||||
assert.strictEqual(serverSession.srtpProfile, SRTP_PROFILE);
|
||||
|
||||
// Both peers derive identical keying material for the same label and length.
|
||||
const label = 'EXTRACTOR-dtls_srtp';
|
||||
const clientKM = client.exportKeyingMaterial(60, label);
|
||||
const serverKM = serverSession.exportKeyingMaterial(60, label);
|
||||
strictEqual(clientKM.length, 60);
|
||||
deepStrictEqual(clientKM, serverKM);
|
||||
assert.strictEqual(clientKM.length, 60);
|
||||
assert.deepStrictEqual(clientKM, serverKM);
|
||||
|
||||
// The optional context is bound into the derivation: the same context yields
|
||||
// the same material on both peers, and a different context yields different
|
||||
// material.
|
||||
const contextA = Buffer.from('context-a');
|
||||
const contextB = Buffer.from('context-b');
|
||||
deepStrictEqual(
|
||||
assert.deepStrictEqual(
|
||||
client.exportKeyingMaterial(32, label, contextA),
|
||||
serverSession.exportKeyingMaterial(32, label, contextA),
|
||||
);
|
||||
notDeepStrictEqual(
|
||||
assert.notDeepStrictEqual(
|
||||
client.exportKeyingMaterial(32, label, contextA),
|
||||
client.exportKeyingMaterial(32, label, contextB),
|
||||
);
|
||||
|
||||
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { ok, strictEqual, notStrictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const serverCert = readKey('agent1-cert.pem');
|
||||
const serverKey = readKey('agent1-key.pem');
|
||||
const ca = readKey('ca1-cert.pem');
|
||||
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();
|
||||
@@ -33,7 +30,7 @@ const endpoint = listen(mustCall((session) => {
|
||||
serverSession = session;
|
||||
|
||||
session.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), 'hello from client');
|
||||
assert.strictEqual(data.toString(), 'hello from client');
|
||||
session.send('hello from server');
|
||||
serverReceivedData.resolve();
|
||||
});
|
||||
@@ -49,13 +46,13 @@ const endpoint = listen(mustCall((session) => {
|
||||
// --- Endpoint stats should be available immediately ---
|
||||
|
||||
const epStats = endpoint.stats;
|
||||
ok(epStats, 'endpoint.stats should be defined');
|
||||
ok(epStats.isConnected, 'stats should be connected');
|
||||
ok(epStats.createdAt > 0n, 'createdAt should be set');
|
||||
strictEqual(epStats.destroyedAt, 0n);
|
||||
strictEqual(epStats.clientSessions, 0n);
|
||||
strictEqual(epStats.serverSessions, 0n);
|
||||
strictEqual(epStats.serverBusyCount, 0n);
|
||||
assert.ok(epStats, 'endpoint.stats should be defined');
|
||||
assert.ok(epStats.isConnected, 'stats should be connected');
|
||||
assert.ok(epStats.createdAt > 0n, 'createdAt should be set');
|
||||
assert.strictEqual(epStats.destroyedAt, 0n);
|
||||
assert.strictEqual(epStats.clientSessions, 0n);
|
||||
assert.strictEqual(epStats.serverSessions, 0n);
|
||||
assert.strictEqual(epStats.serverBusyCount, 0n);
|
||||
|
||||
// Connect client.
|
||||
const clientSession = connect('127.0.0.1', endpoint.address.port, {
|
||||
@@ -64,7 +61,7 @@ const clientSession = connect('127.0.0.1', endpoint.address.port, {
|
||||
});
|
||||
|
||||
clientSession.onmessage = mustCall((data) => {
|
||||
strictEqual(data.toString(), 'hello from server');
|
||||
assert.strictEqual(data.toString(), 'hello from server');
|
||||
clientReceivedData.resolve();
|
||||
});
|
||||
|
||||
@@ -76,14 +73,14 @@ await clientSession.opened;
|
||||
// --- Client session stats after handshake ---
|
||||
|
||||
const csStats = clientSession.stats;
|
||||
ok(csStats, 'session.stats should be defined');
|
||||
ok(csStats.isConnected, 'session stats should be connected');
|
||||
ok(csStats.createdAt > 0n, 'createdAt should be set');
|
||||
ok(csStats.handshakeCompletedAt > 0n, 'handshake timestamp should be set');
|
||||
ok(csStats.handshakeCompletedAt >= csStats.createdAt,
|
||||
'handshake should complete after creation');
|
||||
strictEqual(csStats.closingAt, 0n);
|
||||
strictEqual(csStats.destroyedAt, 0n);
|
||||
assert.ok(csStats, 'session.stats should be defined');
|
||||
assert.ok(csStats.isConnected, 'session stats should be connected');
|
||||
assert.ok(csStats.createdAt > 0n, 'createdAt should be set');
|
||||
assert.ok(csStats.handshakeCompletedAt > 0n, 'handshake timestamp should be set');
|
||||
assert.ok(csStats.handshakeCompletedAt >= csStats.createdAt,
|
||||
'handshake should complete after creation');
|
||||
assert.strictEqual(csStats.closingAt, 0n);
|
||||
assert.strictEqual(csStats.destroyedAt, 0n);
|
||||
|
||||
// Record bytes before sending application data.
|
||||
const csBytesSentBefore = csStats.bytesSent;
|
||||
@@ -97,58 +94,58 @@ await Promise.all([serverReceivedData.promise, clientReceivedData.promise]);
|
||||
|
||||
// --- Client session stats after data exchange ---
|
||||
|
||||
ok(csStats.bytesSent > csBytesSentBefore,
|
||||
'bytesSent should increase after send');
|
||||
ok(csStats.messagesSent > csMessagesSentBefore,
|
||||
'messagesSent should increase after send');
|
||||
ok(csStats.bytesReceived > 0n, 'bytesReceived should be non-zero');
|
||||
ok(csStats.messagesReceived > 0n, 'messagesReceived should be non-zero');
|
||||
assert.ok(csStats.bytesSent > csBytesSentBefore,
|
||||
'bytesSent should increase after send');
|
||||
assert.ok(csStats.messagesSent > csMessagesSentBefore,
|
||||
'messagesSent should increase after send');
|
||||
assert.ok(csStats.bytesReceived > 0n, 'bytesReceived should be non-zero');
|
||||
assert.ok(csStats.messagesReceived > 0n, 'messagesReceived should be non-zero');
|
||||
|
||||
// --- Server session stats after data exchange ---
|
||||
|
||||
ok(serverSession, 'server session should exist');
|
||||
assert.ok(serverSession, 'server session should exist');
|
||||
const ssStats = serverSession.stats;
|
||||
ok(ssStats.bytesReceived > 0n, 'server bytesReceived should be non-zero');
|
||||
ok(ssStats.messagesReceived > 0n, 'server messagesReceived should be non-zero');
|
||||
ok(ssStats.bytesSent > 0n, 'server bytesSent should be non-zero');
|
||||
ok(ssStats.messagesSent > 0n, 'server messagesSent should be non-zero');
|
||||
ok(ssStats.handshakeCompletedAt > 0n, 'server handshake timestamp should be set');
|
||||
assert.ok(ssStats.bytesReceived > 0n, 'server bytesReceived should be non-zero');
|
||||
assert.ok(ssStats.messagesReceived > 0n, 'server messagesReceived should be non-zero');
|
||||
assert.ok(ssStats.bytesSent > 0n, 'server bytesSent should be non-zero');
|
||||
assert.ok(ssStats.messagesSent > 0n, 'server messagesSent should be non-zero');
|
||||
assert.ok(ssStats.handshakeCompletedAt > 0n, 'server handshake timestamp should be set');
|
||||
|
||||
// --- Endpoint stats after data exchange ---
|
||||
|
||||
ok(epStats.bytesReceived > 0n, 'endpoint bytesReceived should be non-zero');
|
||||
ok(epStats.bytesSent > 0n, 'endpoint bytesSent should be non-zero');
|
||||
ok(epStats.packetsReceived > 0n, 'endpoint packetsReceived should be non-zero');
|
||||
ok(epStats.packetsSent > 0n, 'endpoint packetsSent should be non-zero');
|
||||
strictEqual(epStats.serverSessions, 1n);
|
||||
assert.ok(epStats.bytesReceived > 0n, 'endpoint bytesReceived should be non-zero');
|
||||
assert.ok(epStats.bytesSent > 0n, 'endpoint bytesSent should be non-zero');
|
||||
assert.ok(epStats.packetsReceived > 0n, 'endpoint packetsReceived should be non-zero');
|
||||
assert.ok(epStats.packetsSent > 0n, 'endpoint packetsSent should be non-zero');
|
||||
assert.strictEqual(epStats.serverSessions, 1n);
|
||||
|
||||
// The client's own endpoint should track the client session.
|
||||
const clientEpStats = clientSession.endpoint.stats;
|
||||
strictEqual(clientEpStats.clientSessions, 1n);
|
||||
ok(clientEpStats.bytesSent > 0n);
|
||||
ok(clientEpStats.bytesReceived > 0n);
|
||||
assert.strictEqual(clientEpStats.clientSessions, 1n);
|
||||
assert.ok(clientEpStats.bytesSent > 0n);
|
||||
assert.ok(clientEpStats.bytesReceived > 0n);
|
||||
|
||||
// --- toJSON / toString ---
|
||||
|
||||
const epJson = epStats.toJSON();
|
||||
ok(epJson);
|
||||
strictEqual(typeof epJson.bytesReceived, 'string');
|
||||
strictEqual(typeof epJson.bytesSent, 'string');
|
||||
strictEqual(typeof epJson.connected, 'boolean');
|
||||
assert.ok(epJson);
|
||||
assert.strictEqual(typeof epJson.bytesReceived, 'string');
|
||||
assert.strictEqual(typeof epJson.bytesSent, 'string');
|
||||
assert.strictEqual(typeof epJson.connected, 'boolean');
|
||||
|
||||
const ssJson = csStats.toJSON();
|
||||
ok(ssJson);
|
||||
strictEqual(typeof ssJson.handshakeCompletedAt, 'string');
|
||||
strictEqual(typeof ssJson.messagesReceived, 'string');
|
||||
assert.ok(ssJson);
|
||||
assert.strictEqual(typeof ssJson.handshakeCompletedAt, 'string');
|
||||
assert.strictEqual(typeof ssJson.messagesReceived, 'string');
|
||||
|
||||
const epStr = epStats.toString();
|
||||
ok(typeof epStr === 'string');
|
||||
ok(epStr.includes('bytesReceived'));
|
||||
assert.ok(typeof epStr === 'string');
|
||||
assert.ok(epStr.includes('bytesReceived'));
|
||||
|
||||
// Clean up.
|
||||
await clientSession.close();
|
||||
|
||||
// After close, session closing timestamp should be set.
|
||||
notStrictEqual(csStats.closingAt, 0n);
|
||||
assert.notStrictEqual(csStats.closingAt, 0n);
|
||||
|
||||
await endpoint.close();
|
||||
@@ -6,8 +6,6 @@
|
||||
import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -21,9 +19,9 @@ process.on('unhandledRejection', mustNotCall());
|
||||
|
||||
const { listen, connect } = await import('node:dtls');
|
||||
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
const server = listen(mustCall(), {
|
||||
cert, key, port: 0, host: '127.0.0.1',
|
||||
|
||||
@@ -9,9 +9,6 @@ import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
|
||||
import assert from 'node:assert';
|
||||
import * as fixtures from '../common/fixtures.mjs';
|
||||
|
||||
const { match, rejects, strictEqual } = assert;
|
||||
const { readKey } = fixtures;
|
||||
|
||||
if (!hasCrypto) {
|
||||
skip('missing crypto');
|
||||
}
|
||||
@@ -24,9 +21,9 @@ const { listen, connect } = await import('node:dtls');
|
||||
|
||||
// The agent1 certificate has CN=agent1 and no subjectAltName, so OpenSSL
|
||||
// matches the identity "agent1" (via the subject CN) and rejects other names.
|
||||
const cert = readKey('agent1-cert.pem').toString();
|
||||
const key = readKey('agent1-key.pem').toString();
|
||||
const ca = readKey('ca1-cert.pem').toString();
|
||||
const cert = fixtures.readKey('agent1-cert.pem').toString();
|
||||
const key = fixtures.readKey('agent1-key.pem').toString();
|
||||
const ca = fixtures.readKey('ca1-cert.pem').toString();
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Case 1: a matching servername with rejectUnauthorized succeeds, and the
|
||||
@@ -49,9 +46,9 @@ const ca = readKey('ca1-cert.pem').toString();
|
||||
});
|
||||
|
||||
const { protocol } = await session.opened;
|
||||
match(protocol, /DTLS/i);
|
||||
assert.match(protocol, /DTLS/i);
|
||||
|
||||
strictEqual(await sawServername.promise, 'agent1');
|
||||
assert.strictEqual(await sawServername.promise, 'agent1');
|
||||
|
||||
await session.close();
|
||||
await server.close();
|
||||
@@ -75,7 +72,7 @@ const ca = readKey('ca1-cert.pem').toString();
|
||||
servername: 'wrong.example.com',
|
||||
});
|
||||
|
||||
await rejects(session.opened, /certificate verify failed/i);
|
||||
await assert.rejects(session.opened, /certificate verify failed/i);
|
||||
|
||||
// Closing the session tears down the internally-owned client endpoint.
|
||||
await session.close();
|
||||
|
||||
Reference in new issue
Block a user