52 lines
3.5 KiB
TypeScript
52 lines
3.5 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { parseWireguardImport } from "./wireguard-import.js"
|
|
import { generatePeerClientConf } 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"])
|
|
})
|