//===----------------------------------------------------------------------===// // // This source file is part of the SwiftNIO open source project // // Copyright (c) 2020-2021 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information // See CONTRIBUTORS.txt for the list of SwiftNIO project authors // // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// import NIOConcurrencyHelpers import NIOCore import NIOEmbedded import NIOPosix import NIOTestUtils import XCTest class UniversalBootstrapSupportTest: XCTestCase { func testBootstrappingWorks() { let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) defer { XCTAssertNoThrow(try group.syncShutdownGracefully()) } final class DropChannelReadsHandler: ChannelInboundHandler { typealias InboundIn = Any func channelRead(context: ChannelHandlerContext, data: NIOAny) { // drop } } final class DummyTLSProvider: NIOClientTLSProvider { typealias Bootstrap = ClientBootstrap final class DropInboundEventsHandler: ChannelInboundHandler { typealias InboundIn = Any func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) { // drop } } func enableTLS(_ bootstrap: ClientBootstrap) -> ClientBootstrap { bootstrap.protocolHandlers { [DropInboundEventsHandler()] } } } final class FishOutChannelHandler: ChannelInboundHandler, Sendable { typealias InboundIn = Channel private let _acceptedChannels = NIOLockedValueBox<[Channel]>([]) var acceptedChannels: [Channel] { self._acceptedChannels.withLockedValue { $0 } } let firstArrived: EventLoopPromise init(firstArrived: EventLoopPromise) { self.firstArrived = firstArrived } func channelRead(context: ChannelHandlerContext, data: NIOAny) { let channel = Self.unwrapInboundIn(data) let count = self._acceptedChannels.withLockedValue { channels in channels.append(channel) return channels.count } if count == 1 { self.firstArrived.succeed(()) } context.fireChannelRead(data) } } XCTAssertNoThrow( try withTCPServerChannel(group: group) { server in let firstArrived = group.next().makePromise(of: Void.self) let counter1 = EventCounterHandler() let counter2 = EventCounterHandler() let channelFisher = FishOutChannelHandler(firstArrived: firstArrived) XCTAssertNoThrow(try server.pipeline.addHandler(channelFisher).wait()) let client = try NIOClientTCPBootstrap(ClientBootstrap(group: group), tls: DummyTLSProvider()) .channelInitializer { channel in channel.eventLoop.makeCompletedFuture { try channel.pipeline.syncOperations.addHandlers( counter1, DropChannelReadsHandler(), counter2 ) } } .channelOption(.autoRead, value: false) .connectTimeout(.hours(1)) .enableTLS() .connect(to: server.localAddress!) .wait() defer { XCTAssertNoThrow(try client.close().wait()) } var buffer = client.allocator.buffer(capacity: 16) buffer.writeString("hello") var maybeAcceptedChannel: Channel? = nil XCTAssertNoThrow(try firstArrived.futureResult.wait()) XCTAssertNoThrow( maybeAcceptedChannel = try server.eventLoop.submit { XCTAssertEqual(1, channelFisher.acceptedChannels.count) return channelFisher.acceptedChannels.first }.wait() ) guard let acceptedChannel = maybeAcceptedChannel else { XCTFail("couldn't fish out the accepted channel") return } XCTAssertNoThrow(try acceptedChannel.writeAndFlush(buffer).wait()) // auto-read is off, so we shouldn't see any reads/channelReads XCTAssertEqual(0, counter1.readCalls) XCTAssertEqual(0, counter2.readCalls) XCTAssertEqual(0, counter1.channelReadCalls) XCTAssertEqual(0, counter2.channelReadCalls) // let's do a read XCTAssertNoThrow( try client.eventLoop.submit { client.read() }.wait() ) XCTAssertEqual(1, counter1.readCalls) XCTAssertEqual(1, counter2.readCalls) // let's check that the order is right XCTAssertNoThrow( try client.eventLoop.submit { [buffer] in client.pipeline.fireChannelRead(buffer) client.pipeline.fireUserInboundEventTriggered(buffer) }.wait() ) // the protocol handler which was added by the "TLS" implementation should be first, it however drops // inbound events so we shouldn't see them XCTAssertEqual(0, counter1.userInboundEventTriggeredCalls) XCTAssertEqual(0, counter2.userInboundEventTriggeredCalls) // the channelReads so have gone through but only to counter1 XCTAssertGreaterThanOrEqual(counter1.channelReadCalls, 1) XCTAssertEqual(0, counter2.channelReadCalls) } ) } func testBootstrapOverrideOfShortcutOptions() { final class FakeBootstrap: NIOClientTCPBootstrapProtocol { func channelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture) -> Self { fatalError("Not implemented") } func protocolHandlers(_ handlers: @escaping () -> [ChannelHandler]) -> Self { fatalError("Not implemented") } var regularOptionsSeen = false func channelOption