SPB Git

Repository Public License

MagicDetectorTests.swift 7.0 KB 148 lines swift

1// ORIX Secure2// File: Packages/ORIXFileInspection/Tests/ORIXFileInspectionTests/MagicDetectorTests.swift3// Description: Type detection from content on minimal samples of every recognised format, and its edge cases.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXSecureModel9import Testing10@testable import ORIXFileInspection1112@Suite("Magic-number detection")13struct MagicDetectorTests {14    @Test("Each minimal sample is recognised", arguments: Samples.all)15    func recognises(_ sample: Sample) throws {16        let core = try inspectBytes(sample.bytes, name: sample.name)17        #expect(core.type == sample.type, "\(sample.name): detected \(core.type), signature \(core.signature)")18        expectHonest(core.findings)19    }2021    @Test("Samples named after their type raise no extension finding", arguments: Samples.all)22    func noMismatchOnHonestNames(_ sample: Sample) throws {23        let core = try inspectBytes(sample.bytes, name: sample.name)24        let mismatch = core.findings.filter { $0.id.hasPrefix("file.type.extension") || $0.id == "file.type.executableDisguised" || $0.id == "file.type.signatureMissing" }25        #expect(mismatch.isEmpty, "\(sample.name): \(mismatch.map(\.title))")26    }2728    @Test("The sample set covers every detectable type")29    func coverage() {30        let covered = Set(Samples.all.map(\.type))31        let missing = DetectedFileType.allCases.filter { !covered.contains($0) }32        #expect(missing.isEmpty, "no sample for \(missing)")33    }3435    @Test("Detection through a real file gives the same answer")36    func throughFiles() throws {37        let dir = try TempDir()38        for sample in Samples.all {39            let url = try dir.write(sample.name, sample.bytes)40            let type = try MagicDetector.detect(fileAt: url)41            #expect(type == sample.type, "\(sample.name)")42        }43    }4445    @Test("MZ without a PE header at e_lfanew is not called a Windows program")46    func mzWithoutPE() throws {47        var data = Samples.pe48        data[0x80] = 0x4E49        let wrong = try MagicDetector.detect(source: DataByteSource(data))50        #expect(wrong.type == .dosExecutable)51        var beyond = Samples.pe52        beyond.replaceSubrange(0x3C..<0x40, with: le32(0xFFFF_FFF0))53        let outside = try MagicDetector.detect(source: DataByteSource(beyond))54        #expect(outside.type == .dosExecutable)55    }5657    @Test("CAFEBABE: universal Mach-O and Java class are told apart")58    func cafebabe() throws {59        let fat = try MagicDetector.detect(source: DataByteSource([0xCA, 0xFE, 0xBA, 0xBE] + be32(3) + zeros(80)))60        #expect(fat.type == .machOFat)61        let java8 = try MagicDetector.detect(source: DataByteSource([0xCA, 0xFE, 0xBA, 0xBE] + be16(0) + be16(52) + zeros(80)))62        #expect(java8.type == .javaClass)63        let java21 = try MagicDetector.detect(source: DataByteSource([0xCA, 0xFE, 0xBA, 0xBE] + be16(0) + be16(65) + zeros(80)))64        #expect(java21.type == .javaClass)65        // Claims 30 architectures but is far too short to hold them.66        let lying = try MagicDetector.detect(source: DataByteSource([0xCA, 0xFE, 0xBA, 0xBE] + be32(30) + zeros(16)))67        #expect(lying.type != .machOFat)68    }6970    @Test("A PDF header within the first 1024 bytes is still a PDF")71    func pdfHeaderOffset() throws {72        let data = bytes("junk-line\n") + Samples.pdf73        let core = try inspectBytes(data, name: "shifted.pdf")74        #expect(core.type == .pdf)75        #expect(core.pdf?.headerOffset == 10)76        #expect(core.findings.has("file.pdf.headerOffset"))77        let late = [UInt8](repeating: 0x41, count: 2000) + Samples.pdf78        let other = try MagicDetector.detect(source: DataByteSource(late))79        #expect(other.type == .text)80    }8182    @Test("Text and binary are told apart")83    func textVersusBinary() throws {84        let nul = try MagicDetector.detect(source: DataByteSource(bytes("abc\u{0}def")))85        #expect(nul.type == .unknown)86        let latin1 = try MagicDetector.detect(source: DataByteSource([0x63, 0x61, 0x66, 0xE9, 0x20, 0x21]))87        #expect(latin1.type == .unknown)88        // A sample cut in the middle of a multi-byte character stays text.89        var long = [UInt8](repeating: 0x61, count: MagicDetector.headLength - 1)90        long += bytes("é") + bytes(" suite")91        let cut = try MagicDetector.detect(source: DataByteSource(long))92        #expect(cut.type == .text)93        let script = try MagicDetector.detect(source: DataByteSource(bytes("#!/bin/zsh -e\nls\n")))94        #expect(script.interpreter == "/bin/zsh -e")95    }9697    @Test("A UTF-16 byte order mark is not mistaken for MP3 frame sync")98    func utf16NotMP3() throws {99        let detection = try MagicDetector.detect(source: DataByteSource([0xFF, 0xFE, 0x41, 0x00, 0x42, 0x00]))100        #expect(detection.type == .utf16Text)101    }102103    @Test("The ISO media brand decides between image, audio and video")104    func brands() throws {105        let cases: [(String, [String], DetectedFileType)] = [106            ("heix", [], .heic), ("msf1", ["hevc"], .heic), ("mif1", ["avif"], .avif), ("avis", [], .avif),107            ("M4B ", [], .m4a), ("mp42", ["isom"], .mp4), ("3gp5", [], .mp4), ("qt  ", [], .quickTime),108        ]109        for (major, compatible, expected) in cases {110            let detection = try MagicDetector.detect(source: DataByteSource(Samples.ftyp(major, compatible)))111            #expect(detection.type == expected, "brand \(major)")112        }113    }114115    @Test("ZIP containers are refined from entry names only")116    func zipRefinement() throws {117        // OOXML markers without [Content_Types].xml stay a plain ZIP.118        let fake = buildZip([ZipEntrySpec("word/document.xml", bytes("<x/>"))])119        let core = try inspectBytes(fake, name: "fake.docx")120        #expect(core.type == .zip)121        #expect(core.findings.has("file.ooxml.contentTypes"))122        // A compressed (not stored) mimetype entry is not read; the container hint still works.123        let epub = buildZip([ZipEntrySpec("mimetype", bytes("xxxx"), method: 8), ZipEntrySpec("META-INF/container.xml", bytes("<c/>"))])124        let detected = try inspectBytes(epub, name: "book.epub")125        #expect(detected.type == .epub)126    }127128    @Test("A DMG is recognised by its trailer even when it starts like GZIP or BZIP2")129    func dmgTrailer() throws {130        let gzipStart = [0x1F, 0x8B, 0x08, 0x00] + zeros(700) + Samples.koly131        let first = try MagicDetector.detect(source: DataByteSource(gzipStart))132        #expect(first.type == .dmg)133        // A trailer with the wrong version is ignored.134        var bad = Samples.koly135        bad[7] = 9136        let second = try MagicDetector.detect(source: DataByteSource([0x78, 0x9C] + zeros(600) + bad))137        #expect(second.type == .unknown)138    }139140    @Test("Display names exist for every type and avoid verdict words")141    func displayNames() {142        for type in DetectedFileType.allCases {143            #expect(!type.displayName.isEmpty)144            for word in forbiddenWords { #expect(!type.displayName.lowercased().contains(word)) }145        }146    }147}148
Files main
Menu

Theme