From 044b978525689dceae2e0ab95ccc14dda6faaa40 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 8 Feb 2018 16:50:53 +0000 Subject: [PATCH] Bring up-to-date with NIO latest --- Sources/NIOOpenSSL/OpenSSLHandler.swift | 12 ++++----- .../OpenSSLIntegrationTest.swift | 27 +++++++++++++++---- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Sources/NIOOpenSSL/OpenSSLHandler.swift b/Sources/NIOOpenSSL/OpenSSLHandler.swift index 75257cd9..c5c19219 100644 --- a/Sources/NIOOpenSSL/OpenSSLHandler.swift +++ b/Sources/NIOOpenSSL/OpenSSLHandler.swift @@ -53,7 +53,7 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { public func handlerAdded(ctx: ChannelHandlerContext) { // If this channel is already active, immediately begin handshaking. - if ctx.channel!.isActive { + if ctx.channel.isActive { doHandshakeStep(ctx: ctx) } } @@ -123,7 +123,7 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { // We didn't deliver data. If this channel has got autoread turned off then we should // call read again, because otherwise the user will never see any result from their // read call. - let autoRead = try! ctx.channel!.getOption(option: ChannelOptions.autoRead) + let autoRead = try! ctx.channel.getOption(option: ChannelOptions.autoRead) if !autoRead { ctx.read(promise: nil) } @@ -248,7 +248,7 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { /// no more available. private func doDecodeData(ctx: ChannelHandlerContext) { readLoop: while true { - let result = connection.readDataFromNetwork(allocator: ctx.channel!.allocator) + let result = connection.readDataFromNetwork(allocator: ctx.channel.allocator) switch result { case .complete(let buf): @@ -276,11 +276,11 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { /// is intended. private func writeDataToNetwork(ctx: ChannelHandlerContext, promise: EventLoopPromise?) { // There may be no data to write, in which case we can just exit early. - guard let dataToWrite = connection.getDataForNetwork(allocator: ctx.channel!.allocator) else { + guard let dataToWrite = connection.getDataForNetwork(allocator: ctx.channel.allocator) else { if let promise = promise { // If we have a promise, we need to enforce ordering so we issue a zero-length write that // the event loop will have to handle. - let buffer = ctx.channel!.allocator.buffer(capacity: 0) + let buffer = ctx.channel.allocator.buffer(capacity: 0) ctx.writeAndFlush(data: wrapInboundOut(buffer), promise: promise) } return @@ -310,7 +310,7 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler { // If there is no remote address, something weird is happening here. We can't // validate a certificate without it, so bail. - guard let ipAddress = ctx.channel?.remoteAddress else { + guard let ipAddress = ctx.channel.remoteAddress else { throw NIOOpenSSLError.cannotFindPeerIP } diff --git a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift index 39be77b8..cc9fe60b 100644 --- a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift +++ b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift @@ -523,7 +523,7 @@ class OpenSSLIntegrationTest: XCTestCase { } try clientChannel.flush().wait() - let writeCount = try readPromise.futureResult.then { _ in + let writeCount = try readPromise.futureResult.map { _ in // Here we're in the I/O loop, so we know that no further channel action will happen // while we dispatch this callback. This is the perfect time to check how many writes // happened. @@ -678,11 +678,11 @@ class OpenSSLIntegrationTest: XCTestCase { var originalBuffer = clientChannel.allocator.buffer(capacity: 5) originalBuffer.write(string: "Hello") let writeFuture = clientChannel.writeAndFlush(data: NIOAny(originalBuffer)) - let errorsFuture: EventLoopFuture<[NIOOpenSSLError]> = writeFuture.thenIfError { _ in + let errorsFuture: EventLoopFuture<[NIOOpenSSLError]> = writeFuture.mapIfError { _ in // We're swallowing errors here, on purpose, because we'll definitely // hit them. return () - }.then { _ in + }.map { _ in return errorHandler.errors } let actualErrors = try errorsFuture.wait() @@ -730,6 +730,11 @@ class OpenSSLIntegrationTest: XCTestCase { func testDontLoseClosePromises() throws { let serverChannel = EmbeddedChannel() let clientChannel = EmbeddedChannel() + defer { + // We know this will throw. + _ = try? serverChannel.finish() + _ = try? clientChannel.finish() + } let ctx = try configuredSSLContext() @@ -763,6 +768,11 @@ class OpenSSLIntegrationTest: XCTestCase { XCTFail("Unexpected result: \($0)") } } + + // Now clean up the client channel. We need to also fire the channel inactive here as there is + // no-one left for the client channel to hang up on. + _ = clientChannel.close() + clientChannel.pipeline.fireChannelInactive() } func testTrustStoreOnDisk() throws { @@ -837,11 +847,11 @@ class OpenSSLIntegrationTest: XCTestCase { var originalBuffer = clientChannel.allocator.buffer(capacity: 5) originalBuffer.write(string: "Hello") let writeFuture = clientChannel.writeAndFlush(data: NIOAny(originalBuffer)) - let errorsFuture: EventLoopFuture<[NIOOpenSSLError]> = writeFuture.thenIfError { _ in + let errorsFuture: EventLoopFuture<[NIOOpenSSLError]> = writeFuture.mapIfError { _ in // We're swallowing errors here, on purpose, because we'll definitely // hit them. return () - }.then { _ in + }.map { _ in return errorHandler.errors } let actualErrors = try errorsFuture.wait() @@ -938,6 +948,10 @@ class OpenSSLIntegrationTest: XCTestCase { func testZeroLengthWritePromisesFireInOrder() throws { let serverChannel = EmbeddedChannel() let clientChannel = EmbeddedChannel() + defer { + _ = try? serverChannel.finish() + _ = try? clientChannel.finish() + } let ctx = try configuredSSLContext() @@ -979,5 +993,8 @@ class OpenSSLIntegrationTest: XCTestCase { XCTAssertEqual(writeCount, 0) writeDelayer.forceFlush() XCTAssertEqual(writeCount, 3) + + serverChannel.pipeline.fireChannelInactive() + clientChannel.pipeline.fireChannelInactive() } }