DomainDigTests/InspectionScopeTests.swift
193 lines · 7729 bytes
1import XCTest
2@testable import DomainDig
3
4/// Section selection and the enrichment choice reach the inspection through
5/// `inspectSnapshot`'s signature, so what each scope runs is a pure function
6/// and these are what pin it. The inspection service itself takes a concrete
7/// `LookupRuntime`, so there is no seam to assert the network calls through —
8/// `DomainAvailabilityTests` covers the one service that can be stubbed.
9final class InspectionScopeTests: XCTestCase {
10
11 private var scratch: UserDefaults!
12 private var suiteName: String!
13
14 override func setUpWithError() throws {
15 try super.setUpWithError()
16 suiteName = "net.cleberg.DomainDig.tests.scope.\(UUID().uuidString)"
17 scratch = try XCTUnwrap(UserDefaults(suiteName: suiteName))
18 }
19
20 override func tearDownWithError() throws {
21 scratch.removePersistentDomain(forName: suiteName)
22 scratch = nil
23 suiteName = nil
24 try super.tearDownWithError()
25 }
26
27 // MARK: What each scope runs
28
29 func testFullWithEnrichmentRunsEverySection() {
30 let scope = InspectionScope.full
31
32 for section in LookupSectionKind.allCases {
33 XCTAssertTrue(scope.runs(section), "full should run \(section.rawValue)")
34 XCTAssertNil(scope.skipReason(for: section))
35 }
36 }
37
38 func testQuickSkipsTheExpensiveSections() {
39 let scope = InspectionScope(profile: .quick, allowsThirdPartyEnrichment: true)
40
41 let skipped = LookupSectionKind.allCases.filter { !scope.runs($0) }
42
43 XCTAssertEqual(
44 Set(skipped),
45 [.reachability, .portScan, .ipGeolocation, .subdomains, .suggestions]
46 )
47 }
48
49 func testQuickStillRunsTheCoreSections() {
50 let scope = InspectionScope(profile: .quick, allowsThirdPartyEnrichment: true)
51
52 for section in [LookupSectionKind.dns, .availability, .ownership, .ssl, .hsts, .httpHeaders, .redirectChain, .ptr, .emailSecurity] {
53 XCTAssertTrue(scope.runs(section), "quick should still run \(section.rawValue)")
54 }
55 }
56
57 func testDecliningEnrichmentSkipsOnlyTheThirdPartySections() {
58 let scope = InspectionScope(profile: .full, allowsThirdPartyEnrichment: false)
59
60 let skipped = LookupSectionKind.allCases.filter { !scope.runs($0) }
61
62 XCTAssertEqual(Set(skipped), [.ownership, .ipGeolocation, .subdomains, .suggestions])
63 }
64
65 /// Availability keeps running without enrichment: RDAP is only half of it,
66 /// and the DNS fallback still recognises a domain that resolves.
67 func testDecliningEnrichmentKeepsAvailabilityButDropsItsRDAPHalf() {
68 let scope = InspectionScope(profile: .full, allowsThirdPartyEnrichment: false)
69
70 XCTAssertTrue(scope.runs(.availability))
71 XCTAssertFalse(scope.allowsRDAP)
72 XCTAssertTrue(InspectionScope.full.allowsRDAP)
73 }
74
75 func testDecliningEnrichmentOnQuickSkipsTheUnionOfBoth() {
76 let scope = InspectionScope(profile: .quick, allowsThirdPartyEnrichment: false)
77
78 let skipped = LookupSectionKind.allCases.filter { !scope.runs($0) }
79
80 XCTAssertEqual(
81 Set(skipped),
82 [.ownership, .reachability, .portScan, .ipGeolocation, .subdomains, .suggestions]
83 )
84 }
85
86 // MARK: Skip reasons
87
88 /// A skipped section shows why, so the UI does not read as a failed lookup.
89 func testTheSkipReasonNamesTheProfileOrTheEnrichmentSetting() {
90 let quick = InspectionScope(profile: .quick, allowsThirdPartyEnrichment: true)
91 let declined = InspectionScope(profile: .full, allowsThirdPartyEnrichment: false)
92
93 XCTAssertEqual(quick.skipReason(for: .portScan), "Not run in the Quick profile.")
94 XCTAssertEqual(declined.skipReason(for: .ownership), "Not run: third-party enrichment is off in Settings.")
95 }
96
97 /// When both apply, the consent decision is the one worth reporting.
98 func testEnrichmentOutranksTheProfileInTheSkipReason() {
99 let scope = InspectionScope(profile: .quick, allowsThirdPartyEnrichment: false)
100
101 XCTAssertEqual(scope.skipReason(for: .subdomains), "Not run: third-party enrichment is off in Settings.")
102 XCTAssertEqual(scope.skipReason(for: .portScan), "Not run in the Quick profile.")
103 }
104
105 func testASectionThatRunsHasNoSkipReason() {
106 XCTAssertNil(InspectionScope.full.skipReason(for: .dns))
107 XCTAssertNil(InspectionScope(profile: .quick, allowsThirdPartyEnrichment: false).skipReason(for: .dns))
108 }
109
110 // MARK: The stored setting
111
112 /// Both settings default on, which is how the app behaved before they
113 /// existed.
114 func testAnEmptyStoreReadsAsFullWithEnrichment() {
115 XCTAssertEqual(InspectionScope.current(defaults: scratch), .full)
116 }
117
118 func testStoredValuesAreRead() {
119 scratch.set(InspectionProfile.quick.rawValue, forKey: InspectionScope.profileDefaultsKey)
120 scratch.set(false, forKey: InspectionScope.enrichmentDefaultsKey)
121
122 XCTAssertEqual(
123 InspectionScope.current(defaults: scratch),
124 InspectionScope(profile: .quick, allowsThirdPartyEnrichment: false)
125 )
126 }
127
128 /// A profile string the app does not know falls back to Full rather than
129 /// quietly reducing what a lookup covers.
130 func testAnUnknownStoredProfileFallsBackToFull() {
131 scratch.set("exhaustive", forKey: InspectionScope.profileDefaultsKey)
132
133 XCTAssertEqual(InspectionScope.current(defaults: scratch).profile, .full)
134 }
135
136 // MARK: The Local API's profile parameter
137
138 private func apiProfile(_ queryItems: [URLQueryItem]) throws -> InspectionProfile {
139 try XCTUnwrap(InspectionScope.requested(from: queryItems, defaults: scratch)).profile
140 }
141
142 /// A `v1` consumer sends no `profile`, so it has to keep getting Full.
143 func testTheLocalAPIDefaultsToFullWithoutAProfileParameter() throws {
144 XCTAssertEqual(try apiProfile([]), InspectionProfile.full)
145 XCTAssertEqual(
146 try apiProfile([URLQueryItem(name: "other", value: "quick")]),
147 InspectionProfile.full
148 )
149 }
150
151 func testTheLocalAPIReadsAnExplicitProfile() throws {
152 XCTAssertEqual(
153 try apiProfile([URLQueryItem(name: "profile", value: "quick")]),
154 InspectionProfile.quick
155 )
156 XCTAssertEqual(
157 try apiProfile([URLQueryItem(name: "profile", value: " FULL ")]),
158 InspectionProfile.full
159 )
160 }
161
162 /// An unrecognised profile is a request error rather than a silent Full, so
163 /// a typo does not look like it worked.
164 func testTheLocalAPIRejectsAnUnknownProfile() {
165 XCTAssertNil(InspectionScope.requested(from: [URLQueryItem(name: "profile", value: "deep")], defaults: scratch))
166 }
167
168 func testAnEmptyProfileValueIsTreatedAsAbsent() throws {
169 for value in ["", " ", nil] {
170 XCTAssertEqual(
171 try apiProfile([URLQueryItem(name: "profile", value: value)]),
172 InspectionProfile.full,
173 "an empty profile should read as absent, not as an error"
174 )
175 }
176 }
177
178 /// The enrichment setting is the device owner's consent decision, so the
179 /// Local API follows it and a request cannot turn it back on.
180 func testTheLocalAPIFollowsTheStoredEnrichmentSetting() throws {
181 scratch.set(false, forKey: InspectionScope.enrichmentDefaultsKey)
182
183 let scope = try XCTUnwrap(
184 InspectionScope.requested(
185 from: [URLQueryItem(name: "profile", value: "full")],
186 defaults: scratch
187 )
188 )
189
190 XCTAssertEqual(scope.profile, InspectionProfile.full)
191 XCTAssertFalse(scope.allowsThirdPartyEnrichment)
192 }
193}