如何在我的 iOS 模拟器上自动安装自定义 CA 根证书以进行测试?

How can I automatically install a custom CA root certificate on my iOS simulators for testing?

我的 iOS 应用程序与我的具有有效证书的后端服务器通信。在开发过程中,我的 iOS 应用程序配置为与我的内部测试服务器通信,该服务器具有由我的自签名根证书签名的证书。

在 iOS11 之前,我可以使用此处列出的说明将我的应用配置为信任由我的自签名根签名的连接:https://developer.apple.com/library/content/technotes/tn2232/_index.html#//apple_ref/doc/uid/DTS40012884-CH1-SECCUSTOMCERT

但是在 iOS 11 中,应用程序不再可能在不在 info.plist 中创建 ATS 异常的情况下绕过 ATS。此线程包含来自 Apple 的一些解释:https://forums.developer.apple.com/thread/89694

那么现在 iOS 我如何才能以某种自动化方式在我的所有 iOS 模拟器上安装我的自定义 CA 根?

由于 iOS 11 支持多应用程序 UI 测试,我创建了一个 UI 测试,使 iOS 系统使用 Safari 和设置应用程序。 UI测试的来源如下:

import XCTest

class InstallRootCerts: XCTestCase {

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        XCUIApplication().launch()
    }

    override func tearDown() {
        super.tearDown()
    }

    func testInstallTestingRootCertOnDevice() {

        // Set test time env var ROOT_CA to point to your custom CA's .cer file.
        let cert = ProcessInfo.processInfo.environment["ROOT_CA"]
        // Set test time env var ROOT_CA_NAME to contain the name of your CA.
        let caName = ProcessInfo.processInfo.environment["ROOT_CA_NAME"]

        let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")

        let settings = XCUIApplication(bundleIdentifier: "com.apple.Preferences")

        safari.activate()

        XCTAssertNotNil(cert)

        let certUrl = "file://\(cert!)"

        if safari.otherElements["URL"].exists {
            safari.otherElements["URL"].tap()
        }

        let addressBar = safari.textFields["URL"]

        addressBar.typeText(certUrl)

        safari.buttons["Go"].tap()

        safari.buttons["Allow"].tap()

        XCTAssertTrue( settings.wait(for: .runningForeground, timeout: 30) )

        if !settings.staticTexts["Verified"].exists {
            settings.buttons["Install"].tap()
            settings.buttons["Install"].tap()
            settings.sheets.buttons["Install"].tap()
        }

        // Now trust the root certificate
        settings.buttons["Cancel"].tap()
        XCTAssertTrue( safari.wait(for: .runningForeground, timeout: 120) )
        settings.activate()
        XCTAssertTrue( settings.wait(for: .runningForeground, timeout: 120) )

        settings.buttons["General"].tap()
        settings.cells["About"].tap()
        settings.cells["Certificate Trust Settings"].tap()
        let cell = settings.cells.containing(.staticText, identifier: caName)
        let toggle = cell.switches.firstMatch
        if toggle.value as? String != "1" {
            toggle.tap()
            settings.buttons["Continue"].tap()
        }

    }

}

此测试用例仅适用于模拟器 运行ning iOS 11.0。我认为如果您将 .cer 文件放在 Web 服务器上并通过 http 打开它,它可以与实际设备一起使用。

此测试用例可以 运行 作为您 Xcode 服务器上的机器人,或者您可以 运行 在任何本地模拟器上对其进行设置。这是我知道的技巧。