PlatformSupportTests.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. import NIOCore
  18. import NIOPosix
  19. import NIOTransportServices
  20. import XCTest
  21. @testable import GRPC
  22. #if canImport(Network)
  23. import Network
  24. #endif
  25. class PlatformSupportTests: GRPCTestCase {
  26. var group: EventLoopGroup!
  27. override func tearDown() {
  28. XCTAssertNoThrow(try self.group?.syncShutdownGracefully())
  29. super.tearDown()
  30. }
  31. func testMakeEventLoopGroupReturnsMultiThreadedGroupForPosix() {
  32. self.group = PlatformSupport.makeEventLoopGroup(
  33. loopCount: 1,
  34. networkPreference: .userDefined(.posix)
  35. )
  36. XCTAssertTrue(self.group is MultiThreadedEventLoopGroup)
  37. }
  38. func testMakeEventLoopGroupReturnsNIOTSGroupForNetworkFramework() {
  39. // If we don't have Network.framework then we can't test this.
  40. #if canImport(Network)
  41. guard #available(macOS 10.14, *) else { return }
  42. self.group = PlatformSupport.makeEventLoopGroup(
  43. loopCount: 1,
  44. networkPreference: .userDefined(.networkFramework)
  45. )
  46. XCTAssertTrue(self.group is NIOTSEventLoopGroup)
  47. #endif
  48. }
  49. func testMakeClientBootstrapReturnsClientBootstrapForMultiThreadedGroup() {
  50. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  51. let bootstrap = PlatformSupport.makeClientBootstrap(group: self.group)
  52. XCTAssertTrue(bootstrap is ClientBootstrap)
  53. }
  54. func testMakeClientBootstrapReturnsClientBootstrapForEventLoop() {
  55. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  56. let eventLoop = self.group.next()
  57. let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
  58. XCTAssertTrue(bootstrap is ClientBootstrap)
  59. }
  60. func testMakeClientBootstrapReturnsNIOTSConnectionBootstrapForNIOTSGroup() {
  61. // If we don't have Network.framework then we can't test this.
  62. #if canImport(Network)
  63. guard #available(macOS 10.14, *) else { return }
  64. self.group = NIOTSEventLoopGroup(loopCount: 1)
  65. let bootstrap = PlatformSupport.makeClientBootstrap(group: self.group)
  66. XCTAssertTrue(bootstrap is NIOTSConnectionBootstrap)
  67. #endif
  68. }
  69. func testMakeClientBootstrapReturnsNIOTSConnectionBootstrapForQoSEventLoop() {
  70. // If we don't have Network.framework then we can't test this.
  71. #if canImport(Network)
  72. guard #available(macOS 10.14, *) else { return }
  73. self.group = NIOTSEventLoopGroup(loopCount: 1)
  74. let eventLoop = self.group.next()
  75. XCTAssertTrue(eventLoop is QoSEventLoop)
  76. let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
  77. XCTAssertTrue(bootstrap is NIOTSConnectionBootstrap)
  78. #endif
  79. }
  80. func testMakeServerBootstrapReturnsServerBootstrapForMultiThreadedGroup() {
  81. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  82. let bootstrap = PlatformSupport.makeServerBootstrap(group: self.group)
  83. XCTAssertTrue(bootstrap is ServerBootstrap)
  84. }
  85. func testMakeServerBootstrapReturnsServerBootstrapForEventLoop() {
  86. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  87. let eventLoop = self.group.next()
  88. let bootstrap = PlatformSupport.makeServerBootstrap(group: eventLoop)
  89. XCTAssertTrue(bootstrap is ServerBootstrap)
  90. }
  91. func testMakeServerBootstrapReturnsNIOTSListenerBootstrapForNIOTSGroup() {
  92. // If we don't have Network.framework then we can't test this.
  93. #if canImport(Network)
  94. guard #available(macOS 10.14, *) else { return }
  95. self.group = NIOTSEventLoopGroup(loopCount: 1)
  96. let bootstrap = PlatformSupport.makeServerBootstrap(group: self.group)
  97. XCTAssertTrue(bootstrap is NIOTSListenerBootstrap)
  98. #endif
  99. }
  100. func testMakeServerBootstrapReturnsNIOTSListenerBootstrapForQoSEventLoop() {
  101. // If we don't have Network.framework then we can't test this.
  102. #if canImport(Network)
  103. guard #available(macOS 10.14, *) else { return }
  104. self.group = NIOTSEventLoopGroup(loopCount: 1)
  105. let eventLoop = self.group.next()
  106. XCTAssertTrue(eventLoop is QoSEventLoop)
  107. let bootstrap = PlatformSupport.makeServerBootstrap(group: eventLoop)
  108. XCTAssertTrue(bootstrap is NIOTSListenerBootstrap)
  109. #endif
  110. }
  111. func testRequiresZeroLengthWorkaroundWithMTELG() {
  112. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  113. // No MTELG or individual loop requires the workaround.
  114. XCTAssertFalse(
  115. PlatformSupport
  116. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: true)
  117. )
  118. XCTAssertFalse(
  119. PlatformSupport
  120. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: false)
  121. )
  122. XCTAssertFalse(
  123. PlatformSupport
  124. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: true)
  125. )
  126. XCTAssertFalse(
  127. PlatformSupport
  128. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: false)
  129. )
  130. }
  131. func testRequiresZeroLengthWorkaroundWithNetworkFramework() {
  132. // If we don't have Network.framework we can't test this.
  133. #if canImport(Network)
  134. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  135. self.group = NIOTSEventLoopGroup(loopCount: 1)
  136. // We require the workaround for any of these loops when TLS is not enabled.
  137. XCTAssertFalse(
  138. PlatformSupport
  139. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: true)
  140. )
  141. XCTAssertTrue(
  142. PlatformSupport
  143. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: false)
  144. )
  145. XCTAssertFalse(
  146. PlatformSupport
  147. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: true)
  148. )
  149. XCTAssertTrue(
  150. PlatformSupport
  151. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: false)
  152. )
  153. #endif
  154. }
  155. func testIsTransportServicesGroup() {
  156. #if canImport(Network)
  157. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  158. let tsGroup = NIOTSEventLoopGroup(loopCount: 1)
  159. defer {
  160. XCTAssertNoThrow(try tsGroup.syncShutdownGracefully())
  161. }
  162. XCTAssertTrue(PlatformSupport.isTransportServicesEventLoopGroup(tsGroup))
  163. XCTAssertTrue(PlatformSupport.isTransportServicesEventLoopGroup(tsGroup.next()))
  164. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  165. defer {
  166. XCTAssertNoThrow(try group.syncShutdownGracefully())
  167. }
  168. XCTAssertFalse(PlatformSupport.isTransportServicesEventLoopGroup(group))
  169. XCTAssertFalse(PlatformSupport.isTransportServicesEventLoopGroup(group.next()))
  170. #endif
  171. }
  172. func testIsTLSConfigruationCompatible() {
  173. #if canImport(Network)
  174. #if canImport(NIOSSL)
  175. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  176. let nwConfiguration = GRPCTLSConfiguration.makeClientConfigurationBackedByNetworkFramework()
  177. let nioSSLConfiguration = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL()
  178. let tsGroup = NIOTSEventLoopGroup(loopCount: 1)
  179. defer {
  180. XCTAssertNoThrow(try tsGroup.syncShutdownGracefully())
  181. }
  182. XCTAssertTrue(tsGroup.isCompatible(with: nwConfiguration))
  183. XCTAssertTrue(tsGroup.isCompatible(with: nioSSLConfiguration))
  184. XCTAssertTrue(tsGroup.next().isCompatible(with: nwConfiguration))
  185. XCTAssertTrue(tsGroup.next().isCompatible(with: nioSSLConfiguration))
  186. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  187. defer {
  188. XCTAssertNoThrow(try group.syncShutdownGracefully())
  189. }
  190. XCTAssertFalse(group.isCompatible(with: nwConfiguration))
  191. XCTAssertTrue(group.isCompatible(with: nioSSLConfiguration))
  192. XCTAssertFalse(group.next().isCompatible(with: nwConfiguration))
  193. XCTAssertTrue(group.next().isCompatible(with: nioSSLConfiguration))
  194. #endif
  195. #endif
  196. }
  197. func testMakeCompatibleEventLoopGroupForNIOSSL() {
  198. #if canImport(NIOSSL)
  199. let configuration = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL()
  200. let group = PlatformSupport.makeEventLoopGroup(compatibleWith: configuration, loopCount: 1)
  201. XCTAssertNoThrow(try group.syncShutdownGracefully())
  202. XCTAssert(group is MultiThreadedEventLoopGroup)
  203. #endif
  204. }
  205. func testMakeCompatibleEventLoopGroupForNetworkFramework() {
  206. #if canImport(Network)
  207. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  208. let options = NWProtocolTLS.Options()
  209. let configuration = GRPCTLSConfiguration.makeClientConfigurationBackedByNetworkFramework(
  210. options: options
  211. )
  212. let group = PlatformSupport.makeEventLoopGroup(compatibleWith: configuration, loopCount: 1)
  213. XCTAssertNoThrow(try group.syncShutdownGracefully())
  214. XCTAssert(group is NIOTSEventLoopGroup)
  215. #endif
  216. }
  217. }