tests/sims/tcp.test.ts
The automated tests for the /simulators/tcp-handshake simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- A normal open and close walks both ends through the states of the RFC 9293 diagram, and TIME_WAIT lasts 60 s as on Linux.
- When both ends close at once, each goes FIN_WAIT_1, CLOSING, TIME_WAIT.
- SYN and FIN each use one sequence number, data uses one per byte, and every ACK names the next byte expected.
- A lost SYN is sent again after the 1 s initial timeout of RFC 6298, so with a 100 ms round trip the connection opens at 1.1 s instead of 0.1 s.
- Connecting to a port with no listener gets a RST and the client goes straight back to CLOSED.
// /simulators/tcp-handshake: the TCP state machine (RFC 9293) and retransmission timer (RFC 6298).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { TcpSim, MSL_TIMEWAIT, type Side } from '../../src/lib/sims/tcpModel.ts';
const states = (s: TcpSim, side: Side) => s.marks.filter((m) => m.side === side).map((m) => m.state);
const at = (s: TcpSim, side: Side, state: string) => s.marks.find((m) => m.side === side && m.state === state)?.t;
test('a normal open and close walks both ends through the states of the RFC 9293 diagram, and TIME_WAIT lasts 60 s as on Linux', () => {
const s = new TcpSim();
s.connect(); s.runAll();
s.send('c', 1000); s.runAll();
s.close('c'); s.runAll();
s.close('s'); s.runAll();
assert.deepEqual(states(s, 'c'), ['CLOSED', 'SYN_SENT', 'ESTABLISHED', 'FIN_WAIT_1', 'FIN_WAIT_2', 'TIME_WAIT', 'CLOSED']);
assert.deepEqual(states(s, 's'), ['LISTEN', 'SYN_RCVD', 'ESTABLISHED', 'CLOSE_WAIT', 'LAST_ACK', 'CLOSED', 'LISTEN']);
assert.equal(MSL_TIMEWAIT, 60_000);
const closedAgain = s.marks.filter((m) => m.side === 'c').at(-1)!;
assert.equal(closedAgain.state, 'CLOSED');
assert.equal(closedAgain.t - at(s, 'c', 'TIME_WAIT')!, MSL_TIMEWAIT);
});
test('when both ends close at once, each goes FIN_WAIT_1, CLOSING, TIME_WAIT', () => {
const s = new TcpSim();
s.connect(); s.runAll();
s.close('c'); s.close('s'); s.runAll();
for (const side of ['c', 's'] as Side[]) {
const st = states(s, side);
assert.deepEqual(st.slice(st.indexOf('FIN_WAIT_1'), st.indexOf('FIN_WAIT_1') + 3), ['FIN_WAIT_1', 'CLOSING', 'TIME_WAIT'], side);
}
});
test('SYN and FIN each use one sequence number, data uses one per byte, and every ACK names the next byte expected', () => {
const s = new TcpSim();
s.connect(); s.runAll();
s.send('c', 1000); s.runAll();
const [syn, synack, ack, data, dataAck] = s.segs;
assert.ok(syn.flags.syn && !syn.flags.ack && synack.flags.syn && synack.flags.ack);
assert.equal(synack.ack, (syn.seq + 1) >>> 0);
assert.equal(ack.ack, (synack.seq + 1) >>> 0);
assert.equal(data.seq, (syn.seq + 1) >>> 0);
assert.equal(dataAck.ack, (data.seq + 1000) >>> 0);
s.close('c'); s.runAll();
const fin = s.segs.find((g) => g.flags.fin && g.from === 'c')!;
const finAck = s.segs.find((g) => g.from === 's' && g.ack === ((fin.seq + 1) >>> 0));
assert.ok(finAck, 'the FIN is acknowledged at its sequence number + 1');
});
test('a lost SYN is sent again after the 1 s initial timeout of RFC 6298, so with a 100 ms round trip the connection opens at 1.1 s instead of 0.1 s', () => {
const ok = new TcpSim();
ok.connect(); ok.runAll();
assert.equal(at(ok, 'c', 'ESTABLISHED'), 100);
const lost = new TcpSim();
lost.dropNext = true;
lost.connect(); lost.runAll();
assert.deepEqual(lost.segs.filter((g) => g.flags.syn && !g.flags.ack).map((g) => g.sent), [0, 1000]);
assert.equal(at(lost, 'c', 'ESTABLISHED'), 1100);
assert.equal(lost.retransmissions, 1);
});
test('connecting to a port with no listener gets a RST and the client goes straight back to CLOSED', () => {
const s = new TcpSim();
s.setListening(false);
s.connect(); s.runAll();
assert.deepEqual(states(s, 'c'), ['CLOSED', 'SYN_SENT', 'CLOSED']);
assert.ok(s.segs.some((g) => g.from === 's' && g.flags.rst), 'no RST was sent');
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.