The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest" |
| 2 | import * as Duration from "effect/Duration" |
| 3 | import * as Effect from "effect/Effect" |
| 4 | import * as Hex from "effect/encoding/Hex" |
| 5 | import * as Equal from "effect/Equal" |
| 6 | import * as Dns from "effect/net/Dns" |
| 7 | import * as Host from "effect/net/Host" |
| 8 | import * as NetAddress from "effect/net/NetAddress" |
| 9 | import * as Result from "effect/Result" |
| 10 | import type * as Scope from "effect/Scope" |
| 11 | import * as NodeDnsApi from "node:dns" |
| 12 | import * as Fs from "node:fs/promises" |
| 13 | import * as Os from "node:os" |
| 14 | import * as Path from "node:path" |
| 15 | import { GenericContainer, type StartedTestContainer, Wait } from "testcontainers" |
| 16 | import { afterAll, beforeAll } from "vitest" |
| 17 | |
| 18 | const name = Host.domainNameFromStringUnsafe |
| 19 | const ip = NetAddress.ipFromStringUnsafe |
| 20 | |
| 21 | /** |
| 22 | * The records every platform should return for the fixture zone below. |
| 23 | */ |
| 24 | const expected: { readonly [K in Dns.RecordType]: ReadonlyArray<Dns.RecordFor<K>> } = { |
| 25 | A: [ |
| 26 | Dns.makeRecordUnsafe("A", { address: ip("192.0.2.1") as NetAddress.Ipv4Address }), |
| 27 | Dns.makeRecordUnsafe("A", { address: ip("192.0.2.2") as NetAddress.Ipv4Address }) |
| 28 | ], |
| 29 | AAAA: [Dns.makeRecordUnsafe("AAAA", { address: ip("2001:db8::1") as NetAddress.Ipv6Address })], |
| 30 | CAA: [Dns.makeRecordUnsafe("CAA", { critical: false, tag: "issue", value: "ca.example.test" })], |
| 31 | CNAME: [Dns.makeRecordUnsafe("CNAME", { target: name("example.test.") })], |
| 32 | MX: [Dns.makeRecordUnsafe("MX", { exchange: name("mail.example.test."), priority: 10 })], |
| 33 | NAPTR: [ |
| 34 | Dns.makeRecordUnsafe("NAPTR", { |
| 35 | order: 100, |
| 36 | preference: 10, |
| 37 | flags: "S", |
| 38 | service: "SIP+D2U", |
| 39 | regexp: "", |
| 40 | replacement: name("_sip._udp.example.test.") |
| 41 | }) |
| 42 | ], |
| 43 | NS: [Dns.makeRecordUnsafe("NS", { host: name("ns1.example.test.") })], |
| 44 | PTR: [Dns.makeRecordUnsafe("PTR", { host: name("example.test.") })], |
| 45 | SOA: [ |
| 46 | Dns.makeRecordUnsafe("SOA", { |
| 47 | primary: name("ns1.example.test."), |
| 48 | admin: "hostmaster.example.test.", |
| 49 | serial: 2024010101, |
| 50 | refresh: Duration.seconds(3600), |
| 51 | retry: Duration.seconds(600), |
| 52 | expire: Duration.seconds(604800), |
| 53 | minimum: Duration.seconds(300) |
| 54 | }) |
| 55 | ], |
| 56 | SRV: [Dns.makeRecordUnsafe("SRV", { target: name("db1.example.test."), port: 5432, priority: 10, weight: 5 })], |
| 57 | TLSA: [ |
| 58 | Dns.makeRecordUnsafe("TLSA", { |
| 59 | certUsage: 3, |
| 60 | selector: 1, |
| 61 | matchingType: 1, |
| 62 | data: Result.getOrThrow(Hex.decode("38a88126a15ae8e643ce9447c3ce9a874ea0e05255d07ee12227809edbe5c7f1")) |
| 63 | }) |
| 64 | ], |
| 65 | TXT: [Dns.makeRecordUnsafe("TXT", { chunks: ["v=spf1 ", "-all"] })] |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Asserts that two record lists hold the same records, in any order. Name |
| 70 | * servers may rotate the order of records in an answer. |
| 71 | */ |
| 72 | const assertRecords = (actual: ReadonlyArray<Dns.DnsRecord>, records: ReadonlyArray<Dns.DnsRecord>) => { |
| 73 | assert.strictEqual(actual.length, records.length, `expected ${records.map(Dns.formatRecord).join(", ")}`) |
| 74 | for (const record of records) { |
| 75 | assert.isTrue( |
| 76 | actual.some((candidate) => Equal.equals(candidate, record)), |
| 77 | `expected ${Dns.formatRecord(record)} in ${actual.map(Dns.formatRecord).join(", ")}` |
| 78 | ) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | const corefile = ` |
| 83 | example.test { |
| 84 | file /etc/coredns/example.test.db |
| 85 | } |
| 86 | 2.0.192.in-addr.arpa { |
| 87 | file /etc/coredns/2.0.192.in-addr.arpa.db |
| 88 | } |
| 89 | edge.test { |
| 90 | file /etc/coredns/edge.test.db |
| 91 | } |
| 92 | ` |
| 93 | |
| 94 | // Mirrors `expected`, plus records that cannot be represented. Names outside |
| 95 | // the zones are refused. |
| 96 | const exampleZone = ` |
| 97 | $ORIGIN example.test. |
| 98 | $TTL 300 |
| 99 | @ IN SOA ns1.example.test. hostmaster.example.test. 2024010101 3600 600 604800 300 |
| 100 | @ IN NS ns1.example.test. |
| 101 | @ IN A 192.0.2.1 |
| 102 | @ IN A 192.0.2.2 |
| 103 | @ IN AAAA 2001:db8::1 |
| 104 | @ IN MX 10 mail.example.test. |
| 105 | @ IN TXT "v=spf1 " "-all" |
| 106 | @ IN CAA 0 issue "ca.example.test" |
| 107 | @ IN NAPTR 100 10 "S" "SIP+D2U" "" _sip._udp.example.test. |
| 108 | ns1 IN A 192.0.2.53 |
| 109 | www IN CNAME example.test. |
| 110 | _pg._tcp IN SRV 10 5 5432 db1.example.test. |
| 111 | _443._tcp IN TLSA 3 1 1 38a88126a15ae8e643ce9447c3ce9a874ea0e05255d07ee12227809edbe5c7f1 |
| 112 | bad-mx IN MX 10 bad\\032host.example.test. |
| 113 | ` |
| 114 | |
| 115 | const reverseZone = ` |
| 116 | $ORIGIN 2.0.192.in-addr.arpa. |
| 117 | $TTL 300 |
| 118 | @ IN SOA ns1.example.test. hostmaster.example.test. 1 3600 600 604800 300 |
| 119 | @ IN NS ns1.example.test. |
| 120 | 1 IN PTR example.test. |
| 121 | 2 IN PTR good.example.test. |
| 122 | 2 IN PTR bad\\032host.example.test. |
| 123 | ` |
| 124 | |
| 125 | // Records that platform resolvers report in unusual forms: a root primary name, |
| 126 | // a mailbox with an escaped dot, timers of 2^31 seconds or more, UTF-8 text, |
| 127 | // and a PTR name whose label holds a dot, a space, UTF-8, and a backslash. |
| 128 | const edgeZone = ` |
| 129 | $ORIGIN edge.test. |
| 130 | $TTL 300 |
| 131 | @ IN SOA . john\\.doe.example.test. 1 4294967295 2147483648 604800 300 |
| 132 | @ IN NS ns1.example.test. |
| 133 | @ IN TXT "gr\\195\\188\\195\\159" |
| 134 | _svc._tcp IN PTR v2\\.0\\032Caf\\195\\169\\092x._svc._tcp.edge.test. |
| 135 | _dot._tcp IN PTR printer\\.. |
| 136 | ` |
| 137 | |
| 138 | /** |
| 139 | * Starts a CoreDNS container serving the fixture zones and returns the address |
| 140 | * of its UDP listener. |
| 141 | */ |
| 142 | export const startDnsServer = async (): Promise<{ |
| 143 | readonly nameServer: NetAddress.InetAddressV4 |
| 144 | readonly stop: () => Promise<void> |
| 145 | }> => { |
| 146 | // The fixtures are bind-mounted rather than copied: under Bun, the archive |
| 147 | // testcontainers builds for copied content loses its last file. |
| 148 | const directory = await Fs.mkdtemp(Path.join(Os.tmpdir(), "effect-dns-")) |
| 149 | let container: StartedTestContainer | undefined |
| 150 | const stop = async () => { |
| 151 | try { |
| 152 | await container?.stop() |
| 153 | } finally { |
| 154 | await Fs.rm(directory, { recursive: true, force: true }) |
| 155 | } |
| 156 | } |
| 157 | try { |
| 158 | await Fs.chmod(directory, 0o755) |
| 159 | for ( |
| 160 | const [file, content] of [ |
| 161 | ["Corefile", corefile], |
| 162 | ["example.test.db", exampleZone], |
| 163 | ["2.0.192.in-addr.arpa.db", reverseZone], |
| 164 | ["edge.test.db", edgeZone] |
| 165 | ] |
| 166 | ) { |
| 167 | await Fs.writeFile(Path.join(directory, file), content, { mode: 0o644 }) |
| 168 | } |
| 169 | container = await new GenericContainer("coredns/coredns:1.14.7") |
| 170 | .withBindMounts([{ source: directory, target: "/etc/coredns", mode: "ro" }]) |
| 171 | .withCommand(["-conf", "/etc/coredns/Corefile"]) |
| 172 | .withExposedPorts("53/udp") |
| 173 | .withWaitStrategy(Wait.forLogMessage(/CoreDNS-/)) |
| 174 | .start() |
| 175 | // The resolver APIs accept only IP addresses for name servers. |
| 176 | const { address } = await NodeDnsApi.promises.lookup(container.getHost(), { family: 4 }) |
| 177 | return { |
| 178 | nameServer: NetAddress.inetAddressFromStringUnsafe( |
| 179 | `${address}:${container.getMappedPort("53/udp")}` |
| 180 | ) as NetAddress.InetAddressV4, |
| 181 | stop |
| 182 | } |
| 183 | } catch (error) { |
| 184 | await stop() |
| 185 | throw error |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | const isBun = typeof process !== "undefined" && process.versions.bun !== undefined |
| 190 | const isDeno = "Deno" in globalThis |
| 191 | |
| 192 | /** |
| 193 | * Runs end-to-end tests of a platform `Dns` service against a CoreDNS |
| 194 | * container. The only skipped tests cover documented runtime bugs that the |
| 195 | * platform services cannot work around. |
| 196 | */ |
| 197 | export const describeDnsServer = ( |
| 198 | label: string, |
| 199 | make: ( |
| 200 | nameServer: NetAddress.InetAddress |
| 201 | ) => Effect.Effect<Dns.Dns["Service"], NetAddress.NetAddressError, Scope.Scope> |
| 202 | ) => |
| 203 | describe(label, () => { |
| 204 | let server: Awaited<ReturnType<typeof startDnsServer>> |
| 205 | beforeAll(async () => { |
| 206 | server = await startDnsServer() |
| 207 | }, 120_000) |
| 208 | afterAll(async () => { |
| 209 | await server?.stop() |
| 210 | }) |
| 211 | |
| 212 | const dns = () => make(server.nameServer) |
| 213 | |
| 214 | it.effect("looks up localhost from the hosts file", () => |
| 215 | Effect.gen(function*() { |
| 216 | const addresses = yield* (yield* dns()).lookup(name("localhost"), { family: "IPv4" }) |
| 217 | assert.isTrue(addresses.some((address) => NetAddress.formatIp(address) === "127.0.0.1")) |
| 218 | })) |
| 219 | |
| 220 | it.effect("queries every record type", () => |
| 221 | Effect.gen(function*() { |
| 222 | const resolver = yield* dns() |
| 223 | for (const type of ["A", "AAAA", "CAA", "MX", "NAPTR", "NS", "SOA"] as const) { |
| 224 | assertRecords(yield* resolver.resolve(name("example.test."), type), expected[type]) |
| 225 | } |
| 226 | assertRecords(yield* resolver.resolve(name("www.example.test"), "CNAME"), expected.CNAME) |
| 227 | assertRecords(yield* resolver.resolve(name("_pg._tcp.example.test"), "SRV"), expected.SRV) |
| 228 | })) |
| 229 | |
| 230 | // Bun's resolver and `Deno.resolveDns` cannot query TLSA records. |
| 231 | it.effect("queries TLSA records where the runtime supports them", () => |
| 232 | Effect.gen(function*() { |
| 233 | const query = (yield* dns()).resolve(name("_443._tcp.example.test"), "TLSA") |
| 234 | if (isBun || isDeno) { |
| 235 | const error = yield* Effect.flip(query) |
| 236 | assert.strictEqual(error.reason, "Unsupported") |
| 237 | assert.strictEqual(error.recordType, "TLSA") |
| 238 | } else { |
| 239 | assertRecords(yield* query, expected.TLSA) |
| 240 | } |
| 241 | })) |
| 242 | |
| 243 | // Bun returns each character string of a TXT record as a separate record, |
| 244 | // so the chunks of a record cannot be reassembled: |
| 245 | // https://github.com/oven-sh/bun/issues/44692 |
| 246 | it.effect.skipIf(isBun)("keeps the chunks of a TXT record together", () => |
| 247 | Effect.gen(function*() { |
| 248 | assertRecords(yield* (yield* dns()).resolve(name("example.test."), "TXT"), expected.TXT) |
| 249 | })) |
| 250 | |
| 251 | it.effect("converts unusual record data", () => |
| 252 | Effect.gen(function*() { |
| 253 | const resolver = yield* dns() |
| 254 | assertRecords(yield* resolver.resolve(name("edge.test"), "SOA"), [ |
| 255 | Dns.makeRecordUnsafe("SOA", { |
| 256 | primary: name("."), |
| 257 | admin: "john\\.doe.example.test.", |
| 258 | serial: 1, |
| 259 | refresh: Duration.seconds(4294967295), |
| 260 | retry: Duration.seconds(2147483648), |
| 261 | expire: Duration.seconds(604800), |
| 262 | minimum: Duration.seconds(300) |
| 263 | }) |
| 264 | ]) |
| 265 | assertRecords(yield* resolver.resolve(name("edge.test"), "TXT"), [ |
| 266 | Dns.makeRecordUnsafe("TXT", { chunks: ["grüß"] }) |
| 267 | ]) |
| 268 | assertRecords(yield* resolver.resolve(name("_svc._tcp.edge.test"), "PTR"), [ |
| 269 | Dns.makeRecordUnsafe("PTR", { host: "v2\\.0 Café\\\\x._svc._tcp.edge.test." }) |
| 270 | ]) |
| 271 | })) |
| 272 | |
| 273 | it.effect("keeps the root after a label ending with an escaped dot", () => |
| 274 | Effect.gen(function*() { |
| 275 | const records = yield* (yield* dns()).resolve(name("_dot._tcp.edge.test"), "PTR") |
| 276 | assert.deepStrictEqual(records.map(Dns.formatRecord), ["PTR printer\\.."]) |
| 277 | })) |
| 278 | |
| 279 | it.effect("looks up the host names of an address, skipping invalid names", () => |
| 280 | Effect.gen(function*() { |
| 281 | const resolver = yield* dns() |
| 282 | assert.deepStrictEqual<ReadonlyArray<string>>(yield* resolver.reverse(ip("192.0.2.1")), ["example.test."]) |
| 283 | assert.deepStrictEqual<ReadonlyArray<string>>(yield* resolver.reverse(ip("192.0.2.2")), [ |
| 284 | "good.example.test." |
| 285 | ]) |
| 286 | })) |
| 287 | |
| 288 | it.effect("fails when no record can be represented", () => |
| 289 | Effect.gen(function*() { |
| 290 | const error = yield* Effect.flip((yield* dns()).resolve(name("bad-mx.example.test"), "MX")) |
| 291 | assert.strictEqual(error.reason, "InvalidResponse") |
| 292 | })) |
| 293 | |
| 294 | it.effect("reports missing names and records", () => |
| 295 | Effect.gen(function*() { |
| 296 | const resolver = yield* dns() |
| 297 | const missingName = yield* Effect.flip(resolver.resolve(name("missing.example.test"), "A")) |
| 298 | assert.strictEqual(missingName.reason, "NotFound") |
| 299 | const missingRecord = yield* Effect.flip(resolver.resolve(name("ns1.example.test."), "SRV")) |
| 300 | assert.strictEqual(missingRecord.reason, "NotFound") |
| 301 | const missingAddress = yield* Effect.flip(resolver.reverse(ip("192.0.2.9"))) |
| 302 | assert.strictEqual(missingAddress.reason, "NotFound") |
| 303 | })) |
| 304 | |
| 305 | // `Deno.resolveDns` reports every error response, including refused |
| 306 | // queries, with the same `NotFound` error as a missing name. |
| 307 | it.effect.skipIf(isDeno)("reports refused queries", () => |
| 308 | Effect.gen(function*() { |
| 309 | const error = yield* Effect.flip((yield* dns()).resolve(name("outside.invalid"), "A")) |
| 310 | assert.strictEqual(error.reason, "Refused") |
| 311 | })) |
| 312 | }) |