40 lines
3.2 KiB
TypeScript
40 lines
3.2 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto"
|
|
import { mkdtemp, rm, writeFile } from "node:fs/promises"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import { test } from "node:test"
|
|
import { canonicalBandwidthNodeValue } from "./bandwidth-node-bootstrap.js"
|
|
import { createBandwidthOperatorWriterComposition, type BandwidthOperatorWriterCompositionDependencies } from "./bandwidth-operator-writer-composition.js"
|
|
|
|
test("actual composition refuses missing external enforcement before source lease or credentials", async () => {
|
|
const directory = await mkdtemp(join(tmpdir(), "operator-composition-"))
|
|
const keys = generateKeyPairSync("ed25519")
|
|
const trustKeyFile = join(directory, "operator.pub"), grantFile = join(directory, "grant.json")
|
|
const root = { installationId: randomUUID(), issuerKeySha256: "a".repeat(64), quiescenceProof: "b".repeat(64) }
|
|
const grant = { format: "mmapp-bandwidth-operator-enrollment-v1", ...root, grantId: randomUUID(), operatorId: "synthetic-test",
|
|
executorInventorySha256: "c".repeat(64), executorsStoppedAt: Date.now() - 2000, issuedAt: Date.now() - 1000, expiresAt: Date.now() + 4000,
|
|
peerRef: { kind: "external", enrollmentId: randomUUID() }, sourceProof: "d".repeat(64), authorityGeneration: "original",
|
|
routingEndpointEpoch: randomUUID(), claims: [{ kind: "system-id", value: "synthetic-system" }] }
|
|
const payload = Buffer.from(canonicalBandwidthNodeValue(grant))
|
|
await writeFile(trustKeyFile, keys.publicKey.export({ type: "spki", format: "pem" }))
|
|
await writeFile(grantFile, JSON.stringify({ format: "mmapp-bandwidth-operator-enrollment-envelope-v1",
|
|
payload: payload.toString("base64url"), signature: sign(null, payload, keys.privateKey).toString("base64url") }))
|
|
let leases = 0, credentials = 0, external = 0
|
|
// This deliberate synthetic source cannot open: the test proves ordering and
|
|
// refusal, never a genuine service stop, production source or native success.
|
|
const nodeSource = { async open() { leases++; throw Error("must not open") }, async openWithOwnershipFence() { leases++; throw Error("must not open") } } as unknown as BandwidthOperatorWriterCompositionDependencies["nodeSource"]
|
|
const composition = createBandwidthOperatorWriterComposition({
|
|
config: { ...root, trustKeyFile, grantFile, trustKeySha256: createHash("sha256").update(keys.publicKey.export({ type: "spki", format: "der" })).digest("hex") },
|
|
nodeSource, observationSource: { async read() { credentials++; throw Error("must not resolve") } },
|
|
externalStop: { async acquire(original) { external++; assert.deepEqual(original.grant, grant); throw Error("No enforced external stop/restart prohibition") } },
|
|
})
|
|
try {
|
|
await assert.rejects(composition.register({ deadlineAt: Date.now() + 3000 }), { code: "BANDWIDTH_OPERATOR_WRITER_FENCE_REFUSED" })
|
|
assert.equal(external, 1); assert.equal(leases, 0); assert.equal(credentials, 0)
|
|
composition.close()
|
|
await assert.rejects(composition.register({ deadlineAt: Date.now() + 3000 }))
|
|
assert.equal(external, 1)
|
|
} finally { composition.close(); await rm(directory, { recursive: true, force: true }) }
|
|
})
|