diff --git a/src/application/binaryDiagnostics/AppleIpsService.test.ts b/src/application/binaryDiagnostics/AppleIpsService.test.ts index 69df4b2456..6e0c077b98 100644 --- a/src/application/binaryDiagnostics/AppleIpsService.test.ts +++ b/src/application/binaryDiagnostics/AppleIpsService.test.ts @@ -1,3 +1,4 @@ +import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js"; import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { createHash } from "node:crypto"; import { parseAppleIps } from "../../native/apple/AppleIpsParser.js"; @@ -7,7 +8,6 @@ import { AppleIpsService } from "./AppleIpsService.js"; import { APPLE_IPS_PROVIDER } from "../../domain/investigationProviders.js"; import type { AppleIps } from "../../domain/native/appleIps.js"; import { ok } from "../../domain/result.js"; -import { AnalysisInputError } from "../../domain/analysisErrorCore.js"; const identity = APPLE_IPS_PROVIDER; const bytes = Buffer.from( @@ -117,26 +117,12 @@ it.each([null, { caller: "selected IPS request" }])( }, ); -it("rejects a relative path and extra arguments before reading", async () => { - let calls = 0; - const service = new AppleIpsService({ - identity, - inspect: () => { - calls += 1; - return Promise.resolve(ok(report)); - }, - }); - const relative = await service.inspect({ path: "report.ips" }); - const extra = await service.inspect({ - path: report.artifact.path, - extra: true, - }); - expect(relative.ok).toBe(false); - expect(extra.ok).toBe(false); - if (!relative.ok) expect(relative.error).toBeInstanceOf(AnalysisInputError); - if (!extra.ok) expect(extra.error).toBeInstanceOf(AnalysisInputError); - expect(calls).toBe(0); -}); +it("rejects a relative path and extra arguments before reading", () => + expectInvalidInspectionInputs( + report, + "report.ips", + (inspect) => new AppleIpsService({ identity, inspect }), + )); it("records the selected report as a file subject", async () => { const result = await new AppleIpsService({ diff --git a/src/application/binaryDiagnostics/DwarfUnitsService.test.ts b/src/application/binaryDiagnostics/DwarfUnitsService.test.ts index bedaf87492..a883e40c90 100644 --- a/src/application/binaryDiagnostics/DwarfUnitsService.test.ts +++ b/src/application/binaryDiagnostics/DwarfUnitsService.test.ts @@ -1,8 +1,8 @@ +import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js"; import { parseEvidence } from "../../domain/evidence.js"; import { expect, it } from "vitest"; import { AnalysisCancelledError, - AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; import { DWARF_UNITS_PROVIDER } from "../../domain/investigationProviders.js"; @@ -40,26 +40,12 @@ const report: DwarfUnitsReport = { ], }; -it("rejects a relative path and extra arguments before reading", async () => { - let calls = 0; - const service = new DwarfUnitsService({ - identity, - inspect: () => { - calls += 1; - return Promise.resolve(ok(report)); - }, - }); - const relative = await service.inspect({ path: "program" }); - const extra = await service.inspect({ - path: report.artifact.path, - extra: true, - }); - expect(relative.ok).toBe(false); - expect(extra.ok).toBe(false); - if (!relative.ok) expect(relative.error).toBeInstanceOf(AnalysisInputError); - if (!extra.ok) expect(extra.error).toBeInstanceOf(AnalysisInputError); - expect(calls).toBe(0); -}); +it("rejects a relative path and extra arguments before reading", () => + expectInvalidInspectionInputs( + report, + "program", + (inspect) => new DwarfUnitsService({ identity, inspect }), + )); it("records absent debug info for this file", async () => { const result = await new DwarfUnitsService({ diff --git a/src/application/binaryDiagnostics/ElfDependenciesService.test.ts b/src/application/binaryDiagnostics/ElfDependenciesService.test.ts index fbc0cd724e..2c3f1f7b36 100644 --- a/src/application/binaryDiagnostics/ElfDependenciesService.test.ts +++ b/src/application/binaryDiagnostics/ElfDependenciesService.test.ts @@ -1,13 +1,11 @@ +import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js"; import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { ElfDependenciesService } from "./ElfDependenciesService.js"; import { ELF_DEPENDENCIES_PROVIDER } from "../../domain/investigationProviders.js"; import type { ElfDependencies } from "../../domain/native/elfDependencies.js"; import { ok } from "../../domain/result.js"; -import { - AnalysisInputError, - AnalysisOutputError, -} from "../../domain/analysisErrorCore.js"; +import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; const identity = ELF_DEPENDENCIES_PROVIDER; const digest = "a".repeat(64); @@ -47,26 +45,12 @@ const report: ElfDependencies = { ], }; -it("rejects a relative path and extra arguments before reading", async () => { - let calls = 0; - const service = new ElfDependenciesService({ - identity, - inspect: () => { - calls += 1; - return Promise.resolve(ok(report)); - }, - }); - const relative = await service.inspect({ path: "program" }); - const extra = await service.inspect({ - path: report.artifact.path, - extra: true, - }); - expect(relative.ok).toBe(false); - expect(extra.ok).toBe(false); - if (!relative.ok) expect(relative.error).toBeInstanceOf(AnalysisInputError); - if (!extra.ok) expect(extra.error).toBeInstanceOf(AnalysisInputError); - expect(calls).toBe(0); -}); +it("rejects a relative path and extra arguments before reading", () => + expectInvalidInspectionInputs( + report, + "program", + (inspect) => new ElfDependenciesService({ identity, inspect }), + )); it("records an empty PT_DYNAMIC inventory and its x86_64 architecture", async () => { const result = await new ElfDependenciesService({ diff --git a/src/application/binaryDiagnostics/ElfNotesService.test.ts b/src/application/binaryDiagnostics/ElfNotesService.test.ts index 501b9e437b..ff5b696adc 100644 --- a/src/application/binaryDiagnostics/ElfNotesService.test.ts +++ b/src/application/binaryDiagnostics/ElfNotesService.test.ts @@ -1,3 +1,4 @@ +import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js"; import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { ElfNotesService } from "./ElfNotesService.js"; @@ -9,7 +10,6 @@ import { parseElfNotesFromHandle } from "../../native/elf/ElfNoteParser.js"; import { ok } from "../../domain/result.js"; import { AnalysisCancelledError, - AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; import { bufferByteSource } from "../../domain/byteSource.js"; @@ -28,26 +28,12 @@ const report = await parseElfNotesFromHandle( async () => {}, ); -it("rejects a relative path and extra arguments before reading", async () => { - let calls = 0; - const service = new ElfNotesService({ - identity, - inspect: () => { - calls += 1; - return Promise.resolve(ok(report)); - }, - }); - const relative = await service.inspect({ path: "app" }); - const extra = await service.inspect({ - path: report.artifact.path, - extra: true, - }); - expect(relative.ok).toBe(false); - expect(extra.ok).toBe(false); - if (!relative.ok) expect(relative.error).toBeInstanceOf(AnalysisInputError); - if (!extra.ok) expect(extra.error).toBeInstanceOf(AnalysisInputError); - expect(calls).toBe(0); -}); +it("rejects a relative path and extra arguments before reading", () => + expectInvalidInspectionInputs( + report, + "app", + (inspect) => new ElfNotesService({ identity, inspect }), + )); it("records the selected ELF and an absent note table", async () => { const result = await new ElfNotesService({ diff --git a/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts b/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts index cb0b08f3f0..1c7ef3ff68 100644 --- a/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts +++ b/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts @@ -1,3 +1,4 @@ +import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js"; import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { ElfUnwindRangesService } from "./ElfUnwindRangesService.js"; @@ -14,10 +15,7 @@ import { elfUnwindSectionlessFixture, } from "../../native/elf/ElfUnwindTables.fixture.js"; import { ok } from "../../domain/result.js"; -import { - AnalysisInputError, - AnalysisOutputError, -} from "../../domain/analysisErrorCore.js"; +import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; import { bufferByteSource } from "../../domain/byteSource.js"; const identity = ELF_UNWIND_RANGES_PROVIDER; @@ -62,26 +60,12 @@ const accept = async ( }).inspect(input); }; -it("rejects a relative path and extra arguments before reading", async () => { - let calls = 0; - const service = new ElfUnwindRangesService({ - identity, - inspect: () => { - calls += 1; - return Promise.resolve(ok(report)); - }, - }); - const relative = await service.inspect({ path: "app" }); - const extra = await service.inspect({ - path: report.artifact.path, - extra: true, - }); - expect(relative.ok).toBe(false); - expect(extra.ok).toBe(false); - if (!relative.ok) expect(relative.error).toBeInstanceOf(AnalysisInputError); - if (!extra.ok) expect(extra.error).toBeInstanceOf(AnalysisInputError); - expect(calls).toBe(0); -}); +it("rejects a relative path and extra arguments before reading", () => + expectInvalidInspectionInputs( + report, + "app", + (inspect) => new ElfUnwindRangesService({ identity, inspect }), + )); it("records an absent unwind table for the selected ELF", async () => { const result = await accept(report); diff --git a/src/application/binaryDiagnostics/PeCertificatesService.test.ts b/src/application/binaryDiagnostics/PeCertificatesService.test.ts index fa65a0fe32..95b6f66176 100644 --- a/src/application/binaryDiagnostics/PeCertificatesService.test.ts +++ b/src/application/binaryDiagnostics/PeCertificatesService.test.ts @@ -1,3 +1,4 @@ +import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js"; import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { inspectPeCertificatesInputSchema } from "../../domain/native/peCertificates.js"; import { @@ -10,10 +11,7 @@ import { PeCertificatesService } from "./PeCertificatesService.js"; import { PE_CERTIFICATES_PROVIDER } from "../../domain/investigationProviders.js"; import type { PeCertificates } from "../../domain/native/peCertificates.js"; import { ok } from "../../domain/result.js"; -import { - AnalysisInputError, - AnalysisOutputError, -} from "../../domain/analysisErrorCore.js"; +import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; const identity = PE_CERTIFICATES_PROVIDER; const input = inspectPeCertificatesInputSchema.parse({ @@ -24,26 +22,12 @@ const report = await parsePeCertificateFixture( input, ); -it("rejects a relative path and extra arguments before reading", async () => { - let calls = 0; - const service = new PeCertificatesService({ - identity, - inspect: () => { - calls += 1; - return Promise.resolve(ok(report)); - }, - }); - const relative = await service.inspect({ path: "app.exe" }); - const extra = await service.inspect({ - path: report.artifact.path, - extra: true, - }); - expect(relative.ok).toBe(false); - expect(extra.ok).toBe(false); - if (!relative.ok) expect(relative.error).toBeInstanceOf(AnalysisInputError); - if (!extra.ok) expect(extra.error).toBeInstanceOf(AnalysisInputError); - expect(calls).toBe(0); -}); +it("rejects a relative path and extra arguments before reading", () => + expectInvalidInspectionInputs( + report, + "app.exe", + (inspect) => new PeCertificatesService({ identity, inspect }), + )); it("retains a completed observation when the caller cancels as the provider completes", async () => { const evidence = await lateCancellationEvidence( diff --git a/src/application/binaryDiagnostics/PeImportsService.test.ts b/src/application/binaryDiagnostics/PeImportsService.test.ts index 86320fee7c..6d548f2bfb 100644 --- a/src/application/binaryDiagnostics/PeImportsService.test.ts +++ b/src/application/binaryDiagnostics/PeImportsService.test.ts @@ -1,5 +1,6 @@ import { bufferByteSource } from "../../domain/byteSource.js"; import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; +import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js"; import { createHash } from "node:crypto"; import { peImportImage, @@ -15,10 +16,7 @@ import { expect, it } from "vitest"; import { PeImportsService } from "./PeImportsService.js"; import { PE_IMPORTS_PROVIDER } from "../../domain/investigationProviders.js"; import { ok } from "../../domain/result.js"; -import { - AnalysisInputError, - AnalysisOutputError, -} from "../../domain/analysisErrorCore.js"; +import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; const identity = PE_IMPORTS_PROVIDER; const path = "/artifacts/app.exe"; @@ -33,26 +31,12 @@ const report = await parsePeImportsFromHandle( inspectPeImportsInputSchema.parse({ path }), ); -it("rejects a relative path and extra arguments before reading", async () => { - let calls = 0; - const service = new PeImportsService({ - identity, - inspect: () => { - calls += 1; - return Promise.resolve(ok(report)); - }, - }); - const relative = await service.inspect({ path: "app.exe" }); - const extra = await service.inspect({ - path: report.artifact.path, - extra: true, - }); - expect(relative.ok).toBe(false); - expect(extra.ok).toBe(false); - if (!relative.ok) expect(relative.error).toBeInstanceOf(AnalysisInputError); - if (!extra.ok) expect(extra.error).toBeInstanceOf(AnalysisInputError); - expect(calls).toBe(0); -}); +it("rejects a relative path and extra arguments before reading", () => + expectInvalidInspectionInputs( + report, + "app.exe", + (inspect) => new PeImportsService({ identity, inspect }), + )); it.each(["imports", "delay_imports", "examined_dlls"] as const)( "rejects contradictory complete %s coverage", diff --git a/src/application/binaryDiagnostics/WindowsShortcutService.test.ts b/src/application/binaryDiagnostics/WindowsShortcutService.test.ts index 11e9585b5e..bf18b9270a 100644 --- a/src/application/binaryDiagnostics/WindowsShortcutService.test.ts +++ b/src/application/binaryDiagnostics/WindowsShortcutService.test.ts @@ -1,3 +1,4 @@ +import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js"; import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { WindowsShortcutService } from "./WindowsShortcutService.js"; @@ -6,10 +7,7 @@ import { createHash } from "node:crypto"; import { inspectWindowsShortcutInputSchema } from "../../domain/native/windowsShortcut.js"; import { parseWindowsShortcut } from "../../native/windows/WindowsShortcutParser.js"; import { ok } from "../../domain/result.js"; -import { - AnalysisInputError, - AnalysisOutputError, -} from "../../domain/analysisErrorCore.js"; +import { AnalysisOutputError } from "../../domain/analysisErrorCore.js"; const identity = WINDOWS_SHORTCUT_PROVIDER; @@ -28,26 +26,12 @@ const report = await parseWindowsShortcut( inspectWindowsShortcutInputSchema.parse({ path }), ); -it("rejects a relative path and extra arguments before reading", async () => { - let calls = 0; - const service = new WindowsShortcutService({ - identity, - inspect: () => { - calls += 1; - return Promise.resolve(ok(report)); - }, - }); - const relative = await service.inspect({ path: "shortcut.lnk" }); - const extra = await service.inspect({ - path: report.artifact.path, - extra: true, - }); - expect(relative.ok).toBe(false); - expect(extra.ok).toBe(false); - if (!relative.ok) expect(relative.error).toBeInstanceOf(AnalysisInputError); - if (!extra.ok) expect(extra.error).toBeInstanceOf(AnalysisInputError); - expect(calls).toBe(0); -}); +it("rejects a relative path and extra arguments before reading", () => + expectInvalidInspectionInputs( + report, + "shortcut.lnk", + (inspect) => new WindowsShortcutService({ identity, inspect }), + )); it("records the selected shortcut as a file subject", async () => { const result = await new WindowsShortcutService({ diff --git a/tests/support/expectInvalidInspectionInputs.ts b/tests/support/expectInvalidInspectionInputs.ts new file mode 100644 index 0000000000..e2e98b3440 --- /dev/null +++ b/tests/support/expectInvalidInspectionInputs.ts @@ -0,0 +1,26 @@ +import { expect, vi } from "vitest"; +import type { AnalysisError } from "../../src/domain/analysisErrorBase.js"; +import { AnalysisInputError } from "../../src/domain/analysisErrorCore.js"; +import { ok, type Result } from "../../src/domain/result.js"; + +export async function expectInvalidInspectionInputs< + Report extends { artifact: { path: string } }, +>( + report: Report, + relativePath: string, + createService: (inspect: () => Promise>) => { + inspect(input: unknown): Promise>; + }, +): Promise { + const inspect = vi.fn(() => Promise.resolve(ok(report))); + const service = createService(inspect); + for (const input of [ + { path: relativePath }, + { path: report.artifact.path, extra: true }, + ]) { + const result = await service.inspect(input); + expect(result.ok).toBe(false); + if (!result.ok) expect(result.error).toBeInstanceOf(AnalysisInputError); + } + expect(inspect).not.toHaveBeenCalled(); +}