|
| 1 | +// Flags: --experimental-dtls --no-warnings |
| 2 | + |
| 3 | +// Test: DTLSEndpoint/DTLSSession state fields and callback accessors reflect |
| 4 | +// what is set and the connection lifecycle. |
| 5 | + |
| 6 | +import { |
| 7 | + hasCrypto, skip, mustCall, mustNotCall, mustCallAtLeast, |
| 8 | +} from '../common/index.mjs'; |
| 9 | +import assert from 'node:assert'; |
| 10 | +import * as fixtures from '../common/fixtures.mjs'; |
| 11 | + |
| 12 | +const { strictEqual } = assert; |
| 13 | +const { readKey } = fixtures; |
| 14 | + |
| 15 | +if (!hasCrypto) { |
| 16 | + skip('missing crypto'); |
| 17 | +} |
| 18 | + |
| 19 | +if (!process.features.dtls) { |
| 20 | + skip('DTLS is not enabled'); |
| 21 | +} |
| 22 | + |
| 23 | +const { listen, connect } = await import('node:dtls'); |
| 24 | + |
| 25 | +const cert = readKey('agent1-cert.pem').toString(); |
| 26 | +const key = readKey('agent1-key.pem').toString(); |
| 27 | +const ca = readKey('ca1-cert.pem').toString(); |
| 28 | + |
| 29 | +const gotServerSession = Promise.withResolvers(); |
| 30 | + |
| 31 | +const server = listen(mustCall((session) => { |
| 32 | + gotServerSession.resolve(session); |
| 33 | +}), { cert, key, port: 0, host: '127.0.0.1' }); |
| 34 | + |
| 35 | +// --- Endpoint state after listen(): bound and listening. --- |
| 36 | +const es = server.state; |
| 37 | +strictEqual(es.bound, true); |
| 38 | +strictEqual(es.listening, true); |
| 39 | +strictEqual(es.closing, false); |
| 40 | +strictEqual(es.destroyed, false); |
| 41 | +strictEqual(es.sessionCount, 0); |
| 42 | + |
| 43 | +// The busy property is settable via the endpoint and reflected in the state view. |
| 44 | +strictEqual(server.busy, false); |
| 45 | +strictEqual(es.busy, false); |
| 46 | +server.busy = true; |
| 47 | +strictEqual(server.busy, true); |
| 48 | +strictEqual(es.busy, true); |
| 49 | +server.busy = false; |
| 50 | +strictEqual(es.busy, false); |
| 51 | + |
| 52 | +// --- Endpoint onerror accessor. --- |
| 53 | +strictEqual(server.onerror, undefined); |
| 54 | +const onEndpointError = mustNotCall(); |
| 55 | +server.onerror = onEndpointError; |
| 56 | +strictEqual(server.onerror, onEndpointError); |
| 57 | + |
| 58 | +const client = connect('127.0.0.1', server.address.port, { |
| 59 | + ca: [ca], |
| 60 | + rejectUnauthorized: false, |
| 61 | +}); |
| 62 | + |
| 63 | +// --- Session state during the handshake. --- |
| 64 | +const cs = client.state; |
| 65 | +strictEqual(cs.handshaking, true); |
| 66 | +strictEqual(cs.open, false); |
| 67 | +strictEqual(cs.closing, false); |
| 68 | +strictEqual(cs.destroyed, false); |
| 69 | +strictEqual(cs.hasMessageListener, false); |
| 70 | + |
| 71 | +// --- Session callback accessors: unset, then set. --- |
| 72 | +strictEqual(client.onmessage, undefined); |
| 73 | +strictEqual(client.onerror, undefined); |
| 74 | +strictEqual(client.onhandshake, undefined); |
| 75 | +strictEqual(client.onkeylog, undefined); |
| 76 | +// A connect() session owns its internal endpoint. |
| 77 | +strictEqual(client.ownsEndpoint, true); |
| 78 | + |
| 79 | +client.onmessage = mustNotCall(); |
| 80 | +strictEqual(typeof client.onmessage, 'function'); |
| 81 | +// Attaching a message listener flips the shared flag. |
| 82 | +strictEqual(cs.hasMessageListener, true); |
| 83 | + |
| 84 | +client.onerror = mustNotCall(); |
| 85 | +strictEqual(typeof client.onerror, 'function'); |
| 86 | + |
| 87 | +client.onhandshake = mustCall(); |
| 88 | +strictEqual(typeof client.onhandshake, 'function'); |
| 89 | + |
| 90 | +client.onkeylog = mustCallAtLeast(); |
| 91 | +strictEqual(typeof client.onkeylog, 'function'); |
| 92 | + |
| 93 | +await client.opened; |
| 94 | + |
| 95 | +// --- Session state after the handshake completes. --- |
| 96 | +strictEqual(cs.handshaking, false); |
| 97 | +strictEqual(cs.open, true); |
| 98 | + |
| 99 | +const serverSession = await gotServerSession.promise; |
| 100 | +await serverSession.opened; |
| 101 | +strictEqual(es.sessionCount, 1); |
| 102 | + |
| 103 | +await client.close(); |
| 104 | +await server.close(); |
0 commit comments