About: how pages are checked

tests/sims/dns.test.ts

The automated tests for the /simulators/dns-resolution 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. Over 200 random runs of lookups and waits, every answer matches reading the zone data directly, whether it came from servers or a cache.
  2. A cold lookup asks the root, then .com, then example.com; a repeat is answered from cache with the TTL counted down.
  3. A name whose nameservers are outside its zone needs a separate lookup for their address, because the parent can't give glue.
  4. NXDOMAIN and NODATA are cached for the smaller of the SOA TTL and SOA minimum (RFC 2308): 300 s in example.com, a day at the root.
  5. When every nameserver is silent, SERVFAIL is cached for 5 s, doubling on each repeat up to 5 minutes, as in RFC 9520.
// /simulators/dns-resolution: an iterative resolver with caching, CNAMEs, negative answers and failures.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { Sim, buildWorld, isSub, CLIENTS, FAIL_FIRST, FAIL_MAX, type RType } from '../../src/lib/sims/dns.ts';
import { checker, lcg } from './check.ts';

// The right answer, read straight from the zone data: find the deepest zone holding the name, follow
// CNAMEs, and tell NXDOMAIN (no such name) from NODATA (name exists, not that type). No caching, no servers.
function truth(qname: string, qtype: RType) {
	const { zones } = buildWorld();
	const chain: string[] = [];
	let name = qname;
	for (let hop = 0; hop < 8; hop++) {
		const z = zones.filter((z) => isSub(name, z.apex)).sort((a, b) => b.apex.length - a.apex.length)[0];
		const here = z.records.filter((r) => r.name === name);
		const cname = here.find((r) => r.type === 'CNAME');
		if (cname && qtype !== 'CNAME') { chain.push(`${name} CNAME ${cname.data}`); name = cname.data; continue; }
		const hits = here.filter((r) => r.type === qtype);
		if (hits.length) return { rcode: 'NOERROR', chain: [...chain, ...hits.map((r) => `${r.name} ${r.type} ${r.data}`)].sort() };
		const exists = here.length > 0 || z.records.some((r) => r.name.endsWith('.' + name));
		return { rcode: exists ? 'NODATA' : 'NXDOMAIN', chain: chain.sort() };
	}
	throw new Error('CNAME loop');
}
const NAMES = ['www.example.com', 'example.com', 'mail.example.com', 'api.example.com', 'nosuch.example.com', 'www.example.net', 'api.example.net', 'ns1.example.com', 'fileserver.internal', 'com', 'a.gtld-servers.net'];
const TYPES: RType[] = ['A', 'AAAA', 'NS', 'CNAME'];

test('over 200 random runs of lookups and waits, every answer matches reading the zone data directly, whether it came from servers or a cache', () => {
	const { ok, done } = checker();
	const r = lcg(22);
	for (let run = 0; run < 200; run++) {
		const s = new Sim();
		for (let k = 0; k < 40; k++) {
			if (r() < 0.25) { s.wait(Math.floor(r() * 5000)); continue; }
			const name = NAMES[Math.floor(r() * NAMES.length)], type = TYPES[Math.floor(r() * TYPES.length)], client = CLIENTS[Math.floor(r() * CLIENTS.length)];
			const a = s.lookup(client, name, type), want = truth(name, type);
			const got = a.chain.map((x) => `${x.name} ${x.type} ${x.data}`).sort();
			ok(a.rcode === want.rcode && JSON.stringify(got) === JSON.stringify(want.chain), `run ${run}: ${name} ${type} from ${a.from}: ${a.rcode} ${got}, expected ${want.rcode} ${want.chain}`);
			ok(a.chain.every((x) => x.ttl >= 0), `run ${run}: negative TTL on ${name}`);
		}
	}
	done();
});

test('a cold lookup asks the root, then .com, then example.com; a repeat is answered from cache with the TTL counted down', () => {
	const s = new Sim();
	const cold = s.lookup('Laptop', 'www.example.com', 'A');
	assert.equal(cold.upstream, 3);
	assert.deepEqual(cold.exchanges.filter((e) => e.from === 'resolver').map((e) => e.to), ['a.root-servers.net', 'a.gtld-servers.net', 'ns1.example.com']);
	const again = s.lookup('Laptop', 'www.example.com', 'A');
	assert.equal(again.from, 'client-cache');
	assert.equal(again.upstream, 0);
	s.wait(600);
	const other = s.lookup('Phone', 'www.example.com', 'A');
	assert.equal(other.from, 'resolver-cache');
	assert.equal(other.chain[0].ttl, 3600 - 600);
});

test("a name whose nameservers are outside its zone needs a separate lookup for their address, because the parent can't give glue", () => {
	const net = new Sim().lookup('Laptop', 'www.example.net', 'A');
	assert.ok(net.exchanges.some((e) => e.q.startsWith('ns1.example.com') || e.q.startsWith('ns2.example.com')), 'no lookup for the nameserver address');
	assert.ok(net.upstream > new Sim().lookup('Laptop', 'www.example.com', 'A').upstream);
});

test('NXDOMAIN and NODATA are cached for the smaller of the SOA TTL and SOA minimum (RFC 2308): 300 s in example.com, a day at the root', () => {
	const s = new Sim();
	assert.equal(s.lookup('Laptop', 'nosuch.example.com', 'A').negTtl, 300);
	assert.equal(s.lookup('Laptop', 'mail.example.com', 'AAAA').negTtl, 300);
	assert.equal(s.lookup('Laptop', 'fileserver.internal', 'A').negTtl, 86400);
});

test('when every nameserver is silent, SERVFAIL is cached for 5 s, doubling on each repeat up to 5 minutes, as in RFC 9520', () => {
	const s = new Sim();
	s.down.add('ns1.example.com'); s.down.add('ns2.example.com');
	const fails: number[] = [];
	for (let k = 0; k < 10; k++) {
		const a = s.lookup('Laptop', `www.example.com`, 'A');
		assert.equal(a.rcode, 'SERVFAIL');
		if (a.failFor) fails.push(a.failFor);
		s.wait(FAIL_MAX + 1);
	}
	assert.deepEqual(fails, [5, 10, 20, 40, 80, 160, 300, 300, 300, 300]);
	assert.equal(fails[0], FAIL_FIRST);
	const b = new Sim();
	b.down.add('ns1.example.com'); b.down.add('ns2.example.com');
	b.lookup('Laptop', 'www.example.com', 'A');
	assert.equal(b.lookup('Phone', 'www.example.com', 'A').failCached, true, 'a second client inside the 5 s gets the cached failure');
	b.down.clear();
	b.wait(6);
	assert.equal(b.lookup('Phone', 'www.example.com', 'A').rcode, 'NOERROR');
	assert.equal(b.failStreak.size, 0, 'a success resets the backoff');
});

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

Published