Skip to content

Commit 3bd7c33

Browse files
Extract integration tests related to certificate-based authentication
1 parent 1d11cb0 commit 3bd7c33

File tree

4 files changed

+164
-99
lines changed

4 files changed

+164
-99
lines changed

RMQClient.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
1029A0E22087A97E00C72924 /* ConnectionDeadlockTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1029A0E12087A97E00C72924 /* ConnectionDeadlockTests.swift */; };
1111
70311B0B21ED538600AE1804 /* RMQConnectionDefaults.h in Headers */ = {isa = PBXBuildFile; fileRef = 70311B0A21ED538600AE1804 /* RMQConnectionDefaults.h */; };
12+
70338A2421FBAA7C00C9069D /* CertificateAuthenticationIntegrationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70338A2321FBAA7C00C9069D /* CertificateAuthenticationIntegrationTest.swift */; };
1213
705359A921D174A400CF6456 /* TestCertificates in Resources */ = {isa = PBXBuildFile; fileRef = 705359A821D174A400CF6456 /* TestCertificates */; };
1314
705359AA21D174A400CF6456 /* TestCertificates in Resources */ = {isa = PBXBuildFile; fileRef = 705359A821D174A400CF6456 /* TestCertificates */; };
1415
705359AC21D1871600CF6456 /* AtomicInteger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 705359AB21D1871600CF6456 /* AtomicInteger.swift */; };
@@ -236,6 +237,7 @@
236237
/* Begin PBXFileReference section */
237238
1029A0E12087A97E00C72924 /* ConnectionDeadlockTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionDeadlockTests.swift; sourceTree = "<group>"; };
238239
70311B0A21ED538600AE1804 /* RMQConnectionDefaults.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RMQConnectionDefaults.h; sourceTree = "<group>"; };
240+
70338A2321FBAA7C00C9069D /* CertificateAuthenticationIntegrationTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CertificateAuthenticationIntegrationTest.swift; sourceTree = "<group>"; };
239241
705359A821D174A400CF6456 /* TestCertificates */ = {isa = PBXFileReference; lastKnownFileType = folder; path = TestCertificates; sourceTree = SOURCE_ROOT; };
240242
705359AB21D1871600CF6456 /* AtomicInteger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AtomicInteger.swift; sourceTree = "<group>"; };
241243
705CB9CB21D253AF00996B64 /* ChannelLifecycleIntegrationTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelLifecycleIntegrationTest.swift; sourceTree = "<group>"; };
@@ -690,6 +692,7 @@
690692
70970AD521E68D6100E9CC8B /* Helpers */,
691693
AE607C681C3C0EAD00BE9AC4 /* OriginalIntegrationTest.swift */,
692694
705CB9CD21D258C300996B64 /* ConnectionLifecycleIntegrationTest.swift */,
695+
70338A2321FBAA7C00C9069D /* CertificateAuthenticationIntegrationTest.swift */,
693696
AEEDB0B41CE3284200005A92 /* ConnectionRecoveryIntegrationTest.swift */,
694697
705CB9CB21D253AF00996B64 /* ChannelLifecycleIntegrationTest.swift */,
695698
70FC58B021DF4DB900BB25CB /* ChannelIntegrationTest.swift */,
@@ -1152,6 +1155,7 @@
11521155
AEA45EE21C440FFD00FE1F62 /* OriginalIntegrationTest.swift in Sources */,
11531156
AEEB8B501C7DE8D1002D3FCE /* ControlledInteractionTransport.swift in Sources */,
11541157
AE063BE11CD7565B00F90EB5 /* FakeClock.swift in Sources */,
1158+
70338A2421FBAA7C00C9069D /* CertificateAuthenticationIntegrationTest.swift in Sources */,
11551159
AEEDB0B51CE3284200005A92 /* ConnectionRecoveryIntegrationTest.swift in Sources */,
11561160
70970AD921E6A64700E9CC8B /* QueueIntegrationTest.swift in Sources */,
11571161
AEA8A77D1CCA734400371E38 /* FakeWaiterFactory.swift in Sources */,
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
}

RMQClientIntegrationTests/OriginalIntegrationTest.swift

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -116,45 +116,6 @@ class OriginalIntegrationTest: XCTestCase {
116116
}
117117
}
118118

119-
func testSubscribeWithClientCertificateAuthentication() {
120-
let delegate = RMQConnectionDelegateLogger()
121-
let noisyHeartbeats = 1
122-
let tlsOptions = RMQTLSOptions(
123-
peerName: "localhost",
124-
verifyPeer: false,
125-
pkcs12: fixtureClientCertificatePKCS12() as Data,
126-
pkcs12Password: CertificateFixtures.password
127-
)
128-
let conn = RMQConnection(uri: "amqps://localhost",
129-
tlsOptions: tlsOptions,
130-
delegate: delegate)
131-
conn.start()
132-
defer { conn.blockingClose() }
133-
134-
let semaphore = DispatchSemaphore(value: 0)
135-
let ch = conn.createChannel()
136-
let q = ch.queue("", options: [.autoDelete, .exclusive])
137-
138-
var delivered: RMQMessage?
139-
140-
q.subscribe(withAckMode: [.manual]) { message in
141-
delivered = message
142-
ch.ack(message.deliveryTag)
143-
semaphore.signal()
144-
}
145-
146-
let body = "my message".data(using: String.Encoding.utf8)!
147-
148-
q.publish(body)
149-
150-
XCTAssertEqual(.success,
151-
semaphore.wait(timeout: TestHelper.dispatchTimeFromNow(10)),
152-
"Timed out waiting for message")
153-
154-
XCTAssertEqual(1, delivered!.deliveryTag)
155-
XCTAssertEqual(body, delivered!.body)
156-
}
157-
158119
func testClientChannelCloseCausesFutureOperationsToFail() {
159120
let delegate = ConnectionDelegateSpy()
160121
let conn = RMQConnection(uri: plainEndpoint, delegate: delegate, recoverAfter: 0)
@@ -201,12 +162,4 @@ class OriginalIntegrationTest: XCTestCase {
201162
ch.basicPublish("".data(using: String.Encoding.utf8)!, routingKey: "irrelevant",
202163
exchange: "a non-existent exchange", properties: [], options: [])
203164
}
204-
205-
fileprivate func fixtureClientCertificatePKCS12() -> Data {
206-
do {
207-
return try CertificateFixtures.guestBunniesP12()
208-
} catch {
209-
fatalError("Failed to load the fixture client certificate")
210-
}
211-
}
212165
}

RMQClientIntegrationTests/RMQTCPSocketTransportTest.swift

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -200,58 +200,6 @@ class RMQTCPSocketTransportTest: XCTestCase {
200200
}
201201
}
202202

203-
func testConnectsViaTLSWithClientCert() {
204-
let semaphore = DispatchSemaphore(value: 0)
205-
let tlsOptions = RMQTLSOptions(
206-
peerName: "localhost",
207-
verifyPeer: false,
208-
pkcs12: fixtureClientCertificatePKCS12() as Data,
209-
pkcs12Password: CertificateFixtures.password
210-
)
211-
let transport = RMQTCPSocketTransport(host: "localhost",
212-
port: 5671,
213-
tlsOptions: tlsOptions,
214-
connectTimeout: 15,
215-
readTimeout: 30,
216-
writeTimeout: 30)
217-
try! transport.connect()
218-
transport.write(RMQProtocolHeader().amqEncoded())
219-
220-
var receivedData: Data?
221-
transport.readFrame { data in
222-
receivedData = data
223-
semaphore.signal()
224-
}
225-
226-
XCTAssertEqual(.success, semaphore.wait(timeout: TestHelper.dispatchTimeFromNow(5)),
227-
"Timed out waiting for read")
228-
let parser = RMQParser(data: receivedData!)
229-
XCTAssert(RMQFrame(parser: parser).payload.isKind(of: RMQConnectionStart.self))
230-
231-
if(transport.isConnected()) {
232-
transport.close()
233-
}
234-
}
235-
236-
func testThrowsWhenTLSPasswordIncorrect() {
237-
let tlsOptions = RMQTLSOptions(
238-
peerName: "localhost",
239-
verifyPeer: false,
240-
pkcs12: fixtureClientCertificatePKCS12() as Data,
241-
pkcs12Password: "incorrect-password"
242-
)
243-
let transport = RMQTCPSocketTransport(host: "127.0.0.1",
244-
port: 5671,
245-
tlsOptions: tlsOptions,
246-
connectTimeout: 15,
247-
readTimeout: 30,
248-
writeTimeout: 30)
249-
250-
#if os(iOS)
251-
XCTAssertThrowsError(try transport.connect())
252-
#endif
253-
}
254-
255203
func testSimulatedDisconnectCausesTransportToReportAsDisconnected() {
256204
let transport = RMQTCPSocketTransport(host: "127.0.0.1",
257205
port: 5672,

0 commit comments

Comments
 (0)