SecTrustPolicy失败并带有自签名证书

时间:2015-08-12 16:59:10

标签: ssl alamofire self-signed

所以我创建了这个测试用例(现有的alamofire测试用例的混搭):

func testHTTPBasicAuthenticationWithValidCredentialsSelfSignedSuccess() {
    // Given
    let expectation = expectationWithDescription("\(URLString) 200")

    var request: NSURLRequest?
    var response: NSHTTPURLResponse?
    var data: NSData?
    var error: NSError?

    setRootCertificateAsLoneAnchorCertificateForTrust(serverTrust)
    let policies = [SecPolicyCreateBasicX509()]
    SecTrustSetPolicies(serverTrust, policies)

    // When
    Alamofire.request(.GET, URLString)
        .authenticate(user: user, password: password)
        .response { responseRequest, responseResponse, responseData, responseError in
            request = responseRequest
            response = responseResponse
            data = responseData
            error = responseError

            expectation.fulfill()
    }

    waitForExpectationsWithTimeout(defaultTimeout, handler: nil)

    // Then
    XCTAssertNotNil(request, "request should not be nil")
    XCTAssertNotNil(response, "response should not be nil")
    XCTAssertTrue(data?.length > 0, "Data not found.")
    XCTAssertNil(error, "error should be nil")

}

根CA是现有视线的自签名证书。

但是我收到了这个错误:

  

测试套件'选择的测试'开始于2015-08-12 12:46:37.512测试   Suite' StageAuthentication'开始于2015-08-12 12:46:37.514测试   案例' - [Alamofire_iOS_Tests.StageAuthentication   testHTTPBasicAuthenticationWithValidCredentialsSelfSignedSuccess]'   开始。 2015-08-12 12:46:37.663 xctest [3641:12220875]   NSURLSession / NSURLConnection HTTP加载失败   (kCFStreamErrorDomainSSL,-9813)   /Users/wynne_b/Alamofire/Tests/QuestAuthentication.swift:309:错误:    - [Alamofire_iOS_Tests.StageAuthentication testHTTPBasicAuthenticationWithValidCredentialsSelfSignedSuccess]:   XCTAssertNotNil失败 - 响应不应为零   /Users/wynne_b/Alamofire/Tests/QuestAuthentication.swift:310:错误:    - [Alamofire_iOS_Tests.StageAuthentication testHTTPBasicAuthenticationWithValidCredentialsSelfSignedSuccess]:   XCTAssertTrue失败 - 找不到数据。   /Users/wynne_b/Alamofire/Tests/QuestAuthentication.swift:311:错误:    - [Alamofire_iOS_Tests.StageAuthentication testHTTPBasicAuthenticationWithValidCredentialsSelfSignedSuccess]:   XCTAssertNil失败:"错误域= NSURLErrorDomain代码= -1202"   此服务器的证书无效。你可能正在连接到   假装是“portal.care180.com”的服务器可以放   您的机密信息存在风险。"   UserInfo = {NSLocalizedDescription =此服务器的证书是   无效。您可能正在连接到假装的服务器   “portal.care180.com”,可以提供您的机密信息   冒险。,NSLocalizedRecoverySuggestion =你想连接到   无论如何服务器?,_kCFStreamErrorDomainKey = 3,   NSUnderlyingError = 0x7ae21c60 {错误域= kCFErrorDomainCFNetwork   Code = -1202"(null)"   的UserInfo = {_ kCFStreamPropertySSLClientCertificateState = 0,   _kCFNetworkCFStreamSSLErrorOriginalValue = -9813,_kCFStreamErrorCodeKey = -9813,_kCFStreamErrorDomainKey = 3,kCFStreamPropertySSLPeerTrust =,   kCFStreamPropertySSLPeerCertificates = {type = immutable,count = 1,values =(0:    )}}},_kCFStreamErrorCodeKey = -9813,   NSErrorFailingURLStringKey = https://portal.care180.com/services/init.json,   NSErrorPeerCertificateChainKey = {type =   immutable,count = 1,values =(0:)},   NSErrorClientCertificateStateKey = 0,   NSURLErrorFailingURLPeerTrustErrorKey =,   NSErrorFailingURLKey = https://portal.care180.com/services/init.json}" -   错误应该是零测试用例   ' - [Alamofire_iOS_Tests.StageAuthentication   testHTTPBasicAuthenticationWithValidCredentialsSelfSignedSuccess]'   失败(0.156秒)。测试套件' StageAuthentication'失败了   2015-08-12 12:46:37.671执行1次测试,3次失败(0   在0.156(0.157)秒内测试套件'选择测试'   在2015-08-12 12:46:37.672失败。执行1次测试,3次失败   (0意外)在0.156(0.160)秒内程序以退出代码结束:   1

对于密集的抱歉:我做错了什么?或者是否有Alamofire测试使用不同的证书和主持人进行此操作?

1 个答案:

答案 0 :(得分:0)

我混淆了根和叶子。我的坏。