DomainDigTests/DomainNameTests.swift
130 lines · 5274 bytes
1import XCTest
2@testable import DomainDig
3
4/// `run()` used to hand anything non-empty to 14 section services, which each
5/// reported their own network error for input that was never a domain. These pin
6/// the input-boundary validator that answers before the lookup starts.
7final class DomainNameTests: XCTestCase {
8
9 func testAcceptsAPlainHost() {
10 XCTAssertEqual(DomainName.validate("example.com"), .success("example.com"))
11 }
12
13 func testLowercasesAndTrims() {
14 XCTAssertEqual(DomainName.validate(" EXAMPLE.COM\n"), .success("example.com"))
15 }
16
17 func testStripsSchemeAndPath() {
18 XCTAssertEqual(DomainName.validate("https://example.com/path?q=1"), .success("example.com"))
19 }
20
21 func testStripsAPort() {
22 XCTAssertEqual(DomainName.validate("example.com:8443"), .success("example.com"))
23 }
24
25 func testStripsUserInfo() {
26 XCTAssertEqual(DomainName.validate("user:pass@example.com"), .success("example.com"))
27 }
28
29 func testStripsAQueryWithNoPath() {
30 XCTAssertEqual(DomainName.validate("example.com?q=1"), .success("example.com"))
31 }
32
33 func testRejectsASingleLabel() {
34 XCTAssertEqual(DomainName.validate("localhost"), .failure(.singleLabel))
35 }
36
37 func testRejectsAnInteriorSpace() {
38 XCTAssertEqual(DomainName.validate("exa mple.com"), .failure(.invalidCharacter(" ")))
39 }
40
41 /// `DomainHostURL` decodes escapes, so it reads `https://ex%20ample.com` as
42 /// host `ex ample.com` and forms a URL no section service can resolve.
43 func testRejectsAPercentEscape() {
44 XCTAssertEqual(DomainName.validate("https://ex%20ample.com"), .failure(.invalidCharacter("%")))
45 }
46
47 func testRejectsAnEmptyLabel() {
48 XCTAssertEqual(DomainName.validate("a..b.com"), .failure(.malformedLabel("")))
49 }
50
51 /// `-x.com` forms a URL, so `DomainHostURL` is not a substitute for these
52 /// label rules — the invariant only runs the other way.
53 func testRejectsALeadingHyphenLabel() {
54 XCTAssertEqual(DomainName.validate("-x.com"), .failure(.malformedLabel("-x")))
55 }
56
57 func testRejectsALabelOver63Bytes() {
58 let label = String(repeating: "a", count: 64)
59 XCTAssertEqual(DomainName.validate("\(label).com"), .failure(.labelTooLong))
60 }
61
62 func testRejectsAHostOver253Bytes() {
63 let label = String(repeating: "a", count: 63)
64 let host = Array(repeating: label, count: 4).joined(separator: ".")
65 XCTAssertEqual(DomainName.validate(host), .failure(.tooLong))
66 }
67
68 /// The limit applies to the IDNA-encoded label, so it turns on the encoded
69 /// size rather than the typed one: these 40 distinct CJK characters encode
70 /// to 83 bytes, while 40 repeats of one character encode to 46 and pass.
71 func testMeasuresLengthOnPunycode() {
72 let label = String((0..<40).compactMap { UnicodeScalar(UInt32(0x4E00 + $0 * 7)).map(Character.init) })
73 XCTAssertEqual(DomainName.validate("\(label).com"), .failure(.labelTooLong))
74 XCTAssertEqual(
75 DomainName.validate("\(String(repeating: "域", count: 40)).com"),
76 .success("\(String(repeating: "域", count: 40)).com")
77 )
78 }
79
80 func testAcceptsAUnicodeHost() {
81 XCTAssertEqual(DomainName.validate("münchen.de"), .success("münchen.de"))
82 }
83
84 func testAcceptsATrailingDot() {
85 XCTAssertEqual(DomainName.validate("example.com."), .success("example.com"))
86 }
87
88 /// One-directional invariant: everything the validator accepts can be built
89 /// into a request URL.
90 func testAcceptedHostsFormARequestURL() {
91 for host in ["example.com", "münchen.de", "a.b.c.example.com"] {
92 guard case .success(let validated) = DomainName.validate(host) else {
93 return XCTFail("\(host) should validate")
94 }
95 XCTAssertNotNil(DomainHostURL.https(validated))
96 }
97 }
98}
99
100/// The validator sits at the input boundary only. `normalizedDomain` is the key
101/// that matches already-stored rows, so it stays lenient: routing it through the
102/// validator would turn tracking and workflow lookups into no-ops for any stored
103/// domain the rules reject.
104@MainActor
105final class DomainViewModelInputTests: XCTestCase {
106 /// Shared and never released: deallocating a `DomainViewModel` runs its
107 /// implicit main-actor-isolated deinit through the iOS 17 back-deployment
108 /// shim, which aborts in the simulator. The app holds one for its whole
109 /// lifetime, so nothing releases one there either.
110 private static let viewModel = DomainViewModel()
111
112 func testAnInvalidDomainDoesNotStartALookup() {
113 let viewModel = Self.viewModel
114 viewModel.domain = "localhost"
115
116 viewModel.run()
117
118 XCTAssertFalse(viewModel.hasRun)
119 XCTAssertNotNil(viewModel.domainInputError)
120 }
121
122 func testAStoredSingleLabelDomainStillTracksAndMatchesWorkflows() {
123 let viewModel = Self.viewModel
124 viewModel.trackedDomains = []
125 viewModel.workflows = [DomainWorkflow(name: "Internal", domains: ["localhost"])]
126
127 XCTAssertTrue(viewModel.trackDomain(domain: "localhost", availabilityStatus: nil))
128 XCTAssertEqual(viewModel.workflowsContaining(domain: "localhost").count, 1)
129 }
130}