Files
mdg-filius/packages/fls/test/dnsHosts.test.ts
T
julian.kiedaischandClaude Opus 5.5 8f9930f1ff fix(fls): follow Java's formats, not Java's errors
Java stays the reference for the .fls format, but its errors are no longer
copied. The Java oracle still measures Java as it is; each deliberate
departure is named in the tests with its reason (JAVA_BUGS) and must really
depart. The corpus - what Java itself writes - still reads exactly as in
Java.

- /dns/hosts: a label may start with a digit, holds no backslash, and the
  last one is not all digits (RFC 1123, RFC 3696); A, MX and NS are read in
  any case (RFC 4343); a negative TTL is invalid, so 3600 (RFC 2181). Also
  for the e-mail server's domain.
- /mailserver/konten.txt: empty fields count (Java's StringTokenizer skips
  them and shifts the rest, so an empty password breaks the account); a
  short line alone is left out, with a warning (Java throws for the whole
  file); a message keeps an empty text or subject (Java's split loses it);
  a ";" in an unescaped subject stays in the message; CRLF lines.
- /konten.txt: the last complete line is the account, also with an empty
  address (Java takes the last line and loses the account); CRLF lines.
- Static DHCP assignments: a broken entry alone is left out (Java loses
  every entry after it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 07:20:52 +00:00

206 lines
8.5 KiB
TypeScript

// Java's reading of /dns/hosts (DNSServer.leseRecordListe): every case in
// dns-hosts.cases.txt was run through Java (dns-hosts.java.json) and through
// Swift's hosts-file codec (dns-hosts.swift.json, tools/swift-oracle). The
// reader must match Java except where Java errs (JAVA_BUGS); the records the
// model keeps must match Swift except where Java's format says otherwise —
// each case named below.
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
import { dnsRecordsOf, readDnsHosts } from '../src/dnsHosts.js';
const here = dirname(fileURLToPath(import.meta.url));
type Row = [string, string, number, string];
const javaMeasured = JSON.parse(readFileSync(join(here, 'dns-hosts.java.json'), 'utf8')) as [string, Row[]][];
const swiftMeasured = JSON.parse(readFileSync(join(here, 'dns-hosts.swift.json'), 'utf8')) as [string, Row[]][];
/** The cases file's escapes: a backslash, `u` and four hex digits for one UTF-16 unit. */
function unescape(line: string): string {
return line.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex: string) => String.fromCharCode(Number.parseInt(hex, 16)));
}
function modelRows(line: string, warnings: string[] = []): Row[] {
return dnsRecordsOf(readDnsHosts(unescape(line)), (message) => warnings.push(message)).map((record) => [
record.name,
record.type,
record.ttlSeconds,
record.target,
]);
}
/** Lines Swift reads as records that Java never answers. */
const SWIFT_READS_JAVA_NEVER_ANSWERS: { reason: string; lines: string[] }[] = [
{
reason: 'Java splits fields at single spaces only: a tab or NBSP is part of a field',
lines: [
String.raw`www.filius.de\u0009A\u00093600\u00091.2.3.4\u000A`,
String.raw`www.filius.de\u00A0A 3600 1.2.3.4\u000A`,
String.raw`www.filius.de A 3600 1.2.3.4\u00A0\u000A`,
String.raw`www.filius.de \u0009 A 3600 1.2.3.4\u000A`,
String.raw`www.filius.de A \u0009 3600 1.2.3.4\u000A`,
],
},
{
reason: 'Java splits lines at LF only: CR, LS and NEL stay in the line',
lines: [
String.raw`www.filius.de A 3600 1.2.3.4\u000Dmail.filius.de A 3600 1.2.3.5\u000D`,
String.raw`www.filius.de A 3600 1.2.3.4\u2028mail.filius.de A 3600 1.2.3.5\u000A`,
String.raw`www.filius.de A 3600 1.2.3.4\u0085\u000A`,
],
},
{
reason: 'Java has no comments, no "IN" and no zone-file order',
lines: [
String.raw`www.filius.de A 3600 1.2.3.4#x\u000A`,
String.raw`www.filius.de A IN 3600 1.2.3.4\u000A`,
String.raw`www.filius.de 3600 IN A 1.2.3.4\u000A`,
],
},
{
reason: 'two final dots make the name empty in Java',
lines: [String.raw`www.filius.de.. A 3600 1.2.3.4\u000A`],
},
{
reason: 'an MX or NS target must be a name Java accepts: no preference, no address, one final dot',
lines: [
String.raw`filius.de MX 3600 10 mail.filius.de\u000A`,
String.raw`filius.de MX 3600 1.2.3.4\u000A`,
String.raw`filius.de NS 3600 dns.filius.de..\u000A`,
],
},
];
/** Lines Java answers that Swift drops or reads otherwise. */
const JAVA_ANSWERS_SWIFT_DROPS: { reason: string; lines: string[] }[] = [
{
reason: 'a TTL that is no number, or a negative one, is 3600',
lines: [
String.raw`www.filius.de A x 1.2.3.4\u000A`,
String.raw`www.filius.de A 4294967295 1.2.3.4\u000A`,
String.raw`www.filius.de A 99999999999 1.2.3.4\u000A`,
String.raw`www.filius.de A 2147483648 1.2.3.4\u000A`,
String.raw`www.filius.de A -5 1.2.3.4\u000A`,
String.raw`www.filius.de A -2147483648 1.2.3.4\u000A`,
],
},
{
reason: "a TTL is read by Java's Integer.parseInt, in any script's digits",
lines: [String.raw`www.filius.de A \u0661\u0660 1.2.3.4\u000A`],
},
{
reason: 'Java trims control characters off a line (String.trim)',
lines: [
String.raw`www.filius.de A 3600 1.2.3.4\u0001\u000A`,
String.raw`\u001Fwww.filius.de A 3600 1.2.3.4\u000A`,
],
},
{
reason: 'an NS record may name the root',
lines: [
String.raw`de. NS 3600 .\u000A`,
],
},
{
reason: 'duplicates stay, as in Java\'s list',
lines: [
String.raw`www.filius.de A 3600 1.2.3.4\u000Awww.filius.de A 3600 1.2.3.4\u000A`,
String.raw`www.filius.de A 3600 1.2.3.4\u000Awww.filius.de A 7200 1.2.3.4\u000A`,
String.raw`www.filius.de A 3600 1.2.3.4\u000AWWW.filius.de. A 3600 1.2.3.4\u000A`,
],
},
];
/** Lines where Java errs and this reader does not (see dnsHosts.ts). */
const JAVA_BUGS: { reason: string; lines: string[] }[] = [
{
reason: 'a label may start with a digit (RFC 1123); Java empties the name',
lines: [String.raw`1und1.de A 3600 1.2.3.4\u000A`, String.raw`www.1und1.de A 3600 1.2.3.4\u000A`],
},
{
reason: "a label holds no backslash; Java's pattern lets one in after the first label",
lines: [String.raw`www.a\u005Cb.de A 3600 1.2.3.4\u000A`],
},
{
reason: 'the type is read in any case (RFC 4343); Java never answers "a" or "mx"',
lines: [String.raw`www.filius.de a 3600 1.2.3.4\u000A`, String.raw`filius.de mx 3600 mail.filius.de\u000A`],
},
{
reason: 'a negative TTL is invalid (RFC 2181) and so 3600; Java keeps it',
lines: [String.raw`www.filius.de A -5 1.2.3.4\u000A`, String.raw`www.filius.de A -2147483648 1.2.3.4\u000A`],
},
];
const javaBugs = new Map(JAVA_BUGS.flatMap(({ reason, lines }) => lines.map((line) => [line, reason] as const)));
const departures = new Map(
[...SWIFT_READS_JAVA_NEVER_ANSWERS, ...JAVA_ANSWERS_SWIFT_DROPS].flatMap(({ reason, lines }) =>
lines.map((line) => [line, reason] as const),
),
);
describe('readDnsHosts against Java, but for its errors', () => {
it('covers every line of the cases file, in both oracles', () => {
const lines = readFileSync(join(here, 'dns-hosts.cases.txt'), 'ascii').split('\n').slice(0, -1);
expect(javaMeasured.map(([line]) => line)).toEqual(lines);
expect(swiftMeasured.map(([line]) => line)).toEqual(lines);
});
it('names only cases of the cases file as Java bugs', () => {
const lines = new Set(javaMeasured.map(([line]) => line));
expect([...javaBugs.keys()].filter((line) => !lines.has(line))).toEqual([]);
});
for (const [line, expected] of javaMeasured) {
const bug = javaBugs.get(line);
it(bug === undefined ? JSON.stringify(line) : `${JSON.stringify(line)} (Java errs: ${bug})`, () => {
const records = readDnsHosts(unescape(line)).map((record) => [record.domainName, record.type, record.ttl, record.rdata]);
if (bug === undefined) expect(records).toEqual(expected);
else expect(records).not.toEqual(expected);
});
}
});
describe('the records the model keeps, against Swift', () => {
it('names only cases of the cases file as departures', () => {
const lines = new Set(swiftMeasured.map(([line]) => line));
expect([...departures.keys()].filter((line) => !lines.has(line))).toEqual([]);
});
for (const [line, swift] of swiftMeasured) {
const reason = departures.get(line);
it(reason === undefined ? JSON.stringify(line) : `${JSON.stringify(line)} (departs: ${reason})`, () => {
if (reason === undefined) expect(modelRows(line)).toEqual(swift);
else expect(modelRows(line)).not.toEqual(swift);
});
}
});
describe('the records Java answers', () => {
it('leave out records Java never answers: an empty name, or a type other than A, MX, NS', () => {
expect(modelRows(String.raw`a_b.de A 3600 1.2.3.4\u000Afilius.de CNAME 3600 www.filius.de\u000A1.2.3.4 A 3600 1.2.3.4`)).toEqual([]);
});
it('read a name with a digit first and a type in any case, but no numeric last label', () => {
expect(modelRows(String.raw`1und1.de a 60 1.2.3.4\u000A1und1.de Mx 60 mx.1und1.de\u000A1und1.de MX 60 10`)).toEqual([
['1und1.de', 'A', 60, '1.2.3.4'],
['1und1.de', 'MX', 60, 'mx.1und1.de.'],
]);
});
it('take names in lower case without the final dot, A targets as FILIUS normalises addresses', () => {
expect(modelRows(String.raw`WWW.Filius.DE. A 60 010.000.0.1\u000Afilius.de MX 3600 Mail.Filius.DE\u000A. NS 3600 dns-root`)).toEqual([
['www.filius.de', 'A', 60, '10.0.0.1'],
['filius.de', 'MX', 3600, 'mail.filius.de.'],
['.', 'NS', 3600, 'dns-root.'],
]);
});
it('warn about an A record Java answers with an address FILIUS cannot use', () => {
const warnings: string[] = [];
expect(modelRows(String.raw`www.filius.de A 3600 .1.2.3`, warnings)).toEqual([]);
expect(warnings).toEqual(["DNS record 'www.filius.de A .1.2.3' has no usable IPv4 address, left out (it stays in /dns/hosts)"]);
});
});