SPB Git

Repository Public

OutgoingEMLTests.swift 5.7 KB 65 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/SPMailCore/Tests/SPMailCoreTests/OutgoingEMLTests.swift3// Description: The `.eml` export of an unsent message: headers (RFC 2047, no injection), MIME structure with and without HTML, inline4// images and attachments, the bytes that are not on the device listed and never invented.5import Foundation6import Testing7@testable import SPMailCore89@Suite struct OutgoingEMLTests {10    private func text(_ d: Data) -> String { String(decoding: d, as: UTF8.self) }11    private func decoded(_ eml: String, after marker: String) -> String {12        guard let r = eml.range(of: marker), let blank = eml.range(of: "\r\n\r\n", range: r.upperBound..<eml.endIndex) else { return "" }13        let rest = eml[blank.upperBound...]14        let b64 = rest.components(separatedBy: "\r\n--").first ?? ""15        return String(decoding: Data(base64Encoded: b64.replacingOccurrences(of: "\r\n", with: "")) ?? Data(), as: UTF8.self)16    }1718    @Test func plainMessageIsASinglePart() {19        let m = OutgoingMessage(from: EmailAddress(name: "Moi", email: "a@stg.spboucher.ai"), to: [EmailAddress(email: "b@stg.spboucher.ai")], subject: "Essai", text: "Bonjour,\nà bientôt.")20        let eml = text(OutgoingEML.data(m, date: Date(timeIntervalSince1970: 1_796_187_600)))21        #expect(eml.hasPrefix("From: Moi <a@stg.spboucher.ai>\r\nTo: b@stg.spboucher.ai\r\nSubject: Essai\r\nDate: "))22        #expect(eml.contains("MIME-Version: 1.0\r\nX-Unsent: 1\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: base64\r\n\r\n") && !eml.contains("multipart") && !eml.contains("Message-ID"))23        #expect(decoded(eml, after: "Content-Type: text/plain") == "Bonjour,\nà bientôt.")24        #expect(eml.contains("Date: ") && eml.contains("+0000"))25    }2627    @Test func richMessageWithInlineImageAttachmentAndQuote() {28        var m = OutgoingMessage(from: EmailAddress(name: "Zoé Été", email: "a@stg.spboucher.ai"), to: [EmailAddress(name: "B, Inc.", email: "b@stg.spboucher.ai")], cc: [EmailAddress(email: "c@stg.spboucher.ai")],29                                bcc: [EmailAddress(email: "d@stg.spboucher.ai")], subject: "Réunion d’équipe — compte rendu détaillé du troisième trimestre", text: "Voir l’image.", html: "<p>Voir <img src=\"cid:logo@x\"></p>",30                                attachments: [OutgoingAttachment(id: "i1", name: "logo.png", type: "image/png", size: 3, cid: "<logo@x>", inline: true), OutgoingAttachment(id: "f1", name: "été 2026.pdf", type: "application/pdf", size: 4),31                                              OutgoingAttachment(id: "gone", name: "ancien.zip", type: "application/zip", size: 9, blobId: "B1")],32                                inReplyTo: ["1@x"], references: ["0@x", "1@x"])33        m.quotedHTML = OutgoingMessage.quoteBlockOpen + "<p>Original</p></div>"34        m.quotedText = "> Original"35        m.messageId = "abc@stg.spboucher.ai"36        let eml = text(OutgoingEML.data(m, attachments: ["i1": Data([1, 2, 3]), "f1": Data("%PDF".utf8)]))37        // headers: non-ASCII names and subject as encoded words, every address list, threading, the missing file named38        #expect(eml.contains("From: =?UTF-8?B?\(Data("Zoé Été".utf8).base64EncodedString())?= <a@stg.spboucher.ai>\r\n"))39        #expect(eml.contains("To: \"B, Inc.\" <b@stg.spboucher.ai>\r\nCc: c@stg.spboucher.ai\r\nBcc: d@stg.spboucher.ai\r\nSubject: =?UTF-8?B?"))40        #expect(eml.contains("Message-ID: <abc@stg.spboucher.ai>\r\nIn-Reply-To: <1@x>\r\nReferences: <0@x> <1@x>\r\n") && eml.contains("X-SPMail-Missing-Attachments: ancien.zip\r\n"))41        for line in eml.components(separatedBy: "\r\n").prefix(14) { #expect(line.count <= 78, "long header line: \(line)") }42        // structure: mixed ⊃ related ⊃ alternative43        let mixed = eml.range(of: "multipart/mixed")!, related = eml.range(of: "multipart/related")!, alt = eml.range(of: "multipart/alternative")!44        #expect(mixed.lowerBound < related.lowerBound && related.lowerBound < alt.lowerBound)45        #expect(decoded(eml, after: "Content-Type: text/plain") == "Voir l’image.\n\n> Original")46        #expect(decoded(eml, after: "Content-Type: text/html") == "<p>Voir <img src=\"cid:logo@x\"></p>" + OutgoingMessage.quoteBlockOpen + "<p>Original</p></div>")47        #expect(eml.contains("Content-Type: image/png; name=\"logo.png\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: inline; filename=\"logo.png\"\r\nContent-ID: <logo@x>\r\n\r\nAQID\r\n"))48        #expect(eml.contains("Content-Disposition: attachment; filename*=UTF-8''%C3%A9t%C3%A9%202026.pdf\r\n\r\nJVBERg==\r\n") && !eml.contains("ancien.zip\"") && !eml.contains("application/zip"))49        // every boundary that opens also closes50        for kind in ["mixed", "related", "alternative"] {51            let b = eml.components(separatedBy: "boundary=\"spmail-\(kind)-")[1].components(separatedBy: "\"")[0]52            #expect(eml.contains("--spmail-\(kind)-\(b)--\r\n"))53        }54    }5556    @Test func headersCannotBeInjected() {57        let m = OutgoingMessage(from: EmailAddress(name: "Moi\r\nBcc: x@evil.example", email: "a@stg.spboucher.ai\r\nX: 1"), to: [], subject: "Objet\r\nBcc: y@evil.example", text: "",58                                attachments: [OutgoingAttachment(id: "f", name: "a\r\nX-Evil: 1.txt", type: "text/plain\r\nX-Evil: 2", size: 1)])59        let eml = text(OutgoingEML.data(m, attachments: ["f": Data("x".utf8)]))60        let headerLines = eml.components(separatedBy: "\r\n")61        #expect(!headerLines.contains { $0.hasPrefix("Bcc:") || $0.hasPrefix("X-Evil") || $0.hasPrefix("X: ") })62        #expect(eml.contains("Subject: Objet Bcc: y@evil.example\r\n") && eml.contains("Content-Type: application/octet-stream; name=\"a X-Evil: 1.txt\""))63    }64}65
Files main
Menu

Theme