RadrootsNativeDestinationTests.swift (3415B)
1 import Foundation 2 @testable import RadrootsKit 3 import Testing 4 5 @Test(arguments: [ 6 "https://user@example.org/upload", "https://user:secret@example.org/upload", 7 "https://example.org/upload?token=secret", "https://example.org/upload#fragment", 8 "https://example.org:0/upload", "https://example.org:65536/upload", 9 "http://example.org/upload", "https://example.org/a/../upload", 10 "https://example.org/%2e/upload", "https://example.org/%2Fupload", 11 "https://example.org/%5cupload", "https://example.org/%00upload" 12 ]) func nativeDestinationRejectsAmbiguousAuthority(_ raw: String) throws { 13 let url = try #require(URL(string: raw)) 14 #expect(throws: RadrootsBackgroundTransferError.invalidRequest) { 15 try RadrootsNativeDestinationPolicy.validate(url, policy: .publicHTTPS) 16 } 17 } 18 19 @Test(arguments: ["127.0.0.2", "127.1", "[::ffff:127.0.0.1]", "localhost.example.org", "example.org"]) 20 func nativeDestinationRejectsFixtureEscape(_ host: String) throws { 21 let url = try #require(URL(string: "http://\(host):8080/upload")) 22 #expect(throws: RadrootsBackgroundTransferError.invalidRequest) { 23 try RadrootsNativeDestinationPolicy.validate(url, policy: .simulatorLoopbackHTTP) 24 } 25 } 26 27 @Test func nativeDestinationAcceptsExplicitAuthorityAndReportsPlatformCapability() throws { 28 try RadrootsNativeDestinationPolicy.validate(#require(URL(string: "https://example.org:443/upload")), 29 policy: .publicHTTPS) 30 #expect(!RadrootsAppleBackgroundTransferAdapters.supportsNewEnqueue(for: .publicHTTPS)) 31 #if os(iOS) && targetEnvironment(simulator) 32 #expect(RadrootsAppleBackgroundTransferAdapters.supportsNewEnqueue(for: .simulatorLoopbackHTTP)) 33 #else 34 #expect(!RadrootsAppleBackgroundTransferAdapters.supportsNewEnqueue(for: .simulatorLoopbackHTTP)) 35 #endif 36 #if !os(iOS) || targetEnvironment(simulator) 37 for host in ["127.0.0.1", "localhost", "[::1]"] { 38 try RadrootsNativeDestinationPolicy.validate(#require(URL(string: "http://\(host):8080/upload")), 39 policy: .simulatorLoopbackHTTP) 40 } 41 #endif 42 } 43 44 @Test func nativeDestinationRequiresOriginalMethodOriginAndPath() throws { 45 var original = try URLRequest(url: #require(URL(string: "https://example.org/upload"))) 46 original.httpMethod = "PUT" 47 #expect(RadrootsNativeDestinationPolicy.responseMatches( 48 URL(string: "https://EXAMPLE.org:443/upload"), original: original, current: original 49 )) 50 for raw in ["http://example.org/upload", "https://other.org/upload", "https://example.org:444/upload", 51 "https://example.org/other", "https://example.org/upload?token=secret"] { 52 #expect(!RadrootsNativeDestinationPolicy.responseMatches( 53 URL(string: raw), 54 original: original, 55 current: original 56 )) 57 } 58 var redirected = original 59 redirected.httpMethod = "GET" 60 #expect(!RadrootsNativeDestinationPolicy.responseMatches(original.url, original: original, current: redirected)) 61 redirected = original 62 redirected.url = URL(string: "https://example.org/other") 63 #expect(!RadrootsNativeDestinationPolicy.responseMatches(original.url, original: original, current: redirected)) 64 #expect(!RadrootsNativeDestinationPolicy.responseMatches(nil, original: original, current: original)) 65 }