Commit 917a5c8a08
Verified · cmc
Layout: unified · split
DomainDig.xcodeproj/project.pbxproj +4
| @@ -27,6 +27,7 @@ | |||
| 27 | C7CA9E02B0DC2708DE7A8563 /* DomainDataPortabilityServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC948A02EC0184228BC4630E /* DomainDataPortabilityServiceTests.swift */; }; | 27 | C7CA9E02B0DC2708DE7A8563 /* DomainDataPortabilityServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC948A02EC0184228BC4630E /* DomainDataPortabilityServiceTests.swift */; }; |
| 28 | E959C4D24DAAB3CB80D854B2 /* DiffServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15A25DF2B8BB52589D49986B /* DiffServiceTests.swift */; }; | 28 | E959C4D24DAAB3CB80D854B2 /* DiffServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15A25DF2B8BB52589D49986B /* DiffServiceTests.swift */; }; |
| 29 | A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000201 /* InspectionInputTests.swift */; }; | 29 | A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000201 /* InspectionInputTests.swift */; }; |
| 30 | A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; }; | ||
| 30 | EA12012CDB4C21ABB4217D86 /* LocalAPIContractTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D85A44C5F315A1644AC9073 /* LocalAPIContractTests.swift */; }; | 31 | EA12012CDB4C21ABB4217D86 /* LocalAPIContractTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D85A44C5F315A1644AC9073 /* LocalAPIContractTests.swift */; }; |
| 31 | /* End PBXBuildFile section */ | 32 | /* End PBXBuildFile section */ |
| 32 | 33 | ||
| @@ -98,6 +99,7 @@ | |||
| 98 | 8BF9DA842F9B13FB00EF41D5 /* DomainDataPortabilityService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainDataPortabilityService.swift; sourceTree = "<group>"; }; | 99 | 8BF9DA842F9B13FB00EF41D5 /* DomainDataPortabilityService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainDataPortabilityService.swift; sourceTree = "<group>"; }; |
| 99 | A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = HistoryEntryCodableTests.swift; sourceTree = "<group>"; }; | 100 | A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = HistoryEntryCodableTests.swift; sourceTree = "<group>"; }; |
| 100 | A1B2C3D40000000000000201 /* InspectionInputTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = InspectionInputTests.swift; sourceTree = "<group>"; }; | 101 | A1B2C3D40000000000000201 /* InspectionInputTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = InspectionInputTests.swift; sourceTree = "<group>"; }; |
| 102 | A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; }; | ||
| 101 | A77F7ED29E7C6669F5E61C04 /* DomainDigTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DomainDigTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; | 103 | A77F7ED29E7C6669F5E61C04 /* DomainDigTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DomainDigTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; |
| 102 | CC948A02EC0184228BC4630E /* DomainDataPortabilityServiceTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainDataPortabilityServiceTests.swift; sourceTree = "<group>"; }; | 104 | CC948A02EC0184228BC4630E /* DomainDataPortabilityServiceTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainDataPortabilityServiceTests.swift; sourceTree = "<group>"; }; |
| 103 | DE8B269A01CC5E593DA3DFC2 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS26.0.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; }; | 105 | DE8B269A01CC5E593DA3DFC2 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS26.0.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; }; |
| @@ -236,6 +238,7 @@ | |||
| 236 | 499B99A1866999A38AC475F5 /* AppInfoTests.swift */, | 238 | 499B99A1866999A38AC475F5 /* AppInfoTests.swift */, |
| 237 | A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */, | 239 | A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */, |
| 238 | A1B2C3D40000000000000201 /* InspectionInputTests.swift */, | 240 | A1B2C3D40000000000000201 /* InspectionInputTests.swift */, |
| 241 | A1B2C3D40000000000000301 /* AvailabilityTests.swift */, | ||
| 239 | ); | 242 | ); |
| 240 | path = DomainDigTests; | 243 | path = DomainDigTests; |
| 241 | sourceTree = "<group>"; | 244 | sourceTree = "<group>"; |
| @@ -497,6 +500,7 @@ | |||
| 497 | 4F96CEB875EC3501E784CE39 /* DataMigrationServiceTests.swift in Sources */, | 500 | 4F96CEB875EC3501E784CE39 /* DataMigrationServiceTests.swift in Sources */, |
| 498 | 99024E91EED2275FD80E83A9 /* AppInfoTests.swift in Sources */, | 501 | 99024E91EED2275FD80E83A9 /* AppInfoTests.swift in Sources */, |
| 499 | A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */, | 502 | A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */, |
| 503 | A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */, | ||
| 500 | ); | 504 | ); |
| 501 | runOnlyForDeploymentPostprocessing = 0; | 505 | runOnlyForDeploymentPostprocessing = 0; |
| 502 | }; | 506 | }; |
DomainDigTests/AvailabilityTests.swift added +185
| @@ -0,0 +1,185 @@ | |||
| 1 | import XCTest | ||
| 2 | @testable import DomainDig | ||
| 3 | |||
| 4 | /// Availability has two sources that disagree about what silence means: a | ||
| 5 | /// registry 404 means the name is free, while a DNS lookup with no records | ||
| 6 | /// means nothing, because the DoH layer drops the response code. These pin the | ||
| 7 | /// mapping for each path through `DomainAvailabilityService.check`. | ||
| 8 | final class DomainAvailabilityTests: XCTestCase { | ||
| 9 | |||
| 10 | /// A resolving name under a TLD with no RDAP server: the 404 comes from the | ||
| 11 | /// bootstrap redirector, which says nothing about registration, so DNS | ||
| 12 | /// decides. | ||
| 13 | func testBootstrapNotFoundWithDNSRecordsIsRegistered() async { | ||
| 14 | let transport = StubRDAPTransport { _ in | ||
| 15 | (Data(), stubHTTPResponse(status: 404, host: "rdap.org", domain: "example.quux")) | ||
| 16 | } | ||
| 17 | let dns = StubDNSLookup(records: [.A: [DNSRecord(value: "203.0.113.10", ttl: 300)]]) | ||
| 18 | |||
| 19 | let result = await DomainAvailabilityService.check( | ||
| 20 | domain: "example.quux", | ||
| 21 | rdapTransport: transport, | ||
| 22 | dns: dns | ||
| 23 | ) | ||
| 24 | |||
| 25 | XCTAssertEqual(result.status, .registered) | ||
| 26 | } | ||
| 27 | |||
| 28 | func testRegistryNotFoundIsAvailable() async { | ||
| 29 | let transport = StubRDAPTransport { _ in | ||
| 30 | (Data(), stubHTTPResponse(status: 404, host: "rdap.verisign.com", domain: "example.com")) | ||
| 31 | } | ||
| 32 | let dns = StubDNSLookup(records: [:]) | ||
| 33 | |||
| 34 | let result = await DomainAvailabilityService.check( | ||
| 35 | domain: "example.com", | ||
| 36 | rdapTransport: transport, | ||
| 37 | dns: dns | ||
| 38 | ) | ||
| 39 | |||
| 40 | XCTAssertEqual(result.status, .available) | ||
| 41 | XCTAssertEqual(dns.lookupCount, 0) | ||
| 42 | } | ||
| 43 | |||
| 44 | func testDomainRecordIsRegistered() async { | ||
| 45 | let transport = StubRDAPTransport { _ in | ||
| 46 | (domainRecordBody, stubHTTPResponse(status: 200, host: "rdap.verisign.com", domain: "example.com")) | ||
| 47 | } | ||
| 48 | let dns = StubDNSLookup(records: [:]) | ||
| 49 | |||
| 50 | let result = await DomainAvailabilityService.check( | ||
| 51 | domain: "example.com", | ||
| 52 | rdapTransport: transport, | ||
| 53 | dns: dns | ||
| 54 | ) | ||
| 55 | |||
| 56 | XCTAssertEqual(result.status, .registered) | ||
| 57 | XCTAssertEqual(dns.lookupCount, 0) | ||
| 58 | } | ||
| 59 | |||
| 60 | func testNonDomainRecordFallsBackToDNS() async { | ||
| 61 | let transport = StubRDAPTransport { _ in | ||
| 62 | (nonDomainRecordBody, stubHTTPResponse(status: 200, host: "rdap.verisign.com", domain: "example.com")) | ||
| 63 | } | ||
| 64 | let dns = StubDNSLookup(records: [.NS: [DNSRecord(value: "ns1.example.net", ttl: 300)]]) | ||
| 65 | |||
| 66 | let result = await DomainAvailabilityService.check( | ||
| 67 | domain: "example.com", | ||
| 68 | rdapTransport: transport, | ||
| 69 | dns: dns | ||
| 70 | ) | ||
| 71 | |||
| 72 | XCTAssertEqual(result.status, .registered) | ||
| 73 | XCTAssertEqual(dns.lookupCount, 2) | ||
| 74 | } | ||
| 75 | |||
| 76 | func testTransportErrorWithDNSRecordsIsRegistered() async { | ||
| 77 | let transport = StubRDAPTransport { _ in throw URLError(.timedOut) } | ||
| 78 | let dns = StubDNSLookup(records: [.A: [DNSRecord(value: "203.0.113.10", ttl: 300)]]) | ||
| 79 | |||
| 80 | let result = await DomainAvailabilityService.check( | ||
| 81 | domain: "example.com", | ||
| 82 | rdapTransport: transport, | ||
| 83 | dns: dns | ||
| 84 | ) | ||
| 85 | |||
| 86 | XCTAssertEqual(result.status, .registered) | ||
| 87 | } | ||
| 88 | |||
| 89 | /// No RDAP answer and no DNS records is undecided, not available: a | ||
| 90 | /// resolver failure and NXDOMAIN arrive here identically. | ||
| 91 | func testTransportErrorWithoutDNSRecordsIsUnknown() async { | ||
| 92 | let transport = StubRDAPTransport { _ in throw URLError(.timedOut) } | ||
| 93 | let dns = StubDNSLookup(records: [:]) | ||
| 94 | |||
| 95 | let result = await DomainAvailabilityService.check( | ||
| 96 | domain: "example.com", | ||
| 97 | rdapTransport: transport, | ||
| 98 | dns: dns | ||
| 99 | ) | ||
| 100 | |||
| 101 | XCTAssertEqual(result.status, .unknown) | ||
| 102 | } | ||
| 103 | |||
| 104 | func testBlankInputIsUnknownWithoutALookup() async { | ||
| 105 | let transport = StubRDAPTransport { _ in | ||
| 106 | XCTFail("blank input should not reach the network") | ||
| 107 | return (Data(), stubHTTPResponse(status: 200, host: "rdap.org", domain: "")) | ||
| 108 | } | ||
| 109 | let dns = StubDNSLookup(records: [:]) | ||
| 110 | |||
| 111 | for input in ["", " \n"] { | ||
| 112 | let result = await DomainAvailabilityService.check( | ||
| 113 | domain: input, | ||
| 114 | rdapTransport: transport, | ||
| 115 | dns: dns | ||
| 116 | ) | ||
| 117 | XCTAssertEqual(result.status, .unknown) | ||
| 118 | } | ||
| 119 | |||
| 120 | XCTAssertEqual(transport.requestedDomains, []) | ||
| 121 | XCTAssertEqual(dns.lookupCount, 0) | ||
| 122 | } | ||
| 123 | |||
| 124 | func testSuggestionsMapEachCandidateStatus() async { | ||
| 125 | let transport = StubRDAPTransport { request in | ||
| 126 | let domain = request.url?.lastPathComponent ?? "" | ||
| 127 | if domain == "example.net" { | ||
| 128 | return (Data(), stubHTTPResponse(status: 404, host: "rdap.nic.net", domain: domain)) | ||
| 129 | } | ||
| 130 | return (domainRecordBody, stubHTTPResponse(status: 200, host: "rdap.nic.io", domain: domain)) | ||
| 131 | } | ||
| 132 | let dns = StubDNSLookup(records: [:]) | ||
| 133 | |||
| 134 | let results = await DomainAvailabilityService.suggestions( | ||
| 135 | for: "example.com", | ||
| 136 | limit: 2, | ||
| 137 | rdapTransport: transport, | ||
| 138 | dns: dns | ||
| 139 | ) | ||
| 140 | |||
| 141 | XCTAssertEqual(results.map(\.domain), ["example.net", "example.io"]) | ||
| 142 | XCTAssertEqual(results.map(\.status), [.available, .registered]) | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | private final class StubRDAPTransport: RDAPTransport { | ||
| 147 | private let respond: (URLRequest) throws -> (Data, URLResponse) | ||
| 148 | private(set) var requestedDomains: [String] = [] | ||
| 149 | |||
| 150 | init(respond: @escaping (URLRequest) throws -> (Data, URLResponse)) { | ||
| 151 | self.respond = respond | ||
| 152 | } | ||
| 153 | |||
| 154 | func fetch(_ request: URLRequest) async throws -> (Data, URLResponse) { | ||
| 155 | if let domain = request.url?.lastPathComponent { | ||
| 156 | requestedDomains.append(domain) | ||
| 157 | } | ||
| 158 | return try respond(request) | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | private final class StubDNSLookup: DNSLookup { | ||
| 163 | private let records: [DNSRecordType: [DNSRecord]] | ||
| 164 | private(set) var lookupCount = 0 | ||
| 165 | |||
| 166 | init(records: [DNSRecordType: [DNSRecord]]) { | ||
| 167 | self.records = records | ||
| 168 | } | ||
| 169 | |||
| 170 | func lookup(domain: String, recordType: DNSRecordType) async throws -> [DNSRecord] { | ||
| 171 | lookupCount += 1 | ||
| 172 | return records[recordType] ?? [] | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | private let domainRecordBody = Data(#"{"objectClassName":"domain","ldhName":"example.com"}"#.utf8) | ||
| 177 | |||
| 178 | private let nonDomainRecordBody = Data(#"{"objectClassName":"help"}"#.utf8) | ||
| 179 | |||
| 180 | /// The response URL matters as much as the status code: it is how a registry's | ||
| 181 | /// 404 is told apart from the bootstrap redirector's. | ||
| 182 | private func stubHTTPResponse(status: Int, host: String, domain: String) -> URLResponse { | ||
| 183 | let url = URL(string: "https://\(host)/domain/\(domain)")! | ||
| 184 | return HTTPURLResponse(url: url, statusCode: status, httpVersion: nil, headerFields: nil)! | ||
| 185 | } | ||