我的 iOS 应用程序与具有有效证书的后端服务器通信。在开发期间,我的 iOS 应用程序配置为与我的内部测试服务器通信,该服务器具有由我的自签名根证书签名的证书。
在 iOS 11 之前,我可以使用此处列出的说明将我的应用配置为信任由我的自签名根签名的连接: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 和“设置”应用安装我的自定义 CA 根目录。 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()
}
}
}
此测试用例仅适用于运行 iOS 11.0 的模拟器。如果您将 .cer 文件放在 Web 服务器上并通过 http 打开它,我认为它可以与实际设备一起使用。
此测试用例可以作为 Bot 在您的 Xcode 服务器上运行,或者您可以在本地的任何模拟器上运行它以进行设置。这是我知道的黑客攻击。
关于ios - 如何在我的 iOS 模拟器上自动安装自定义 CA 根证书以进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940888/