Commit b12109a00b
Unsigned
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 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* DomainNameTests.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 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.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 /* DomainNameTests.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 /* DomainNameTests.swift in Sources */, | ||
| 500 | ); | 504 | ); |
| 501 | runOnlyForDeploymentPostprocessing = 0; | 505 | runOnlyForDeploymentPostprocessing = 0; |
| 502 | }; | 506 | }; |
DomainDig/DomainName.swift added +16
| @@ -0,0 +1,16 @@ | |||
| 1 | import Foundation | ||
| 2 | |||
| 3 | enum DomainName { | ||
| 4 | static func validate(_ raw: String) -> Result<String, DomainNameError> { | ||
| 5 | .failure(.empty) | ||
| 6 | } | ||
| 7 | } | ||
| 8 | |||
| 9 | enum DomainNameError: LocalizedError, Equatable { | ||
| 10 | case empty | ||
| 11 | case singleLabel | ||
| 12 | case invalidCharacter(Character) | ||
| 13 | case malformedLabel(String) | ||
| 14 | case labelTooLong | ||
| 15 | case tooLong | ||
| 16 | } | ||
DomainDigTests/DomainNameTests.swift added +93
| @@ -0,0 +1,93 @@ | |||
| 1 | import 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. | ||
| 7 | final 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 | /// 40 CJK characters are 40 typed characters but over 63 bytes once IDNA | ||
| 69 | /// encodes them, and the registry limit applies to the encoded form. | ||
| 70 | func testMeasuresLengthOnPunycode() { | ||
| 71 | let label = String(repeating: "域", count: 40) | ||
| 72 | XCTAssertEqual(DomainName.validate("\(label).com"), .failure(.labelTooLong)) | ||
| 73 | } | ||
| 74 | |||
| 75 | func testAcceptsAUnicodeHost() { | ||
| 76 | XCTAssertEqual(DomainName.validate("münchen.de"), .success("münchen.de")) | ||
| 77 | } | ||
| 78 | |||
| 79 | func testAcceptsATrailingDot() { | ||
| 80 | XCTAssertEqual(DomainName.validate("example.com."), .success("example.com")) | ||
| 81 | } | ||
| 82 | |||
| 83 | /// One-directional invariant: everything the validator accepts can be built | ||
| 84 | /// into a request URL. | ||
| 85 | func testAcceptedHostsFormARequestURL() { | ||
| 86 | for host in ["example.com", "münchen.de", "a.b.c.example.com"] { | ||
| 87 | guard case .success(let validated) = DomainName.validate(host) else { | ||
| 88 | return XCTFail("\(host) should validate") | ||
| 89 | } | ||
| 90 | XCTAssertNotNil(DomainHostURL.https(validated)) | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||