From bcdbe4bc4648d8018051d6da9b3e503efd247ca6 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Fri, 15 Sep 2017 09:29:59 +0100 Subject: [PATCH] ChannelHandlerContext.connect shouldn't bind --- Sources/NIO/ChannelPipeline.swift | 2 +- .../NIOTests/ChannelPipelineTest+XCTest.swift | 1 + Tests/NIOTests/ChannelPipelineTest.swift | 30 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Sources/NIO/ChannelPipeline.swift b/Sources/NIO/ChannelPipeline.swift index cf4cb599c..d4f09acf8 100644 --- a/Sources/NIO/ChannelPipeline.swift +++ b/Sources/NIO/ChannelPipeline.swift @@ -712,7 +712,7 @@ public final class ChannelHandlerContext : ChannelInvoker { } public func connect(to address: SocketAddress, promise: Promise?) { - outboundNext!.invokeBind(to: address, promise: promise) + outboundNext!.invokeConnect(to: address, promise: promise) } public func write(data: IOData, promise: Promise?) { diff --git a/Tests/NIOTests/ChannelPipelineTest+XCTest.swift b/Tests/NIOTests/ChannelPipelineTest+XCTest.swift index b17a1ae1a..efc50843c 100644 --- a/Tests/NIOTests/ChannelPipelineTest+XCTest.swift +++ b/Tests/NIOTests/ChannelPipelineTest+XCTest.swift @@ -28,6 +28,7 @@ extension ChannelPipelineTest { return [ ("testAddAfterClose", testAddAfterClose), ("testOutboundOrdering", testOutboundOrdering), + ("testConnectingDoesntCallBind", testConnectingDoesntCallBind), ] } } diff --git a/Tests/NIOTests/ChannelPipelineTest.swift b/Tests/NIOTests/ChannelPipelineTest.swift index 8ed668b81..582102786 100644 --- a/Tests/NIOTests/ChannelPipelineTest.swift +++ b/Tests/NIOTests/ChannelPipelineTest.swift @@ -76,6 +76,23 @@ class ChannelPipelineTest: XCTestCase { } + func testConnectingDoesntCallBind() throws { + let channel = EmbeddedChannel() + var ipv4SocketAddress = sockaddr_in() + ipv4SocketAddress.sin_port = (12345 as UInt16).bigEndian + let sa = SocketAddress(IPv4Address: ipv4SocketAddress, host: "foobar.com") + + _ = try channel.pipeline.add(handler: NoBindAllowed()).wait() + _ = try channel.pipeline.add(handler: TestChannelOutboundHandler({ data in + return data + })).wait() + + _ = try channel.connect(to: sa).wait() + _ = try channel.close().wait() + + return + } + private final class TestChannelOutboundHandler: ChannelOutboundHandler { typealias OutboundIn = In typealias OutboundOut = Out @@ -94,4 +111,17 @@ class ChannelPipelineTest: XCTestCase { } } } + + private final class NoBindAllowed: ChannelOutboundHandler { + typealias OutboundIn = ByteBuffer + typealias OutboundOut = ByteBuffer + + enum TestFailureError: Error { + case CalledBind + } + + public func bind(ctx: ChannelHandlerContext, to address: SocketAddress, promise: Promise?) { + promise!.fail(error: TestFailureError.CalledBind) + } + } }