import assert from "node:assert/strict" import { test } from "node:test" import type { IxRouterObject } from "@mmapp/contracts/internet-exchange" import { planIxNative } from "./internet-exchange-execution.js" import { ixNamespace } from "./internet-exchange-plan.js" const id = "aaaaaaaa-1111-4222-8333-123456789abc" const name = ixNamespace(id) const vrf: IxRouterObject = { serverId: 17, path: "/ip/vrf", fields: { name, comment: name + ":vrf", interfaces: "wg-ix" }, } const route: IxRouterObject = { serverId: 17, path: "/ip/route", fields: { comment: name + ":exit-4-0", "routing-table": name, "dst-address": "0.0.0.0/0", gateway: "192.0.2.1@" + name }, } const device = (rows: Record>>) => [{ serverId: 17, source: "", rows }] test("managed MSS rules are backfilled and removed without adopting a hand-written rule", () => { const path = "/ip/firewall/mangle" const from = { serverId: 17, path, fields: { comment: name + ":linux-mss-from-il-wg", action: "change-mss", chain: "prerouting", "in-interface": "il-wg", "new-mss": "1380", "tcp-mss": "1381-65535" } } const to = { serverId: 17, path, fields: { comment: name + ":linux-mss-to-il-wg", action: "change-mss", chain: "postrouting", "out-interface": "il-wg", "new-mss": "1380", "tcp-mss": "1381-65535" } } const manual = { ...to.fields, comment: "manual MSS", ".id": "*A" } const fromRow = { ...from.fields, ".id": "*B" } const toRow = { ...to.fields, ".id": "*C" } const desired = [from, to] const live = (rows: Array>) => device({ [path]: rows }) assert.deepEqual(planIxNative(id, desired, desired, live([manual])).map(task => [task.fields, task.placeBefore]), [[from.fields, "*A"], [to.fields, "*A"]]) assert.deepEqual(planIxNative(id, desired, desired, live([fromRow, manual])).map(task => task.fields), [to.fields]) assert.deepEqual(planIxNative(id, desired, desired, live([fromRow, toRow, manual])), []) assert.deepEqual(planIxNative(id, [], desired, live([fromRow, toRow, manual])).map(task => task.rosId), ["*C", "*B"]) assert.throws(() => planIxNative(id, desired, [], live([fromRow, manual])), /незавершённой операции/) assert.throws(() => planIxNative(id, desired, desired, live([{ ...fromRow, "new-mss": "1460" }, toRow])), /изменён вне операции/) }) test("a new VRF is inserted before main and no foreign route is altered", () => { const changes = planIxNative(id, [vrf, route], [], device({ "/ip/vrf": [{ ".id": "*A", name: "main", interfaces: "all", builtin: "true" }], "/ip/route": [{ ".id": "*B", "routing-table": "main", "dst-address": "0.0.0.0/0", gateway: "203.0.113.1" }], })) assert.deepEqual(changes.map(task => ({ path: task.path, position: task.placeBefore, id: task.rosId })), [{ path: "/ip/vrf", position: "*A", id: undefined }, { path: "/ip/route", position: undefined, id: undefined }]) }) test("managed changes update one exact ID and never adopt unknown objects after an interrupted job", () => { const state = device({ "/ip/route": [{ ".id": "*B", ...route.fields }] }) const next = { ...route, fields: { ...route.fields, gateway: "192.0.2.2@" + name } } const [task] = planIxNative(id, [next], [route], state) assert.equal(task.rosId, "*B") assert.deepEqual(task.fields, { gateway: "192.0.2.2@" + name }) assert.throws(() => planIxNative(id, [next], [], state), /незавершённой операции/) assert.throws(() => planIxNative(id, [next], [route], device({ "/ip/route": [{ ".id": "*B", ...next.fields }] })), /изменён вне операции/) const resumed = planIxNative(id, [next, vrf], [route], device({ "/ip/route": [{ ".id": "*B", ...next.fields }], "/ip/vrf": [{ ".id": "*A", name: "main", interfaces: "all", builtin: "true" }], }), true) assert.deepEqual(resumed.map(task => task.path), ["/ip/vrf"]) }) test("revocation disables the owned client tunnel before withdrawing rules and deleting its native object", () => { const tunnel = { serverId: 17, path: "/interface/gre", fields: { name: "il-client", comment: name + ":gre-il-client", disabled: "false", } } const changes = planIxNative(id, [], [tunnel, route], device({ [tunnel.path]: [{ ".id": "*A", ...tunnel.fields }], [route.path]: [{ ".id": "*B", ...route.fields }], })) assert.deepEqual(changes.map(task => [task.rosId, task.fields, task.before?.disabled]), [ ["*A", { disabled: "true" }, "false"], ["*B", undefined, undefined], ["*A", undefined, "true"], ]) }) test("a foreign route in the IX table and implicit native field removal are refused", () => { assert.throws(() => planIxNative(id, [route], [], device({ "/ip/route": [{ ".id": "*B", "routing-table": name, "dst-address": "192.0.2.0/24" }], })), /пересекается с существующей/) const { gateway: _gateway, ...fields } = route.fields assert.throws(() => planIxNative(id, [{ ...route, fields }], [route], device({ "/ip/route": [{ ".id": "*B", ...route.fields }] })), /неявно сбросить/) }) test("IX does not take a main-table service address from another route", () => { const resolver = { ...route, fields: { ...route.fields, comment: name + ":bgp-resolve-test", "routing-table": "main", "dst-address": "198.18.0.101/32" } } const occupied = { ".id": "*B", "routing-table": "main", "dst-address": "198.18.0.101/32", dynamic: "true" } assert.throws(() => planIxNative(id, [resolver], [], device({ "/ip/route": [occupied] })), /уже занят/) }) test("RouterOS REST host matches allow only the equivalent explicit host length", () => { for (const [address, hostLength, wider] of [["192.0.2.1", "32", "24"], ["2001:db8::1", "128", "64"]]) { const rule = { ...route, path: "/ip/firewall/filter", fields: { comment: name + ":host", "src-address": address + "/" + hostLength } } const rows = (source: string) => device({ [rule.path]: [{ ".id": "*B", ...rule.fields, "src-address": source }] }) assert.deepEqual(planIxNative(id, [rule], [rule], rows(address)), []) assert.throws(() => planIxNative(id, [rule], [rule], rows(address + "/" + wider)), /изменён вне операции/) } }) test("RouterOS REST encodes an enabled blackhole route as a present empty flag", () => { const fields = { comment: name + ":default-discard-4", "routing-table": name, "dst-address": "0.0.0.0/0", blackhole: "yes" } const discard = { ...route, fields } const { blackhole: _blackhole, ...unflagged } = fields const observed = (fields: Record) => device({ "/ip/route": [{ ".id": "*B", ...fields }] }) assert.deepEqual(planIxNative(id, [discard], [discard], observed({ ...fields, blackhole: "" })), []) assert.throws(() => planIxNative(id, [discard], [discard], observed(unflagged)), /изменён вне операции/) assert.throws(() => planIxNative(id, [discard], [discard], observed({ ...fields, blackhole: "false" })), /изменён вне операции/) }) test("the complete member filter can be added to a legacy applied segment", () => { const path = "/ip/firewall/filter" const object = (suffix: string, fields: Record) => ({ serverId: 17, path, fields: { comment: name + ":" + suffix, chain: "forward", ...fields }, }) const boundary = object("deny-outside-4", { action: "drop", "in-interface": name }) const allow = object("egress-allow-4", { action: "accept", "in-interface": name, "out-interface-list": name }) const deny = object("egress-deny-4", { action: "drop", "out-interface-list": name }) const objects = [allow, boundary, deny] const legacy = { ".id": "*B", ...boundary.fields } assert.deepEqual(planIxNative(id, objects, objects, device({ [path]: [legacy] })).map(task => [task.fields, task.placeBefore]), [[allow.fields, "*B"], [deny.fields, "*B"]]) assert.throws(() => planIxNative(id, objects, objects, device({ [path]: [{ ".id": "*C", ...allow.fields }, legacy] })), /предыдущей ревизии IX исчез/) })