DomainDigTests/SMTPMessageTests.swift
85 lines · 3293 bytes
1import XCTest
2@testable import DomainDig
3
4/// The notification mail is assembled by joining lines with CRLF and sending
5/// them raw, so values that reach it decide the wire format. Two things can go
6/// wrong: a bare line break ends a header and lets the rest be read as more
7/// headers, and a line of "." alone ends the DATA command and lets the rest be
8/// read as SMTP commands. Both values are reachable — the inspected domain
9/// arrives in the subject, and the event details carry arbitrary keys.
10final class SMTPMessageTests: XCTestCase {
11
12 // MARK: Header values
13
14 func testAPlainValueIsAllowed() {
15 XCTAssertEqual(SMTPMessage.validatedHeaderValue("example.com"), "example.com")
16 }
17
18 func testALineFeedIsRefused() {
19 XCTAssertNil(SMTPMessage.validatedHeaderValue("example.com\nBcc: attacker@example.net"))
20 }
21
22 func testACarriageReturnIsRefused() {
23 XCTAssertNil(SMTPMessage.validatedHeaderValue("example.com\r"))
24 }
25
26 func testACRLFPairIsRefused() {
27 XCTAssertNil(SMTPMessage.validatedHeaderValue("a\r\nb"))
28 }
29
30 // MARK: Dot stuffing
31
32 /// The case that ends the message early: a line holding nothing but a dot.
33 func testALoneDotIsDoubled() {
34 XCTAssertEqual(SMTPMessage.dotStuffed("before\r\n.\r\nafter"), "before\r\n..\r\nafter")
35 }
36
37 func testALineStartingWithADotIsDoubled() {
38 XCTAssertEqual(SMTPMessage.dotStuffed("a\r\n.MAIL FROM:<x>"), "a\r\n..MAIL FROM:<x>")
39 }
40
41 func testTheFirstLineIsStuffedToo() {
42 XCTAssertEqual(SMTPMessage.dotStuffed(".leading"), "..leading")
43 }
44
45 func testOrdinaryTextIsUnchanged() {
46 let body = "Subject: fine\r\n\r\nA domain changed. Nothing to stuff."
47 XCTAssertEqual(SMTPMessage.dotStuffed(body), body)
48 }
49
50 func testADotInsideALineIsNotTouched() {
51 XCTAssertEqual(SMTPMessage.dotStuffed("example.com changed"), "example.com changed")
52 }
53}
54
55/// The deep link is the one path into these values from outside the app.
56final class DeepLinkSanitizingTests: XCTestCase {
57
58 func testAPlainDomainIsAccepted() {
59 let url = URL(string: "domaindig://inspect?domain=example.com")!
60 XCTAssertEqual(DomainDigDeepLink.action(from: url), .inspect("example.com"))
61 }
62
63 func testAnEncodedLineBreakRefusesTheLink() {
64 let url = URL(string: "domaindig://inspect?domain=example.com%0ABcc:%20attacker@example.net")!
65 XCTAssertNil(DomainDigDeepLink.action(from: url))
66 }
67
68 /// A trailing break is trimmed before the check, which leaves a clean
69 /// value — so the link is honoured rather than refused. Only an interior
70 /// one survives trimming, and that is the case worth refusing.
71 func testATrailingCarriageReturnIsTrimmed() {
72 let url = URL(string: "domaindig://watch?domain=example.com%0D")!
73 XCTAssertEqual(DomainDigDeepLink.action(from: url), .watch("example.com"))
74 }
75
76 func testAnInteriorCarriageReturnRefusesTheLink() {
77 let url = URL(string: "domaindig://watch?domain=example%0D.com")!
78 XCTAssertNil(DomainDigDeepLink.action(from: url))
79 }
80
81 func testAnEncodedNullRefusesTheLink() {
82 let url = URL(string: "domaindig://domain?domain=example%00.com")!
83 XCTAssertNil(DomainDigDeepLink.action(from: url))
84 }
85}