Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocFormatsTests/FacadeAndRobustnessTests.swift3// Description: The DocumentIO facade and preflight, Unicode torture across every format, run-boundary healing, and hostile4// inputs: 10 000-level nesting, huge and broken tables, invalid UTF-8, pathological delimiter runs.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Testing9import Foundation10import DocModel11import DocContainer12@testable import DocFormats1314@Suite struct DocumentIOTests {15 @Test func everyFormatGoesThroughTheFacade() throws {16 let source = DocumentModel(meta: DocumentMetadata(title: "Facade"), body: [17 .paragraph(Paragraph("Heading", style: StyleID.heading(1))),18 .paragraph(Paragraph(runs: [Run("Some "), Run("bold", props: CharProps(bold: true)), Run(" text.")])),19 ])20 #expect(DocumentIO.exportFormats == [.orixdoc, .markdown, .html, .rtf, .txt])21 #expect(DocumentIO.importFormats == [.orixdoc, .markdown, .html, .rtf, .txt, .pdf])22 for format in DocumentIO.exportFormats {23 let exported = try DocumentIO.export(source, format: format)24 #expect(!exported.data.isEmpty)25 let back = try DocumentIO.importDocument(exported.data, format: format)26 #expect(Describe.texts(back.document) == ["Heading", "Some bold text."], "\(format)")27 if format != .txt {28 let bold = back.document.body[1].paragraph?.runs.first { $0.text == "bold" }29 #expect(bold.map { ModelIdiom.isBold(ModelIdiom.explicitChar($0, styles: back.document.styles)) } == true, "\(format)")30 }31 if format == .orixdoc { #expect(back.document == source) }32 if format == .markdown || format == .html { #expect(back.document.body[0].paragraph?.style == StyleID.heading(1)) }33 }34 let pdf = try DocumentIO.importDocument(Fixture.pdf(pages: [["Hello from a PDF."]]), format: .pdf)35 #expect(Describe.texts(pdf.document) == ["Hello from a PDF."])36 // Formats that live elsewhere are refused with a message that says where.37 #expect(throws: FormatError.unreadable("DOCX documents are read by the DocDOCX module")) { _ = try DocumentIO.importDocument(Data(), format: .docx) }38 #expect(throws: FormatError.unreadable("DOCX documents are written by the DocDOCX module")) { _ = try DocumentIO.export(source, format: .docx) }39 #expect(throws: FormatError.self) { _ = try DocumentIO.export(source, format: .pdf) }40 // Errors surface as errors here (the per-format importDocument entry points turn them into warnings).41 #expect(throws: FormatError.self) { _ = try DocumentIO.importDocument(Data("x".utf8), format: .orixdoc) }42 #expect(throws: FormatError.self) { _ = try DocumentIO.importDocument(Data("x".utf8), format: .rtf) }43 #expect(throws: FormatError.self) { _ = try DocumentIO.importDocument(Data("x".utf8), format: .pdf) }44 #expect(throws: FormatError.self) { _ = try DocumentIO.importDocument(Data(count: FormatLimits.maxTextBytes + 1), format: .markdown) }45 #expect(throws: FormatError.self) { _ = try DocumentIO.importDocument(Data(count: FormatLimits.maxTextBytes + 1), format: .html) }46 }4748 @Test func textFormatsAreDecodedFromAnyEncoding() throws {49 let latin1 = Data([0x23, 0x20, 0x43, 0x61, 0x66, 0xE9, 0x0A, 0x0A, 0x74, 0xEA, 0x74, 0x65]) // "# Café\n\ntête" in Windows-125250 let md = try DocumentIO.importDocument(latin1, format: .markdown)51 #expect(Describe.blocks(md.document) == ["Heading1: Café", "Normal: tête"])52 #expect(md.warnings.first?.feature == "Text encoding")53 var utf16 = Data([0xFF, 0xFE])54 for u in "<p>Zoë — 中文</p>".utf16 { utf16.append(UInt8(u & 0xFF)); utf16.append(UInt8(u >> 8)) }55 let html = try DocumentIO.importDocument(utf16, format: .html)56 #expect(Describe.blocks(html.document) == ["Normal: Zoë — 中文"])57 #expect(html.warnings.isEmpty)58 }5960 @Test func resourceLoaderIsPassedThrough() throws {61 var asked: [String] = []62 let loader: (String) -> Data? = { asked.append($0); return Fixture.png() }63 let md = try DocumentIO.importDocument(Data("".utf8), format: .markdown, resourceLoader: loader)64 let html = try DocumentIO.importDocument(Data("<img src='two.png' alt='b'><img src='https://x/y.png'>".utf8), format: .html, resourceLoader: loader)65 #expect(asked == ["one.png", "two.png"])66 #expect(md.document.assets.count == 1 && html.document.assets.count == 1)67 }6869 @Test func preflightSaysWhatWouldBeLostBeforeExporting() throws {70 let rich = Fixture.rich()71 #expect(DocumentIO.preflight(rich, format: .orixdoc).isEmpty)72 #expect(DocumentIO.preflight(rich, format: .docx).map(\.severity) == [.info])73 #expect(DocumentIO.preflight(rich, format: .pdf).map(\.severity) == [.info])74 for format in [DocFormat.markdown, .html, .rtf, .txt] {75 let predicted = DocumentIO.preflight(rich, format: format)76 let actual = try DocumentIO.export(rich, format: format).warnings77 // Everything preflight announces is reported by the export, with the same wording; the export may add what it only78 // discovers while writing (a font that is not installed, a title approximated as a heading).79 for warning in predicted { #expect(actual.contains(warning), "\(format): \(warning.feature) predicted but not reported") }80 let extra = Set(actual.map(\.feature)).subtracting(predicted.map(\.feature))81 #expect(extra.isSubset(of: ["Fonts", "Title", "Heading numbers", "Headings", "Tables", "Encoding", "Missing images", "Images"]), "\(format): \(extra)")82 #expect(predicted.contains { $0.feature == "Comments" && $0.severity == .dropped })83 #expect(predicted.contains { $0.feature == "Tracked changes" && $0.detail.contains("4 occurrences") })84 }85 // A plain document loses nothing worth a warning beyond the informational notes.86 let plain = DocumentModel(body: [.paragraph(Paragraph("Just text."))])87 #expect(DocumentIO.preflight(plain, format: .txt).isEmpty)88 #expect(DocumentIO.preflight(plain, format: .markdown).map(\.feature) == ["Page setup"])89 #expect(DocumentIO.preflight(plain, format: .html).map(\.feature) == ["Page setup"])90 #expect(DocumentIO.preflight(plain, format: .rtf).map(\.feature) == ["Styles"])91 // Options change the report: a formatter and a MathML converter remove the corresponding approximations.92 var options = ExportOptions()93 options.citations = StubCitations()94 options.mathML = { _, _ in "<math/>" }95 options.acceptChanges = false96 let withOptions = DocumentIO.preflight(rich, format: .html, options: options)97 #expect(!withOptions.contains { $0.feature == "Equations" })98 #expect(!withOptions.contains { $0.feature == "Bibliography" })99 #expect(withOptions.first { $0.feature == "Citations" }?.detail.contains("formatted text") == true)100 #expect(withOptions.first { $0.feature == "Tracked changes" }?.detail.contains("rejected") == true)101 // Severities sort the way the user reads them.102 #expect(FormatWarning.Severity.info < .approximated && FormatWarning.Severity.approximated < .dropped)103 }104105 @Test func changeResolution() {106 let rich = Fixture.rich()107 #expect(ChangeResolver.hasChanges(rich))108 let accepted = ChangeResolver.resolved(rich, accept: true), rejected = ChangeResolver.resolved(rich, accept: false)109 #expect(!ChangeResolver.hasChanges(accepted) && !ChangeResolver.hasChanges(rejected))110 let a = accepted.body[3].paragraph!.visibleText, r = rejected.body[3].paragraph!.visibleText111 #expect(a.contains(" inserted") && !a.contains(" deleted"))112 #expect(r.contains(" deleted") && !r.contains(" inserted"))113 #expect(rejected.body[3].paragraph?.runs[0].props.bold == true && rejected.body[3].paragraph?.runs[0].charStyle == StyleID.strong) // the formatting "Plain " had before the tracked change114 #expect(accepted.body[3].paragraph?.runs[0].props.bold == nil)115 // A document without changes is returned untouched.116 let plain = DocumentModel(body: [.paragraph(Paragraph(id: "p", runs: [Run("x")]))])117 #expect(ChangeResolver.resolved(plain, accept: true) == plain)118 // Changes inside table cells and notes are resolved too.119 var cell = TableCell(paragraphs: [Paragraph(runs: [Run("keep"), Run(" gone", revision: Revision(kind: .deletion, author: "a"))])])120 cell.paragraphs[0].markRevision = Revision(kind: .deletion, author: "a")121 cell.paragraphs.append(Paragraph(runs: [Run(" joined")]))122 var doc = DocumentModel(body: [.table(Table(columnWidths: [100], rows: [TableRow(cells: [cell])]))])123 doc.notes["n"] = Note(id: "n", kind: .footnote, paragraphs: [Paragraph(runs: [Run("added", revision: Revision(kind: .insertion, author: "a"))])])124 let resolved = ChangeResolver.resolved(doc, accept: true)125 #expect(resolved.body[0].table?.rows[0].cells[0].paragraphs.map(\.visibleText) == ["keep joined"])126 #expect(ChangeResolver.resolved(doc, accept: false).notes["n"]?.paragraphs.map(\.plainText) == [""])127 }128}129130@Suite struct UnicodeTortureTests {131 static let samples: [String] = [132 "👩👩👧👦 family, 🏳️🌈 flag, 👍🏽 skin tone, 🇨🇦🇫🇷 flags, 1️⃣ keycap",133 "e\u{301}a\u{300}o\u{302}\u{303} combining, Z\u{351}\u{36B}\u{343}a\u{363}\u{326}l\u{34E}g\u{340}o\u{36F} zalgo",134 "العربية: مرحبا بالعالم — مع أرقام ١٢٣ و English mixed",135 "עברית: שלום עולם, with (parentheses) and \"quotes\"",136 "中文,日本語のテキスト、한국어 텍스트。全角ABC",137 "𝒳𝓎𝔷 𝟘𝟙𝟚 𠀀𠀁 astral plane \u{1F9D1}\u{200D}\u{1F680}",138 "\u{200B}zero\u{200B}width\u{FEFF} joiners\u{200D}\u{200C} and \u{00A0}nbsp\u{202F}narrow",139 "\u{202E}RTL override\u{202C} and \u{2066}isolates\u{2069}",140 "fi ligature, dž digraph, ß sharp s, İ dotted I, ς final sigma",141 ]142143 private func document() -> DocumentModel {144 var blocks: [Block] = UnicodeTortureTests.samples.map { .paragraph(Paragraph($0)) }145 // Formatting boundaries placed right after base characters, so the following combining mark / ZWJ lands at a run boundary.146 blocks.append(.paragraph(Paragraph(runs: [Run("a"), Run("\u{301}b", props: CharProps(bold: true)), Run("👩"), Run("\u{200D}👧 end", props: CharProps(italic: true))])))147 blocks.append(.paragraph(Paragraph(runs: [Run("x😀", props: CharProps(bold: true)), Run("😀y"), Run("🇨"), Run("🇦", props: CharProps(bold: true))])))148 return DocumentModel(body: blocks)149 }150151 @Test(arguments: [DocFormat.orixdoc, .markdown, .html, .rtf, .txt])152 func textSurvivesEveryFormat(format: DocFormat) throws {153 let source = document()154 let exported = try DocumentIO.export(source, format: format)155 let back = try DocumentIO.importDocument(exported.data, format: format).document156 var texts = Describe.texts(back)157 var samples = UnicodeTortureTests.samples158 var sourceTexts = Describe.texts(source)159 if format == .rtf {160 // The system RTF codec writes precomposed characters where Windows-1252 has them and drops the bidirectional embedding161 // controls (U+202A...U+202E): the text is canonically equivalent, not identical. Compared under NFC, without those controls.162 func normalised(_ s: String) -> String { String(s.precomposedStringWithCanonicalMapping.unicodeScalars.filter { !(0x202A...0x202E).contains($0.value) }.map(Character.init)) }163 texts = texts.map(normalised); samples = samples.map(normalised); sourceTexts = sourceTexts.map(normalised)164 }165 #expect(Array(texts.prefix(UnicodeTortureTests.samples.count)) == samples)166 #expect(texts[UnicodeTortureTests.samples.count] == (format == .rtf ? "\u{E1}b👩\u{200D}👧 end" : "a\u{301}b👩\u{200D}👧 end"))167 #expect(texts[UnicodeTortureTests.samples.count + 1] == "x😀😀y🇨🇦")168 // UTF-16 lengths are what the model counts in; they must be identical after the trip.169 #expect(texts.map(\.utf16.count) == sourceTexts.map(\.utf16.count))170 // No importer leaves a run boundary inside a composed character sequence.171 if format != .orixdoc {172 DocumentModel.forEachParagraph(in: back.body) { p, _ in173 let full = p.plainText as NSString174 var offset = 0175 for run in p.runs.dropLast() {176 offset += run.length177 #expect(full.rangeOfComposedCharacterSequence(at: offset).location == offset, "\(format): run boundary inside a character in \(p.plainText)")178 }179 }180 }181 }182183 @Test func runBoundariesAreHealed() {184 var p = Paragraph(runs: [Run("a"), Run("\u{301}b", props: CharProps(bold: true)), Run("👩"), Run("\u{200D}👧", props: CharProps(italic: true)), Run("\u{200D}👦 z"),185 Run("🇨"), Run("🇦", props: CharProps(bold: true)), Run(inline: .bookmark("k")), Run("\u{301}", props: CharProps(bold: true))])186 let before = p.plainText187 p.healRunBoundaries()188 #expect(p.plainText == before) // nothing added, removed or reordered189 #expect(p.runs.map(\.text) == ["a\u{301}", "b", "👩\u{200D}👧\u{200D}👦", " z🇨🇦", "\u{FFFC}", "\u{301}"])190 #expect(p.runs[1].props.bold == true && p.runs[0].props.bold == nil)191 let full = p.plainText as NSString192 var offset = 0193 for run in p.runs.dropLast(2) { offset += run.length; #expect(full.rangeOfComposedCharacterSequence(at: offset).location == offset) }194 // A string with lone surrogates never traps and never splits a pair.195 var odd = Paragraph(runs: [Run(String(decoding: [0xD83D, 0x0041, 0xDE00] as [UInt16], as: UTF16.self)), Run("😀", props: CharProps(bold: true))])196 odd.healRunBoundaries()197 #expect(odd.runs.count == 2 && odd.runs[1].text == "😀")198 }199200 @Test func reservedCharactersNeverEnterTheModel() throws {201 // U+FFFC stands for inline objects: text from outside must never contain it.202 let hostile = "a\u{FFFC}b\u{0}c\u{1}d"203 for (format, data) in [(DocFormat.markdown, Data(hostile.utf8)), (.html, Data("<p>\(hostile)�</p>".utf8)), (.txt, Data(hostile.utf8))] {204 let doc = try DocumentIO.importDocument(data, format: format).document205 DocumentModel.forEachParagraph(in: doc.body) { p, _ in206 for run in p.runs where run.inline == nil { #expect(!run.text.contains("\u{FFFC}") && !run.text.contains("\u{0}"), "\(format)") }207 }208 #expect(Describe.texts(doc).joined().hasPrefix("a\u{FFFD}bcd"), "\(format)")209 }210 #expect(TextSanitizer.clean("a\r\nb\rc\nd\u{7F}\u{1B}[0m") == "a\u{2028}b\u{2028}c\u{2028}d[0m")211 #expect(TextSanitizer.clean("untouched ✓") == "untouched ✓")212 }213214 @Test func invalidUTF8IsRepairedOrReinterpretedNeverFatal() throws {215 let cases: [[UInt8]] = [216 [0x61, 0xFF, 0x62], [0xC3], [0xE2, 0x82], [0xF0, 0x9F, 0x98], [0xC0, 0x80], [0xED, 0xA0, 0x80], [0xF8, 0x88, 0x80, 0x80, 0x80], [0x80, 0x81, 0x82],217 Array("Valid: é ü ö — then broken ".utf8) + [0xE2, 0x28, 0xA1] + Array(" and more é".utf8),218 ]219 for bytes in cases {220 for format in [DocFormat.txt, .markdown, .html] {221 let result = try DocumentIO.importDocument(Data(bytes), format: format)222 #expect(result.warnings.contains { $0.feature == "Text encoding" }, "\(bytes) \(format)")223 DocumentModel.forEachParagraph(in: result.document.body) { p, _ in #expect(p.plainText.utf16.count == p.length) }224 }225 }226 // Overlong and surrogate encodings do not smuggle characters in: the slash below never appears.227 let overlong = try DocumentIO.importDocument(Data([0x2E, 0x2E, 0xC0, 0xAF, 0x65, 0x74, 0x63]), format: .txt)228 #expect(!Describe.texts(overlong.document).joined().contains("/"))229 }230}231232@Suite struct HostileInputTests {233 @Test(.timeLimit(.minutes(2))) func tenThousandNestedQuotesAndLists() {234 // Block quotes 10 000 deep on one line, and across lines.235 let deepQuote = String(repeating: "> ", count: 10_000) + "bottom"236 let quote = MarkdownImporter.importDocument(deepQuote)237 #expect(Describe.texts(quote.document).joined().hasSuffix("bottom"))238 #expect(quote.document.body.count == 1)239 #expect(quote.warnings.contains { $0.feature == "Deep nesting" })240 var stair = ""241 for depth in 1...2_000 { stair += String(repeating: ">", count: depth) + " line \(depth)\n" }242 #expect(Describe.texts(MarkdownImporter.importDocument(stair).document).joined(separator: " ").contains("line 2000"))243 // Lists nested 10 000 levels (one space of extra indent per level is the least Markdown accepts after a one-character marker… here two).244 var list = ""245 for depth in 0..<1_000 { list += String(repeating: " ", count: depth) + "- item \(depth)\n" }246 let nested = MarkdownImporter.importDocument(list)247 #expect(Describe.texts(nested.document).joined(separator: " ").contains("item 999"))248 #expect(nested.document.body.compactMap { $0.paragraph?.list?.level }.max() == FormatLimits.maxListLevel)249 // The same in HTML, where depth costs nothing to write: 10 000 nested lists, quotes, divs and inline elements.250 for (open, close) in [("<ul><li>", "</li></ul>"), ("<blockquote>", "</blockquote>"), ("<div>", "</div>"), ("<b><i>", "</i></b>"), ("<table><tr><td>", "</td></tr></table>")] {251 let html = String(repeating: open, count: 10_000) + "bottom" + String(repeating: close, count: 10_000) + "<p>after</p>"252 let result = HTMLImporter.importDocument(html)253 let text = Describe.texts(result.document).joined(separator: " ")254 #expect(text.contains("bottom"), "\(open)")255 #expect(text.hasSuffix("after"), "\(open)")256 #expect(result.warnings.contains { $0.feature == "Deep nesting" }, "\(open)")257 }258 // Never closed at all.259 let unclosed = HTMLImporter.importDocument(String(repeating: "<div><span><ul><li>", count: 10_000) + "bottom")260 #expect(Describe.texts(unclosed.document).joined().contains("bottom"))261 }262263 @Test(.timeLimit(.minutes(2))) func pathologicalInlineInputIsLinearEnough() {264 // Thousands of unmatched delimiters, brackets, backticks and entities: classic quadratic traps.265 let n = 20_000266 let inputs = [267 String(repeating: "*a ", count: n), String(repeating: "a* ", count: n), String(repeating: "_a_*b* ", count: n / 4),268 String(repeating: "[", count: n) + "x" + String(repeating: "]", count: n), String(repeating: "[a](", count: n),269 String(repeating: "`", count: n) + "x", String(repeating: "` ", count: n), String(repeating: "&", count: n),270 String(repeating: "<", count: n), String(repeating: "![", count: n), String(repeating: "$x ", count: n), String(repeating: "~x^y ", count: n),271 String(repeating: "**", count: n) + "x", String(repeating: "\\", count: n), String(repeating: "[^1]", count: n),272 ]273 for input in inputs {274 let result = MarkdownImporter.importDocument(input)275 #expect(!Describe.texts(result.document).isEmpty)276 // Whatever the markup means, no visible character is lost except the markup itself.277 let letters = input.filter { $0.isLetter }.count278 #expect(Describe.texts(result.document).joined().filter { $0.isLetter }.count >= letters * 9 / 10, "\(input.prefix(12))")279 }280 let html = [String(repeating: "<", count: n), String(repeating: "<a href='x'>", count: n) + "t", String(repeating: "&", count: n), String(repeating: "<!--", count: n),281 String(repeating: "<p", count: n), String(repeating: "<b>x</i>", count: n), String(repeating: "</p>", count: n), "<p " + String(repeating: "a=b ", count: n) + ">x"]282 for input in html { #expect(!Describe.blocks(HTMLImporter.importDocument(input).document).isEmpty) }283 }284285 @Test(.timeLimit(.minutes(2))) func hugeTables() throws {286 // 500 × 40 table through Markdown and HTML, and back out.287 let rows = 500, columns = 40288 var md = "|" + (0..<columns).map { " h\($0) " }.joined(separator: "|") + "|\n|" + String(repeating: "---|", count: columns) + "\n"289 var html = "<table>"290 for r in 0..<rows {291 md += "|" + (0..<columns).map { " r\(r)c\($0) " }.joined(separator: "|") + "|\n"292 html += "<tr>" + (0..<columns).map { "<td>r\(r)c\($0)</td>" }.joined() + "</tr>"293 }294 html += "</table>"295 let fromMarkdown = try #require(MarkdownImporter.importDocument(md).document.body.first?.table)296 #expect(fromMarkdown.rows.count == rows + 1 && fromMarkdown.columnCount == columns)297 #expect(fromMarkdown.rows[rows].cells[columns - 1].plainText == "r\(rows - 1)c\(columns - 1)")298 let fromHTML = try #require(HTMLImporter.importDocument(html).document.body.first?.table)299 #expect(fromHTML.rows.count == rows && fromHTML.columnCount == columns)300 #expect(fromHTML.rows[rows - 1].cells[columns - 1].plainText == "r\(rows - 1)c\(columns - 1)")301 let doc = DocumentModel(body: [.table(fromHTML)])302 for format in [DocFormat.markdown, .html, .orixdoc] {303 let back = try DocumentIO.importDocument(try DocumentIO.export(doc, format: format).data, format: format).document304 #expect(Describe.texts(back).filter { !$0.isEmpty }.count == rows * columns, "\(format)")305 }306 // Plain text: one line per row, cells separated by tabs.307 let lines = String(decoding: try DocumentIO.export(doc, format: .txt).data, as: UTF8.self).components(separatedBy: "\n")308 #expect(lines.count == rows && lines.allSatisfy { $0.components(separatedBy: "\t").count == columns })309 #expect(lines[rows - 1].hasSuffix("\tr\(rows - 1)c\(columns - 1)"))310 // Too many columns: a Markdown row that wide is not a table, an HTML one is cut at the limit with a warning.311 let wideRow = (0..<1000).map { "c\($0)" }.joined(separator: " | ")312 let wideMD = MarkdownImporter.importDocument(wideRow + "\n" + String(repeating: "--- | ", count: 999) + "---\n")313 #expect(wideMD.document.body.first?.table == nil)314 let wideHTML = HTMLImporter.importDocument("<table><tr>" + String(repeating: "<td>x</td>", count: 5000) + "</tr></table>")315 #expect(wideHTML.document.body.first?.table?.columnCount == FormatLimits.maxTableColumns)316 #expect(wideHTML.warnings.contains { $0.feature == "Large tables" && $0.severity == .dropped })317 // Row spans in the tens of thousands do not allocate a grid of that height.318 let tall = HTMLImporter.importDocument("<table><tr><td rowspan=65000>a</td><td>b</td></tr><tr><td>c</td></tr></table>")319 #expect(tall.document.body.first?.table?.rows.count == 2)320 }321322 @Test func brokenMarkdownTables() {323 func blocks(_ md: String) -> [String] { Describe.blocks(MarkdownImporter.importDocument(md).document) }324 #expect(blocks("| a | b |\n|---|\n| 1 | 2 |") == ["Normal: | a | b | |---| | 1 | 2 |"]) // delimiter row with the wrong number of columns325 #expect(blocks("| a | b |\n|---|---|") == ["TABLE 2 cols", " H TableText: a | TableText: b"]) // header only326 #expect(blocks("|||\n|-|-|\n|||") == ["TABLE 2 cols", " H TableText: | TableText: ", " R TableText: | TableText: "])327 #expect(blocks("| a |\n| - |\n| `x | y` | z |").last == " R TableText: `x") // pipes are not protected by code spans: documented GFM behaviour328 #expect(blocks("a|b\n-|-\n\\|\\||c") == ["TABLE 2 cols", " H TableText: a | TableText: b", " R TableText: || | TableText: c"])329 }330331 @Test func malformedContainersNeverTrap() throws {332 let good = try NativeContainer.write(Fixture.rich())333 // Flip every 97th byte in turn, and truncate at a spread of lengths: each result either reads to a document or throws a FormatError.334 var outcomes = (read: 0, failed: 0, recovered: 0)335 for offset in stride(from: 0, to: good.count, by: 97) {336 var damaged = good337 damaged[offset] ^= 0xA5338 do { _ = try NativeContainer.read(damaged); outcomes.read += 1 } catch is FormatError { outcomes.failed += 1 } catch { Issue.record("unexpected error type \(error)") }339 if (try? NativeContainer.recover(damaged)) != nil { outcomes.recovered += 1 }340 _ = NativeContainer.validate(damaged)341 }342 for length in stride(from: 0, to: good.count, by: 211) {343 let cut = Data(good.prefix(length))344 #expect(throws: FormatError.self) { _ = try NativeContainer.read(cut) }345 _ = try? NativeContainer.recover(cut)346 }347 #expect(outcomes.failed > 0)348 #expect(outcomes.recovered >= outcomes.read) // recovery is never worse than a strict read349 // Random garbage of every small size.350 var generator = SystemRandomNumberGenerator()351 for size in 0..<300 {352 let noise = Data((0..<size).map { _ in UInt8.random(in: 0...255, using: &generator) })353 #expect(throws: FormatError.self) { _ = try NativeContainer.read(noise) }354 _ = try? NativeContainer.recover(noise)355 _ = RTFImporter.importDocument(noise)356 _ = PDFTextImporter.importDocument(noise)357 _ = PlainText.importDocument(noise)358 _ = HTMLImporter.importDocument(String(decoding: noise, as: UTF8.self))359 _ = MarkdownImporter.importDocument(String(decoding: noise, as: UTF8.self))360 }361 }362363 @Test func fuzzedMarkupNeverTrapsAndKeepsModelInvariants() {364 // Random sequences of markup fragments: the result must always be a well-formed model.365 let fragments = ["<p>", "</p>", "<b>", "</b>", "<i>", "<table>", "<tr>", "<td>", "<td colspan=3 rowspan=2>", "</td>", "</tr>", "</table>", "<ul>", "<ol>", "<li>", "</li>", "</ul>",366 "<blockquote>", "</blockquote>", "<pre>", "</pre>", "<br>", "<hr>", "<a href='#x'>", "</a>", "<span class='math' data-latex='x'>", "</span>", "<img src='data:,'>",367 "<a class='footnote-ref' href='#n'>", "<section class='footnotes'>", "<li id='n'>", "</section>", "<h2 id='x'>", "</h2>", "<caption>", "<math>", "</math>", "<title>",368 "text ", "&", "\n", "*", "_", "`", "[", "]", "(", ")", "#", "- ", "1. ", "> ", "|", "---", "$", "$$", "~~", "[^1]", "[^1]: ", " ", "```", "![", "@k", "\\", "<!--", "-->"]369 var generator = SystemRandomNumberGenerator()370 for _ in 0..<1500 {371 var source = ""372 for _ in 0..<Int.random(in: 1...60, using: &generator) { source += fragments.randomElement(using: &generator)! }373 for doc in [HTMLImporter.importDocument(source).document, MarkdownImporter.importDocument(source).document] {374 #expect(!doc.body.isEmpty)375 let listIDs = Set(doc.lists.map(\.id))376 DocumentModel.forEachParagraph(in: doc.body) { p, _ in377 for run in p.runs {378 #expect((run.inline != nil) == (run.text == "\u{FFFC}"), "inline/text mismatch in: \(source)")379 if case .note(let ref)? = run.inline { #expect(doc.notes[ref.id] != nil, "dangling note in: \(source.debugDescription)") }380 if case .image(let image)? = run.inline { #expect(doc.assets[image.asset] != nil) }381 }382 #expect(!p.runs.contains { if case .note(let ref)? = $0.inline { return ref.id.hasPrefix(pendingNotePrefix) } else { return false } })383 if let item = p.list { #expect(listIDs.contains(item.listID) && (0...FormatLimits.maxListLevel).contains(item.level), "bad list in: \(source)") }384 }385 for block in doc.body {386 guard case .table(let table) = block else { continue }387 #expect(table.columnCount > 0 && !table.rows.isEmpty)388 for row in table.rows {389 #expect(row.cells.reduce(0) { $0 + $1.colSpan } == table.columnCount, "ragged row in: \(source)")390 #expect(row.cells.allSatisfy { !$0.paragraphs.isEmpty })391 }392 }393 // Notes only reference notes that exist, and no temporary reference survives anywhere.394 for note in doc.notes.values {395 for p in note.paragraphs { for run in p.runs { if case .note(let ref)? = run.inline { #expect(doc.notes[ref.id] != nil, "dangling note in a note: \(source.debugDescription)") } } }396 }397 // And the result can always be written and read back natively.398 #expect((try? NativeContainer.read(NativeContainer.write(doc)).document) == doc)399 }400 }401 }402}403