SPB Git

Repository Public License

SafetyTests.swift 15.8 KB 233 lines swift

1// ORIX Convert — Packages/EngineFont/Tests/EngineFontTests/SafetyTests.swift2// Description: Hostile and damaged fonts: truncated directories, overlapping tables, out-of-range offsets, absurd3//              table counts, WOFF decompression bombs, mutated files, plus a sweep over every system font.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import ConvertCore6@testable import EngineFont7import Foundation8import Testing9import zlib1011@Suite struct SafetyTests {12    struct LCG { var state: UInt64; mutating func next(_ bound: Int) -> Int { state = state &* 6364136223846793005 &+ 1442695040888963407; return Int((state >> 33) % UInt64(max(1, bound))) } }1314    static func be32(_ v: UInt32) -> [UInt8] { [UInt8(v >> 24), UInt8(v >> 16 & 0xFF), UInt8(v >> 8 & 0xFF), UInt8(v & 0xFF)] }15    static func be16(_ v: UInt16) -> [UInt8] { [UInt8(v >> 8), UInt8(v & 0xFF)] }1617    /// A minimal sfnt with the given directory entries (tag, offset, length) and a body of `bodyLength` bytes.18    static func sfnt(numTables: UInt16, entries: [(String, UInt32, UInt32)], bodyLength: Int) -> Data {19        var d = Data(be32(0x0001_0000) + be16(numTables) + be16(0) + be16(0) + be16(0))20        for (tag, offset, length) in entries { d.append(contentsOf: Array(tag.utf8) + be32(0) + be32(offset) + be32(length)) }21        d.append(Data(count: bodyLength))22        return d23    }2425    static func expectMalformed(_ data: Data, _ needle: String, sourceLocation: SourceLocation = #_sourceLocation) {26        do {27            _ = try FontFile.open(data: data)28            Issue.record("accepted a malformed font (\(needle))", sourceLocation: sourceLocation)29        } catch let error as ConversionError {30            #expect(error.localizedDescription.contains(needle), "\(error.localizedDescription)", sourceLocation: sourceLocation)31        } catch { Issue.record("unexpected error \(error)", sourceLocation: sourceLocation) }32    }3334    @Test func malformedSfntDirectoriesFailCleanly() throws {35        Self.expectMalformed(Data([0, 1, 0, 0, 0]), "truncated")36        Self.expectMalformed(Data("not a font at all".utf8), "unknown signature")37        Self.expectMalformed(Self.sfnt(numTables: 40, entries: [("head", 28, 4)], bodyLength: 8), "runs past the end")                 // truncated directory38        Self.expectMalformed(Self.sfnt(numTables: 65_535, entries: [], bodyLength: 64), "65535 tables")                                  // absurd numTables39        Self.expectMalformed(Self.sfnt(numTables: 0, entries: [], bodyLength: 64), "0 tables")40        Self.expectMalformed(Self.sfnt(numTables: 1, entries: [("glyf", 4_000_000_000, 16)], bodyLength: 32), "lies outside the file")     // offset out of range41        Self.expectMalformed(Self.sfnt(numTables: 1, entries: [("glyf", 28, 0xFFFF_FFF0)], bodyLength: 32), "lies outside the file")       // length out of range42        Self.expectMalformed(Self.sfnt(numTables: 2, entries: [("glyf", 44, 16), ("loca", 52, 16)], bodyLength: 64), "overlap")           // overlapping tables43        Self.expectMalformed(Self.sfnt(numTables: 2, entries: [("glyf", 44, 8), ("glyf", 52, 8)], bodyLength: 64), "twice")44        Self.expectMalformed(Self.sfnt(numTables: 1, entries: [("glyf", 4, 16)], bodyLength: 64), "overlaps the table directory")45        Self.expectMalformed(Data(Array("ttcf".utf8) + Self.be32(0x10000) + Self.be32(1_000_000) + Self.be32(12)), "more than the file can hold")46        Self.expectMalformed(Data(Array("ttcf".utf8) + Self.be32(0x10000) + Self.be32(1) + Self.be32(9999)), "truncated")47        // A structurally valid font whose tables are nonsense parses, and the facts simply stay empty.48        let hollow = Self.sfnt(numTables: 3, entries: [("OS/2", 60, 4), ("head", 64, 4), ("name", 68, 4)], bodyLength: 32)49        let facts = try FontFile.open(data: hollow).facts()50        #expect(facts.family == nil && facts.unitsPerEm == nil && facts.embedding == nil && facts.glyphCount == nil && facts.codePointCount == nil)51        #expect(throws: ConversionError.self) { _ = try LoadedFont(file: try FontFile.open(data: hollow)) }52    }5354    /// WOFF with one table: `stored` bytes on disk claiming `orig` uncompressed bytes.55    static func woff(stored: Data, orig: UInt32, tables: UInt16 = 1, lengthOverride: UInt32? = nil) -> Data {56        var d = Data(Array("wOFF".utf8) + be32(0x0001_0000))57        let total = 44 + 20 + ((stored.count + 3) & ~3)58        d.append(contentsOf: be32(lengthOverride ?? UInt32(total)) + be16(tables) + be16(0) + be32(12 + 16 + orig) + be16(1) + be16(0))59        d.append(contentsOf: [UInt8](repeating: 0, count: 20))60        d.append(contentsOf: Array("glyf".utf8) + be32(64) + be32(UInt32(stored.count)) + be32(orig) + be32(0))61        d.append(stored)62        while d.count % 4 != 0 { d.append(0) }63        return d64    }6566    @Test func woffDecompressionBombsAndLiesAreRefused() throws {67        let kilobyte = try #require(WOFF.deflate(Data(count: 1024), level: 9))68        // Honest table: accepted.69        #expect(try FontFile.open(data: Self.woff(stored: kilobyte, orig: 1024)).fonts[0].tables[0].length == 1024)70        // Claims more than deflate can ever produce from these bytes.71        Self.expectMalformed(Self.woff(stored: kilobyte, orig: 3_000_000_000), "which no compressed table can do")72        // Claims less than the stream really inflates to (a bomb hidden behind a small declared size).73        let megabyte = try #require(WOFF.deflate(Data(count: 1 << 20), level: 9))74        Self.expectMalformed(Self.woff(stored: megabyte, orig: 4096), "could not be decompressed")75        // Claims more than the stream gives.76        Self.expectMalformed(Self.woff(stored: kilobyte, orig: 2048), "could not be decompressed")77        // Within deflate's ratio but beyond the global size limit: refused before any allocation.78        let huge = try #require(WOFF.deflate(Data(count: 700 << 20), level: 1))79        #expect(huge.count < 4 << 20)80        Self.expectMalformed(Self.woff(stored: huge, orig: 700 << 20), "would exceed 512 MB")81        // Other header lies.82        Self.expectMalformed(Self.woff(stored: kilobyte, orig: 1024, lengthOverride: 9999), "but it is")83        Self.expectMalformed(Self.woff(stored: kilobyte, orig: 1024, tables: 60_000), "more than it can hold")84        Self.expectMalformed(Self.woff(stored: Data(count: 64), orig: 32), "larger compressed than uncompressed")85        Self.expectMalformed(Self.woff(stored: Data([1, 2, 3, 4, 5, 6, 7, 8]), orig: 4096), "could not be decompressed")86        var collection = Self.woff(stored: kilobyte, orig: 1024)87        collection.replaceSubrange(4..<8, with: Array("ttcf".utf8))88        Self.expectMalformed(collection, "cannot contain a font collection")89        Self.expectMalformed(Data(Array("wOF2".utf8) + [UInt8](repeating: 0xFF, count: 60)), "512 MB")90        Self.expectMalformed(Data(Array("wOF2".utf8) + [UInt8](repeating: 0, count: 60)), "WOFF2")91        Self.expectMalformed(Data(Array("wOF2".utf8)), "not a WOFF2")92    }9394    @Test(arguments: ["ttf", "woff", "woff2", "ttc"])95    func mutatedFontsNeverCrash(kind: String) async throws {96        let scratch = try Scratch(); defer { scratch.cleanUp() }97        var seed: Data98        switch kind {99        case "woff": seed = try await convert(try scratch.fixture(.glyf), .ttf, .woff, in: scratch).data100        case "woff2": seed = try await convert(try scratch.fixture(.glyf), .ttf, .woff2, options: ["quality": .int(4)], in: scratch).data101        case "ttc": seed = try Data(contentsOf: SystemFont.collection.url)102        default: seed = try Data(contentsOf: SystemFont.glyf.url)103        }104        var rng = LCG(state: UInt64(kind.utf8.reduce(17) { $0 &* 31 &+ Int($1) }))105        var opened = 0, refused = 0106        for round in 0..<400 {107            var bytes = [UInt8](seed)108            switch round % 4 {109            case 0: for _ in 0..<(1 + rng.next(4)) { bytes[rng.next(min(bytes.count, 512))] = UInt8(rng.next(256)) }          // header / directory damage110            case 1: for _ in 0..<(1 + rng.next(40)) { bytes[rng.next(bytes.count)] = UInt8(rng.next(256)) }                    // damage anywhere111            case 2: bytes.removeLast(1 + rng.next(bytes.count - 1))                                                             // truncation112            default:113                let at = rng.next(bytes.count / 4) * 4                                                                          // a 32-bit field set to an extreme114                if at + 4 <= bytes.count { bytes.replaceSubrange(at..<at + 4, with: [[0xFF, 0xFF, 0xFF, 0xFF], [0x7F, 0xFF, 0xFF, 0xFF], [0, 0, 0, 0], [0x80, 0, 0, 0]][rng.next(4)]) }115            }116            let data = Data(bytes)117            do {118                let file = try FontFile.open(data: data)119                opened += 1120                // Everything the app can ask of an opened font must hold up: facts, rebuild, re-encode, Core Text.121                for member in file.fonts.indices.prefix(2) {122                    _ = try? file.facts(member: member)123                    let rebuilt = SFNT.build(file.fonts[member]).data124                    _ = try SFNT.parse(rebuilt)125                    if round % 20 == 0 {126                        _ = try? WOFF.decode(try WOFF.encode(file.fonts[member], level: 1))127                        _ = try? WOFF2.encode(sfnt: rebuilt, quality: 1)128                        if let loaded = try? LoadedFont(file: file, member: member) { _ = loaded.glyphs(in: 0..<16); _ = loaded.preview("Ag", pointSize: 20) }129                    }130                }131            } catch is ConversionError { refused += 1 }132        }133        #expect(opened + refused == 400)134        #expect(refused > 40, "\(kind): only \(refused) mutations were refused")135    }136137    @Test func everySystemFontParsesAndSmallOnesRoundTripThroughWOFF() throws {138        let folders = ["/System/Library/Fonts", "/System/Library/Fonts/Supplemental"]139        var files = 0, fonts = 0, roundTripped = 0, checksumIssues = 0140        for folder in folders {141            for name in try FileManager.default.contentsOfDirectory(atPath: folder).sorted() {142                let lower = name.lowercased()143                guard lower.hasSuffix(".ttf") || lower.hasSuffix(".otf") || lower.hasSuffix(".ttc") else { continue }144                let url = URL(fileURLWithPath: folder).appendingPathComponent(name)145                let file = try FontFile.open(url)146                files += 1; fonts += file.fonts.count147                let facts = try file.facts()148                #expect(facts.glyphCount != nil && facts.unitsPerEm != nil, "\(name)")149                #expect(facts.family != nil || facts.postScriptName != nil, "\(name)")150                if !SFNT.checksumMismatches(file.fonts[0]).isEmpty { checksumIssues += 1 }151                guard file.fileSize < 1 << 20 else { continue }152                for font in file.fonts {153                    let decoded = try WOFF.decode(try WOFF.encode(font, level: 1)).font154                    #expect(FontEngine.tablesEqual(font, decoded, except: []) && decoded.flavor == font.flavor, "\(name)")155                    roundTripped += 1156                }157            }158        }159        #expect(files > 200 && fonts > files && roundTripped > 150)160        print("FONT-SWEEP files=\(files) fonts=\(fonts) woffRoundTrips=\(roundTripped) filesWithChecksumMismatch=\(checksumIssues)")161    }162163    @Test func cancellationStopsAConversionAndLeavesNoOutput() async throws {164        let scratch = try Scratch(); defer { scratch.cleanUp() }165        let source = try scratch.fixture(.geneva)166        let work = try scratch.work()167        let task = Task { () -> ConversionOutput in168            withUnsafeCurrentTask { $0?.cancel() }169            return try await FontEngine().convert(.init(source: source, sourceFormat: .ttf, destinationFormat: .woff), context: .init(workDirectory: work))170        }171        await #expect(throws: CancellationError.self) { _ = try await task.value }172        #expect(try FileManager.default.contentsOfDirectory(atPath: work.path).isEmpty)173        // Cancelled between tables.174        let flag = Collector()175        let midway = Task { () -> ConversionOutput in176            try await FontEngine().convert(.init(source: source, sourceFormat: .ttf, destinationFormat: .woff),177                                           context: .init(workDirectory: work, progress: { p in flag.add(p); if (p.fraction ?? 0) > 0.3 { withUnsafeCurrentTask { $0?.cancel() } } }))178        }179        await #expect(throws: CancellationError.self) { _ = try await midway.value }180        #expect(try FileManager.default.contentsOfDirectory(atPath: work.path).isEmpty && flag.progress.count > 3)181        // Unreadable inputs.182        await #expect(throws: ConversionError.self) { _ = try await FontEngine().inspect(scratch.root.appendingPathComponent("missing.ttf"), format: .ttf) }183        let tiny = try scratch.file("tiny.ttf", Data([0, 1, 0]))184        await #expect(throws: ConversionError.self) { _ = try await FontEngine().inspect(tiny, format: .ttf) }185    }186187    @Test func woffMetadataTravelsToWoff2AndIsReportedLostElsewhere() async throws {188        let scratch = try Scratch(); defer { scratch.cleanUp() }189        let font = try FontFile.open(SystemFont.cff.url).fonts[0]190        let xml = Data("<?xml version=\"1.0\" encoding=\"UTF-8\"?><metadata version=\"1.0\"><uniqueid id=\"test.orix\"/></metadata>".utf8)191        var woff = try WOFF.encode(font, metadata: xml)192        let info = try WOFF.decode(woff).info193        #expect(info.metadata == xml && info.metadataLength > 0)194        let source = try scratch.file("meta.woff", woff)195        let toOTF = try await convert(source, .woff, .otf, in: scratch)196        #expect(toOTF.collector.has("metadata-block-dropped"))197        let toWOFF2 = try await convert(source, .woff, .woff2, options: ["quality": .int(4)], in: scratch)198        #expect(!toWOFF2.collector.has("metadata-block-dropped"))199        #expect((WOFF2.info(toWOFF2.data)?.metadataLength ?? 0) > 0)200        expectAllPass(toWOFF2.checks, "metadata")201        let back = try await convert(toWOFF2.url, .woff2, .woff, in: scratch)202        #expect(back.collector.has("metadata-block-dropped"))203        // A private block is declared lost.204        let privateOffset = UInt32(woff.count)205        woff.append(contentsOf: [1, 2, 3, 4])206        woff.replaceSubrange(36..<44, with: Self.be32(privateOffset) + Self.be32(4))207        woff.replaceSubrange(8..<12, with: Self.be32(UInt32(woff.count)))208        let withPrivate = try scratch.file("private.woff", woff)209        #expect(FontEngine().preflight(.init(source: withPrivate, sourceFormat: .woff, destinationFormat: .otf), inspection: nil).contains { $0.code == "private-block-dropped" })210    }211}212213@Suite struct MeasurementTests {214    /// Not an assertion about speed: prints the numbers quoted in docs/compatibility/engines/font.md.215    @Test func measureSizesAndTimes() async throws {216        let scratch = try Scratch(); defer { scratch.cleanUp() }217        var lines: [String] = []218        for font in [SystemFont.glyf, .geneva, .cffText, .variable] {219            let source = try scratch.fixture(font)220            let size = try #require(try source.resourceValues(forKeys: [.fileSizeKey]).fileSize)221            let clock = ContinuousClock()222            var woff: Converted?, woff2: Converted?223            let woffTime = try await clock.measure { woff = try await convert(source, font.format, .woff, in: scratch) }224            let woff2Time = try await clock.measure { woff2 = try await convert(source, font.format, .woff2, in: scratch) }225            var back: Converted?226            let decodeTime = try await clock.measure { back = try await convert(try #require(woff2).url, .woff2, font.format, in: scratch) }227            _ = back228            lines.append("\(font.url.lastPathComponent): \(size) B → WOFF \(try #require(woff).data.count) B in \(woffTime), WOFF2 \(try #require(woff2).data.count) B in \(woff2Time), WOFF2→sfnt in \(decodeTime) (times include validation)")229        }230        print("FONT-PERF " + lines.joined(separator: " | "))231    }232}233
Files main
Menu

Theme