also looking at this
feat: own runtime firmware candidates
#49
24 files
+1245
-92
| @@ -0,0 +1,188 @@ | ||
| 1 | import { afterEach, expect, test } from 'bun:test' | |
| 2 | import { Database } from 'bun:sqlite' | |
| 3 | import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' | |
| 4 | import { tmpdir } from 'node:os' | |
| 5 | import { join } from 'node:path' | |
| 6 | import { enrollRuntimeTrust } from '../../resources/functions/drone/updates/enrollment' | |
| 7 | import { createRuntimeTrustStore, inspectRuntimeFirmwareCandidates, inspectRuntimeTrustStore, recoverRuntimeTrustStore, refreshRuntimeTrustStore, reserveRuntimeFirmwareCandidate, retireRuntimeFirmwareCandidate } from '../../resources/functions/drone/updates/store' | |
| 8 | import { platform, repository, target } from '../fixtures/updates/repository' | |
| 9 | import { qualifyCandidateInterruptions } from '../fixtures/updates/store-interruptions' | |
| 10 | import { epochRepository } from '../fixtures/updates/epochs' | |
| 11 | ||
| 12 | const roots: string[] = [] | |
| 13 | afterEach(async () => { for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true }) }) | |
| 14 | const clock = { source: 'independent-test-clock', read: () => new Date('2030-01-01T00:00:00Z') } | |
| 15 | async function setup() { | |
| 16 | const root = await mkdtemp(join(tmpdir(), 'drone-owned-candidate-')); roots.push(root) | |
| 17 | const source = join(root, 'repository'), fixture = await repository(source, { custom: { releaseSequence: 9 } }) | |
| 18 | const enrollmentDirectory = join(root, 'enrollment') | |
| 19 | const enrolled = enrollRuntimeTrust({ target, platform, trustedRootSha256: fixture.rootSha256, minimumReleaseSequence: 7, trustedRoot: fixture.rootPath, output: enrollmentDirectory }) | |
| 20 | const directory = join(root, 'store'), created = await createRuntimeTrustStore({ enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, output: directory }) | |
| 21 | return { root, source, fixture, reference: { enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, directory, storeSha256: created.storeSha256 } } | |
| 22 | } | |
| 23 | ||
| 24 | test('the pinned store exclusively owns a prepared candidate and retains its authenticated identity across reopening', async () => { | |
| 25 | const item = await setup(), before = await inspectRuntimeTrustStore(item.reference) | |
| 26 | expect((await inspectRuntimeFirmwareCandidates(item.reference)).candidate).toBeNull() | |
| 27 | const result = await reserveRuntimeFirmwareCandidate({ ...item.reference, repository: item.source, clock }) | |
| 28 | expect(result.schema).toBe('drone-runtime-candidates-v1') | |
| 29 | expect(result.stateFormat).toBe(2) | |
| 30 | expect(result.store.storeId).toBe(before.storeId) | |
| 31 | expect(result.store.releaseFloor).toBe(9) | |
| 32 | expect(result.store.generation).toBe(3) | |
| 33 | expect(result.store.attempt?.phase).toBe('verified') | |
| 34 | expect(result.retainedCandidates).toBe(1) | |
| 35 | expect(result.candidate?.phase).toBe('prepared') | |
| 36 | expect(result.candidate?.integrity).toBe('recorded-bytes-match') | |
| 37 | expect(result.candidate?.description.sha256).toBe(item.fixture.imageHash) | |
| 38 | expect(result.candidate?.description.releaseSequence).toBe(9) | |
| 39 | expect(result.candidate?.currentTrustValidated).toBe(false) | |
| 40 | expect(result.physicalDeploymentAllowed).toBe(false) | |
| 41 | expect(await readFile(join(result.candidate!.directory, 'image.bin'))).toEqual(item.fixture.image) | |
| 42 | expect(await inspectRuntimeFirmwareCandidates(item.reference)).toEqual(result) | |
| 43 | await expect(reserveRuntimeFirmwareCandidate({ ...item.reference, repository: item.source, clock })).rejects.toThrow('active candidate') | |
| 44 | expect(await inspectRuntimeFirmwareCandidates(item.reference)).toEqual(result) | |
| 45 | }) | |
| 46 | ||
| 47 | test('explicit retirement retains old bytes and floors while allowing a distinct new candidate identity', async () => { | |
| 48 | const item = await setup() | |
| 49 | const first = await reserveRuntimeFirmwareCandidate({ ...item.reference, repository: item.source, clock }) | |
| 50 | const candidate = first.candidate!, original = await readFile(join(candidate.directory, 'stage.json')) | |
| 51 | await expect(retireRuntimeFirmwareCandidate({ ...item.reference, candidateId: '0'.repeat(32) })).rejects.toThrow('active candidate') | |
| 52 | expect(await inspectRuntimeFirmwareCandidates(item.reference)).toEqual(first) | |
| 53 | const retired = await retireRuntimeFirmwareCandidate({ ...item.reference, candidateId: candidate.id }) | |
| 54 | expect(retired.candidate).toBeNull() | |
| 55 | expect(retired.retainedCandidates).toBe(1) | |
| 56 | expect(retired.history.map(row => ({ id: row.id, phase: row.phase, sha256: row.description.sha256 }))).toEqual([{ id: candidate.id, phase: 'retired', sha256: item.fixture.imageHash }]) | |
| 57 | expect(retired.store.releaseFloor).toBe(9) | |
| 58 | expect(await readFile(join(candidate.directory, 'stage.json'))).toEqual(original) | |
| 59 | expect(await readFile(join(candidate.directory, 'image.bin'))).toEqual(item.fixture.image) | |
| 60 | const next = await reserveRuntimeFirmwareCandidate({ ...item.reference, repository: item.source, clock }) | |
| 61 | expect(next.candidate?.id).not.toBe(candidate.id) | |
| 62 | expect(next.candidate?.integrity).toBe('recorded-bytes-match') | |
| 63 | expect(next.retainedCandidates).toBe(2) | |
| 64 | expect(next.store.releaseFloor).toBe(9) | |
| 65 | expect(await readFile(join(candidate.directory, 'stage.json'))).toEqual(original) | |
| 66 | await expect(retireRuntimeFirmwareCandidate({ ...item.reference, candidateId: candidate.id })).rejects.toThrow('active candidate') | |
| 67 | expect(await inspectRuntimeFirmwareCandidates(item.reference)).toEqual(next) | |
| 68 | }) | |
| 69 | ||
| 70 | test('a completed stage marker after an interrupted reservation cannot promote itself during metadata recovery', async () => { | |
| 71 | const item = await setup(), interruption = new Error('Stop after durable candidate files') | |
| 72 | await expect(reserveRuntimeFirmwareCandidate({ ...item.reference, repository: item.source, clock, onProgress(event) { | |
| 73 | if (event.phase === 'candidate-completed') throw interruption | |
| 74 | } })).rejects.toBe(interruption) | |
| 75 | const stopped = await inspectRuntimeFirmwareCandidates(item.reference), candidate = stopped.candidate! | |
| 76 | expect(candidate.phase).toBe('reserved') | |
| 77 | expect(candidate.integrity).toBe('incomplete') | |
| 78 | expect(JSON.parse(await readFile(join(candidate.directory, 'stage.json'), 'utf8')).sha256).toBe(item.fixture.imageHash) | |
| 79 | expect(stopped.store.releaseFloor).toBe(9) | |
| 80 | expect(stopped.store.attempt?.phase).toBe('maintenance') | |
| 81 | await expect(retireRuntimeFirmwareCandidate({ ...item.reference, candidateId: candidate.id })).rejects.toThrow('Resolve the metadata attempt') | |
| 82 | await recoverRuntimeTrustStore(item.reference) | |
| 83 | const recovered = await inspectRuntimeFirmwareCandidates(item.reference) | |
| 84 | expect(recovered.candidate).toEqual(candidate) | |
| 85 | expect(recovered.store.attempt?.phase).toBe('recovered') | |
| 86 | expect(recovered.store.releaseFloor).toBe(9) | |
| 87 | expect(recovered.physicalDeploymentAllowed).toBe(false) | |
| 88 | const original = await readFile(join(candidate.directory, 'stage.json')) | |
| 89 | const retired = await retireRuntimeFirmwareCandidate({ ...item.reference, candidateId: candidate.id }) | |
| 90 | expect(retired.candidate).toBeNull() | |
| 91 | expect(await readFile(join(candidate.directory, 'stage.json'))).toEqual(original) | |
| 92 | }) | |
| 93 | ||
| 94 | test('a rejected actual image retains the reserved identity and accepted floors without creating a prepared artifact', async () => { | |
| 95 | const item = await setup() | |
| 96 | await writeFile(item.fixture.imagePath, Buffer.alloc(item.fixture.image.length, 0xff)) | |
| 97 | await expect(reserveRuntimeFirmwareCandidate({ ...item.reference, repository: item.source, clock })).rejects.toThrow() | |
| 98 | const stopped = await inspectRuntimeFirmwareCandidates(item.reference) | |
| 99 | expect(stopped.candidate?.phase).toBe('reserved') | |
| 100 | expect(stopped.candidate?.integrity).toBe('incomplete') | |
| 101 | expect(stopped.store.releaseFloor).toBe(9) | |
| 102 | expect(stopped.store.attempt?.phase).toBe('maintenance') | |
| 103 | expect(await Bun.file(join(stopped.candidate!.directory, 'stage.json')).exists()).toBe(false) | |
| 104 | await recoverRuntimeTrustStore(item.reference) | |
| 105 | expect((await inspectRuntimeFirmwareCandidates(item.reference)).candidate?.phase).toBe('reserved') | |
| 106 | }) | |
| 107 | ||
| 108 | test('missing or corrupt owned bytes produce actionable integrity status and never current-trust authorization', async () => { | |
| 109 | const item = await setup(), prepared = await reserveRuntimeFirmwareCandidate({ ...item.reference, repository: item.source, clock }) | |
| 110 | const candidate = prepared.candidate!, image = join(candidate.directory, 'image.bin') | |
| 111 | await writeFile(image, Buffer.alloc(item.fixture.image.length, 0xff)) | |
| 112 | expect((await inspectRuntimeFirmwareCandidates(item.reference)).candidate?.integrity).toBe('mismatch') | |
| 113 | await rm(image) | |
| 114 | const missing = await inspectRuntimeFirmwareCandidates(item.reference) | |
| 115 | expect(missing.candidate?.integrity).toBe('unavailable') | |
| 116 | expect(missing.candidate?.phase).toBe('prepared') | |
| 117 | expect(missing.candidate?.currentTrustValidated).toBe(false) | |
| 118 | expect(missing.store.releaseFloor).toBe(9) | |
| 119 | expect(missing.physicalDeploymentAllowed).toBe(false) | |
| 120 | await writeFile(image, item.fixture.image, { mode: 0o600 }) | |
| 121 | await writeFile(join(candidate.directory, 'repository/metadata/1.targets.json'), '{}\n') | |
| 122 | expect((await inspectRuntimeFirmwareCandidates(item.reference)).candidate?.integrity).not.toBe('recorded-bytes-match') | |
| 123 | }) | |
| 124 | ||
| 125 | test('actual killed candidate writers preserve reserved, prepared and retired states with concurrent writers excluded', async () => { | |
| 126 | const root = await mkdtemp(join(tmpdir(), 'drone-owned-candidate-cuts-')); roots.push(root) | |
| 127 | const result = await qualifyCandidateInterruptions(join(root, 'campaign')) | |
| 128 | expect(result.cuts).toBe(5) | |
| 129 | expect(result.physicalDeploymentAllowed).toBe(false) | |
| 130 | }, 90000) | |
| 131 | ||
| 132 | test('managed candidates reject repository overlap before advancing state and preserve a foreign state format unchanged', async () => { | |
| 133 | const item = await setup(), initial = await inspectRuntimeTrustStore(item.reference) | |
| 134 | for (const repository of [item.root, item.reference.directory]) { | |
| 135 | await expect(reserveRuntimeFirmwareCandidate({ ...item.reference, repository, clock })).rejects.toThrow('separate') | |
| 136 | expect(await inspectRuntimeTrustStore(item.reference)).toEqual(initial) | |
| 137 | } | |
| 138 | await reserveRuntimeFirmwareCandidate({ ...item.reference, repository: item.source, clock }) | |
| 139 | const file = join(item.reference.directory, 'state.sqlite'), db = new Database(file, { create: false, readwrite: true }) | |
| 140 | try { db.exec('PRAGMA user_version=99') } | |
| 141 | finally { db.close() } | |
| 142 | const original = await readFile(file) | |
| 143 | await expect(inspectRuntimeFirmwareCandidates(item.reference)).rejects.toThrow('schema') | |
| 144 | expect(await readFile(file)).toEqual(original) | |
| 145 | }) | |
| 146 | ||
| 147 | test('migration retains a prior rotated-root epoch, metadata history and clock/release floors across later rejections', async () => { | |
| 148 | const root = await mkdtemp(join(tmpdir(), 'drone-owned-candidate-migration-')); roots.push(root) | |
| 149 | const source = join(root, 'repository'), fixture = await epochRepository(source) | |
| 150 | const enrollmentDirectory = join(root, 'enrollment'), directory = join(root, 'store') | |
| 151 | const enrolled = enrollRuntimeTrust({ target, platform, trustedRootSha256: fixture.rootSha256, minimumReleaseSequence: 7, trustedRoot: fixture.rootPath, output: enrollmentDirectory }) | |
| 152 | const created = await createRuntimeTrustStore({ enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, output: directory }) | |
| 153 | const reference = { enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, directory, storeSha256: created.storeSha256 } | |
| 154 | await fixture.publish(1, 100, 12) | |
| 155 | await refreshRuntimeTrustStore({ ...reference, repository: source, clock }) | |
| 156 | const nextClock = { source: clock.source, read: () => new Date('2030-01-02T00:00:00Z') } | |
| 157 | const image = await fixture.publish(2, 1, 12) | |
| 158 | const before = await refreshRuntimeTrustStore({ ...reference, repository: source, clock: nextClock }) | |
| 159 | expect(before.epoch).toBe(1) | |
| 160 | expect(before.generation).toBe(7) | |
| 161 | expect(before.releaseFloor).toBe(12) | |
| 162 | expect((await inspectRuntimeFirmwareCandidates(reference)).stateFormat).toBe(1) | |
| 163 | const candidateClock = { source: clock.source, read: () => new Date('2030-01-03T00:00:00Z') } | |
| 164 | const prepared = await reserveRuntimeFirmwareCandidate({ ...reference, repository: source, clock: candidateClock }) | |
| 165 | expect(prepared.stateFormat).toBe(2) | |
| 166 | expect(prepared.store.storeId).toBe(before.storeId) | |
| 167 | expect(prepared.store.storeSha256).toBe(before.storeSha256) | |
| 168 | expect(prepared.store.enrollmentSha256).toBe(before.enrollmentSha256) | |
| 169 | expect(prepared.store.metadata).toEqual(before.metadata) | |
| 170 | expect(prepared.store.retainedMetadataRecords).toBe(before.retainedMetadataRecords) | |
| 171 | expect(prepared.store.epoch).toBe(1) | |
| 172 | expect(prepared.store.releaseFloor).toBe(12) | |
| 173 | expect(prepared.store.clockFloorMs).toBe(Date.parse('2030-01-03T00:00:00Z')) | |
| 174 | expect(prepared.candidate?.description.rootVersion).toBe(2) | |
| 175 | const retired = await retireRuntimeFirmwareCandidate({ ...reference, candidateId: prepared.candidate!.id }) | |
| 176 | await expect(reserveRuntimeFirmwareCandidate({ ...reference, repository: source, clock })).rejects.toThrow('clock') | |
| 177 | expect(await inspectRuntimeFirmwareCandidates(reference)).toEqual(retired) | |
| 178 | await fixture.publish(2, 2, 11) | |
| 179 | await expect(reserveRuntimeFirmwareCandidate({ ...reference, repository: source, clock: candidateClock })).rejects.toThrow('below the trusted minimum') | |
| 180 | await recoverRuntimeTrustStore(reference) | |
| 181 | const recovered = await inspectRuntimeFirmwareCandidates(reference) | |
| 182 | expect(recovered.store.releaseFloor).toBe(12) | |
| 183 | expect(recovered.store.epoch).toBe(1) | |
| 184 | expect(recovered.store.attempt?.phase).toBe('recovered') | |
| 185 | expect(recovered.candidate).toBeNull() | |
| 186 | expect(recovered.history[0]?.description.releaseSequence).toBe(12) | |
| 187 | expect(await readFile(join(prepared.candidate!.directory, 'image.bin'))).toEqual(image.image) | |
| 188 | }) | |