|
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import { _reloadEnvForTesting } from "./env" |
| 3 | +import { _setClientForTesting, scanBytes } from "./clamav" |
| 4 | + |
| 5 | +function fakeClient(impl: { isInfected: boolean | null; viruses?: string[]; throws?: Error }) { |
| 6 | + return { |
| 7 | + scanStream: async () => { |
| 8 | + if (impl.throws) throw impl.throws |
| 9 | + return { isInfected: impl.isInfected, viruses: impl.viruses ?? [] } |
| 10 | + }, |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +describe("scanBytes", () => { |
| 15 | + afterEach(() => { |
| 16 | + _setClientForTesting(null) |
| 17 | + delete process.env.INTAKE_USER_FILE_SCAN_ENABLED |
| 18 | + _reloadEnvForTesting() |
| 19 | + }) |
| 20 | + |
| 21 | + test("returns clean immediately when scanning is disabled", async () => { |
| 22 | + delete process.env.INTAKE_USER_FILE_SCAN_ENABLED |
| 23 | + _reloadEnvForTesting() |
| 24 | + // Inject a client that would mark anything infected — proves we skipped it. |
| 25 | + _setClientForTesting(fakeClient({ isInfected: true, viruses: ["FAIL"] })) |
| 26 | + const result = await scanBytes(new Uint8Array([1, 2, 3])) |
| 27 | + expect(result).toEqual({ clean: true }) |
| 28 | + }) |
| 29 | + |
| 30 | + test("returns clean when scanner reports no infection", async () => { |
| 31 | + process.env.INTAKE_USER_FILE_SCAN_ENABLED = "true" |
| 32 | + _reloadEnvForTesting() |
| 33 | + _setClientForTesting(fakeClient({ isInfected: false })) |
| 34 | + const result = await scanBytes(new Uint8Array([1, 2, 3])) |
| 35 | + expect(result).toEqual({ clean: true }) |
| 36 | + }) |
| 37 | + |
| 38 | + test("returns clean=false with the first virus name when scanner finds one", async () => { |
| 39 | + process.env.INTAKE_USER_FILE_SCAN_ENABLED = "true" |
| 40 | + _reloadEnvForTesting() |
| 41 | + _setClientForTesting( |
| 42 | + fakeClient({ isInfected: true, viruses: ["Eicar-Test-Signature", "Other"] }), |
| 43 | + ) |
| 44 | + const result = await scanBytes(new Uint8Array([1, 2, 3])) |
| 45 | + expect(result).toEqual({ clean: false, reason: "Eicar-Test-Signature" }) |
| 46 | + }) |
| 47 | + |
| 48 | + test("falls back to a generic reason when viruses array is empty", async () => { |
| 49 | + process.env.INTAKE_USER_FILE_SCAN_ENABLED = "true" |
| 50 | + _reloadEnvForTesting() |
| 51 | + _setClientForTesting(fakeClient({ isInfected: true, viruses: [] })) |
| 52 | + const result = await scanBytes(new Uint8Array([1, 2, 3])) |
| 53 | + expect(result).toEqual({ clean: false, reason: "infected" }) |
| 54 | + }) |
| 55 | + |
| 56 | + test("throws (fail-closed) when scanner errors", async () => { |
| 57 | + process.env.INTAKE_USER_FILE_SCAN_ENABLED = "true" |
| 58 | + _reloadEnvForTesting() |
| 59 | + _setClientForTesting(fakeClient({ isInfected: null, throws: new Error("ECONNREFUSED") })) |
| 60 | + await expect(scanBytes(new Uint8Array([1, 2, 3]))).rejects.toThrow(/scan failed/) |
| 61 | + }) |
| 62 | +}) |
0 commit comments