Repository Public License
1// ORIX Secure2// File: Packages/ORIXReporting/Tests/ORIXReportingTests/TextFormatTests.swift3// Description: Tests of the Markdown, plain-text and JSON renderers, of file export and of redaction.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXSecureModel9import Testing10@testable import ORIXReporting1112@Suite("Markdown")13struct MarkdownTests {14 let generator = ReportGenerator()1516 @Test func containsEveryFieldOfEveryFinding() {17 let document = Fixtures.rich18 let output = generator.markdown(document)19 #expect(output.hasPrefix("# \(Markdown.inline(document.title))\n"))20 #expect(output.contains(Markdown.inline(ReportStrings.date(document.createdAt))))21 #expect(output.contains(Markdown.inline(document.generator)))22 #expect(output.contains(Markdown.inline(document.kind.title)))23 for paragraph in Markdown.paragraphs(document.summary) { #expect(output.contains(paragraph)) }24 for section in document.sections {25 #expect(output.contains("## \(Markdown.inline(section.title))\n"))26 for paragraph in section.paragraphs.flatMap(Markdown.paragraphs) { #expect(output.contains(paragraph)) }27 for fact in section.facts { #expect(output.contains("| \(Markdown.cell(fact.label)) | \(Markdown.cell(fact.value)) |")) }28 for bullet in section.bullets { #expect(output.contains("- \(Markdown.inline(bullet))")) }29 }30 #expect(document.allFindings.count == AssessmentState.allCases.count + 3)31 for finding in document.allFindings {32 #expect(output.contains("### \(Markdown.inline(finding.title))\n"))33 #expect(output.contains("- **\(ReportStrings.state):** \(Markdown.inline(finding.state.title))"))34 #expect(output.contains("- **\(ReportStrings.severity):** \(Markdown.inline(finding.severity.title))"))35 #expect(output.contains("- **\(ReportStrings.confidence):** \(Markdown.inline(finding.confidence.title))"))36 #expect(output.contains("- **\(ReportStrings.verification):** \(Markdown.inline(finding.verification.title))"))37 #expect(output.contains("- **\(ReportStrings.assessed):** \(Markdown.inline(ReportStrings.date(finding.assessedAt)))"))38 for evidence in finding.evidence {39 #expect(output.contains("| \(Markdown.cell(evidence.label)) | \(Markdown.cell(evidence.value)) | \(Markdown.cell(evidence.source ?? "")) |"))40 }41 for paragraph in Markdown.paragraphs(finding.explanation) { #expect(output.contains(paragraph)) }42 for paragraph in Markdown.paragraphs(finding.recommendation ?? "") { #expect(output.contains(paragraph)) }43 for item in finding.notInspected { #expect(output.contains("- \(Markdown.inline(item))")) }44 }45 #expect(output.contains("## \(ReportStrings.unavailableTitle)"))46 for item in document.unavailable { #expect(output.contains("- \(Markdown.inline(item))")) }47 #expect(output.contains("*\(Markdown.inline(ReportGenerator.closingStatement))*"))48 #expect(output.contains(ReportStrings.noEvidence))49 }5051 @Test func escapesHostileUserData() {52 let output = generator.markdown(Fixtures.rich)53 #expect(!output.contains("<script>"))54 #expect(!output.contains("<b>"))55 #expect(!output.contains("<img"))56 #expect(output.contains("<script>alert(1)</script>"))57 #expect(output.contains("a\\|b"))58 #expect(output.contains("second \\`line\\` \\*bold\\* \\[link\\](x) # not a heading"))59 // The only HTML tag ever produced is the line break inside table cells.60 let tags = output.matches(of: /<[A-Za-z\/][^>]*>/).map { String($0.output) }61 #expect(tags.allSatisfy { $0 == "<br>" })62 #expect(tags.contains("<br>"))63 // A recommendation that starts with list or heading markers stays text.64 #expect(output.contains("\n\\- do not open it\n"))65 #expect(output.contains("\n1\\. report it\n"))66 #expect(output.contains("\n\\# and stay calm\n"))67 }6869 @Test func tablesStayWellFormed() {70 let output = generator.markdown(Fixtures.rich)71 var rows = 072 for line in output.split(separator: "\n") where line.hasPrefix("|") {73 rows += 174 // Unescaped pipes only: a fact row has 3, an evidence row 4.75 var unescaped = 076 var previous: Character = " "77 for character in line {78 if character == "|", previous != "\\" { unescaped += 1 }79 previous = character == "\\" && previous == "\\" ? " " : character80 }81 #expect(unescaped == 3 || unescaped == 4, "\(line)")82 #expect(line.hasSuffix("|"))83 }84 #expect(rows > 20)85 }8687 @Test func headingHierarchyAndStableOutput() {88 let document = Fixtures.rich89 let output = generator.markdown(document)90 let lines = output.split(separator: "\n", omittingEmptySubsequences: false)91 #expect(lines.filter { $0.hasPrefix("# ") }.count == 1)92 #expect(lines.first?.hasPrefix("# ") == true)93 var seenSection = false94 for line in lines {95 if line.hasPrefix("## ") { seenSection = true }96 if line.hasPrefix("### ") { #expect(seenSection) }97 #expect(!line.hasPrefix("#### "))98 }99 let headings = lines.filter { $0.hasPrefix("## ") }.map(String.init)100 #expect(headings == ["## Summary", "## Device overview", "## Empty section", "## Liens et confidentialité", "## Unicode", "## \(ReportStrings.unavailableTitle)"])101 #expect(output == generator.markdown(document))102 }103104 @Test func emptyDocumentStillHasFrameAndClosing() {105 let output = generator.markdown(Fixtures.empty)106 #expect(output.contains(ReportStrings.unavailableNone))107 #expect(output.contains(ReportGenerator.closingStatement))108 }109}110111@Suite("Plain text")112struct PlainTextTests {113 let generator = ReportGenerator()114115 @Test func containsEveryFieldOfEveryFinding() {116 let document = Fixtures.rich117 let output = generator.plainText(document).squashed118 func has(_ text: String, _ comment: Comment? = nil) { #expect(output.contains(text.squashed), comment) }119 has(document.title)120 has(ReportStrings.date(document.createdAt))121 has(document.generator)122 has(document.summary)123 for section in document.sections {124 has(section.title.uppercased())125 for paragraph in section.paragraphs { has(paragraph) }126 for fact in section.facts { has("\(fact.label): \(fact.value)") }127 for bullet in section.bullets { has("* \(bullet)") }128 }129 for finding in document.allFindings {130 has(">> \(finding.title)")131 has("\(ReportStrings.state): \(finding.state.title)")132 has("\(ReportStrings.severity): \(finding.severity.title)")133 has("\(ReportStrings.confidence): \(finding.confidence.title)")134 has("\(ReportStrings.verification): \(finding.verification.title)")135 has("\(ReportStrings.assessed): \(ReportStrings.date(finding.assessedAt))")136 for evidence in finding.evidence {137 has("- \(evidence.label): \(evidence.value)", "\(evidence.label)")138 if let source = evidence.source { has("\(ReportStrings.source): \(source)") }139 }140 has(finding.explanation)141 if let recommendation = finding.recommendation { has(recommendation) }142 for item in finding.notInspected { has("- \(item)") }143 }144 has(ReportStrings.unavailableTitle.uppercased())145 for item in document.unavailable { has(item) }146 has(ReportGenerator.closingStatement)147 }148149 @Test func wrapsAtEightyColumns() {150 for document in [Fixtures.rich, Fixtures.long(20), Fixtures.empty] {151 let lines = generator.plainText(document).split(separator: "\n", omittingEmptySubsequences: false)152 #expect(lines.count > 10)153 for line in lines where !line.contains("://") {154 #expect(line.count <= 80, "\(line.count): \(line)")155 }156 #expect(lines.allSatisfy { $0.last != " " })157 }158 }159160 @Test func rulesAreASCIIAndURLsStayWhole() {161 let output = generator.plainText(Fixtures.rich)162 #expect(output.contains(String(repeating: "=", count: 80)))163 #expect(output.contains(String(repeating: "-", count: 80)))164 #expect(output.contains("https://example.com/a/very/long/path/that/goes/on/and/on/and/on/and/on/and/on/and/on/index.html?token=abc&user=simon"))165 #expect(!output.contains("\n\n\n"))166 }167168 @Test func wrapHelperHandlesLongWordsAndPrefixes() {169 let lines = TextWrap.wrap(String(repeating: "x", count: 205) + " tail", width: 80, first: " - ", rest: " ")170 #expect(lines.allSatisfy { $0.count <= 80 })171 #expect(lines.joined().squashed == ("-" + String(repeating: "x", count: 205) + "tail"))172 #expect(lines.dropFirst().allSatisfy { $0.hasPrefix(" ") })173 #expect(TextWrap.wrap("", width: 80).isEmpty)174 }175}176177@Suite("JSON")178struct JSONTests {179 let generator = ReportGenerator()180181 @Test func roundTripsToAnEqualDocument() throws {182 for document in [Fixtures.rich, Fixtures.empty, Fixtures.long(30)] {183 let data = try generator.json(document)184 let decoded = try generator.decode(json: data)185 #expect(decoded == document)186 }187 }188189 @Test func envelopeDatesAndOrdering() throws {190 let document = Fixtures.rich191 let data = try generator.json(document)192 let object = try JSONSerialization.jsonObject(with: data)193 let root = try #require(object as? [String: Any])194 #expect(root["schema"] as? String == "ai.spboucher.orix.secure.report")195 #expect(root["schemaVersion"] as? Int == 1)196 let report = try #require(root["report"] as? [String: Any])197 #expect(report["createdAt"] as? String == "2026-11-20T12:00:00Z")198 #expect(report["title"] as? String == document.title)199 let text = try #require(String(data: data, encoding: .utf8))200 #expect(text.contains("\n \"schema\""))201 let report1 = try #require(text.range(of: "\"report\""))202 let schema = try #require(text.range(of: "\"schema\""))203 let version = try #require(text.range(of: "\"schemaVersion\""))204 #expect(report1.lowerBound < schema.lowerBound && schema.lowerBound < version.lowerBound)205 let again = try generator.json(document)206 #expect(again == data)207 }208209 @Test func subSecondDatesKeepMillisecondsAndStabilize() throws {210 var document = Fixtures.rich211 document.createdAt = Date(timeIntervalSince1970: 1_795_176_000.123456)212 let data = try generator.json(document)213 let once = try generator.decode(json: data)214 #expect(abs(once.createdAt.timeIntervalSince(document.createdAt)) < 0.001)215 let dataAgain = try generator.json(once)216 let twice = try generator.decode(json: dataAgain)217 #expect(twice == once)218 }219220 @Test func rejectsForeignOrNewerFiles() throws {221 let data = try generator.json(Fixtures.empty)222 let text = try #require(String(data: data, encoding: .utf8))223 let foreign = Data(text.replacingOccurrences(of: "ai.spboucher.orix.secure.report", with: "com.example.other").utf8)224 #expect(throws: ReportDecodingError.unknownSchema("com.example.other")) { try generator.decode(json: foreign) }225 let newer = Data(text.replacingOccurrences(of: "\"schemaVersion\" : 1", with: "\"schemaVersion\" : 2").utf8)226 #expect(throws: ReportDecodingError.unsupportedSchemaVersion(2)) { try generator.decode(json: newer) }227 #expect(throws: (any Error).self) { try generator.decode(json: Data("{}".utf8)) }228 }229}230231@Suite("Export")232struct ExportTests {233 let generator = ReportGenerator()234235 private func temporaryDirectory() throws -> URL {236 let url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-reporting-\(UUID().uuidString)")237 try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)238 return url239 }240241 @Test func formatsDescribeThemselves() {242 #expect(ReportFormat.allCases == [.pdf, .markdown, .json, .plainText])243 #expect(ReportFormat.allCases.map(\.fileExtension) == ["pdf", "md", "json", "txt"])244 #expect(ReportFormat.allCases.map(\.typeIdentifier) == ["com.adobe.pdf", "net.daringfireball.markdown", "public.json", "public.plain-text"])245 #expect(ReportFormat.allCases.allSatisfy { !$0.title.isEmpty && !$0.mimeType.isEmpty })246 }247248 @Test func fileNameIsSafe() {249 #expect(generator.fileName(for: Fixtures.rich) == "Security-check-of-Simon’s-iPhone-2026-11-20")250 var hostile = Fixtures.empty251 hostile.title = "../../etc/passwd: a\\b*c?\"<d>|e\n\u{0}f ..hidden"252 let name = generator.fileName(for: hostile)253 #expect(name == "etc-passwd-a-b-c-d-e-f-..hidden-2026-11-20")254 #expect(name.unicodeScalars.allSatisfy { !"/\\:*?\"<>|\n\u{0}".unicodeScalars.contains($0) })255 #expect(!name.hasPrefix("."))256 #expect(generator.fileName(for: Fixtures.empty) == "ORIX-Secure-Report-2026-11-20")257 hostile.title = String(repeating: "long ", count: 100)258 #expect(generator.fileName(for: hostile).count <= 91)259 }260261 @Test func exportsEveryFormatWithoutOverwriting() throws {262 let directory = try temporaryDirectory()263 defer { try? FileManager.default.removeItem(at: directory) }264 let document = Fixtures.rich265 for format in ReportFormat.allCases {266 let first = try generator.export(document, as: format, to: directory)267 #expect(first.lastPathComponent == "Security-check-of-Simon’s-iPhone-2026-11-20.\(format.fileExtension)")268 #expect(first.deletingLastPathComponent().standardizedFileURL.path == directory.standardizedFileURL.path)269 let original = try Data(contentsOf: first)270 #expect(!original.isEmpty)271272 var changed = document273 changed.summary = "Changed"274 let second = try generator.export(changed, as: format, to: directory)275 let third = try generator.export(changed, as: format, to: directory)276 #expect(second.lastPathComponent == "Security-check-of-Simon’s-iPhone-2026-11-20-2.\(format.fileExtension)")277 #expect(third.lastPathComponent == "Security-check-of-Simon’s-iPhone-2026-11-20-3.\(format.fileExtension)")278 let untouched = try Data(contentsOf: first)279 #expect(untouched == original)280 }281 let listing = try FileManager.default.contentsOfDirectory(atPath: directory.path)282 #expect(listing.count == 12)283 let markdown = try String(contentsOf: directory.appendingPathComponent("Security-check-of-Simon’s-iPhone-2026-11-20.md"), encoding: .utf8)284 #expect(markdown == generator.markdown(document))285 }286287 @Test func exportToMissingDirectoryThrows() {288 let missing = FileManager.default.temporaryDirectory.appendingPathComponent("orix-missing-\(UUID().uuidString)")289 #expect(throws: ReportExportError.notADirectory(missing.path)) {290 try generator.export(Fixtures.empty, as: .json, to: missing)291 }292 }293}294295@Suite("Redaction")296struct RedactionTests {297 let mask = ReportStrings.redacted298299 private func document() -> ReportDocument {300 let finding = Finding(301 id: "net", category: .network, title: "Network seen from Studio-de-Simon", state: .attention, severity: .low,302 confidence: .measured, verification: .publicAPI,303 evidence: [304 Evidence("Device name", "Studio-de-Simon"),305 Evidence("Wi-Fi SSID", "MaisonBoucher-5G"),306 Evidence("BSSID", "a4:83:e7:1c:9b:02"),307 Evidence("Local address", "192.168.2.104"),308 Evidence("IPv6", "fe80::1c2a:3bff:fe4d:5e6f%en0"),309 Evidence("Path", "/Users/simon-pierreboucher/Library/Caches/report.json"),310 Evidence("Container", "/private/var/mobile/Containers/Data/Application/1B9F6C2E-0000-4000-8000-ABCDEFABCDEF/Documents/x.pdf"),311 Evidence("Link", "https://example.com/login?token=SECRET123&user=simon#top"),312 Evidence("System version", "26.5.1"),313 Evidence("Time", "12:30:45"),314 ],315 explanation: "Gateway 10.0.0.1 and resolver 2001:4860:4860::8888 answered; joined MaisonBoucher-5G at 12:30:45 via a4:83:e7:1c:9b:02.",316 recommendation: "Open file:///Users/simon-pierreboucher/Desktop/a.txt or http://192.168.2.1/admin?session=abc",317 notInspected: ["Traffic of Studio-de-Simon"],318 assessedAt: Fixtures.date319 )320 return ReportDocument(321 kind: .network, title: "Network report for Studio-de-Simon", summary: "Connected to MaisonBoucher-5G with 192.168.2.104.",322 sections: [.init(title: "Network", paragraphs: ["Address ::1 is loopback."], facts: [.init("Hostname", "studio.local"), .init("Public address", "142.170.25.2")], findings: [finding], bullets: ["DNS 8.8.8.8"])],323 unavailable: ["Router at 192.168.2.1 not inspected"], createdAt: Fixtures.date324 )325 }326327 private func allText(_ document: ReportDocument) -> String {328 ReportGenerator().plainText(document)329 }330331 @Test func noOptionChangesNothing() {332 let original = document()333 #expect(original.redacted(.none) == original)334 }335336 @Test func deviceName() {337 let redacted = document().redacted(RedactionOptions(deviceName: true))338 let text = allText(redacted)339 #expect(!text.contains("Studio-de-Simon"))340 #expect(!text.contains("studio.local"))341 #expect(redacted.title == "Network report for \(mask)")342 #expect(redacted.sections[0].findings[0].evidence[0].value == mask)343 #expect(redacted.sections[0].findings[0].notInspected == ["Traffic of \(mask)"])344 #expect(text.contains("MaisonBoucher-5G"))345 #expect(text.contains("192.168.2.104"))346 }347348 @Test func additionalTerms() {349 let redacted = document().redacted(RedactionOptions(additionalTerms: ["simon", "ab"]))350 let text = allText(redacted)351 #expect(!text.lowercased().contains("simon"))352 #expect(text.contains("abc")) // too short a term is ignored rather than shredding words353 }354355 @Test func ipAddresses() {356 let redacted = document().redacted(RedactionOptions(ipAddresses: true))357 let text = allText(redacted)358 for address in ["192.168.2.104", "10.0.0.1", "142.170.25.2", "8.8.8.8", "192.168.2.1", "2001:4860:4860::8888", "fe80::1c2a:3bff:fe4d:5e6f", "%en0", "::1"] {359 #expect(!text.contains(address), "\(address)")360 }361 let evidence = redacted.sections[0].findings[0].evidence362 #expect(evidence[3].value == mask)363 #expect(evidence[4].value == mask)364 // Not addresses: a three-part version, a time of day, a MAC address.365 #expect(evidence[8].value == "26.5.1")366 #expect(evidence[9].value == "12:30:45")367 #expect(evidence[2].value == "a4:83:e7:1c:9b:02")368 #expect(redacted.sections[0].findings[0].recommendation?.contains("http://\(mask)/admin?session=abc") == true)369 #expect(redacted.sections[0].paragraphs == ["Address \(mask) is loopback."])370 }371372 @Test func ipv6Recognition() {373 for valid in ["::1", "fe80::1", "2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8::", "::ffff:192.0.2.128", "fe80::1%en0"] {374 #expect(Redactor.isIPv6(valid), "\(valid)")375 }376 for invalid in ["12:30:45", "a4:83:e7:1c:9b:02", "::", "1:2:3:4:5:6:7:8:9", "std::vector"] {377 #expect(!Redactor.isIPv6(invalid), "\(invalid)")378 }379 }380381 @Test func networkIdentifiers() {382 let redacted = document().redacted(RedactionOptions(networkIdentifiers: true))383 let text = allText(redacted)384 #expect(!text.contains("MaisonBoucher-5G"))385 #expect(!text.contains("a4:83:e7:1c:9b:02"))386 let evidence = redacted.sections[0].findings[0].evidence387 #expect(evidence[1].value == mask)388 #expect(evidence[2].value == mask)389 #expect(redacted.summary == "Connected to \(mask) with 192.168.2.104.")390 #expect(evidence[9].value == "12:30:45")391 #expect(text.contains("Studio-de-Simon"))392 }393394 @Test func filePathUsers() {395 let redacted = document().redacted(RedactionOptions(filePathUsers: true))396 let finding = redacted.sections[0].findings[0]397 #expect(finding.evidence[5].value == "/Users/\(mask)/Library/Caches/report.json")398 #expect(finding.evidence[6].value == "/private/var/mobile/Containers/Data/Application/\(mask)/Documents/x.pdf")399 #expect(finding.recommendation?.hasPrefix("Open file:///Users/\(mask)/Desktop/a.txt") == true)400 #expect(!allText(redacted).contains("simon-pierreboucher"))401 }402403 @Test func urlQueries() {404 let redacted = document().redacted(RedactionOptions(urlQueries: true))405 let finding = redacted.sections[0].findings[0]406 #expect(finding.evidence[7].value == "https://example.com/login?\(mask)#top")407 #expect(finding.recommendation?.hasSuffix("http://192.168.2.1/admin?\(mask)") == true)408 #expect(!allText(redacted).contains("SECRET123"))409 }410411 @Test func everythingAtOnce() {412 let redacted = document().redacted(.all)413 let text = allText(redacted) + (String(data: (try? ReportGenerator().json(redacted)) ?? Data(), encoding: .utf8) ?? "")414 for secret in ["Studio-de-Simon", "studio.local", "MaisonBoucher", "a4:83", "192.168", "142.170", "fe80", "2001:4860", "simon-pierreboucher", "SECRET123", "1B9F6C2E"] {415 #expect(!text.contains(secret), "\(secret)")416 }417 #expect(redacted.sections[0].findings[0].evidence[8].value == "26.5.1")418 }419}420