| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- /*
- * Copyright 2019, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import Foundation
- import NIOCore
- import NIOPosix
- import NIOTransportServices
- import XCTest
- @testable import GRPC
- #if canImport(Network)
- import Network
- #endif
- class PlatformSupportTests: GRPCTestCase {
- var group: EventLoopGroup!
- override func tearDown() {
- XCTAssertNoThrow(try self.group?.syncShutdownGracefully())
- super.tearDown()
- }
- func testMakeEventLoopGroupReturnsMultiThreadedGroupForPosix() {
- self.group = PlatformSupport.makeEventLoopGroup(
- loopCount: 1,
- networkPreference: .userDefined(.posix)
- )
- XCTAssertTrue(self.group is MultiThreadedEventLoopGroup)
- }
- func testMakeEventLoopGroupReturnsNIOTSGroupForNetworkFramework() {
- // If we don't have Network.framework then we can't test this.
- #if canImport(Network)
- guard #available(macOS 10.14, *) else { return }
- self.group = PlatformSupport.makeEventLoopGroup(
- loopCount: 1,
- networkPreference: .userDefined(.networkFramework)
- )
- XCTAssertTrue(self.group is NIOTSEventLoopGroup)
- #endif
- }
- func testMakeClientBootstrapReturnsClientBootstrapForMultiThreadedGroup() {
- self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- let bootstrap = PlatformSupport.makeClientBootstrap(group: self.group)
- XCTAssertTrue(bootstrap is ClientBootstrap)
- }
- func testMakeClientBootstrapReturnsClientBootstrapForEventLoop() {
- self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- let eventLoop = self.group.next()
- let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
- XCTAssertTrue(bootstrap is ClientBootstrap)
- }
- func testMakeClientBootstrapReturnsNIOTSConnectionBootstrapForNIOTSGroup() {
- // If we don't have Network.framework then we can't test this.
- #if canImport(Network)
- guard #available(macOS 10.14, *) else { return }
- self.group = NIOTSEventLoopGroup(loopCount: 1)
- let bootstrap = PlatformSupport.makeClientBootstrap(group: self.group)
- XCTAssertTrue(bootstrap is NIOTSConnectionBootstrap)
- #endif
- }
- func testMakeClientBootstrapReturnsNIOTSConnectionBootstrapForQoSEventLoop() {
- // If we don't have Network.framework then we can't test this.
- #if canImport(Network)
- guard #available(macOS 10.14, *) else { return }
- self.group = NIOTSEventLoopGroup(loopCount: 1)
- let eventLoop = self.group.next()
- XCTAssertTrue(eventLoop is QoSEventLoop)
- let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
- XCTAssertTrue(bootstrap is NIOTSConnectionBootstrap)
- #endif
- }
- func testMakeServerBootstrapReturnsServerBootstrapForMultiThreadedGroup() {
- self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- let bootstrap = PlatformSupport.makeServerBootstrap(group: self.group)
- XCTAssertTrue(bootstrap is ServerBootstrap)
- }
- func testMakeServerBootstrapReturnsServerBootstrapForEventLoop() {
- self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- let eventLoop = self.group.next()
- let bootstrap = PlatformSupport.makeServerBootstrap(group: eventLoop)
- XCTAssertTrue(bootstrap is ServerBootstrap)
- }
- func testMakeServerBootstrapReturnsNIOTSListenerBootstrapForNIOTSGroup() {
- // If we don't have Network.framework then we can't test this.
- #if canImport(Network)
- guard #available(macOS 10.14, *) else { return }
- self.group = NIOTSEventLoopGroup(loopCount: 1)
- let bootstrap = PlatformSupport.makeServerBootstrap(group: self.group)
- XCTAssertTrue(bootstrap is NIOTSListenerBootstrap)
- #endif
- }
- func testMakeServerBootstrapReturnsNIOTSListenerBootstrapForQoSEventLoop() {
- // If we don't have Network.framework then we can't test this.
- #if canImport(Network)
- guard #available(macOS 10.14, *) else { return }
- self.group = NIOTSEventLoopGroup(loopCount: 1)
- let eventLoop = self.group.next()
- XCTAssertTrue(eventLoop is QoSEventLoop)
- let bootstrap = PlatformSupport.makeServerBootstrap(group: eventLoop)
- XCTAssertTrue(bootstrap is NIOTSListenerBootstrap)
- #endif
- }
- func testRequiresZeroLengthWorkaroundWithMTELG() {
- self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- // No MTELG or individual loop requires the workaround.
- XCTAssertFalse(
- PlatformSupport
- .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: true)
- )
- XCTAssertFalse(
- PlatformSupport
- .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: false)
- )
- XCTAssertFalse(
- PlatformSupport
- .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: true)
- )
- XCTAssertFalse(
- PlatformSupport
- .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: false)
- )
- }
- func testRequiresZeroLengthWorkaroundWithNetworkFramework() {
- // If we don't have Network.framework we can't test this.
- #if canImport(Network)
- guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
- self.group = NIOTSEventLoopGroup(loopCount: 1)
- // We require the workaround for any of these loops when TLS is not enabled.
- XCTAssertFalse(
- PlatformSupport
- .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: true)
- )
- XCTAssertTrue(
- PlatformSupport
- .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: false)
- )
- XCTAssertFalse(
- PlatformSupport
- .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: true)
- )
- XCTAssertTrue(
- PlatformSupport
- .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: false)
- )
- #endif
- }
- func testIsTransportServicesGroup() {
- #if canImport(Network)
- guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
- let tsGroup = NIOTSEventLoopGroup(loopCount: 1)
- defer {
- XCTAssertNoThrow(try tsGroup.syncShutdownGracefully())
- }
- XCTAssertTrue(PlatformSupport.isTransportServicesEventLoopGroup(tsGroup))
- XCTAssertTrue(PlatformSupport.isTransportServicesEventLoopGroup(tsGroup.next()))
- let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- defer {
- XCTAssertNoThrow(try group.syncShutdownGracefully())
- }
- XCTAssertFalse(PlatformSupport.isTransportServicesEventLoopGroup(group))
- XCTAssertFalse(PlatformSupport.isTransportServicesEventLoopGroup(group.next()))
- #endif
- }
- func testIsTLSConfigruationCompatible() {
- #if canImport(Network)
- #if canImport(NIOSSL)
- guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
- let nwConfiguration = GRPCTLSConfiguration.makeClientConfigurationBackedByNetworkFramework()
- let nioSSLConfiguration = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL()
- let tsGroup = NIOTSEventLoopGroup(loopCount: 1)
- defer {
- XCTAssertNoThrow(try tsGroup.syncShutdownGracefully())
- }
- XCTAssertTrue(tsGroup.isCompatible(with: nwConfiguration))
- XCTAssertTrue(tsGroup.isCompatible(with: nioSSLConfiguration))
- XCTAssertTrue(tsGroup.next().isCompatible(with: nwConfiguration))
- XCTAssertTrue(tsGroup.next().isCompatible(with: nioSSLConfiguration))
- let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- defer {
- XCTAssertNoThrow(try group.syncShutdownGracefully())
- }
- XCTAssertFalse(group.isCompatible(with: nwConfiguration))
- XCTAssertTrue(group.isCompatible(with: nioSSLConfiguration))
- XCTAssertFalse(group.next().isCompatible(with: nwConfiguration))
- XCTAssertTrue(group.next().isCompatible(with: nioSSLConfiguration))
- #endif
- #endif
- }
- func testMakeCompatibleEventLoopGroupForNIOSSL() {
- #if canImport(NIOSSL)
- let configuration = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL()
- let group = PlatformSupport.makeEventLoopGroup(compatibleWith: configuration, loopCount: 1)
- XCTAssertNoThrow(try group.syncShutdownGracefully())
- XCTAssert(group is MultiThreadedEventLoopGroup)
- #endif
- }
- func testMakeCompatibleEventLoopGroupForNetworkFramework() {
- #if canImport(Network)
- guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
- let options = NWProtocolTLS.Options()
- let configuration = GRPCTLSConfiguration.makeClientConfigurationBackedByNetworkFramework(
- options: options
- )
- let group = PlatformSupport.makeEventLoopGroup(compatibleWith: configuration, loopCount: 1)
- XCTAssertNoThrow(try group.syncShutdownGracefully())
- XCTAssert(group is NIOTSEventLoopGroup)
- #endif
- }
- }
|