Repository Public
1import XCTest2@testable import SharedVaultKit34final class URLMatchTests: XCTestCase {5 func domain(_ s: String) -> String? { SPV1.parseOrigin(s)?.domain }67 // ── Public Suffix List ──89 func testPublicSuffixListLoads() throws {10 let psl = try XCTUnwrap(PublicSuffixList.shared)11 XCTAssertGreaterThan(psl.ruleCount, 9000)12 XCTAssertEqual(psl.publicSuffix(of: "example.com"), "com")13 XCTAssertEqual(psl.publicSuffix(of: "www.example.co.uk"), "co.uk")14 XCTAssertEqual(psl.publicSuffix(of: "alice.github.io"), "github.io")15 XCTAssertEqual(psl.publicSuffix(of: "example.unknowntld"), "unknowntld")16 }1718 func testRegistrableDomains() {19 XCTAssertEqual(domain("https://www.example.com/x"), "example.com")20 // multi-label ICANN suffix21 XCTAssertEqual(domain("https://a.b.example.co.uk"), "example.co.uk")22 XCTAssertEqual(domain("https://example.co.uk"), "example.co.uk")23 XCTAssertNil(domain("https://co.uk"), "a public suffix has no registrable domain")24 XCTAssertNotNil(SPV1.parseOrigin("https://co.uk"))25 // private section26 XCTAssertEqual(domain("https://alice.github.io"), "alice.github.io")27 XCTAssertEqual(domain("https://x.alice.github.io"), "alice.github.io")28 XCTAssertNil(domain("https://github.io"))29 XCTAssertNotEqual(domain("https://alice.github.io"), domain("https://bob.github.io"))30 XCTAssertEqual(domain("https://bucket.s3.amazonaws.com"), "bucket.s3.amazonaws.com")31 // wildcard *.ck and exception !www.ck32 XCTAssertNil(domain("https://foo.ck"), "*.ck makes foo.ck a public suffix")33 XCTAssertEqual(domain("https://a.foo.ck"), "a.foo.ck")34 XCTAssertEqual(domain("https://b.a.foo.ck"), "a.foo.ck")35 XCTAssertEqual(domain("https://www.ck"), "www.ck", "!www.ck is an exception")36 XCTAssertEqual(domain("https://x.www.ck"), "www.ck")37 XCTAssertEqual(domain("https://ck"), "ck", "single label: the host itself")38 // wildcard + exception in .jp39 XCTAssertEqual(domain("https://city.kawasaki.jp"), "city.kawasaki.jp")40 XCTAssertNil(domain("https://foo.kawasaki.jp"))41 XCTAssertEqual(domain("https://a.foo.kawasaki.jp"), "a.foo.kawasaki.jp")42 // unknown TLD: default rule43 XCTAssertEqual(domain("https://a.example.unknowntld"), "example.unknowntld")44 }4546 func testIpAddressesAndLocalhost() throws {47 XCTAssertEqual(domain("https://127.0.0.1:3000"), "127.0.0.1")48 XCTAssertEqual(domain("http://192.168.1.10/admin"), "192.168.1.10")49 XCTAssertNotEqual(domain("http://192.168.1.10"), domain("http://192.168.1.11"), "IPs never share a 'domain'")50 XCTAssertNotEqual(domain("http://10.0.1.10"), domain("http://11.0.1.10"))51 XCTAssertEqual(domain("https://[::1]:8443/"), "[::1]")52 XCTAssertEqual(SPV1.parseOrigin("https://[2001:DB8:0:0:0:0:0:1]/x")?.host, "[2001:db8::1]")53 XCTAssertEqual(SPV1.parseOrigin("https://0x7f.1")?.host, "127.0.0.1", "browser-style IPv4 shorthand is canonicalized")54 XCTAssertEqual(SPV1.parseOrigin("https://2130706433")?.host, "127.0.0.1")55 XCTAssertNil(SPV1.parseOrigin("https://999.1.1.1"))56 XCTAssertNil(SPV1.parseOrigin("https://1.2.3.4.5"))57 XCTAssertNil(SPV1.parseOrigin("https://[::1"))58 let local = try XCTUnwrap(SPV1.parseOrigin("localhost"))59 XCTAssertEqual(local.domain, "localhost"); XCTAssertEqual(local.scheme, "https")60 XCTAssertEqual(SPV1.parseOrigin("http://localhost:3000")?.port, "3000")61 XCTAssertEqual(domain("https://intranet"), "intranet")62 XCTAssertEqual(domain("https://foo.localhost"), "foo.localhost")63 }6465 func testIdnHandling() throws {66 let o = try XCTUnwrap(SPV1.parseOrigin("https://bücher.example"))67 XCTAssertEqual(o.host, "xn--bcher-kva.example")68 XCTAssertEqual(o.domain, "xn--bcher-kva.example")69 XCTAssertEqual(o.display, "bücher.example (xn--bcher-kva.example)")70 XCTAssertFalse(o.mixedScript)71 XCTAssertEqual(SPV1.parseOrigin("https://xn--bcher-kva.example"), o, "Unicode and punycode spellings are the same origin")72 // IDN public suffixes: rules stored in Unicode in the list, matched on the punycode form73 XCTAssertEqual(domain("https://www.食狮.公司.cn"), "xn--85x722f.xn--55qx5d.cn")74 XCTAssertEqual(domain("https://www.xn--85x722f.xn--55qx5d.cn"), "xn--85x722f.xn--55qx5d.cn")75 XCTAssertNil(domain("https://公司.cn"))76 XCTAssertEqual(domain("https://shop.пример.рф"), "xn--e1afmkfd.xn--p1ai")77 XCTAssertEqual(SPV1.parseOrigin("https://EXAMPLE.com")?.host, "example.com")78 // mixed script (Cyrillic а in "аpple") is flagged and never prettified79 let mixed = try XCTUnwrap(SPV1.parseOrigin("https://аpple.com"))80 XCTAssertTrue(mixed.mixedScript)81 XCTAssertEqual(mixed.host, "xn--pple-43d.com")82 XCTAssertEqual(mixed.display, "xn--pple-43d.com")83 XCTAssertNotEqual(mixed.domain, "apple.com")84 // punycode helpers85 XCTAssertEqual(Punycode.encode("bücher"), "bcher-kva")86 XCTAssertEqual(Punycode.encode("公司"), "55qx5d")87 XCTAssertEqual(Punycode.encode("пример"), "e1afmkfd")88 XCTAssertEqual(Punycode.decodeForDisplay("bcher-kva"), "bücher")89 XCTAssertEqual(Punycode.decodeForDisplay("85x722f"), "食狮")90 XCTAssertEqual(Punycode.toUnicode("xn--e1afmkfd.xn--p1ai"), "пример.рф")91 for label in ["münchen", "ελλάδα", "日本語", "💩", "a-b-ç", "ñ"] { XCTAssertEqual(Punycode.decodeForDisplay(try XCTUnwrap(Punycode.encode(label))), label) }92 }9394 func testUserinfoTrickAndRejectedInputs() {95 XCTAssertNil(SPV1.parseOrigin("https://bank.com@evil.example"), "userinfo tricks are never parsed into an origin")96 XCTAssertNil(SPV1.parseOrigin("https://user:pw@example.com"))97 XCTAssertNil(SPV1.parseOrigin("https://bank.com%40evil.example"))98 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://bank.com"), pageUrl: "https://bank.com@evil.example"), .no)99 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://evil.example"), pageUrl: "https://bank.com@evil.example"), .no)100 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://bank.com"), pageUrl: "https://bank.com.evil.example"), .no)101 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://bank.com"), pageUrl: "https://evil.example/bank.com"), .no)102 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://bank.com"), pageUrl: "https://evil.example#@bank.com"), .no)103 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://bank.com"), pageUrl: "https://evil.example\\@bank.com"), .no)104 XCTAssertEqual(SPV1.parseOrigin("https://example.com\\@evil.com")?.host, "example.com", "backslash ends the authority, as in browsers")105 for bad in ["", " ", "ftp://example.com", "file:///etc/passwd", "javascript:alert(1)", "https://", "https://exa mple.com", "https://example.com:99999", "https://example.com:abc", "not a url"] {106 XCTAssertNil(SPV1.parseOrigin(bad), bad)107 }108 XCTAssertEqual(SPV1.parseOrigin(" Example.COM ")?.host, "example.com")109 XCTAssertEqual(SPV1.parseOrigin("https://example.com:443")?.port, "")110 XCTAssertEqual(SPV1.parseOrigin("https://example.com:8443")?.port, "8443")111 XCTAssertEqual(SPV1.parseOrigin("https://example.com.")?.host, "example.com")112 }113114 func testMatchModes() {115 let page = "https://login.example.com/account?x=1"116 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://www.example.com", match: .domain), pageUrl: page), .match)117 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "example.com", match: .domain), pageUrl: page), .match)118 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://www.example.com", match: .host), pageUrl: page), .no)119 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://login.example.com", match: .host), pageUrl: page), .match)120 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://login.example.com:8443", match: .host), pageUrl: page), .no)121 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://login.example.com/account", match: .exact), pageUrl: page), .match)122 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://login.example.com/account/", match: .exact), pageUrl: page), .match)123 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://login.example.com/other", match: .exact), pageUrl: page), .no)124 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://login.example.com/account", match: .never), pageUrl: page), .no)125 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://example.org", match: .domain), pageUrl: page), .no)126 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://alice.github.io", match: .domain), pageUrl: "https://bob.github.io"), .no)127 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://github.io", match: .domain), pageUrl: "https://github.io"), .no, "public suffixes never match by domain")128 // scheme downgrade129 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://example.com"), pageUrl: "http://example.com"), .no)130 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "http://example.com"), pageUrl: "http://example.com"), .insecure)131 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "http://example.com"), pageUrl: "https://example.com"), .match)132 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "https://localhost"), pageUrl: "http://localhost:3000"), .match)133 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "http://127.0.0.1"), pageUrl: "http://127.0.0.1:8080/x"), .match)134 XCTAssertEqual(SPV1.matchUrl(saved: ItemUrl(url: "http://192.168.1.10"), pageUrl: "http://192.168.1.10/"), .insecure)135 }136137 func testCandidatesLookalikeAndSavedDomains() {138 func login(_ title: String, _ urls: [ItemUrl]) -> Item { var i = SPV1.newItem(.login, title: title); i.urls = urls; return i }139 var trashed = login("trashed", [ItemUrl(url: "https://example.com")]); trashed.trashedAt = "2026-01-01T00:00:00.000Z"140 var note = SPV1.newItem(.note, title: "note"); note.urls = [ItemUrl(url: "https://example.com")]141 let items: [(ref: Int, item: Item)] = [142 (1, login("a", [ItemUrl(url: "https://example.com")])), (2, login("b", [ItemUrl(url: "http://example.com")])), (3, login("c", [ItemUrl(url: "https://other.org")])),143 (4, trashed), (5, note), (6, login("d", [ItemUrl(url: "https://other.org"), ItemUrl(url: "https://app.example.com", match: .host)])),144 ]145 XCTAssertEqual(SPV1.candidatesFor(items, pageUrl: "https://www.example.com/login").map(\.ref), [1, 2])146 let http = SPV1.candidatesFor(items, pageUrl: "http://www.example.com/login")147 XCTAssertEqual(http.map(\.ref), [2]); XCTAssertEqual(http.first?.insecure, true)148 XCTAssertEqual(SPV1.candidatesFor(items, pageUrl: "https://app.example.com").map(\.ref), [1, 2, 6])149 XCTAssertTrue(SPV1.candidatesFor(items, pageUrl: "https://example.com@evil.com").isEmpty)150151 XCTAssertEqual(SPV1.savedDomains(items.map(\.item)), ["example.com", "other.org"])152 XCTAssertEqual(SPV1.lookalike(pageUrl: "https://paypa1.com", savedDomains: ["paypal.com"]), LookalikeWarning(page: "paypa1.com", resembles: "paypal.com", reason: .confusable))153 XCTAssertEqual(SPV1.lookalike(pageUrl: "https://rnicrosoft.com", savedDomains: ["microsoft.com"])?.reason, .confusable)154 XCTAssertEqual(SPV1.lookalike(pageUrl: "https://xn--pple-43d.com", savedDomains: ["apple.com"])?.reason, .confusable)155 XCTAssertEqual(SPV1.lookalike(pageUrl: "https://paypol.com", savedDomains: ["paypal.com"])?.reason, .near)156 XCTAssertEqual(SPV1.lookalike(pageUrl: "https://paypal.co.uk", savedDomains: ["paypal.com"])?.reason, .near)157 XCTAssertNil(SPV1.lookalike(pageUrl: "https://paypal.com", savedDomains: ["paypal.com"]))158 XCTAssertNil(SPV1.lookalike(pageUrl: "https://example.org", savedDomains: ["paypal.com"]))159 XCTAssertEqual(SPV1.lookalike(pageUrl: "https://аpple.com", savedDomains: ["example.com"])?.reason, .mixedScript)160 XCTAssertEqual(SPV1.skeleton("xn--pple-43d.com"), "apple.com")161 XCTAssertEqual(SPV1.skeleton("vvikipedia.org"), "wikipedia.org")162 }163164 /// Optional bulk comparison against tldts: set SPV_PSL_BULK to a JSON file `[[host, domainOrNull], …]`.165 func testBulkAgainstTldts() throws {166 guard let path = ProcessInfo.processInfo.environment["SPV_PSL_BULK"] else { throw XCTSkip("set SPV_PSL_BULK to run the bulk tldts comparison") }167 let cases = try loadJSON(URL(fileURLWithPath: path)).arrayValue ?? []168 var mismatches: [String] = []169 for c in cases {170 guard let host = c[0]?.stringValue else { continue }171 let got = SPV1.parseOrigin("https://" + host)?.domain172 if got != c[1]?.stringValue { mismatches.append("\(host): swift=\(got ?? "nil") tldts=\(c[1]?.stringValue ?? "nil")") }173 }174 print("bulk PSL comparison: \(cases.count) hosts, \(mismatches.count) mismatches")175 for m in mismatches.prefix(40) { print(" ", m) }176 XCTAssertGreaterThan(cases.count, 1000)177 XCTAssertLessThan(Double(mismatches.count), Double(cases.count) * 0.01, "more than 1 % of hosts differ from tldts")178 }179}180