ident
All repositories: gitoria
4.4 KB
// #375: abrupt TLS EOF/errors must deliver close once and allow reconnect.import assert from 'node:assert/strict';import net from 'node:net';import tls from 'node:tls';import { createHash } from 'node:crypto';import { spawn, spawnSync } from 'node:child_process';import { once } from 'node:events';import { mkdtempSync, readFileSync, readlinkSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import { dirname, resolve, join } from 'node:path';import { fileURLToPath } from 'node:url';const HERE = dirname(fileURLToPath(import.meta.url));const ROOT = resolve(HERE, '../../..');const BIN = process.env.HL_BIN || resolve(ROOT, 'native/zig-out/bin/hybriel');const PORT = Number(process.env.HL_TEST_PORT || 30430);const work = mkdtempSync(join(tmpdir(), 'ws-close-'));function command(cmd, args) {const r = spawnSync(cmd, args, { cwd: ROOT, encoding: 'utf8' });assert.equal(r.status, 0, r.stdout + r.stderr);}let checks = 0;try {command('openssl', ['req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', join(work, 'key.pem'), '-out', join(work, 'cert.pem'), '-days', '1', '-subj', '/CN=localhost', '-addext', 'subjectAltName=DNS:localhost']);command(BIN, ['--native', resolve(HERE, 'ws-client-close.hl'), '-o', join(work, 'client'), '--quiet']);for (const secure of [false, true]) for (const mode of ['abrupt', 'clean', 'half']) {let accepted = 0;const sockets = new Set();const timers = new Set();const handler = socket => {sockets.add(socket);socket.on('error', () => {});socket.on('close', () => sockets.delete(socket));let head = '';socket.on('data', function handshake(data) {head += data.toString();if (!head.includes('\r\n\r\n')) return;socket.removeListener('data', handshake);const key = head.match(/Sec-WebSocket-Key: (.*)/i)?.[1].trim();assert.ok(key);accepted++;socket.write('HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ' + createHash('sha1').update(key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest('base64') + '\r\n\r\n');// Longer than one read tick: a retryable TLS timeout must stay open.const timer = setTimeout(() => {timers.delete(timer);if (mode === 'abrupt') socket.destroy();if (mode === 'half') socket.end(); // TLS close_notify or TCP FIN, no WS closeif (mode === 'clean') socket.end(Buffer.from([0x88, 2, 3, 0xe8]));}, 700);timers.add(timer);});};const server = secure ? tls.createServer({ cert: readFileSync(join(work, 'cert.pem')), key: readFileSync(join(work, 'key.pem')), allowHalfOpen: true }, handler) : net.createServer({ allowHalfOpen: true }, handler);let child, timeout;try {server.listen(PORT, '127.0.0.1');await once(server, 'listening');child = spawn(join(work, 'client'), [`${secure ? 'wss' : 'ws'}://localhost:${PORT}/`, join(work, 'cert.pem')], { cwd: ROOT, env: process.env });let log = '';child.stdout.on('data', d => { log += d; });child.stderr.on('data', d => { log += d; });const exited = once(child, 'exit');timeout = setTimeout(() => {assert.equal(readlinkSync(`/proc/${child.pid}/cwd`), ROOT);spawnSync('ss', ['-ltnp', `sport = ${PORT}`]);child.kill('SIGKILL');}, 6000);const [code] = await exited;assert.equal(code, 0, log);assert.equal(accepted, 2, log);assert.deepEqual(log.split('\n').filter(s => /^(opened|closed|retry)/.test(s)), ['opened', `closed ${mode === 'clean' ? 1000 : 1006}`, 'retry true', 'opened', `closed ${mode === 'clean' ? 1000 : 1006}`]);checks += 3;console.log(`ok ${secure ? 'wss' : 'ws'} ${mode}: two opens, two closes, reconnect`);} finally {clearTimeout(timeout);for (const timer of timers) clearTimeout(timer);if (child && child.exitCode === null && child.signalCode === null) {assert.equal(readlinkSync(`/proc/${child.pid}/cwd`), ROOT);spawnSync('ss', ['-ltnp', `sport = ${PORT}`]);const exited = once(child, 'exit'); child.kill('SIGKILL'); await exited;}for (const socket of sockets) socket.destroy();await new Promise(r => server.close(r));}}console.log(`ok ws-client-close: ${checks} checks across 6 cases`);} finally {rmSync(work, { recursive: true, force: true });}
Branches
Latest commits
- 600c043dMerge branch 'v58-day58' into mainIdent
- 365f8c80|||PR ident: vendor Hybriel day58mre
- 585126e5Merge branch 'f16-setf' into mainIdent
- 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
- a1419d9cMerge branch 'f15-navigate' into mainIdent
- 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
- e36f4516Merge branch 'f14-online' into mainIdent
- 2c5eac91|||PR ident#38 online state without probe timermre
- bf1980f2Merge branch 'v56-day56' into mainIdent
- 62ae0adf|||PR ident: vendor Hybriel day56mre
- 0d18abc4Merge branch 'f13-avatar' into mainIdent
- 2a99730a|||PR ident#37 avatar in hlmre
- fa4ed25bMerge branch 'f11-upsert' into mainIdent
- 8f3d9bc9|||PR ident#42 keyed upsert and removemre
- 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
- 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
- 991641d4Merge branch 'f10-access' into mainIdent
- e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
- df7696dbident#34: tests: access ticks selector quoting fixmre
- 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre