import assert from "node:assert/strict" import type { IpsecPeerDeleteState } from "../../services/ipsec-peer-delete.js" import { planIpsecPeerCreate, projectIpsecPeerCreate, verifyIpsecPeerCreate } from "./ipsec-peer-create-dependencies.js" import { fingerprint, McpError } from "./security.js" const profile = "/ip/ipsec/profile", peer = "/ip/ipsec/peer", identity = "/ip/ipsec/identity", policy = "/ip/ipsec/policy" const state = (): IpsecPeerDeleteState => ({ source: "private-source", proof: "private-proof", protections: [{ object_ref: { kind: "ipsec_profile", id: "*D" } }], catalogs: { [profile]: [{ ".id": "*D", name: "default", default: "true", "opaque-setting": "preserved" }, { ".id": "*C", name: "custom" }], [peer]: [{ ".id": "*A", name: "existing", profile: "default", "opaque-setting": "preserved" }, { ".id": "*B", name: "other", profile: "*C" }], [identity]: [{ ".id": "*I", peer: "existing", secret: "private-secret" }], [policy]: [{ ".id": "*P", peer: "other, existing", "opaque-setting": "preserved" }, { ".id": "*T", peer: "", template: "true" }], "/certificate": [{ ".id": "*Q", "private-key": "opaque" }], } }) const fields = { name: "new-peer", address: "192.0.2.1", comment: "" } const conflict = (error: unknown) => error instanceof McpError && error.code === "STATE_CONFLICT" const invalid = (error: unknown) => error instanceof McpError && error.code === "INVALID_INPUT" const initial = state(), saved = structuredClone(initial) assert.deepEqual(planIpsecPeerCreate(initial, fields), { body: { ...fields, profile: "default" }, profileId: "*D", commandCount: 1, objects: [{ kind: "ipsec_profile", id: "*D" }, { kind: "ipsec_peer", id: "*A" }, { kind: "ipsec_identity", id: "*I" }, { kind: "ipsec_policy", id: "*P" }], }) assert.deepEqual(initial, saved, "preflight never mutates private state") assert.equal(planIpsecPeerCreate(initial, { ...fields, profile: "*C" }).profileId, "*C") assert.deepEqual(planIpsecPeerCreate(initial, { ...fields, profile: "custom" }).objects, [{ kind: "ipsec_profile", id: "*C" }, { kind: "ipsec_peer", id: "*B" }, { kind: "ipsec_policy", id: "*P" }]) for (const change of [ (s: IpsecPeerDeleteState) => { s.catalogs[profile] = [] }, (s: IpsecPeerDeleteState) => { s.catalogs[profile]!.push({ ".id": "*E", name: "default" }) }, (s: IpsecPeerDeleteState) => { s.catalogs[profile]![0]!.dynamic = "true" }, (s: IpsecPeerDeleteState) => { s.catalogs[profile]![0]!.disabled = "unknown"; s.catalogs[profile]![0]!.dynamic = "unknown" }, (s: IpsecPeerDeleteState) => { s.catalogs[profile]![0]![".id"] = "bad" }, (s: IpsecPeerDeleteState) => { s.catalogs[peer]![1]!.name = fields.name }, (s: IpsecPeerDeleteState) => { delete s.catalogs[peer]![0]!.profile }, (s: IpsecPeerDeleteState) => { s.catalogs[peer]![1]!.name = "*A"; s.catalogs[identity]![0]!.peer = "*A" }, (s: IpsecPeerDeleteState) => { s.catalogs[identity]![0]!.peer = fields.name }, (s: IpsecPeerDeleteState) => { s.catalogs[identity]![0]!.peer = "orphan" }, (s: IpsecPeerDeleteState) => { delete s.catalogs[identity]![0]!.peer }, (s: IpsecPeerDeleteState) => { s.catalogs[policy]![0]!.peer = `other, ${fields.name}` }, (s: IpsecPeerDeleteState) => { s.catalogs[policy]![0]!.peer = "existing," }, (s: IpsecPeerDeleteState) => { s.catalogs[policy]![0]!.dynamic = "unknown" }, (s: IpsecPeerDeleteState) => { s.catalogs[identity]!.push({ ...s.catalogs[identity]![0]! }) }, (s: IpsecPeerDeleteState) => { delete s.catalogs[policy] }, ]) { const candidate = state(); change(candidate) assert.throws(() => planIpsecPeerCreate(candidate, fields), conflict) } for (const change of [{ profile: "" }, { profile: " default " }, { profile: "missing" }]) assert.throws(() => planIpsecPeerCreate(state(), { ...fields, ...change }), conflict) for (const change of [{ name: "*F" }, { name: " spaced " }, { address: "" }, { certificate: "*C" }, { passive: "unknown" }, { "exchange-mode": "unknown" }]) assert.throws(() => planIpsecPeerCreate(state(), { ...fields, ...change }), invalid) const dynamic = state() dynamic.catalogs[policy]!.push({ ".id": "*R", dynamic: "true", peer: "transient-live-peer", "ph2-state": "established" }) assert.equal(planIpsecPeerCreate(dynamic, fields).objects.some(ref => ref.id === "*R"), false, "runtime-derived policy is not a configured dependency") const projected = projectIpsecPeerCreate(initial, fields, "*NEW") assert.deepEqual(projected.catalogs[peer]!.at(-1), { ...fields, profile: "default", ".id": "*NEW" }) projected.catalogs[peer]!.pop() assert.deepEqual(projected, initial, "projection conserves all unrelated raw fields, secrets, protections and source") assert.deepEqual(initial, saved) for (const assigned of [undefined, "", "bad", "*A", { ".id": "*NEW" }]) assert.throws(() => projectIpsecPeerCreate(initial, fields, assigned), conflict) const pureProof = async ({ source, catalogs, protections }: IpsecPeerDeleteState) => fingerprint({ source, catalogs, protections }) const actual = projectIpsecPeerCreate(initial, fields, "*NEW") Object.assign(actual.catalogs[peer]!.at(-1)!, { "opaque-native-default": "preserved", passive: "no", disabled: "no" }) const actualSaved = structuredClone(actual) assert.equal(await verifyIpsecPeerCreate(initial, actual, fields, "*NEW", pureProof), true) assert.equal(await verifyIpsecPeerCreate(initial, actual, { ...fields, profile: "*D", disabled: "false" }, "*NEW", pureProof), true) assert.deepEqual(actual, actualSaved, "verification never mutates read-back") for (const change of [ (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.name = "different" }, (s: IpsecPeerDeleteState) => { delete s.catalogs[peer]!.at(-1)!.profile }, (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.profile = "*D" }, (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.dynamic = "true" }, (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.builtin = "unknown" }, (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.at(-1)!.default = "true" }, (s: IpsecPeerDeleteState) => { s.catalogs[peer]![0]!["opaque-setting"] = "changed" }, (s: IpsecPeerDeleteState) => { s.catalogs[identity]![0]!.secret = "changed" }, (s: IpsecPeerDeleteState) => { s.catalogs["/certificate"]![0]!["private-key"] = "changed" }, (s: IpsecPeerDeleteState) => { s.catalogs[peer]!.push({ ".id": "*UNEXPECTED", name: "foreign" }) }, (s: IpsecPeerDeleteState) => { s.source = "changed" }, (s: IpsecPeerDeleteState) => { s.protections = [] }, ]) { const candidate = structuredClone(actual); change(candidate) assert.equal(await verifyIpsecPeerCreate(initial, candidate, fields, "*NEW", pureProof), false) } for (const assigned of [undefined, "bad", "*A", "*MISSING"]) assert.equal(await verifyIpsecPeerCreate(initial, actual, fields, assigned, pureProof), false) console.log("IPsec peer CREATE dependencies: exact/default profile, closure, late-bound references, native identity and private-state conservation passed")