SPB Git

Repository Public License

EbookTests.swift 13.8 KB 177 lines swift

1// ORIX Convert — Packages/EngineDocuments/Tests/EngineDocumentsTests/EbookTests.swift2// Description: EPUB operations (chapter split, cover and media extraction), EPUB creation options, inspection and3//              DRM refusal (Adobe ADEPT, Apple FairPlay, generic content encryption; font obfuscation is accepted).4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Testing6import Foundation7@testable import EngineDocuments8import ConvertCore910@Suite("Ebooks")11struct EbookTests {12    /// An EPUB with a cover, made by the engine itself from the Markdown master.13    static func bookWithCover() async throws -> URL {14        let fx = try await FixtureStore.shared.get()15        let run = try await convert(fx.documents[.markdown]!, .markdown, .epub, options: [16            "epub.cover": .string(fx.pixel.path), "epub.title": .string("Chosen Title Ibis"), "epub.author": .string("Chosen Author"), "epub.language": .string("fr-CA"),17        ])18        #expect(run.failedChecks.isEmpty, "\(run.failedChecks)")19        return run.file20    }2122    @Test func creationOptionsAndInspection() async throws {23        let book = try await Self.bookWithCover()24        let package = try EPUBPackage(url: book)25        #expect(package.title == "Chosen Title Ibis" && package.creators == ["Chosen Author"] && package.language == "fr-CA")26        #expect(package.version?.hasPrefix("3") == true)27        #expect(package.cover != nil && package.protection == .none)28        // Table of contents built from the headings.29        for heading in Probe.headings { #expect(package.toc.contains { $0.title == heading }, "TOC has \(heading): \(package.toc.map(\.title))") }30        // The archive is a valid EPUB container: stored mimetype first.31        #expect(package.zip.entries.first?.name == "mimetype" && package.zip.entries.first?.method == 0)32        let i = try await Engines.ebooks.inspect(book, format: .epub)33        func field(_ key: String) -> String? { i.fields.first { $0.key == key }?.value }34        #expect(field("Title") == "Chosen Title Ibis" && field("Author") == "Chosen Author" && field("Language") == "fr-CA")35        #expect(field("EPUB version")?.hasPrefix("3") == true && field("Protection") == "None" && field("Cover image") != "None declared")36        #expect(Int(field("Spine items") ?? "") == package.spine.count && Int(field("Table of contents entries") ?? "") == package.toc.count)37        #expect(Int(field("Tables") ?? "") == 1 && (Int(field("Words") ?? "") ?? 0) > 30)38        #expect(i.isEncrypted != true && i.hasMetadata == true)39        // Without a TOC page and with other split level.40        let fx = try await FixtureStore.shared.get()41        let plain = try await convert(fx.documents[.docx]!, .docx, .epub, options: ["epub.toc": .bool(false), "epub.split.level": .int(2), "metadata.strip": .bool(true)])42        #expect(plain.failedChecks.isEmpty, "\(plain.failedChecks)")43        let stripped = try EPUBPackage(url: plain.file)44        #expect(stripped.creators.isEmpty && stripped.title == "master", "metadata stripped, file name as required title: \(stripped.title ?? "nil") \(stripped.creators)")45        // Split level 2: the second-level heading starts its own content document.46        let openings = stripped.spine.compactMap { item in (try? stripped.chapterHTML(item)).flatMap { HTMLScanner.scan($0).headings.first?.text } }47        #expect(openings.contains("Second Level Heading"), "first heading of each spine document: \(openings)")48        #expect(stripped.toc.isEmpty == false, "the navigation document is always present")49        await #expect(throws: ConversionError.self) { _ = try await convert(fx.documents[.markdown]!, .markdown, .epub, options: ["epub.cover": .string("/nonexistent/cover.png")]) }50        await #expect(throws: ConversionError.self) { _ = try await convert(fx.documents[.markdown]!, .markdown, .epub, options: ["epub.language": .string("not a tag!")]) }51    }5253    @Test func chapters() async throws {54        let book = try await Self.bookWithCover()55        let spine = try EPUBPackage(url: book).spine.count56        let html = try await convert(book, .epub, .folder, operation: Operations.epubChapters)57        #expect(html.failedChecks.isEmpty, "\(html.failedChecks)")58        let files = try FileManager.default.contentsOfDirectory(at: html.file, includingPropertiesForKeys: nil).sorted { $0.lastPathComponent < $1.lastPathComponent }59        #expect(files.count == spine && files.allSatisfy { $0.pathExtension == "html" })60        // Reading order: numbered, chapter one before chapter two; names come from the table of contents.61        let names = files.map(\.lastPathComponent)62        let one = try #require(names.firstIndex { $0.contains("Chapter One Heading") }), two = try #require(names.firstIndex { $0.contains("Chapter Two Heading") })63        #expect(one < two && names[one].hasPrefix(String(format: "%02d - ", one + 1)), "names: \(names)")64        let first = HTMLScanner.scan(try String(contentsOf: files[one], encoding: .utf8))65        #expect(first.text.contains("boldprobe") && first.tableCount == 1 && first.hasBold && first.links.contains(Probe.link))66        #expect(first.imageSources.contains { $0.hasPrefix("data:image/png") }, "images are embedded in HTML chapters")67        #expect(!first.text.contains("xylophone"), "chapter two's text is not in chapter one")68        // Measured progress: one step per spine item.69        #expect(html.recorder.progress.compactMap(\.fraction).count == spine && html.recorder.progress.last?.fraction == 1)7071        let md = try await convert(book, .epub, .folder, operation: Operations.epubChapters, options: ["chapters.format": .string("markdown")])72        #expect(md.failedChecks.isEmpty, "\(md.failedChecks)")73        let mdFiles = try FileManager.default.contentsOfDirectory(at: md.file, includingPropertiesForKeys: nil).filter { $0.pathExtension == "md" }.sorted { $0.lastPathComponent < $1.lastPathComponent }74        #expect(mdFiles.count == spine)75        let chapter = try String(contentsOf: try #require(mdFiles.first { $0.lastPathComponent.contains("Chapter One") }), encoding: .utf8)76        #expect(chapter.contains("# Chapter One Heading") && chapter.contains("**boldprobe**") && chapter.contains("Kumquat"))77        // The image file referenced by the Markdown chapter is really written into the folder.78        let reference = try #require(DocumentFacts.regexCaptures(#"!\[[^\]]*\]\(([^)\s]+)"#, chapter).first)79        let imageURL = md.file.appendingPathComponent(reference)80        #expect(Raster.imageSize(imageURL)?.width == 64, "referenced image \(reference) exists")81    }8283    @Test func coverAndMedia() async throws {84        let book = try await Self.bookWithCover()85        let fx = try await FixtureStore.shared.get()86        let png = try await convert(book, .epub, .png, operation: Operations.epubCover)87        #expect(png.failedChecks.isEmpty)88        let coverBytes = try Data(contentsOf: png.file), pixelBytes = try Data(contentsOf: fx.pixel)89        #expect(coverBytes == pixelBytes, "a PNG cover is copied byte for byte")90        let jpeg = try await convert(book, .epub, .jpeg, operation: Operations.epubCover)91        #expect(jpeg.failedChecks.isEmpty && jpeg.recorder.has("cover-reencoded"))92        let jpegHead = try Data(contentsOf: jpeg.file).prefix(3)93        #expect(jpegHead == Data([0xFF, 0xD8, 0xFF]) && Raster.imageSize(jpeg.file)?.width == 64)94        // A book without a cover: a clear error, not an empty file.95        await #expect(throws: ConversionError.self) { _ = try await convert(fx.documents[.epub]!, .epub, .png, operation: Operations.epubCover) }96        let media = try await convert(book, .epub, .folder, operation: Operations.mediaExtract)97        #expect(media.failedChecks.isEmpty, "\(media.failedChecks)")98        let files = try FileManager.default.contentsOfDirectory(at: media.file, includingPropertiesForKeys: nil)99        #expect(files.count == 2, "cover + inline picture: \(files.map(\.lastPathComponent))")100        for s in [FormatID.docx, .odt] {101            let m = try await convert(fx.documents[s]!, s, .folder, operation: Operations.mediaExtract)102            #expect(m.failedChecks.isEmpty, "\(m.failedChecks)")103            let extracted = try FileManager.default.contentsOfDirectory(at: m.file, includingPropertiesForKeys: nil)104            #expect(extracted.contains { (try? Data(contentsOf: $0)) == (try? Data(contentsOf: fx.pixel)) }, "the embedded PNG comes out byte for byte from \(s)")105        }106        // A document without any media: a clear error, announced by preflight (regression 2026-11-21: the message was107        // the bare "The file contains no embedded media." and preflight said nothing).108        do {109            _ = try await convert(fx.appleDocx, .docx, .folder, operation: Operations.mediaExtract)110            Issue.record("extracting media from a document without media must fail")111        } catch let ConversionError.unreadableInput(message) {112            #expect(message.hasPrefix("This document contains no embedded image, audio or video file to extract") && message.contains("0 media files found in the DOCX package"))113        }114        let emptyRequest = ConversionRequest(source: fx.appleDocx, sourceFormat: .docx, destinationFormat: .folder, operation: Operations.mediaExtract)115        let pre = Engines.all.first { $0.path(for: emptyRequest) != nil }?.preflight(emptyRequest, inspection: nil) ?? []116        #expect(pre.contains { $0.code == "no-media" && $0.message.contains("no embedded image, audio or video file") }, "\(pre)")117        let fullRequest = ConversionRequest(source: fx.documents[.docx]!, sourceFormat: .docx, destinationFormat: .folder, operation: Operations.mediaExtract)118        #expect(!(Engines.all.first { $0.path(for: fullRequest) != nil }?.preflight(fullRequest, inspection: nil) ?? []).contains { $0.code == "no-media" })119    }120121    static func protected(_ source: URL, name: String, parts: [String: String]) throws -> URL {122        let out = try Fixtures.newDirectory("drm").appendingPathComponent(name)123        try repackage(source, to: out, edits: parts.mapValues { Data($0.utf8) })124        return out125    }126127    static func encryptionXML(algorithm: String, uri: String) -> String {128        """129        <?xml version="1.0" encoding="UTF-8"?>130        <encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container" xmlns:enc="http://www.w3.org/2001/04/xmlenc#">131        <enc:EncryptedData><enc:EncryptionMethod Algorithm="\(algorithm)"/><enc:CipherData><enc:CipherReference URI="\(uri)"/></enc:CipherData></enc:EncryptedData>132        </encryption>133        """134    }135136    @Test func drmIsRefused() async throws {137        let fx = try await FixtureStore.shared.get()138        let book = fx.documents[.epub]!139        let adept = try Self.protected(book, name: "adept.epub", parts: [140            "META-INF/encryption.xml": Self.encryptionXML(algorithm: "http://www.w3.org/2001/04/xmlenc#aes128-cbc", uri: "EPUB/text/ch001.xhtml"),141            "META-INF/rights.xml": "<?xml version=\"1.0\"?><adept:rights xmlns:adept=\"http://ns.adobe.com/adept\"><licenseToken><encryptedKey>AAAA</encryptedKey></licenseToken></adept:rights>",142        ])143        let fairplay = try Self.protected(book, name: "fairplay.epub", parts: [144            "META-INF/encryption.xml": Self.encryptionXML(algorithm: "http://itunes.apple.com/dataenc", uri: "EPUB/text/ch001.xhtml"),145            "META-INF/sinf.xml": "<?xml version=\"1.0\"?><fairplay:sinf xmlns:fairplay=\"http://itunes.apple.com/ns/epub\"><fairplay:sID>1</fairplay:sID></fairplay:sinf>",146        ])147        let generic = try Self.protected(book, name: "generic.epub", parts: ["META-INF/encryption.xml": Self.encryptionXML(algorithm: "http://www.w3.org/2001/04/xmlenc#aes256-cbc", uri: "EPUB/text/ch001.xhtml")])148        for (url, kind) in [(adept, "Adobe ADEPT"), (fairplay, "Apple FairPlay"), (generic, "encrypted content")] {149            let protection = try EPUBPackage(url: url).protection150            #expect(protection != .none)151            for (d, op) in [(FormatID.html, nil as String?), (.pdf, nil), (.docx, nil), (.folder, Operations.epubChapters), (.folder, Operations.mediaExtract), (.png, Operations.epubCover)] {152                do {153                    _ = try await convert(url, .epub, d, operation: op)154                    Issue.record("\(url.lastPathComponent) → \(d) must be refused")155                } catch let error as ConversionError {156                    guard case .encryptedInput(let message) = error else { Issue.record("wrong error \(error)"); continue }157                    #expect(message.contains("DRM") && message.contains(kind), "message: \(message)")158                }159            }160            let i = try await Engines.ebooks.inspect(url, format: .epub)161            #expect(i.isEncrypted == true && i.fields.contains { $0.key == "Protection" && $0.value.contains(kind) })162            #expect(!i.fields.contains { $0.key == "Words" }, "the content of a protected book is not read")163            let warnings = Engines.ebooks.preflight(ConversionRequest(source: url, sourceFormat: .epub, destinationFormat: .html), inspection: nil)164            #expect(warnings.contains { $0.code == "drm-protected" })165        }166        // Font obfuscation alone is not DRM: the book converts, with a note about the fonts.167        let fonts = try Self.protected(book, name: "fonts.epub", parts: ["META-INF/encryption.xml": Self.encryptionXML(algorithm: "http://www.idpf.org/2008/embedding", uri: "EPUB/fonts/x.otf")])168        let fontProtection = try EPUBPackage(url: fonts).protection169        #expect(fontProtection == .fontObfuscation(count: 1))170        let run = try await convert(fonts, .epub, .markdown)171        let converted = try String(contentsOf: run.file, encoding: .utf8)172        #expect(run.failedChecks.isEmpty && converted.contains("boldprobe"))173        let warnings = Engines.ebooks.preflight(ConversionRequest(source: fonts, sourceFormat: .epub, destinationFormat: .markdown), inspection: nil)174        #expect(warnings.contains { $0.code == "fonts-not-carried" })175    }176}177
Files main
Menu

Theme