Repository Public License
1// ORIX Secure2// File: Packages/ORIXURLRisk/Tests/ORIXURLRiskTests/PunycodeAndPrimitivesTests.swift3// Description: Tests of the building blocks: RFC 3492 Punycode vectors, script analysis, confusable skeletons, edit4// distance, public suffix split and IP literal parsing.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10@testable import ORIXURLRisk1112private func text(_ hex: String) -> String {13 var view = String.UnicodeScalarView()14 for part in hex.split(separator: " ") {15 if let value = UInt32(part, radix: 16), let scalar = Unicode.Scalar(value) { view.append(scalar) }16 }17 return String(view)18}1920@Suite("Punycode (RFC 3492)")21struct PunycodeTests {22 /// RFC 3492 section 7.1 sample strings: (name, code points, Punycode).23 static let vectors: [(String, String, String)] = [24 ("A Arabic", "0644 064A 0647 0645 0627 0628 062A 0643 0644 0645 0648 0634 0639 0631 0628 064A 061F", "egbpdaj6bu4bxfgehfvwxn"),25 ("B Chinese simplified", "4ED6 4EEC 4E3A 4EC0 4E48 4E0D 8BF4 4E2D 6587", "ihqwcrb4cv8a8dqg056pqjye"),26 ("C Chinese traditional", "4ED6 5011 7232 4EC0 9EBD 4E0D 8AAA 4E2D 6587", "ihqwctvzc91f659drss3x8bo0yb"),27 ("D Czech", "0050 0072 006F 010D 0070 0072 006F 0073 0074 011B 006E 0065 006D 006C 0075 0076 00ED 010D 0065 0073 006B 0079", "Proprostnemluvesky-uyb24dma41a"),28 ("E Hebrew", "05DC 05DE 05D4 05D4 05DD 05E4 05E9 05D5 05D8 05DC 05D0 05DE 05D3 05D1 05E8 05D9 05DD 05E2 05D1 05E8 05D9 05EA", "4dbcagdahymbxekheh6e0a7fei0b"),29 ("F Hindi", "092F 0939 0932 094B 0917 0939 093F 0928 094D 0926 0940 0915 094D 092F 094B 0902 0928 0939 0940 0902 092C 094B 0932 0938 0915 0924 0947 0939 0948 0902", "i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd"),30 ("G Japanese", "306A 305C 307F 3093 306A 65E5 672C 8A9E 3092 8A71 3057 3066 304F 308C 306A 3044 306E 304B", "n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa"),31 ("H Korean", "C138 ACC4 C758 BAA8 B4E0 C0AC B78C B4E4 C774 D55C AD6D C5B4 B97C C774 D574 D55C B2E4 BA74 C5BC B9C8 B098 C88B C744 AE4C", "989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c"),32 ("I Russian", "043F 043E 0447 0435 043C 0443 0436 0435 043E 043D 0438 043D 0435 0433 043E 0432 043E 0440 044F 0442 043F 043E 0440 0443 0441 0441 043A 0438", "b1abfaaepdrnnbgefbadotcwatmq2g4l"),33 ("J Spanish", "0050 006F 0072 0071 0075 00E9 006E 006F 0070 0075 0065 0064 0065 006E 0073 0069 006D 0070 006C 0065 006D 0065 006E 0074 0065 0068 0061 0062 006C 0061 0072 0065 006E 0045 0073 0070 0061 00F1 006F 006C", "PorqunopuedensimplementehablarenEspaol-fmd56a"),34 ("K Vietnamese", "0054 1EA1 0069 0073 0061 006F 0068 1ECD 006B 0068 00F4 006E 0067 0074 0068 1EC3 0063 0068 1EC9 006E 00F3 0069 0074 0069 1EBF 006E 0067 0056 0069 1EC7 0074", "TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g"),35 ("L 3年B組金八先生", "0033 5E74 0042 7D44 91D1 516B 5148 751F", "3B-ww4c5e180e575a65lsy2b"),36 ("M 安室奈美恵-with-SUPER-MONKEYS", "5B89 5BA4 5948 7F8E 6075 002D 0077 0069 0074 0068 002D 0053 0055 0050 0045 0052 002D 004D 004F 004E 004B 0045 0059 0053", "-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n"),37 ("N Hello-Another-Way", "0048 0065 006C 006C 006F 002D 0041 006E 006F 0074 0068 0065 0072 002D 0057 0061 0079 002D 305D 308C 305E 308C 306E 5834 6240", "Hello-Another-Way--fc4qua05auwb3674vfr0b"),38 ("O ひとつ屋根の下2", "3072 3068 3064 5C4B 6839 306E 4E0B 0032", "2-u9tlzr9756bt3uc0v"),39 ("P MajiでKoiする5秒前", "004D 0061 006A 0069 3067 004B 006F 0069 3059 308B 0035 79D2 524D", "MajiKoi5-783gue6qz075azm5e"),40 ("Q パフィーdeルンバ", "30D1 30D5 30A3 30FC 0064 0065 30EB 30F3 30D0", "de-jg4avhby1noc0d"),41 ("R そのスピードで", "305D 306E 30B9 30D4 30FC 30C9 3067", "d9juau41awczczp"),42 ("S ASCII", "002D 003E 0020 0024 0031 002E 0030 0030 0020 003C 002D", "-> $1.00 <--"),43 ]4445 @Test("RFC 3492 section 7.1 vectors encode", arguments: 0..<vectors.count)46 func encodeVector(_ index: Int) throws {47 let (name, hex, expected) = Self.vectors[index]48 let encoded = try Punycode.encode(text(hex))49 #expect(encoded == expected, "\(name)")50 }5152 @Test("RFC 3492 section 7.1 vectors decode and round-trip", arguments: 0..<vectors.count)53 func decodeVector(_ index: Int) throws {54 let (name, hex, encoded) = Self.vectors[index]55 let decoded = try Punycode.decode(encoded)56 #expect(decoded == text(hex), "\(name)")57 let again = try Punycode.encode(decoded)58 #expect(again == encoded, "\(name)")59 }6061 @Test("Known domain labels")62 func labels() {63 #expect(IDNLabel.toASCII("münchen") == "xn--mnchen-3ya")64 #expect(IDNLabel.toASCII("例え") == "xn--r8jz45g")65 #expect(IDNLabel.toASCII("пример") == "xn--e1afmkfd")66 #expect(IDNLabel.toASCII("рф") == "xn--p1ai")67 #expect(IDNLabel.toASCII("bücher") == "xn--bcher-kva")68 #expect(IDNLabel.toASCII("MÜNCHEN") == "xn--mnchen-3ya")69 #expect(IDNLabel.toASCII("plain") == "plain")70 #expect(IDNLabel.toUnicode("xn--mnchen-3ya") == "münchen")71 #expect(IDNLabel.toUnicode("XN--MNCHEN-3YA") == "münchen")72 #expect(IDNLabel.toUnicode("xn--80ak6aa92e") == "аррӏе")73 #expect(IDNLabel.toUnicode("plain") == "plain")74 }7576 @Test("Malformed Punycode is rejected without crashing")77 func malformed() {78 let bad = ["xn--", "xn--999999999999", "xn--zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", "xn--a", "xn--abc", "xn--é", "xn---", "xn--a-", "xn--\u{0}", "xn--99999999999999999999999999999999999999999999"]79 for label in bad {80 #expect(IDNLabel.toUnicode(label) == nil, "\(label)")81 }82 #expect(throws: PunycodeError.self) { _ = try Punycode.decode("é") }83 #expect(throws: PunycodeError.self) { _ = try Punycode.decode("a-!") }84 #expect(throws: PunycodeError.self) { _ = try Punycode.decode("99999999999") }85 #expect(throws: PunycodeError.self) { _ = try Punycode.decode(String(repeating: "a", count: 5000)) }86 #expect(throws: PunycodeError.self) { _ = try Punycode.encode(String(repeating: "é", count: 5000)) }87 let empty = try? Punycode.decode("")88 #expect(empty == "")89 }9091 @Test("Random strings round-trip and random garbage never crashes")92 func randomized() throws {93 var generator = SplitMix(seed: 42)94 for _ in 0..<500 {95 var view = String.UnicodeScalarView()96 for _ in 0..<Int(generator.next() % 20) {97 let pools: [ClosedRange<UInt32>] = [0x61...0x7A, 0xE0...0xFF, 0x430...0x44F, 0x4E00...0x4FFF, 0x1F600...0x1F64F]98 let pool = pools[Int(generator.next() % UInt64(pools.count))]99 let value = pool.lowerBound + UInt32(generator.next() % UInt64(pool.count))100 if let scalar = Unicode.Scalar(value) { view.append(scalar) }101 }102 let original = String(view)103 let encoded = try Punycode.encode(original)104 let decoded = try Punycode.decode(encoded)105 #expect(decoded == original)106 }107 for _ in 0..<2000 {108 let bytes = (0..<Int(generator.next() % 40)).map { _ in UInt8(truncatingIfNeeded: generator.next()) }109 let garbage = String(decoding: bytes, as: UTF8.self)110 _ = try? Punycode.decode(garbage)111 _ = try? Punycode.encode(garbage)112 _ = IDNLabel.toUnicode("xn--" + garbage)113 }114 }115}116117struct SplitMix: RandomNumberGenerator {118 var state: UInt64119 init(seed: UInt64) { state = seed }120 mutating func next() -> UInt64 {121 state &+= 0x9E37_79B9_7F4A_7C15122 var z = state123 z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9124 z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB125 return z ^ (z >> 31)126 }127}128129@Suite("Scripts, confusables, edit distance")130struct ScriptTests {131 @Test func scriptsOfLabels() {132 #expect(ScriptAnalysis.scripts(in: "apple") == [.latin])133 #expect(ScriptAnalysis.scripts(in: "аррӏе") == [.cyrillic])134 #expect(ScriptAnalysis.scripts(in: "pаypal") == [.latin, .cyrillic])135 #expect(ScriptAnalysis.scripts(in: "例え") == [.han, .hiragana])136 #expect(ScriptAnalysis.scripts(in: "münchen-24") == [.latin])137 #expect(ScriptAnalysis.scripts(in: "ελληνικά") == [.greek])138 #expect(ScriptAnalysis.scripts(in: "հայ") == [.armenian])139 #expect(ScriptAnalysis.scripts(in: "עברית") == [.hebrew])140 #expect(ScriptAnalysis.scripts(in: "عربي") == [.arabic])141 #expect(ScriptAnalysis.scripts(in: "한국어") == [.hangul])142 #expect(ScriptAnalysis.scripts(in: "カタカナ") == [.katakana])143 #expect(ScriptAnalysis.scripts(in: "ไทย") == [.thai])144 #expect(ScriptAnalysis.scripts(in: "123-456") == [])145 }146147 @Test func ordinaryCombinations() {148 #expect(ScriptAnalysis.isOrdinaryCombination([.han, .hiragana, .katakana]))149 #expect(ScriptAnalysis.isOrdinaryCombination([.han, .hangul]))150 #expect(ScriptAnalysis.isOrdinaryCombination([.latin, .han, .katakana]))151 #expect(ScriptAnalysis.isOrdinaryCombination([.latin]))152 #expect(!ScriptAnalysis.isOrdinaryCombination([.latin, .cyrillic]))153 #expect(!ScriptAnalysis.isOrdinaryCombination([.latin, .greek]))154 #expect(!ScriptAnalysis.isOrdinaryCombination([.cyrillic, .greek]))155 #expect(!ScriptAnalysis.isOrdinaryCombination([.hiragana, .hangul]))156 }157158 @Test func skeletons() {159 #expect(Confusables.skeleton("аррӏе") == "apple")160 #expect(Confusables.skeleton("αρρle") == "apple")161 #expect(Confusables.skeleton("paypa1") == "paypal")162 #expect(Confusables.skeleton("g00gle") == "google")163 #expect(Confusables.skeleton("pâypal") == "paypal")164 #expect(Confusables.skeleton("rnicrosoft", foldSequences: true) == "microsoft")165 #expect(Confusables.skeleton("rnicrosoft") == "rnicrosoft")166 #expect(Confusables.skeleton("example") == "example")167 #expect(Confusables.isEntirelyStrongLookalikes("аррӏе"))168 #expect(Confusables.isEntirelyStrongLookalikes("сосо"))169 #expect(!Confusables.isEntirelyStrongLookalikes("пример"))170 #expect(!Confusables.isEntirelyStrongLookalikes("москва"))171 #expect(!Confusables.isEntirelyStrongLookalikes("ελληνικά"))172 }173174 @Test func editDistance() {175 #expect(EditDistance.damerauLevenshtein("paypal", "paypal") == 0)176 #expect(EditDistance.damerauLevenshtein("paypol", "paypal") == 1)177 #expect(EditDistance.damerauLevenshtein("micorsoft", "microsoft") == 1)178 #expect(EditDistance.damerauLevenshtein("netfliix", "netflix") == 1)179 #expect(EditDistance.damerauLevenshtein("netflx", "netflix") == 1)180 #expect(EditDistance.damerauLevenshtein("kitten", "sitting") == 3)181 #expect(EditDistance.damerauLevenshtein("", "abc") == 3)182 #expect(EditDistance.damerauLevenshtein("abc", "") == 3)183 #expect(EditDistance.damerauLevenshtein("ca", "abc") == 3)184 }185}186187@Suite("Public suffix subset")188struct PublicSuffixTests {189 @Test func splits() {190 let cases: [(String, String, String, [String])] = [191 ("www.example.com", "com", "example.com", ["www"]),192 ("example.co.uk", "co.uk", "example.co.uk", []),193 ("a.b.example.co.uk", "co.uk", "example.co.uk", ["a", "b"]),194 ("www.revenu.gouv.qc.ca", "qc.ca", "gouv.qc.ca", ["www", "revenu"]),195 ("shop.example.com.au", "com.au", "example.com.au", ["shop"]),196 ("user.github.io", "github.io", "user.github.io", []),197 ("github.io", "io", "github.io", []),198 ("co.uk", "uk", "co.uk", []),199 ("www.cra-arc.gc.ca", "gc.ca", "cra-arc.gc.ca", ["www"]),200 ("my-app.web.app", "web.app", "my-app.web.app", []),201 ("account.blob.core.windows.net", "blob.core.windows.net", "account.blob.core.windows.net", []),202 ]203 for (host, suffix, registrable, subdomains) in cases {204 let split = PublicSuffix.split(labels: host.split(separator: ".").map(String.init))205 #expect(split?.publicSuffix == suffix, "\(host)")206 #expect(split?.registrableDomain == registrable, "\(host)")207 #expect(split?.subdomainLabels == subdomains, "\(host)")208 }209 #expect(PublicSuffix.split(labels: ["localhost"]) == nil)210 #expect(PublicSuffix.split(labels: []) == nil)211 }212}213214@Suite("IP literals")215struct IPLiteralTests {216 @Test func obfuscatedIPv4() {217 let cases: [(String, String, Bool, IPAddressScope)] = [218 ("192.168.1.1", "192.168.1.1", false, .privateNetwork),219 ("3232235777", "192.168.1.1", true, .privateNetwork),220 ("0xC0A80101", "192.168.1.1", true, .privateNetwork),221 ("0300.0250.0001.0001", "192.168.1.1", true, .privateNetwork),222 ("0xc0.0250.1.1", "192.168.1.1", true, .privateNetwork),223 ("192.168.257", "192.168.1.1", true, .privateNetwork),224 ("0x7f.1", "127.0.0.1", true, .loopback),225 ("017700000001", "127.0.0.1", true, .loopback),226 ("2130706433", "127.0.0.1", true, .loopback),227 ("8.8.8.8", "8.8.8.8", false, .publicInternet),228 ("134744072", "8.8.8.8", true, .publicInternet),229 ("10.0.0.1", "10.0.0.1", false, .privateNetwork),230 ("172.16.5.4", "172.16.5.4", false, .privateNetwork),231 ("172.32.5.4", "172.32.5.4", false, .publicInternet),232 ("169.254.10.10", "169.254.10.10", false, .linkLocal),233 ("100.100.1.1", "100.100.1.1", false, .sharedAddressSpace),234 ("224.0.0.1", "224.0.0.1", false, .reserved),235 ("1.2.3.4.", "1.2.3.4", false, .publicInternet),236 ]237 for (written, canonical, obfuscated, scope) in cases {238 let ip = IPParser.parseIPv4(written)239 #expect(ip?.canonical == canonical, "\(written)")240 #expect(ip?.isObfuscated == obfuscated, "\(written)")241 #expect(ip?.scope == scope, "\(written)")242 }243 for notIP in ["example.com", "1.2.3.4.5", "256.1.1.1", "1.2.3.999", "4294967296", "1..2", "09.1.1.1", "abc", "", "1.2.3.com", "0xGG"] {244 #expect(IPParser.parseIPv4(notIP) == nil, "\(notIP)")245 }246 }247248 @Test func ipv6() {249 #expect(IPParser.parseIPv6("::1")?.scope == .loopback)250 #expect(IPParser.parseIPv6("fe80::1")?.scope == .linkLocal)251 #expect(IPParser.parseIPv6("fe80::1%25en0")?.scope == .linkLocal)252 #expect(IPParser.parseIPv6("fd12:3456:789a::1")?.scope == .privateNetwork)253 #expect(IPParser.parseIPv6("2001:4860:4860::8888")?.scope == .publicInternet)254 #expect(IPParser.parseIPv6("2001:4860:4860:0:0:0:0:8888")?.canonical == "2001:4860:4860::8888")255 #expect(IPParser.parseIPv6("::ffff:192.168.1.1")?.scope == .privateNetwork)256 #expect(IPParser.parseIPv6("not-an-address") == nil)257 #expect(IPParser.parseIPv6("") == nil)258 #expect(IPParser.parseIPv6("1:2:3:4:5:6:7:8:9") == nil)259 }260}261