mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
* perf(artifacts): verify and decode journal header pages in one pass * test(artifacts): verify journal cancellation during header streaming --------- Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
252 lines
7.8 KiB
TypeScript
252 lines
7.8 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { expect, it, vi } from "vitest";
|
|
|
|
import { readFormatRecordHeaderPage } from "./formatRecordHeaderPage.js";
|
|
import { SystemdJournalArtifactReader } from "./SystemdJournalArtifactReader.js";
|
|
import {
|
|
HEADER_BYTES,
|
|
journalBytes,
|
|
} from "../domain/linux/systemdJournal.fixture.js";
|
|
import { ArtifactFileSource } from "../resources/ArtifactFileSource.js";
|
|
import { ArtifactResourceScope } from "../resources/ArtifactResourceScope.js";
|
|
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
|
|
|
|
const journal = async (objects = 40) => {
|
|
const bytes = journalBytes({
|
|
objects: Array.from({ length: objects }, () => ({ type: 1, size: 32 })),
|
|
});
|
|
const directory = await createTestTempDirectory("rea-record-page-");
|
|
const path = join(directory, "system.journal");
|
|
await writeFile(path, bytes);
|
|
return {
|
|
path,
|
|
bytes: bytes.length,
|
|
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
};
|
|
};
|
|
|
|
it("reads the requested window and verifies the digest in one descriptor pass", async () => {
|
|
const { path, sha256, bytes } = await journal(4096);
|
|
const read = ArtifactFileSource.prototype.read;
|
|
let descriptorResults: (() => Promise<number>) | undefined;
|
|
const source = vi
|
|
.spyOn(ArtifactFileSource.prototype, "read")
|
|
.mockImplementationOnce(async function (this: ArtifactFileSource, signal) {
|
|
const admitted = await read.call(this, signal);
|
|
const reads = vi.spyOn(admitted.handle, "read");
|
|
descriptorResults = async () => {
|
|
const results = await Promise.all(
|
|
reads.mock.results.map(({ value }) => value),
|
|
);
|
|
return results.reduce((total, result) => total + result.bytesRead, 0);
|
|
};
|
|
return admitted;
|
|
});
|
|
try {
|
|
const items = await readFormatRecordHeaderPage({
|
|
path,
|
|
format: "systemd-journal",
|
|
sha256,
|
|
start: 4094,
|
|
limit: 2,
|
|
});
|
|
expect(items.map(({ offset }) => offset)).toEqual([
|
|
HEADER_BYTES + 4094 * 32,
|
|
HEADER_BYTES + 4095 * 32,
|
|
]);
|
|
expect(await descriptorResults?.()).toBe(bytes);
|
|
} finally {
|
|
source.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("checks digest mismatch before reporting parse damage from replacement bytes", async () => {
|
|
const { path, sha256 } = await journal();
|
|
await writeFile(path, "replacement");
|
|
const observe = vi.fn();
|
|
await expect(
|
|
readFormatRecordHeaderPage(
|
|
{
|
|
path,
|
|
format: "systemd-journal",
|
|
sha256,
|
|
start: 0,
|
|
limit: 2,
|
|
},
|
|
observe,
|
|
),
|
|
).rejects.toMatchObject({
|
|
reason: "integrity",
|
|
integrityCheck: "container-identity",
|
|
});
|
|
expect(observe).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("stops a page cancelled after descriptor admission and releases its reader", async () => {
|
|
const { path, sha256 } = await journal();
|
|
const controller = new AbortController();
|
|
const read = ArtifactFileSource.prototype.read;
|
|
// Abort after opening the descriptor and before digest/decoder streaming.
|
|
const spy = vi
|
|
.spyOn(ArtifactFileSource.prototype, "read")
|
|
.mockImplementation(async function (this: ArtifactFileSource, signal) {
|
|
const admitted = await read.call(this, signal);
|
|
controller.abort(new Error("caller stopped paging"));
|
|
return admitted;
|
|
});
|
|
const resources = new ArtifactResourceScope();
|
|
try {
|
|
await expect(
|
|
readFormatRecordHeaderPage({
|
|
path,
|
|
format: "systemd-journal",
|
|
sha256,
|
|
start: 32,
|
|
limit: 8,
|
|
signal: controller.signal,
|
|
resources,
|
|
}),
|
|
).rejects.toMatchObject({ reason: "cancelled" });
|
|
// The reader's cleanup completed, so nothing is retained for retry.
|
|
expect(resources.hasPending).toBe(false);
|
|
} finally {
|
|
spy.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("stops a journal page cancelled after decoding starts", async () => {
|
|
const { path, sha256, bytes } = await journal(4096);
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller stopped paging after the first chunk");
|
|
const resources = new ArtifactResourceScope();
|
|
const observePage = vi.fn();
|
|
let decodedBytes = 0;
|
|
const read = vi.spyOn(ArtifactFileSource.prototype, "read");
|
|
const bindSha256 = ArtifactFileSource.prototype.bindSha256;
|
|
const bind = vi
|
|
.spyOn(ArtifactFileSource.prototype, "bindSha256")
|
|
.mockImplementationOnce(function (
|
|
this: ArtifactFileSource,
|
|
expected,
|
|
signal,
|
|
observe,
|
|
) {
|
|
if (observe === undefined) throw new Error("expected a page decoder");
|
|
return bindSha256.call(this, expected, signal, (chunk) => {
|
|
observe(chunk);
|
|
decodedBytes += chunk.length;
|
|
controller.abort(reason);
|
|
});
|
|
});
|
|
try {
|
|
await expect(
|
|
readFormatRecordHeaderPage(
|
|
{
|
|
path,
|
|
format: "systemd-journal",
|
|
sha256,
|
|
start: 0,
|
|
limit: 2,
|
|
signal: controller.signal,
|
|
resources,
|
|
},
|
|
observePage,
|
|
),
|
|
).rejects.toMatchObject({ reason: "cancelled", cause: reason });
|
|
expect(decodedBytes).toBeGreaterThan(HEADER_BYTES + 2 * 32);
|
|
expect(decodedBytes).toBeLessThan(bytes);
|
|
expect(observePage).not.toHaveBeenCalled();
|
|
expect((await read.mock.results[0]?.value)?.handle.fd).toBe(-1);
|
|
expect(resources.hasPending).toBe(false);
|
|
} finally {
|
|
bind.mockRestore();
|
|
read.mockRestore();
|
|
await resources.retryCleanup();
|
|
}
|
|
});
|
|
|
|
it("keeps a completed page when its reader fails to close", async () => {
|
|
const { path, sha256 } = await journal();
|
|
const close = vi
|
|
.spyOn(SystemdJournalArtifactReader.prototype, "close")
|
|
.mockRejectedValueOnce(new Error("descriptor close failed"));
|
|
const resources = new ArtifactResourceScope();
|
|
const observed: unknown[] = [];
|
|
try {
|
|
await expect(
|
|
readFormatRecordHeaderPage(
|
|
{
|
|
path,
|
|
format: "systemd-journal",
|
|
sha256,
|
|
start: 38,
|
|
limit: 8,
|
|
resources,
|
|
},
|
|
(items) => {
|
|
observed.push(items);
|
|
return undefined;
|
|
},
|
|
),
|
|
).rejects.toMatchObject({
|
|
cleanup: { reason: "descriptor close failed", resources: [path] },
|
|
});
|
|
// The finished page reached the caller's observation before the failure.
|
|
expect(observed).toEqual([
|
|
[
|
|
expect.objectContaining({ offset: HEADER_BYTES + 38 * 32 }),
|
|
expect.objectContaining({ offset: HEADER_BYTES + 39 * 32 }),
|
|
],
|
|
]);
|
|
// The reader stays retained for a cleanup retry.
|
|
expect(resources.hasPending).toBe(true);
|
|
} finally {
|
|
close.mockRestore();
|
|
await resources.retryCleanup().catch(() => undefined);
|
|
}
|
|
});
|
|
|
|
it("retries a reader an earlier page failed to close before reading again", async () => {
|
|
const { path, sha256 } = await journal();
|
|
const close = vi
|
|
.spyOn(SystemdJournalArtifactReader.prototype, "close")
|
|
.mockRejectedValueOnce(new Error("descriptor close failed"));
|
|
const resources = new ArtifactResourceScope();
|
|
const request = {
|
|
path,
|
|
format: "systemd-journal",
|
|
sha256,
|
|
start: 32,
|
|
limit: 8,
|
|
resources,
|
|
};
|
|
try {
|
|
await expect(readFormatRecordHeaderPage(request)).rejects.toMatchObject({
|
|
cleanup: { resources: [path] },
|
|
});
|
|
expect(resources.hasPending).toBe(true);
|
|
// The next page first retries the retained reader through the scope.
|
|
await expect(readFormatRecordHeaderPage(request)).resolves.toHaveLength(8);
|
|
expect(resources.hasPending).toBe(false);
|
|
expect(close).toHaveBeenCalledTimes(3);
|
|
} finally {
|
|
close.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("refuses a format that declares no record window", async () => {
|
|
const { path, sha256 } = await journal();
|
|
await expect(
|
|
readFormatRecordHeaderPage({
|
|
path,
|
|
format: "zip",
|
|
sha256,
|
|
start: 0,
|
|
limit: 8,
|
|
}),
|
|
).rejects.toMatchObject({ reason: "unavailable" });
|
|
});
|