|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import XCTest |
| 6 | + |
| 7 | +@testable import webview_flutter_wkwebview |
| 8 | + |
| 9 | +#if os(iOS) |
| 10 | + import Flutter |
| 11 | +#elseif os(macOS) |
| 12 | + import FlutterMacOS |
| 13 | +#else |
| 14 | + #error("Unsupported platform.") |
| 15 | +#endif |
| 16 | + |
| 17 | +class SecTrustProxyAPITests: XCTestCase { |
| 18 | + func createTrust(delegate: TestSecTrustProxyAPIDelegate) -> SecTrustWrapper { |
| 19 | + var trust: SecTrust? |
| 20 | + SecTrustCreateWithCertificates( |
| 21 | + [delegate.createDummyCertificate()] as AnyObject, SecPolicyCreateBasicX509(), &trust) |
| 22 | + |
| 23 | + return SecTrustWrapper(value: trust!) |
| 24 | + } |
| 25 | + |
| 26 | + func testEvaluateWithError() { |
| 27 | + let registrar = TestProxyApiRegistrar() |
| 28 | + let delegate = TestSecTrustProxyAPIDelegate() |
| 29 | + let api = PigeonApiSecTrust(pigeonRegistrar: registrar, delegate: delegate) |
| 30 | + |
| 31 | + let expect = expectation(description: "Wait for setCookie.") |
| 32 | + let trust = createTrust(delegate: delegate) |
| 33 | + var resultValue: Bool? |
| 34 | + |
| 35 | + api.pigeonDelegate.evaluateWithError(pigeonApi: api, trust: trust) { result in |
| 36 | + switch result { |
| 37 | + case .success(let value): |
| 38 | + resultValue = value |
| 39 | + case .failure(_): |
| 40 | + break |
| 41 | + } |
| 42 | + expect.fulfill() |
| 43 | + } |
| 44 | + |
| 45 | + wait(for: [expect], timeout: 5.0) |
| 46 | + XCTAssertEqual(resultValue, true) |
| 47 | + } |
| 48 | + |
| 49 | + func testCopyExceptions() { |
| 50 | + let registrar = TestProxyApiRegistrar() |
| 51 | + let delegate = TestSecTrustProxyAPIDelegate() |
| 52 | + let api = PigeonApiSecTrust(pigeonRegistrar: registrar, delegate: delegate) |
| 53 | + |
| 54 | + let trust = createTrust(delegate: delegate) |
| 55 | + let value = try? api.pigeonDelegate.copyExceptions(pigeonApi: api, trust: trust) |
| 56 | + |
| 57 | + XCTAssertEqual(value?.data, Data()) |
| 58 | + } |
| 59 | + |
| 60 | + func testSetExceptions() { |
| 61 | + let registrar = TestProxyApiRegistrar() |
| 62 | + let delegate = TestSecTrustProxyAPIDelegate() |
| 63 | + let api = PigeonApiSecTrust(pigeonRegistrar: registrar, delegate: delegate) |
| 64 | + |
| 65 | + let trust = createTrust(delegate: delegate) |
| 66 | + let value = try? api.pigeonDelegate.setExceptions( |
| 67 | + pigeonApi: api, trust: trust, exceptions: FlutterStandardTypedData(bytes: Data())) |
| 68 | + |
| 69 | + XCTAssertEqual(value, false) |
| 70 | + } |
| 71 | + |
| 72 | + func testGetTrustResult() { |
| 73 | + let registrar = TestProxyApiRegistrar() |
| 74 | + let delegate = TestSecTrustProxyAPIDelegate() |
| 75 | + let api = PigeonApiSecTrust(pigeonRegistrar: registrar, delegate: delegate) |
| 76 | + |
| 77 | + let trust = createTrust(delegate: delegate) |
| 78 | + let value = try? api.pigeonDelegate.getTrustResult(pigeonApi: api, trust: trust) |
| 79 | + |
| 80 | + XCTAssertEqual(value?.result, SecTrustResultType.invalid) |
| 81 | + XCTAssertEqual(value?.resultCode, -1) |
| 82 | + } |
| 83 | + |
| 84 | + func testCopyCertificateChain() { |
| 85 | + let registrar = TestProxyApiRegistrar() |
| 86 | + let delegate = TestSecTrustProxyAPIDelegate() |
| 87 | + let api = PigeonApiSecTrust(pigeonRegistrar: registrar, delegate: delegate) |
| 88 | + |
| 89 | + let trust = createTrust(delegate: delegate) |
| 90 | + let value = try? api.pigeonDelegate.copyCertificateChain(pigeonApi: api, trust: trust) |
| 91 | + |
| 92 | + XCTAssertEqual(value?.count, 1) |
| 93 | + XCTAssertNotNil(value?.first?.value) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +class TestSecTrustProxyAPIDelegate: SecTrustProxyAPIDelegate { |
| 98 | + func createDummyCertificate() -> SecCertificate { |
| 99 | + let url = FlutterAssetManager().urlForAsset("assets/test_cert.der")! |
| 100 | + let certificateData = NSData(contentsOf: url) |
| 101 | + |
| 102 | + return SecCertificateCreateWithData(nil, certificateData!)! |
| 103 | + } |
| 104 | + |
| 105 | + override func secTrustEvaluateWithError( |
| 106 | + _ trust: SecTrust, _ error: UnsafeMutablePointer<CFError?>? |
| 107 | + ) -> Bool { |
| 108 | + return true |
| 109 | + } |
| 110 | + |
| 111 | + override func secTrustCopyExceptions(_ trust: SecTrust) -> CFData? { |
| 112 | + return Data() as CFData |
| 113 | + } |
| 114 | + |
| 115 | + override func secTrustSetExceptions(_ trust: SecTrust, _ exceptions: CFData?) -> Bool { |
| 116 | + return false |
| 117 | + } |
| 118 | + |
| 119 | + override func secTrustGetTrustResult( |
| 120 | + _ trust: SecTrust, _ result: UnsafeMutablePointer<SecTrustResultType> |
| 121 | + ) -> OSStatus { |
| 122 | + result.pointee = SecTrustResultType.invalid |
| 123 | + return -1 |
| 124 | + } |
| 125 | + |
| 126 | + override func secTrustCopyCertificateChain(_ trust: SecTrust) -> CFArray? { |
| 127 | + if #available(iOS 15.0, *) { |
| 128 | + return [createDummyCertificate()] as CFArray |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | + } |
| 133 | +} |
0 commit comments