Repository Public License
1// ORIX Secure2// File: Packages/ORIXFileInspection/Tests/ORIXFileInspectionTests/InspectorTests.swift3// Description: End-to-end inspections through real files: metadata, hash, reports, errors, coding round trip, and the4// wording rules applied to everything the inspector can say.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import ORIXSecureModel10import Testing11@testable import ORIXFileInspection1213@Suite("File inspector end to end")14struct InspectorTests {15 @Test("Every sample inspects through a real file, with hash, limits and honest wording")16 func allSamples() async throws {17 let dir = try TempDir()18 let inspector = FileInspector()19 for sample in Samples.all {20 let url = try dir.write(sample.name, sample.bytes)21 let inspection = try await inspector.inspect(fileAt: url)22 #expect(inspection.detectedType == sample.type, "\(sample.name)")23 #expect(inspection.fileName == sample.name)24 #expect(inspection.byteSize == Int64(sample.bytes.count))25 #expect(inspection.sha256 == FileHasher.sha256(of: Data(sample.bytes)), "\(sample.name)")26 #expect(inspection.modificationDate != nil)27 #expect(inspection.findings.has("file.type.detected"))28 #expect(Set(inspection.findings.map(\.id)).count == inspection.findings.count, "\(sample.name): duplicate finding ids")29 for limit in FileInspector.standingLimits { #expect(inspection.notInspected.contains(limit)) }30 expectHonest(inspection.findings)31 for text in inspection.notInspected {32 for word in forbiddenWords where word != "malware detected" { #expect(!text.lowercased().contains(word), "\(text)") }33 }34 #expect((inspection.archive != nil) == (sample.type.isZipFamily || sample.type == .tar || sample.type == .gzip), "\(sample.name)")35 #expect((inspection.pdf != nil) == (sample.type == .pdf))36 }37 }3839 @Test("Standing limits say what the app is not")40 func standingLimits() {41 let limits = FileInspector.standingLimits.joined(separator: " ")42 #expect(limits.contains("no malware signature database"))43 #expect(limits.contains("not executed"))44 #expect(limits.contains("Macros"))45 #expect(limits.contains("Encrypted"))46 #expect(limits.contains("not decompressed"))47 }4849 @Test("Metadata, declared type and options")50 func metadata() async throws {51 let dir = try TempDir()52 let url = try dir.write("Holiday Photo.JPG", Samples.jpegImage(width: 640, height: 480))53 let date = Date(timeIntervalSince1970: 1_700_000_000)54 try FileManager.default.setAttributes([.modificationDate: date, .creationDate: date], ofItemAtPath: url.path)55 let inspection = try await FileInspector().inspect(fileAt: url)56 #expect(inspection.declaredExtension == "jpg")57 #expect(inspection.declaredTypeIdentifier == "public.jpeg")58 #expect(inspection.detectedType == .jpeg)59 #expect(inspection.modificationDate == date)60 #expect(inspection.creationDate == date)61 #expect(inspection.detectedSignature.contains("FF D8 FF"))62 #expect(!inspection.findings.contains { $0.state == .attention })63 #expect(inspection.findings.with("file.type.detected")?.verification == .parsedData)6465 var options = InspectionOptions()66 options.computeSHA256 = false67 let unhashed = try await FileInspector().inspect(fileAt: url, options: options)68 #expect(unhashed.sha256 == nil)69 #expect(unhashed.notInspected.contains { $0.contains("SHA-256") })70 let odd = try dir.write("data.zzqq", [1, 0, 2, 0, 3])71 let unknown = try await FileInspector().inspect(fileAt: odd)72 #expect(unknown.declaredTypeIdentifier == nil)73 #expect(unknown.detectedType == .unknown)74 #expect(unknown.findings.with("file.type.detected")?.state == .notAvailable)75 }7677 @Test("Folders and missing files throw a described error")78 func errors() async throws {79 let dir = try TempDir()80 await #expect(throws: FileInspectionError.notARegularFile(dir.url.lastPathComponent)) {81 _ = try await FileInspector().inspect(fileAt: dir.url)82 }83 await #expect(throws: FileInspectionError.notFound("absent.pdf")) {84 _ = try await FileInspector().inspect(fileAt: dir.url.appendingPathComponent("absent.pdf"))85 }86 let unreadable = try dir.write("locked.bin", [1, 2, 3])87 #expect(chmod(unreadable.path, 0o000) == 0)88 defer { _ = chmod(unreadable.path, 0o644) }89 await #expect(throws: FileInspectionError.unreadable("locked.bin")) {90 _ = try await FileInspector().inspect(fileAt: unreadable)91 }92 #expect(FileInspectionError.notFound("x").errorDescription?.contains("x") == true)93 }9495 @Test("A disguised program gathers the expected findings, most severe first")96 func disguisedProgram() async throws {97 let dir = try TempDir()98 let url = try dir.write("Invoice 2026\u{202E}fdp.pdf.exe", Samples.pe)99 let inspection = try await FileInspector().inspect(fileAt: url)100 expectHonest(inspection.findings)101 let ids = Set(inspection.findings.map(\.id))102 #expect(ids.isSuperset(of: ["file.name.bidi", "file.name.doubleExtension", "file.type.program", "file.type.detected"]))103 #expect(inspection.findings.highestSeverity == .high)104 #expect(inspection.findings.prioritized().first?.severity == .high)105 let renamed = try dir.write("statement.pdf", Samples.pe)106 let second = try await FileInspector().inspect(fileAt: renamed)107 #expect(second.findings.prioritized().first?.id == "file.type.executableDisguised")108 }109110 @Test("Configuration profiles and scripts are described, not judged")111 func profilesAndScripts() async throws {112 let dir = try TempDir()113 let profile = try await FileInspector().inspect(fileAt: try dir.write("vpn.mobileconfig", Samples.mobileconfig))114 let finding = try #require(profile.findings.with("file.type.configurationProfile"))115 #expect(finding.severity == .moderate)116 #expect(finding.recommendation != nil)117 let script = try await FileInspector().inspect(fileAt: try dir.write("deploy.sh", bytes("#!/bin/bash\nset -e\n")))118 let scriptFinding = try #require(script.findings.with("file.type.script"))119 #expect(scriptFinding.evidence.contains { $0.value == "#!/bin/bash" })120 expectHonest(profile.findings + script.findings)121 }122123 @Test("Inspections survive a JSON round trip and produce a report document")124 func codingAndReport() async throws {125 let dir = try TempDir()126 let macro = buildZip([ZipEntrySpec("[Content_Types].xml", bytes("<Types/>")), ZipEntrySpec("word/document.xml", bytes("<d/>")), ZipEntrySpec("word/vbaProject.bin", zeros(64)), ZipEntrySpec("../x", zeros(1))])127 for (name, data) in [("contract.docx", macro), ("form.pdf", PDFTests.active), ("bundle.tar", buildTar([("a", bytes("1"), "0")])), ("photo.png", Samples.png)] {128 let inspection = try await FileInspector().inspect(fileAt: try dir.write(name, data))129 let encoded = try JSONEncoder().encode(inspection)130 let decoded = try JSONDecoder().decode(FileInspection.self, from: encoded)131 #expect(decoded == inspection, "\(name)")132 #expect(decoded.hashValue == inspection.hashValue)133 let document = inspection.reportDocument()134 #expect(document.kind == .fileInspection)135 #expect(document.allFindings.count == inspection.findings.count)136 #expect(document.unavailable == inspection.notInspected)137 #expect(document.sections.first?.facts.contains { $0.label == "SHA-256" && $0.value == inspection.sha256 } == true)138 for word in forbiddenWords { #expect(!(document.title + document.summary).lowercased().contains(word)) }139 }140 let options = InspectionOptions()141 let optionsData = try JSONEncoder().encode(options)142 let optionsBack = try JSONDecoder().decode(InspectionOptions.self, from: optionsData)143 #expect(optionsBack == options)144 }145146 @Test("A file larger than every scan window is read in bounded pieces")147 func largeSparseFile() async throws {148 let dir = try TempDir()149 let url = dir.url.appendingPathComponent("huge.pdf")150 FileManager.default.createFile(atPath: url.path, contents: Data(bytes("%PDF-1.4\n/OpenAction ")))151 let handle = try FileHandle(forWritingTo: url)152 try handle.truncate(atOffset: 300 << 20)153 try handle.seekToEnd()154 try handle.write(contentsOf: Data(bytes("\n/JavaScript\n%%EOF\n")))155 try handle.close()156 var options = InspectionOptions()157 options.computeSHA256 = false158 let start = Date()159 let inspection = try await FileInspector().inspect(fileAt: url, options: options)160 let elapsed = Date().timeIntervalSince(start)161 print("Inspection of a 300 MB sparse PDF without hashing: \(String(format: "%.3f", elapsed)) s")162 let report = try #require(inspection.pdf)163 #expect(inspection.byteSize > 300 << 20)164 #expect(report.rawScanTruncated)165 #expect(report.rawScanByteCount == 16 << 20)166 #expect(report.count("/OpenAction") == 1)167 #expect(report.count("/JavaScript") == 1)168 #expect(report.hasEOFMarker)169 #expect(report.pageCount.isMeasured == false)170 #expect(inspection.notInspected.contains { $0.contains("larger than") })171 #expect(elapsed < 10)172 }173}174