67 lines
5.7 KiB
TypeScript
67 lines
5.7 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import type { IpsecPeerDeleteState } from "./ipsec-peer-delete.js"
|
|
import { ipsecCertificateUpdateNativePatch, planIpsecCertificateUpdate } from "./ipsec-certificate-update-model.js"
|
|
|
|
const paths = ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"]
|
|
const fixture = (): IpsecPeerDeleteState => ({ source: "endpoint-seal", proof: "proof", protections: [{ object_ref: { kind: "certificate", id: "*a" } }], catalogs: {
|
|
...Object.fromEntries(paths.map(path => [path, []])),
|
|
"/certificate": [
|
|
{ ".id": "*a", name: "leaf", ca: "issuer", trusted: "false", "private-key": "true", opaque: "leaf" },
|
|
{ ".id": "*b", name: "issuer", authority: "true", opaque: "issuer" },
|
|
{ ".id": "*c", name: "child", ca: "leaf", opaque: "child" },
|
|
{ ".id": "*d", name: "id-child", ca: "*a" },
|
|
],
|
|
"/ip/ipsec/identity": [{ ".id": "*i", peer: "site", certificate: "leaf", "remote-certificate": "*a", secret: "opaque-secret", comment: "leaf" }, { ".id": "*j", peer: "*p", "remote-certificate": "child" }],
|
|
"/ip/ipsec/peer": [{ ".id": "*p", name: "site", opaque: "leaf" }, { ".id": "*q", name: "direct", certificate: "leaf", "remote-certificate": "*a" }],
|
|
"/ip/ipsec/profile": [{ ".id": "*f", name: "profile", opaque: "leaf", certificate: "leaf" }],
|
|
} })
|
|
const original = fixture(), saved = structuredClone(original)
|
|
const plan = planIpsecCertificateUpdate(original, "*a", { name: "renamed", trusted: true })
|
|
assert.deepEqual(original, saved)
|
|
assert.deepEqual(plan.body, { name: "renamed", trusted: "true" })
|
|
assert.equal(plan.commandCount, 1)
|
|
assert.deepEqual(plan.target, { kind: "certificate", id: "*a" })
|
|
assert.deepEqual(new Set(plan.companions.map(row => `${row.kind}:${row.id}`)), new Set(["certificate:*b", "certificate:*c", "certificate:*d", "ipsec_identity:*i", "ipsec_identity:*j", "ipsec_peer:*p", "ipsec_peer:*q"]))
|
|
const expected = structuredClone(saved)
|
|
Object.assign(expected.catalogs["/certificate"][0], { name: "renamed", trusted: "true" })
|
|
expected.catalogs["/certificate"][2].ca = "renamed"
|
|
expected.catalogs["/ip/ipsec/identity"][0].certificate = "renamed"
|
|
expected.catalogs["/ip/ipsec/peer"][1].certificate = "renamed"
|
|
assert.deepEqual(plan.projected, expected)
|
|
assert.equal(plan.projected.catalogs["/ip/ipsec/identity"][0]["remote-certificate"], "*a")
|
|
assert.equal(plan.projected.catalogs["/ip/ipsec/profile"][0].certificate, "leaf")
|
|
assert.equal(planIpsecCertificateUpdate(original, "*a", {}).commandCount, 0)
|
|
assert.deepEqual(ipsecCertificateUpdateNativePatch({ trusted: false, name: " trimmed " }), { trusted: "false", name: "trimmed" })
|
|
for (const raw of [null, [], { ca: "issuer" }, { trusted: "true" }, { trusted: 1 }, { name: "" }, { name: "a\nb" }, { name: 1 }, { ".id": "*a" }]) assert.throws(() => ipsecCertificateUpdateNativePatch(raw), { code: "INVALID_INPUT" })
|
|
for (const id of ["leaf", "*a/b", " *a", "", "*" + "a".repeat(101)]) assert.throws(() => planIpsecCertificateUpdate(original, id, {}), { code: "INVALID_INPUT" })
|
|
assert.throws(() => planIpsecCertificateUpdate(original, "*z", {}), { code: "NOT_FOUND" })
|
|
for (const flag of ["dynamic", "builtin", "default"]) for (const value of ["true", "yes", "unknown", "0"]) {
|
|
const state = fixture(); state.catalogs["/certificate"][0][flag] = value
|
|
assert.throws(() => planIpsecCertificateUpdate(state, "*a", { trusted: true }), { code: "PROTECTED_OBJECT" })
|
|
}
|
|
for (const change of [
|
|
(state: IpsecPeerDeleteState) => state.catalogs["/certificate"].push({ ".id": "*z", name: "leaf" }),
|
|
(state: IpsecPeerDeleteState) => state.catalogs["/certificate"].push({ ".id": "*z", name: "issuer" }),
|
|
(state: IpsecPeerDeleteState) => { state.catalogs["/certificate"][0].ca = "missing" },
|
|
(state: IpsecPeerDeleteState) => state.catalogs["/ip/ipsec/peer"].push({ ".id": "*z", name: "site" }),
|
|
]) { const state = fixture(); change(state); assert.throws(() => planIpsecCertificateUpdate(state, "*a", { trusted: true }), { code: "STATE_CONFLICT" }) }
|
|
for (const path of paths) {
|
|
const missing = fixture(); delete missing.catalogs[path]
|
|
assert.throws(() => planIpsecCertificateUpdate(missing, "*a", {}), { code: "STATE_UNAVAILABLE" })
|
|
const duplicate = fixture(); duplicate.catalogs[path] = [{ ".id": "*x" }, { ".id": "*x" }]
|
|
assert.throws(() => planIpsecCertificateUpdate(duplicate, "*a", {}), { code: "STATE_UNAVAILABLE" })
|
|
}
|
|
for (const reference of ["leaf,child", "leaf;child", "(leaf)", " leaf "]) {
|
|
const state = fixture(); state.catalogs["/ip/ipsec/identity"][0].certificate = reference
|
|
assert.throws(() => planIpsecCertificateUpdate(state, "*a", { name: "renamed" }), { code: "STATE_UNAVAILABLE" })
|
|
}
|
|
for (const reference of ["renamed", "other,renamed"]) {
|
|
const state = fixture(); state.catalogs["/ip/ipsec/peer"].push({ ".id": "*z", certificate: reference })
|
|
assert.throws(() => planIpsecCertificateUpdate(state, "*a", { name: "renamed" }), { code: "STATE_CONFLICT" })
|
|
}
|
|
assert.throws(() => planIpsecCertificateUpdate(original, "*a", { name: "issuer" }), { code: "STATE_CONFLICT" })
|
|
assert.throws(() => planIpsecCertificateUpdate(original, "*a", { name: "*b" }), { code: "STATE_CONFLICT" })
|
|
const opaque = fixture(); opaque.catalogs["/ip/ipsec/identity"].push({ ".id": "*u", certificate: "opaque-value", note: "leaf" })
|
|
assert.equal(planIpsecCertificateUpdate(opaque, "*a", { name: "renamed" }).projected.catalogs["/ip/ipsec/identity"][2].certificate, "opaque-value")
|
|
console.log("✓ Certificate update model: exact native target, strict typed fields, complete immutable state, chain/identity/peer closure, guarded expected rename references, stable ID aliases and no late binding")
|