Files
MikrotikManager/lib/gre-export.test.ts

35 lines
2.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from "node:assert/strict"
import type { GreTunnel } from "./data"
import { generateGreRsc } from "./gre-export"
import { quoteRosString } from "./vxlan-export"
const tunnel: GreTunnel = { id: "*1", serverId: "1", name: 'gre "edge" $name\n/system reboot', localAddress: "0.0.0.0", remoteAddress: "192.0.2.1", localInnerIp: "10.0.0.1/30", remoteInnerIp: "10.0.0.2/30", poolId: "live", ipsec: { secret: 'key"\\$value\n/system reboot', encAlg: "aes-256", authAlg: "sha256", ikeVersion: "ikev2" }, mtu: 1476, keepaliveInterval: 10, keepaliveRetries: 3, dscp: "inherit", clampTcpMss: true, allowFastPath: false, comment: 'quoted "comment"\n/system reboot', enabled: false, status: "down" }
const code = generateGreRsc(tunnel, {})
const commands = code.split("\n").filter(line => line.startsWith("/"))
assert.equal(commands.length, 2)
assert.ok(commands[0].includes("name=" + quoteRosString(tunnel.name)))
assert.ok(commands[0].includes("ipsec-secret=" + quoteRosString(tunnel.ipsec!.secret)))
assert.ok(commands[0].includes("comment=" + quoteRosString(tunnel.comment)))
assert.ok(commands[0].includes("keepalive=10s,3 dscp=inherit clamp-tcp-mss=yes allow-fast-path=no disabled=yes"))
assert.ok(commands[1].includes('address="10.0.0.1/30" interface=' + quoteRosString(tunnel.name)))
for (const unsupported of ["/ip ipsec peer add", "/ip/ipsec/peer/add", "proposal add", "policy add", "exchange-mode=", "local-address=", "\n/system reboot", "10.0.0.2/30"]) assert.ok(!code.includes(unsupported), unsupported)
const noInner = generateGreRsc({ ...tunnel, localInnerIp: "", ipsec: null, localAddress: "192.0.2.10", keepaliveInterval: 0, dscp: 12, enabled: true }, {})
assert.equal(noInner.split("\n").filter(line => line.startsWith("/")).length, 1)
assert.ok(noInner.includes('local-address="192.0.2.10"'))
assert.ok(noInner.includes("keepalive=<KEEPALIVE_SECONDS>s,<KEEPALIVE_RETRIES> dscp=12"))
assert.ok(!noInner.includes("ipsec-secret="))
console.log("gre-export: escaped strings, observed GRE/PSK only, optional IP and no duplicate IPsec passed")
const masked = generateGreRsc({ ...tunnel, ipsec: { secret: '' } }, {})
assert.ok(masked.includes('ipsec-secret="<IPSEC_PSK>"'))
assert.ok(masked.includes('# Шаблон: ключ IPsec не раскрыт.'))
assert.ok(!masked.includes('ipsec-secret=""'))
console.log('gre-export: hidden configured PSK produces an explicitly labelled template, never an empty live key')
const incompatible = generateGreRsc({ ...tunnel, allowFastPath: true }, {})
assert.ok(incompatible.includes("allow-fast-path=<ALLOW_FAST_PATH>"))
assert.ok(incompatible.includes("IPsec несовместим с FastPath"))
const ipv6 = generateGreRsc({ ...tunnel, ipsec: null, localInnerIp: "fd00:213::1/64" }, {})
assert.ok(ipv6.includes('/ipv6/address/add address="fd00:213::1/64" interface=' + quoteRosString(tunnel.name) + " advertise=no"))
assert.ok(!ipv6.includes("/ip/address/add"))
assert.ok(ipv6.includes("pool/EUI-64 и остальные адреса не включены"))