About: how pages are checked

tests/sims/vpc.test.ts

The automated tests for the /simulators/vpc-packet-flow 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.

  1. CIDR matching agrees with a bit-by-bit comparison.
  2. Route lookup picks the longest matching prefix.
  3. The example networks give the results AWS documents.
// /simulators/vpc-packet-flow: AWS VPC route tables, network ACLs and security groups.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parseConfig, trace, routeLookup, inCidr, ipNum } from '../../src/lib/sims/vpc.ts';
import { CONFIGS, FLOWS } from '../../src/lib/sims/vpcPresets.ts';
import { checker, lcgInt } from './check.ts';

const ipStr = (n: number) => [24, 16, 8, 0].map((s) => (n >>> s) & 255).join('.');

test('CIDR matching agrees with a bit-by-bit comparison', () => {
	const { ok, done } = checker();
	const r = lcgInt(6);
	for (let i = 0; i < 20000; i++) {
		const ip = r() >>> 0, net = r() >>> 0, len = r() % 33;
		const mask = len === 0 ? 0 : (0xffffffff << (32 - len)) >>> 0;
		const netStr = `${ipStr((net & mask) >>> 0)}/${len}`;
		const want = ((ip & mask) >>> 0) === ((net & mask) >>> 0);
		ok(inCidr(ipStr(ip), netStr) === want, `${ipStr(ip)} in ${netStr}`);
	}
	ok(ipNum('10.0.2.10') === 0x0a00020a, 'ipNum');
	done();
});

test('route lookup picks the longest matching prefix', () => {
	const { ok, done } = checker();
	const r = lcgInt(8);
	for (let it = 0; it < 500; it++) {
		const routes: string[] = ['10.0.0.0/16 local'];
		for (let k = 0; k < 6; k++) { const len = r() % 33; const net = len === 0 ? 0 : ((r() & ((0xffffffff << (32 - len)) >>> 0)) >>> 0); routes.push(`${ipStr(net)}/${len} igw`); }
		const cfg = parseConfig(`[route public]\n${routes.join('\n')}\n`);
		for (let q = 0; q < 20; q++) {
			const ip = ipStr(r() >>> 0);
			const got = routeLookup(cfg, 'public', ip);
			const lens = routes.map((x) => x.split(' ')[0]).filter((c) => inCidr(ip, c)).map((c) => +c.split('/')[1]);
			const best = lens.length ? Math.max(...lens) : null;
			ok((got ? +got.dest.split('/')[1] : null) === best, `${ip}: got ${got?.dest} with routes ${routes.join(', ')}`);
		}
	}
	done();
});

test('the example networks give the results AWS documents', () => {
	// Expected outcomes, from the VPC User Guide: security groups are stateful and default-deny; network ACLs are
	// stateless, so return traffic needs ephemeral ports allowed; an internet gateway only translates instances
	// with a public IPv4 address; the most specific route wins, including a blackhole.
	const expected: Record<string, Record<string, string | true>> = {
		base: { 'Web to database': 'security group db', 'Database to the internet': 'data subnet network ACL', 'Internet to app': 'app server: public IPv4 address' },
		ephemeral: { 'Web to database': 'security group db', 'App to the internet': 'private subnet network ACL', 'App to S3': 'private subnet network ACL', 'Database to the internet': 'data subnet network ACL', 'Internet to app': 'app server: public IPv4 address', 'App to peered VPC': 'private subnet network ACL' },
		igw: { 'Web to database': 'security group db', 'App to the internet': 'internet gateway: public IPv4 address', 'Database to the internet': 'data subnet network ACL', 'Internet to app': 'app server: public IPv4 address' },
		blackhole: { 'Web to database': 'security group db', 'App to the internet': 'private subnet route table', 'Database to the internet': 'data subnet network ACL', 'Internet to app': 'app server: public IPv4 address' }
	};
	for (const c of CONFIGS) {
		const cfg = parseConfig(c.text);
		assert.deepEqual(cfg.errors, [], c.id);
		for (const f of FLOWS) {
			const t = trace(cfg, f);
			const want = expected[c.id][f.label];
			if (want === undefined) assert.ok(t.ok, `${c.id}: ${f.label} should get through, blocked at ${t.blockedAt}`);
			else { assert.equal(t.ok, false, `${c.id}: ${f.label} should be blocked`); assert.ok(t.blockedAt!.startsWith(want as string), `${c.id}: ${f.label} blocked at ${t.blockedAt}, expected ${want}`); }
		}
	}
});

Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.

Published