iOS/Tests/Shared/SecTrust+TestAdditions.swift

126 lines
8.3 KiB
Swift

import Foundation
import XCTest
class FailingURLAuthenticationChallengeSender: NSObject, URLAuthenticationChallengeSender {
func use(_ credential: URLCredential, for challenge: URLAuthenticationChallenge) {
XCTFail()
}
func continueWithoutCredential(for challenge: URLAuthenticationChallenge) {
XCTFail()
}
func cancel(_ challenge: URLAuthenticationChallenge) {
XCTFail()
}
}
extension SecTrust {
private static func secTrust(with string: String) throws -> SecTrust {
let certificateData = try XCTUnwrap(Data(base64Encoded: string, options: [.ignoreUnknownCharacters]))
let certificate = try XCTUnwrap(SecCertificateCreateWithData(nil, certificateData as CFData))
var secTrust: SecTrust?
SecTrustCreateWithCertificates([certificate] as CFArray, nil, &secTrust)
return try XCTUnwrap(secTrust)
}
func authenticationChallenge() -> URLAuthenticationChallenge {
URLAuthenticationChallenge(
protectionSpace: {
let space = URLProtectionSpace(
host: "UnitTest.Example.com",
port: 443,
protocol: nil,
realm: nil,
authenticationMethod: NSURLAuthenticationMethodServerTrust
)
// no public method exists to construct with a SecTrust
space.perform(Selector(("_setServerTrust:")), with: self)
return space
}(),
proposedCredential: nil,
previousFailureCount: 0,
failureResponse: nil,
error: nil,
sender: FailingURLAuthenticationChallengeSender()
)
}
static var unitTestDotExampleDotCom1: SecTrust {
get throws {
try secTrust(with: """
MIIFSjCCAzICCQCoZrGH86VmVzANBgkqhkiG9w0BAQsFADBnMQswCQYDVQQGEwJVVDELMAkGA1UECAwCVVQxEjAQBgNVBAcMCVVu
aXQgVGVzdDELMAkGA1UECgwCVVQxCzAJBgNVBAsMAlVUMR0wGwYDVQQDDBRVbml0VGVzdC5FeGFtcGxlLmNvbTAeFw0yMjA1MjIw
NTA4NTZaFw0yMzA1MjEwNTA4NTZaMGcxCzAJBgNVBAYTAlVUMQswCQYDVQQIDAJVVDESMBAGA1UEBwwJVW5pdCBUZXN0MQswCQYD
VQQKDAJVVDELMAkGA1UECwwCVVQxHTAbBgNVBAMMFFVuaXRUZX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HK931o6kuF3nWrb6zpqCzwuU/lAL6S9v26bDi3rZZy7UMj+16LiWA3A16VxK/D9WlPHoQUjZVt8Kx8RJcunbIWnD
omndP+B1mYsaTAHpsJmmbwI5rrkHrDjc/sFsiaSm0vM0yec+VO7YceGWZ4wwjfU+TNuaE7sk9c3y4e/siQGU1leSLbHifAOJq3rP
jDsspDOMwS5iD+ofzFqJE61MBOygqBf6kz0nCQwWOUR77uCXO4goN/Jjkta7PmIOCnRwo8uP0/gXquCtyZtXlBPhLUYVpAvPutLX
uu1ix+3LYmd0B7J0hup/qZIlYuXoarfd909lBj2nfmrS2R8sF205cBQgPZU7gmtoPFby/LcQ/SydLRhj2n3zvpRC+uKIQkzJAwmr
PufRFkjYWLE=
""")
}
}
static var unitTestDotExampleDotCom2: SecTrust {
get throws {
try secTrust(with: """
MIIFSjCCAzICCQDbnKTUkLw1IDANBgkqhkiG9w0BAQsFADBnMQswCQYDVQQGEwJVVDELMAkGA1UECAwCVVQxEjAQBgNVBAcMCVVu
aXQgVGVzdDELMAkGA1UECgwCVVQxCzAJBgNVBAsMAlVUMR0wGwYDVQQDDBRVbml0VGVzdC5FeGFtcGxlLmNvbTAeFw0yMjA1MjIw
NTA5MzBaFw0yMzA1MjEwNTA5MzBaMGcxCzAJBgNVBAYTAlVUMQswCQYDVQQIDAJVVDESMBAGA1UEBwwJVW5pdCBUZXN0MQswCQYD
VQQKDAJVVDELMAkGA1UECwwCVVQxHTAbBgNVBAMMFFVuaXRUZXN0LkV4YW1wbGUuY29tMIICIjANBgkqhkiG9w0BAQEFAAOCAg8A
MIICCgKCAgEA4LVVbRwLUf9sXmO6a/9rftFpV5E9MMSI5aRj73fjcyfmf1v7+Lg4LqS47/11/uSxVzbRP9awHASdoVvzSwqITLDU
agVzK8fK/yDRqq383mfXEZEgQKaWfxMVDm9ywT5MyYkucORGn3GrooG9GdHPSiMtpnsyN+TsInuzMP3Ou+2nr0Tg1CJodJfaq7Wt
MlVvCwEc3xAcdrqmvtqDk0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A1WbLM3+
laDS8AFOvxKa5jDcIIr96WPCcsu4mE1hmSTz9o7wT6vLNziXd9nhyEMJ+Ln/bRhef+OZ/1VPK76XaeAmsdFVBpjFOXSL2IS8650y
z/KgFzrCLbk=
""")
}
}
static var unitTestDotExampleDotCom3: SecTrust {
get throws {
try secTrust(with: """
MIIFPDCCAyQCCQChhRw3eVsWujANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJVVDELMAkGA1UECAwCVVQxCzAJBgNVBAcMAlVU
MQswCQYDVQQKDAJVVDELMAkGA1UECwwCVVQxHTAbBgNVBAMMFFVuaXRUZXN0LkV4YW1wbGUuY29tMB4XDTIyMDUyMjA1MTIzOVoX
DTIzMDUyMTA1MTIzOVowYDELMAkGA1UEBhMCVVQxCzAJBgNVBAgMAlVUMQswCQYDVQQHDAJVVDELMAkGA1UECgwCVVQxCzAJBgNV
BAsMAlVUMR0wGwYDVQQDDBRVbml0VGVzdC5FeGFtcGxlLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAOFXnZF/
KNQLNTZdZJZodpl85iaxw/w5YSF6lKc6Dx3IU+4KI6oFK9VBdGMLxYs9OjbUoEGzDtAot0+SN2ay4c7080zSuRIdB9E9fAnl9mzR
huitAmGly+e88I9NFQMLeFWCshth02rghkYi6tVuSP8G7koIcOMDGEel00sb9N3F1gNIlFz85LjOD8PHJNkcPdSyZWuf7o7HBCf+
QRM5eQSl6/vpsWgQgRucRZkx/GV88ltoaQ1s5nuIQdIE85UDSNIAxySkjpFFFzEf/CaXcgL4hofats089a6NEP82kWIK4Px4QIqX
VTDC1G7rcWMUU4963thK08dV6Gjv07vTDRZpTIQZjrSEiep3uJ5gHdu73LYm2NdEEUkb34ZfcyaFxaswyvMN+O83hASEs5erbqTT
xyuqrpv3vHSKdyatKPA3tvVs1DedwBalbXB5wVJOnmTf2waYXnCYZJqV1YuAHXHmWomPZEKHk6oqS7+PWblsKNoaorpEscNrS4eJ
BByMPvFnYaAaP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