|
| 1 | +// This source code is dual-licensed under the Mozilla Public License ("MPL"), |
| 2 | +// version 1.1 and the Apache License ("ASL"), version 2.0. |
| 3 | +// |
| 4 | +// The ASL v2.0: |
| 5 | +// |
| 6 | +// --------------------------------------------------------------------------- |
| 7 | +// Copyright 2017-2019 Pivotal Software, Inc. |
| 8 | +// |
| 9 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +// you may not use this file except in compliance with the License. |
| 11 | +// You may obtain a copy of the License at |
| 12 | +// |
| 13 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +// |
| 15 | +// Unless required by applicable law or agreed to in writing, software |
| 16 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +// See the License for the specific language governing permissions and |
| 19 | +// limitations under the License. |
| 20 | +// --------------------------------------------------------------------------- |
| 21 | +// |
| 22 | +// The MPL v1.1: |
| 23 | +// |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | +// The contents of this file are subject to the Mozilla Public License |
| 26 | +// Version 1.1 (the "License"); you may not use this file except in |
| 27 | +// compliance with the License. You may obtain a copy of the License at |
| 28 | +// https://www.mozilla.org/MPL/ |
| 29 | +// |
| 30 | +// Software distributed under the License is distributed on an "AS IS" |
| 31 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 32 | +// License for the specific language governing rights and limitations |
| 33 | +// under the License. |
| 34 | +// |
| 35 | +// The Original Code is RabbitMQ |
| 36 | +// |
| 37 | +// The Initial Developer of the Original Code is Pivotal Software, Inc. |
| 38 | +// All Rights Reserved. |
| 39 | +// |
| 40 | +// Alternatively, the contents of this file may be used under the terms |
| 41 | +// of the Apache Standard license (the "ASL License"), in which case the |
| 42 | +// provisions of the ASL License are applicable instead of those |
| 43 | +// above. If you wish to allow use of your version of this file only |
| 44 | +// under the terms of the ASL License and not to allow others to use |
| 45 | +// your version of this file under the MPL, indicate your decision by |
| 46 | +// deleting the provisions above and replace them with the notice and |
| 47 | +// other provisions required by the ASL License. If you do not delete |
| 48 | +// the provisions above, a recipient may use your version of this file |
| 49 | +// under either the MPL or the ASL License. |
| 50 | +// --------------------------------------------------------------------------- |
| 51 | + |
| 52 | +import Foundation |
| 53 | +import XCTest |
| 54 | + |
| 55 | +// see https://github.com/rabbitmq/rabbitmq-objc-client/blob/master/CONTRIBUTING.md |
| 56 | +// to set up your system for running integration tests |
| 57 | +class CertificateAuthenticationIntegrationTest: XCTestCase { |
| 58 | + func testConnectsViaTLSWithClientCert() { |
| 59 | + let semaphore = DispatchSemaphore(value: 0) |
| 60 | + let tlsOptions = RMQTLSOptions( |
| 61 | + peerName: "localhost", |
| 62 | + verifyPeer: false, |
| 63 | + pkcs12: fixtureClientCertificatePKCS12() as Data, |
| 64 | + pkcs12Password: CertificateFixtures.password |
| 65 | + ) |
| 66 | + let transport = RMQTCPSocketTransport(host: "localhost", |
| 67 | + port: 5671, |
| 68 | + tlsOptions: tlsOptions, |
| 69 | + connectTimeout: 15, |
| 70 | + readTimeout: 30, |
| 71 | + writeTimeout: 30) |
| 72 | + try! transport.connect() |
| 73 | + transport.write(RMQProtocolHeader().amqEncoded()) |
| 74 | + |
| 75 | + var receivedData: Data? |
| 76 | + transport.readFrame { data in |
| 77 | + receivedData = data |
| 78 | + semaphore.signal() |
| 79 | + } |
| 80 | + |
| 81 | + XCTAssertEqual(.success, semaphore.wait(timeout: TestHelper.dispatchTimeFromNow(5)), |
| 82 | + "Timed out waiting for read") |
| 83 | + let parser = RMQParser(data: receivedData!) |
| 84 | + XCTAssert(RMQFrame(parser: parser).payload.isKind(of: RMQConnectionStart.self)) |
| 85 | + |
| 86 | + if(transport.isConnected()) { |
| 87 | + transport.close() |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + func testThrowsWhenTLSPasswordIncorrect() { |
| 92 | + let tlsOptions = RMQTLSOptions( |
| 93 | + peerName: "localhost", |
| 94 | + verifyPeer: false, |
| 95 | + pkcs12: fixtureClientCertificatePKCS12() as Data, |
| 96 | + pkcs12Password: "incorrect-password" |
| 97 | + ) |
| 98 | + let transport = RMQTCPSocketTransport(host: "127.0.0.1", |
| 99 | + port: 5671, |
| 100 | + tlsOptions: tlsOptions, |
| 101 | + connectTimeout: 15, |
| 102 | + readTimeout: 30, |
| 103 | + writeTimeout: 30) |
| 104 | + |
| 105 | + #if os(iOS) |
| 106 | + XCTAssertThrowsError(try transport.connect()) |
| 107 | + #endif |
| 108 | + } |
| 109 | + |
| 110 | + func testSubscribeWithClientCertificateAuthentication() { |
| 111 | + let delegate = RMQConnectionDelegateLogger() |
| 112 | + let noisyHeartbeats = 1 |
| 113 | + let tlsOptions = RMQTLSOptions( |
| 114 | + peerName: "localhost", |
| 115 | + verifyPeer: false, |
| 116 | + pkcs12: fixtureClientCertificatePKCS12() as Data, |
| 117 | + pkcs12Password: CertificateFixtures.password |
| 118 | + ) |
| 119 | + let conn = RMQConnection(uri: "amqps://localhost", |
| 120 | + tlsOptions: tlsOptions, |
| 121 | + delegate: delegate) |
| 122 | + conn.start() |
| 123 | + defer { conn.blockingClose() } |
| 124 | + |
| 125 | + let semaphore = DispatchSemaphore(value: 0) |
| 126 | + let ch = conn.createChannel() |
| 127 | + let q = ch.queue("", options: [.autoDelete, .exclusive]) |
| 128 | + |
| 129 | + var delivered: RMQMessage? |
| 130 | + |
| 131 | + q.subscribe(withAckMode: [.manual]) { message in |
| 132 | + delivered = message |
| 133 | + ch.ack(message.deliveryTag) |
| 134 | + semaphore.signal() |
| 135 | + } |
| 136 | + |
| 137 | + let body = "my message".data(using: String.Encoding.utf8)! |
| 138 | + |
| 139 | + q.publish(body) |
| 140 | + |
| 141 | + XCTAssertEqual(.success, |
| 142 | + semaphore.wait(timeout: TestHelper.dispatchTimeFromNow(10)), |
| 143 | + "Timed out waiting for message") |
| 144 | + |
| 145 | + XCTAssertEqual(1, delivered!.deliveryTag) |
| 146 | + XCTAssertEqual(body, delivered!.body) |
| 147 | + } |
| 148 | + |
| 149 | + // |
| 150 | + // Implement |
| 151 | + // |
| 152 | + |
| 153 | + fileprivate func fixtureClientCertificatePKCS12() -> Data { |
| 154 | + do { |
| 155 | + return try CertificateFixtures.guestBunniesP12() |
| 156 | + } catch { |
| 157 | + fatalError("Failed to load the fixture client certificate") |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments