Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 33 additions & 36 deletions test/parallel/test-dtls-accessors.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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();

Expand All @@ -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],
Expand All @@ -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();
4 changes: 2 additions & 2 deletions test/parallel/test-dtls-alpn.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,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();
Expand Down
17 changes: 7 additions & 10 deletions test/parallel/test-dtls-ciphers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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';

Expand All @@ -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' });

Expand All @@ -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' });
15 changes: 6 additions & 9 deletions test/parallel/test-dtls-client-cert.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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.
{
Expand All @@ -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();
Expand All @@ -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/
});

Expand Down
11 changes: 4 additions & 7 deletions test/parallel/test-dtls-connect-error-cleanup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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.
Expand All @@ -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.
Expand Down
8 changes: 3 additions & 5 deletions test/parallel/test-dtls-destroy-in-callback.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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
Expand Down
17 changes: 7 additions & 10 deletions test/parallel/test-dtls-errors.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -20,29 +17,29 @@ 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' });

// Binding the same endpoint twice fails.
{
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();
}
13 changes: 5 additions & 8 deletions test/parallel/test-dtls-keylog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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();

Expand All @@ -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();
});

Expand Down
Loading
Loading