97 lines
7.9 KiB
TypeScript
97 lines
7.9 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { parseWireguardImport } from "./wireguard-import.js"
|
|
import { generatePeerClientConf, parseWgConfig, WG_CONFIG_LIMITS } from "./wireguard-config.js"
|
|
|
|
const key = Buffer.alloc(32, 7).toString("base64")
|
|
const conf = `[Interface]\nPrivateKey = ${key}\nAddress = 198.18.255.241/30\nListenPort = 51879\n[Peer]\nPublicKey = ${key}\nAllowedIPs = 198.18.255.242/32\nEndpoint = 192.0.2.1:51879\n`
|
|
|
|
test("complete conf accepts the documented wg0 name and explicit routes", () => {
|
|
const parsed = parseWireguardImport(conf, "auto")
|
|
assert.equal(parsed.interface.name, "wg0")
|
|
assert.deepEqual(parsed.peers[0]?.allowedAddresses, ["198.18.255.242/32"])
|
|
assert.equal(parseWireguardImport(conf.replace("198.18.255.242/32", "0.0.0.0/0"), "conf").peers[0]?.allowedAddresses[0], "0.0.0.0/0")
|
|
})
|
|
|
|
test("generated client template cannot be imported before it is completed", () => {
|
|
const template = generatePeerClientConf({ serverPublicKey: key, endpoint: "192.0.2.1:51879" })
|
|
assert.throws(() => parseWireguardImport(template, "conf"), /PrivateKey/)
|
|
})
|
|
|
|
for (const [label, content, message] of [
|
|
["absent interface", "[Peer]\nPublicKey = " + key, /Interface/],
|
|
["duplicate interface", conf + "[Interface]\n", /Interface/],
|
|
["missing peer key", conf.replace(`PublicKey = ${key}`, "# PublicKey absent"), /PublicKey/],
|
|
["invalid private key", conf.replace(`PrivateKey = ${key}`, "PrivateKey = placeholder"), /PrivateKey/],
|
|
["invalid peer key", conf.replace(`PublicKey = ${key}`, "PublicKey = placeholder"), /PublicKey/],
|
|
["absent routes", conf.replace("AllowedIPs = 198.18.255.242/32", "# AllowedIPs absent"), /AllowedIPs/],
|
|
["invalid prefix", conf.replace("198.18.255.242/32", "198.18.255.242/33"), /AllowedIPs/],
|
|
["extra prefix separator", conf.replace("198.18.255.242/32", "198.18.255.242/32/"), /AllowedIPs/],
|
|
["invalid address", conf.replace("198.18.255.241/30", "not-an-address"), /Address/],
|
|
["unsupported multiple addresses", conf.replace("198.18.255.241/30", "198.18.255.241/30, 198.18.255.242/30"), /нескольких/],
|
|
["unsupported PSK", conf + `PresharedKey = ${key}\n`, /PresharedKey/],
|
|
["invalid listen port", conf.replace("ListenPort = 51879", "ListenPort = 65536"), /ListenPort/],
|
|
["zero listen port", conf.replace("ListenPort = 51879", "ListenPort = 0"), /ListenPort/],
|
|
["non-numeric listen port", conf.replace("ListenPort = 51879", "ListenPort = invalid"), /ListenPort/],
|
|
["invalid MTU", conf.replace("[Interface]", "[Interface]\nMTU = invalid"), /MTU/],
|
|
["zero MTU", conf.replace("[Interface]", "[Interface]\nMTU = 0"), /MTU/],
|
|
["zero endpoint port", conf.replace("192.0.2.1:51879", "192.0.2.1:0"), /Endpoint/],
|
|
["invalid endpoint port", conf.replace("192.0.2.1:51879", "192.0.2.1:invalid"), /Endpoint/],
|
|
["negative keepalive", conf + "PersistentKeepalive = -1", /PersistentKeepalive/],
|
|
] as const) {
|
|
test(label, () => assert.throws(() => parseWireguardImport(content, "conf"), message))
|
|
}
|
|
|
|
test("RSC peers without routes or interface are rejected without default routes", () => {
|
|
const peer = `/interface wireguard peers add public-key="${key}"`
|
|
assert.throws(() => parseWireguardImport(peer, "rsc"), /распознать/)
|
|
assert.throws(() => parseWireguardImport(`/interface wireguard add name=own-wg\n${peer}`, "rsc"), /allowed-address/)
|
|
assert.throws(() => parseWireguardImport("/interface wireguard add name=own-wg mtu=0", "rsc"), /MTU/)
|
|
assert.deepEqual(parseWireguardImport(`/interface wireguard add name=own-wg\n${peer} allowed-address=192.0.2.0/24`, "rsc").peers[0]?.allowedAddresses, ["192.0.2.0/24"])
|
|
})
|
|
|
|
test("unknown conf semantics, duplicate fields and sections are rejected without content disclosure", () => {
|
|
for (const source of [
|
|
conf + "[Unknown]\nNEVER_SECRET = x",
|
|
conf.replace("[Peer]", "Table = off\n[Peer]"),
|
|
conf.replace("[Peer]", "DNS = 1.1.1.1\n[Peer]"),
|
|
conf.replace("[Peer]", "PostUp = NEVER_SECRET\n[Peer]"),
|
|
conf.replace("[Peer]", "PrivateKey = NEVER_SECRET\n[Peer]"),
|
|
conf + "PublicKey = NEVER_SECRET",
|
|
conf + "Unknown = NEVER_SECRET",
|
|
conf + "[Peer]\nAllowedIPs = 192.0.2.2/32",
|
|
conf.replace("Address = 198.18.255.241/30", "Address = 198.18.255.241/30\nAddress = 192.0.2.1/24"),
|
|
]) assert.throws(() => parseWireguardImport(source, "auto"), (error: Error) => !error.message.includes("NEVER_SECRET"))
|
|
})
|
|
|
|
test("RSC binds peers to the single interface and preserves supported private-key modes and booleans", () => {
|
|
const base = `/interface wireguard add name=own-wg disabled=false\n/interface wireguard peers add interface=own-wg public-key="${key}" allowed-address=192.0.2.2/32`
|
|
for (const [native, expected] of [["yes", true], ["true", true], ["no", false], ["false", false]] as const) {
|
|
const parsed = parseWireguardImport(base + ` disabled=${native} private-key=none`, "rsc")
|
|
assert.equal(parsed.interface.disabled, false); assert.equal(parsed.peers[0].disabled, expected); assert.equal(parsed.peers[0].privateKey, "none")
|
|
}
|
|
assert.equal(parseWireguardImport(base + " private-key=auto", "rsc").peers[0].privateKey, "auto")
|
|
assert.equal(parseWireguardImport(base + ` private-key="${key}"`, "rsc").peers[0].privateKey, key)
|
|
for (const suffix of [" private-key=NEVER_SECRET", " disabled=maybe", " preshared-key=NEVER_SECRET", " allowed-address=192.0.2.3/32", " endpoint-address=bad;script", ' endpoint-address="bad;script"', " persistent-keepalive=65536"]) assert.throws(() => parseWireguardImport(base + suffix, "rsc"), (error: Error) => !error.message.includes("NEVER_SECRET"))
|
|
assert.throws(() => parseWireguardImport(base.replace("interface=own-wg", "interface=foreign"), "rsc"), /другому интерфейсу/)
|
|
assert.throws(() => parseWireguardImport(base + "\n/interface wireguard add name=second", "rsc"), /один интерфейс/)
|
|
})
|
|
|
|
test("bounded multi-peer and allowed-address import remains supported in both formats", () => {
|
|
const peers = Array.from({ length: WG_CONFIG_LIMITS.peers }, (_, index) => ({ key: Buffer.alloc(32, index + 1).toString("base64"), address: `192.0.2.${index + 1}/32` }))
|
|
const confSource = `[Interface]\nPrivateKey = ${key}\n` + peers.map(peer => `[Peer]\nPublicKey = ${peer.key}\nAllowedIPs = ${peer.address}`).join("\n")
|
|
const rscSource = "/interface wireguard add name=wg-bound\n" + peers.map(peer => `/interface wireguard peers add interface=wg-bound public-key="${peer.key}" allowed-address=${peer.address}`).join("\n")
|
|
assert.equal(parseWireguardImport(confSource, "conf").peers.length, WG_CONFIG_LIMITS.peers)
|
|
assert.equal(parseWireguardImport(rscSource, "rsc").peers.length, WG_CONFIG_LIMITS.peers)
|
|
for (const [source, format] of [[confSource + `\n[Peer]\nPublicKey = ${key}\nAllowedIPs = 192.0.2.254/32`, "conf"], [rscSource + `\n/interface wireguard peers add public-key="${key}" allowed-address=192.0.2.254/32`, "rsc"]] as const) assert.throws(() => parseWireguardImport(source, format), /предел/)
|
|
const routes = Array.from({ length: WG_CONFIG_LIMITS.addressesPerPeer }, (_, index) => `198.51.100.${index + 1}/32`).join(",")
|
|
assert.equal(parseWireguardImport(conf.replace("198.18.255.242/32", routes), "conf").peers[0].allowedAddresses.length, WG_CONFIG_LIMITS.addressesPerPeer)
|
|
assert.throws(() => parseWireguardImport(conf.replace("198.18.255.242/32", routes + ",198.51.100.254/32"), "conf"), /AllowedIPs/)
|
|
assert.throws(() => parseWireguardImport(conf + conf.slice(conf.indexOf("[Peer]")), "conf"), /повторяется PublicKey/)
|
|
assert.throws(() => parseWireguardImport(conf.replace("198.18.255.242/32", "2001:db8::1/64,2001:0db8::2/64"), "conf"), /Повторяющиеся/)
|
|
})
|
|
|
|
test("UTF-8, token, line and total bounds apply before semantic parsing", () => {
|
|
for (const source of ["я".repeat(WG_CONFIG_LIMITS.bytes / 2 + 1), "#\n".repeat(WG_CONFIG_LIMITS.lines), "#".repeat(WG_CONFIG_LIMITS.lineBytes + 1), "[Interface]\nName = " + "я".repeat(WG_CONFIG_LIMITS.tokenBytes / 2 + 1)]) assert.throws(() => parseWgConfig(source, "conf"))
|
|
})
|