import { expect, it } from "vitest"; import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; /** * `net` and `tls` validate a numeric port before creating a socket, so a literal * port outside `[0, 65535]`, a fractional or non-finite number, or a BigInt, * boolean or `null` first argument throws synchronously and no socket exists. * * Measured on Node 24.22.1: * * net.connect(0) / 80 / 65535 -> Socket created * net.connect(65536) / -1 / 1.5 -> THREW ERR_SOCKET_BAD_PORT * net.connect(NaN) / Infinity -> THREW ERR_SOCKET_BAD_PORT * net.connect(0n) / true / null -> THREW ERR_INVALID_ARG_TYPE * net.connect("") -> THREW ERR_SOCKET_BAD_PORT * net.connect("/tmp/x.sock") -> Socket created (Unix socket path) * net.connect({ port: 80 }) -> Socket created * net.connect(undefined) / {} / [] -> THREW ERR_MISSING_ARGS (undecided here) */ const socketAcquisitionsFor = (call: string) => analyzeJavaScriptSemantics( `import net from "node:net";\nimport tls from "node:tls";\nfunction g(p) { ${call}; }`, ).resourceOperations.map(({ kind, method }) => ({ kind, method })); it.each([ ["a port above the range", `net.connect(65536)`], ["a negative port", `net.connect(-1)`], ["a fractional port", `net.connect(1.5)`], ["a NaN port", `net.connect(NaN)`], ["an Infinity port", `net.connect(Infinity)`], ["an overflowing numeric port", `net.connect(1e400)`], ["a BigInt port", `net.connect(0n)`], ["a boolean port", `net.connect(true)`], ["a null port", `net.connect(null)`], ["an empty path", `net.connect("")`], ["a tls port above the range", `tls.connect(65536)`], ["a tls negative port", `tls.connect(-1)`], ["a tls NaN port", `tls.connect(NaN)`], ["a createConnection port above the range", `net.createConnection(65536)`], ["a createConnection empty path", `net.createConnection("")`], ] as const)("emits no socket acquisition for %s", (_name, call) => { expect(socketAcquisitionsFor(call)).toEqual([]); }); it.each([ ["a zero port", `net.connect(0)`, "connect"], ["a low port", `net.connect(80)`, "connect"], ["the highest port", `net.connect(65535)`, "connect"], ["a Unix socket path", `net.connect("/tmp/x.sock")`, "connect"], ["an options object", `net.connect({ port: 80 })`, "connect"], ["an omitted argument", `net.connect()`, "connect"], ["an explicit undefined port", `net.connect(undefined)`, "connect"], ["a tls port", `tls.connect(80)`, "connect"], ["a createConnection port", `net.createConnection(80)`, "createConnection"], ] as const)("keeps the socket acquisition for %s", (_name, call, method) => { expect(socketAcquisitionsFor(call)).toEqual([{ kind: "acquire", method }]); });