53 lines
4.8 KiB
TypeScript
53 lines
4.8 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { readOspfLinkCatalog, resolveOspfLinkOwner } from "./ospf-link-catalog.js"
|
|
|
|
function fixture(id: number, vrf: string, own: string, remote: string, iface: string, ownAddress: string, remoteAddress: string, family: "ipv4" | "ipv6" = "ipv4") {
|
|
const version = family === "ipv4" ? "2" : "3", name = "instance" + id
|
|
const data: Record<string, Record<string, unknown>[]> = {
|
|
"/routing/ospf/instance": [{ ".id": "*i" + id, name, "router-id": own, version, vrf, disabled: "false" }],
|
|
"/routing/ospf/neighbor": [{ ".id": "*n" + id, instance: name, interface: iface, "router-id": remote, address: remoteAddress, state: "Full" }],
|
|
[family === "ipv4" ? "/ip/address" : "/ipv6/address"]: [{ ".id": "*a" + id, interface: iface, address: ownAddress, disabled: "false", invalid: "false" }],
|
|
}
|
|
const requests: string[] = []
|
|
const client = {
|
|
async get<T>(path: string): Promise<T> { requests.push(path); return structuredClone(data[path] ?? []) as T },
|
|
async post<T>(path: string, body: Record<string, string>): Promise<T> { requests.push(path + ":" + body["value-name"]); return { ret: "false" } as T },
|
|
}
|
|
return { data, requests, client, read: () => readOspfLinkCatalog(client, family) }
|
|
}
|
|
const local = fixture(1, "source-vrf", "192.0.2.1", "192.0.2.2", "left", "198.18.0.1/30", "198.18.0.2")
|
|
const peer = fixture(2, "different-peer-vrf", "192.0.2.2", "192.0.2.1", "right", "198.18.0.2/30", "198.18.0.1")
|
|
const other = fixture(3, "isolated", "192.0.2.2", "192.0.2.1", "other", "198.51.100.3/24", "198.51.100.1")
|
|
const source = await local.read()
|
|
let target = await peer.read(), isolated = await other.read()
|
|
const owner = () => resolveOspfLinkOwner(1, source, "*i1", "*n1", [{ serverId: 1, catalog: source }, { serverId: 2, catalog: target }, { serverId: 3, catalog: isolated }])
|
|
assert.deepEqual(owner(), { serverId: 2, instanceId: "*i2", vrf: "different-peer-vrf", interface: "right" })
|
|
other.data["/ip/address"][0].address = "198.18.0.2/30"; other.data["/routing/ospf/neighbor"][0].address = "198.18.0.1"
|
|
isolated = await other.read(); assert.throws(owner, /Неоднозначное/)
|
|
other.data["/ip/address"][0].address = "198.51.100.3/24"; isolated = await other.read()
|
|
peer.data["/routing/ospf/neighbor"][0].state = "Down"; target = await peer.read(); assert.equal(owner(), null)
|
|
peer.data["/routing/ospf/neighbor"][0].state = "Full"; peer.data["/routing/ospf/neighbor"][0].address = "198.18.0.9"; target = await peer.read(); assert.equal(owner(), null)
|
|
peer.data["/routing/ospf/neighbor"][0].address = "198.18.0.1"; peer.data["/ip/address"][0].invalid = "true"; target = await peer.read(); assert.equal(owner(), null)
|
|
peer.data["/ip/address"][0].invalid = "false"; target = await peer.read()
|
|
const stable = target.fingerprint
|
|
Object.assign(peer.data["/routing/ospf/neighbor"][0], { adjacency: "9h", "state-changes": "999" })
|
|
assert.equal((await peer.read()).fingerprint, stable, "Runtime counters do not alter link identity")
|
|
const second = { ...peer.data["/routing/ospf/instance"][0], ".id": "*i4", name: "another-vrf", vrf: "another-vrf" }
|
|
peer.data["/routing/ospf/instance"].push(second)
|
|
peer.data["/routing/ospf/neighbor"].push({ ...peer.data["/routing/ospf/neighbor"][0], ".id": "*n4", instance: "another-vrf", interface: "another-iface" })
|
|
peer.data["/ip/address"].push({ ...peer.data["/ip/address"][0], ".id": "*a4", interface: "another-iface" })
|
|
target = await peer.read(); assert.throws(owner, /Неоднозначное/, "Two VRFs on one device remain two contexts")
|
|
const local6 = fixture(1, "left-vrf", "192.0.2.1", "192.0.2.2", "left", "fe80::1/64", "fe80::2%left@left-vrf", "ipv6")
|
|
const peer6 = fixture(2, "right-vrf", "192.0.2.2", "192.0.2.1", "right", "fe80:0:0:0:0:0:0:2/64", "fe80::1%right@right-vrf", "ipv6")
|
|
const a6 = await local6.read(), b6 = await peer6.read()
|
|
assert.equal(resolveOspfLinkOwner(1, a6, "*i1", "*n1", [{ serverId: 2, catalog: b6 }])?.vrf, "right-vrf")
|
|
peer6.data["/routing/ospf/neighbor"][0].address = "fe80::1%wrong@right-vrf"; await assert.rejects(peer6.read())
|
|
const defaults = fixture(1, "main", "192.0.2.1", "192.0.2.2", "left", "198.18.0.1/30", "198.18.0.2")
|
|
delete defaults.data["/ip/address"][0].disabled; delete defaults.data["/ip/address"][0].invalid
|
|
assert.equal((await defaults.read()).addresses[0].invalid, false)
|
|
assert.ok(defaults.requests.some(path => path.endsWith(":invalid")))
|
|
const guarded = fixture(1, "main", "192.0.2.1", "192.0.2.2", "left", "198.18.0.1/30", "198.18.0.2")
|
|
await assert.rejects(readOspfLinkCatalog(guarded.client, "ipv4", async () => { if (guarded.requests.length === 1) throw new Error("Revoked") }))
|
|
assert.deepEqual(guarded.requests, ["/routing/ospf/instance"])
|
|
console.log("OSPF links: reciprocal Full and two address/interface proofs, different VRF names, excluded isolated duplicate, ambiguous devices/VRFs refused, IPv6 scope/canonical IP, stable counters and guarded effective address flags")
|