62 lines
2.2 KiB
JavaScript
62 lines
2.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
|
|
import {
|
|
buildDiagnosticLoader,
|
|
inspectFrankenElf,
|
|
sha256,
|
|
} from '../../scripts/autonomous/build-kstuff-diagnostic-loader.mjs';
|
|
|
|
const MARKER = Buffer.from([0xeb, 0x0b, 0x50, 0x4c, 0x44, 0x00]);
|
|
|
|
function frankenElf(size) {
|
|
const buffer = Buffer.alloc(size, 0x41);
|
|
MARKER.copy(buffer);
|
|
buffer.writeBigUInt64LE(64n, 32);
|
|
buffer.writeUInt16LE(1, 56);
|
|
buffer.writeUInt32LE(1, 64);
|
|
buffer.writeUInt32LE(5, 68);
|
|
buffer.writeBigUInt64LE(0n, 72);
|
|
buffer.writeBigUInt64LE(BigInt(size), 96);
|
|
buffer.writeBigUInt64LE(BigInt(size), 104);
|
|
return buffer;
|
|
}
|
|
|
|
test('in-place diagnostic build preserves bytes after the exact embedded image span', () => {
|
|
const prefix = Buffer.alloc(96, 0x23);
|
|
const officialPayload = frankenElf(192);
|
|
const suffix = Buffer.alloc(80, 0x7d);
|
|
const officialLoader = Buffer.concat([prefix, officialPayload, suffix]);
|
|
const diagnosticPayload = frankenElf(160);
|
|
const manifest = { officialLoader: {
|
|
size: officialLoader.length,
|
|
sha256: sha256(officialLoader),
|
|
embeddedPayloadOffset: prefix.length,
|
|
embeddedPayloadSize: officialPayload.length,
|
|
} };
|
|
|
|
const result = buildDiagnosticLoader({ officialLoader, diagnosticPayload, manifest });
|
|
assert.deepEqual(result.output.subarray(prefix.length + officialPayload.length), suffix);
|
|
assert.equal(result.evidence.zeroPaddingBytes, 32);
|
|
assert.equal(inspectFrankenElf(diagnosticPayload).imageSpan, diagnosticPayload.length);
|
|
});
|
|
|
|
test('in-place diagnostic build rejects a payload larger than the official image span', () => {
|
|
const prefix = Buffer.alloc(96, 0x23);
|
|
const officialPayload = frankenElf(192);
|
|
const suffix = Buffer.alloc(80, 0x7d);
|
|
const officialLoader = Buffer.concat([prefix, officialPayload, suffix]);
|
|
const diagnosticPayload = frankenElf(208);
|
|
const manifest = { officialLoader: {
|
|
size: officialLoader.length,
|
|
sha256: sha256(officialLoader),
|
|
embeddedPayloadOffset: prefix.length,
|
|
embeddedPayloadSize: officialPayload.length,
|
|
} };
|
|
|
|
assert.throws(
|
|
() => buildDiagnosticLoader({ officialLoader, diagnosticPayload, manifest }),
|
|
/rebuild the loader from pinned source/,
|
|
);
|
|
});
|