PlatformSupportTests.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. @testable import GRPC
  18. import NIOCore
  19. import NIOPosix
  20. import NIOTransportServices
  21. import XCTest
  22. class PlatformSupportTests: GRPCTestCase {
  23. var group: EventLoopGroup!
  24. override func tearDown() {
  25. XCTAssertNoThrow(try self.group?.syncShutdownGracefully())
  26. super.tearDown()
  27. }
  28. func testMakeEventLoopGroupReturnsMultiThreadedGroupForPosix() {
  29. self.group = PlatformSupport.makeEventLoopGroup(
  30. loopCount: 1,
  31. networkPreference: .userDefined(.posix)
  32. )
  33. XCTAssertTrue(self.group is MultiThreadedEventLoopGroup)
  34. }
  35. func testMakeEventLoopGroupReturnsNIOTSGroupForNetworkFramework() {
  36. // If we don't have Network.framework then we can't test this.
  37. #if canImport(Network)
  38. guard #available(macOS 10.14, *) else { return }
  39. self.group = PlatformSupport.makeEventLoopGroup(
  40. loopCount: 1,
  41. networkPreference: .userDefined(.networkFramework)
  42. )
  43. XCTAssertTrue(self.group is NIOTSEventLoopGroup)
  44. #endif
  45. }
  46. func testMakeClientBootstrapReturnsClientBootstrapForMultiThreadedGroup() {
  47. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  48. let bootstrap = PlatformSupport.makeClientBootstrap(group: self.group)
  49. XCTAssertTrue(bootstrap is ClientBootstrap)
  50. }
  51. func testMakeClientBootstrapReturnsClientBootstrapForEventLoop() {
  52. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  53. let eventLoop = self.group.next()
  54. let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
  55. XCTAssertTrue(bootstrap is ClientBootstrap)
  56. }
  57. func testMakeClientBootstrapReturnsNIOTSConnectionBootstrapForNIOTSGroup() {
  58. // If we don't have Network.framework then we can't test this.
  59. #if canImport(Network)
  60. guard #available(macOS 10.14, *) else { return }
  61. self.group = NIOTSEventLoopGroup(loopCount: 1)
  62. let bootstrap = PlatformSupport.makeClientBootstrap(group: self.group)
  63. XCTAssertTrue(bootstrap is NIOTSConnectionBootstrap)
  64. #endif
  65. }
  66. func testMakeClientBootstrapReturnsNIOTSConnectionBootstrapForQoSEventLoop() {
  67. // If we don't have Network.framework then we can't test this.
  68. #if canImport(Network)
  69. guard #available(macOS 10.14, *) else { return }
  70. self.group = NIOTSEventLoopGroup(loopCount: 1)
  71. let eventLoop = self.group.next()
  72. XCTAssertTrue(eventLoop is QoSEventLoop)
  73. let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
  74. XCTAssertTrue(bootstrap is NIOTSConnectionBootstrap)
  75. #endif
  76. }
  77. func testMakeServerBootstrapReturnsServerBootstrapForMultiThreadedGroup() {
  78. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  79. let bootstrap = PlatformSupport.makeServerBootstrap(group: self.group)
  80. XCTAssertTrue(bootstrap is ServerBootstrap)
  81. }
  82. func testMakeServerBootstrapReturnsServerBootstrapForEventLoop() {
  83. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  84. let eventLoop = self.group.next()
  85. let bootstrap = PlatformSupport.makeServerBootstrap(group: eventLoop)
  86. XCTAssertTrue(bootstrap is ServerBootstrap)
  87. }
  88. func testMakeServerBootstrapReturnsNIOTSListenerBootstrapForNIOTSGroup() {
  89. // If we don't have Network.framework then we can't test this.
  90. #if canImport(Network)
  91. guard #available(macOS 10.14, *) else { return }
  92. self.group = NIOTSEventLoopGroup(loopCount: 1)
  93. let bootstrap = PlatformSupport.makeServerBootstrap(group: self.group)
  94. XCTAssertTrue(bootstrap is NIOTSListenerBootstrap)
  95. #endif
  96. }
  97. func testMakeServerBootstrapReturnsNIOTSListenerBootstrapForQoSEventLoop() {
  98. // If we don't have Network.framework then we can't test this.
  99. #if canImport(Network)
  100. guard #available(macOS 10.14, *) else { return }
  101. self.group = NIOTSEventLoopGroup(loopCount: 1)
  102. let eventLoop = self.group.next()
  103. XCTAssertTrue(eventLoop is QoSEventLoop)
  104. let bootstrap = PlatformSupport.makeServerBootstrap(group: eventLoop)
  105. XCTAssertTrue(bootstrap is NIOTSListenerBootstrap)
  106. #endif
  107. }
  108. func testRequiresZeroLengthWorkaroundWithMTELG() {
  109. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  110. // No MTELG or individual loop requires the workaround.
  111. XCTAssertFalse(
  112. PlatformSupport
  113. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: true)
  114. )
  115. XCTAssertFalse(
  116. PlatformSupport
  117. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: false)
  118. )
  119. XCTAssertFalse(
  120. PlatformSupport
  121. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: true)
  122. )
  123. XCTAssertFalse(
  124. PlatformSupport
  125. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: false)
  126. )
  127. }
  128. func testRequiresZeroLengthWorkaroundWithNetworkFramework() {
  129. // If we don't have Network.framework we can't test this.
  130. #if canImport(Network)
  131. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  132. self.group = NIOTSEventLoopGroup(loopCount: 1)
  133. // We require the workaround for any of these loops when TLS is not enabled.
  134. XCTAssertFalse(
  135. PlatformSupport
  136. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: true)
  137. )
  138. XCTAssertTrue(
  139. PlatformSupport
  140. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: false)
  141. )
  142. XCTAssertFalse(
  143. PlatformSupport
  144. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: true)
  145. )
  146. XCTAssertTrue(
  147. PlatformSupport
  148. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: false)
  149. )
  150. #endif
  151. }
  152. func testIsTransportServicesGroup() {
  153. #if canImport(Network)
  154. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  155. let tsGroup = NIOTSEventLoopGroup(loopCount: 1)
  156. defer {
  157. XCTAssertNoThrow(try tsGroup.syncShutdownGracefully())
  158. }
  159. XCTAssertTrue(PlatformSupport.isTransportServicesEventLoopGroup(tsGroup))
  160. XCTAssertTrue(PlatformSupport.isTransportServicesEventLoopGroup(tsGroup.next()))
  161. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  162. defer {
  163. XCTAssertNoThrow(try group.syncShutdownGracefully())
  164. }
  165. XCTAssertFalse(PlatformSupport.isTransportServicesEventLoopGroup(group))
  166. XCTAssertFalse(PlatformSupport.isTransportServicesEventLoopGroup(group.next()))
  167. #endif
  168. }
  169. func testIsTLSConfigruationCompatible() {
  170. #if canImport(Network)
  171. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  172. let nwConfiguration = GRPCTLSConfiguration.makeClientConfigurationBackedByNetworkFramework()
  173. let nioSSLConfiguration = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL()
  174. let tsGroup = NIOTSEventLoopGroup(loopCount: 1)
  175. defer {
  176. XCTAssertNoThrow(try tsGroup.syncShutdownGracefully())
  177. }
  178. XCTAssertTrue(tsGroup.isCompatible(with: nwConfiguration))
  179. XCTAssertTrue(tsGroup.isCompatible(with: nioSSLConfiguration))
  180. XCTAssertTrue(tsGroup.next().isCompatible(with: nwConfiguration))
  181. XCTAssertTrue(tsGroup.next().isCompatible(with: nioSSLConfiguration))
  182. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  183. defer {
  184. XCTAssertNoThrow(try group.syncShutdownGracefully())
  185. }
  186. XCTAssertFalse(group.isCompatible(with: nwConfiguration))
  187. XCTAssertTrue(group.isCompatible(with: nioSSLConfiguration))
  188. XCTAssertFalse(group.next().isCompatible(with: nwConfiguration))
  189. XCTAssertTrue(group.next().isCompatible(with: nioSSLConfiguration))
  190. #endif
  191. }
  192. }