2
0

BasicEchoTestCase.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2018, 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 Dispatch
  17. import Foundation
  18. import NIO
  19. import NIOSSL
  20. import GRPC
  21. import GRPCSampleData
  22. import XCTest
  23. extension Echo_EchoRequest {
  24. init(text: String) {
  25. self.text = text
  26. }
  27. }
  28. extension Echo_EchoResponse {
  29. init(text: String) {
  30. self.text = text
  31. }
  32. }
  33. enum TransportSecurity {
  34. case none
  35. case anonymousClient
  36. case mutualAuthentication
  37. }
  38. extension TransportSecurity {
  39. var caCert: NIOSSLCertificate {
  40. let cert = SampleCertificate.ca
  41. cert.assertNotExpired()
  42. return cert.certificate
  43. }
  44. var clientCert: NIOSSLCertificate {
  45. let cert = SampleCertificate.client
  46. cert.assertNotExpired()
  47. return cert.certificate
  48. }
  49. var serverCert: NIOSSLCertificate {
  50. let cert = SampleCertificate.server
  51. cert.assertNotExpired()
  52. return cert.certificate
  53. }
  54. }
  55. extension TransportSecurity {
  56. func makeServerConfiguration() throws -> Server.TLSConfiguration? {
  57. guard let config = try self.makeServerTLSConfiguration() else {
  58. return nil
  59. }
  60. let context = try NIOSSLContext(configuration: config)
  61. return .init(sslContext: context)
  62. }
  63. func makeServerTLSConfiguration() throws -> TLSConfiguration? {
  64. switch self {
  65. case .none:
  66. return nil
  67. case .anonymousClient, .mutualAuthentication:
  68. return .forServer(certificateChain: [.certificate(self.serverCert)],
  69. privateKey: .privateKey(SamplePrivateKey.server),
  70. trustRoots: .certificates ([self.caCert]),
  71. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  72. }
  73. }
  74. func makeClientConfiguration() throws -> ClientConnection.TLSConfiguration? {
  75. guard let config = try self.makeClientTLSConfiguration() else {
  76. return nil
  77. }
  78. let context = try NIOSSLContext(configuration: config)
  79. return ClientConnection.TLSConfiguration(sslContext: context)
  80. }
  81. func makeClientTLSConfiguration() throws -> TLSConfiguration? {
  82. switch self {
  83. case .none:
  84. return nil
  85. case .anonymousClient:
  86. return .forClient(certificateVerification: .noHostnameVerification,
  87. trustRoots: .certificates([self.caCert]),
  88. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  89. case .mutualAuthentication:
  90. return .forClient(certificateVerification: .noHostnameVerification,
  91. trustRoots: .certificates([self.caCert]),
  92. certificateChain: [.certificate(self.clientCert)],
  93. privateKey: .privateKey(SamplePrivateKey.client),
  94. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  95. }
  96. }
  97. }
  98. class EchoTestCaseBase: XCTestCase {
  99. var defaultTestTimeout: TimeInterval = 1.0
  100. var serverEventLoopGroup: EventLoopGroup!
  101. var clientEventLoopGroup: EventLoopGroup!
  102. var transportSecurity: TransportSecurity { return .none }
  103. var server: Server!
  104. var client: Echo_EchoServiceClient!
  105. var port: Int!
  106. // Prefer POSIX: subclasses can override this and add availability checks to ensure NIOTS
  107. // variants run where possible.
  108. var networkPreference: NetworkPreference {
  109. return .userDefined(.posix)
  110. }
  111. func makeClientConfiguration(port: Int) throws -> ClientConnection.Configuration {
  112. return .init(
  113. target: .hostAndPort("localhost", port),
  114. eventLoopGroup: self.clientEventLoopGroup,
  115. tlsConfiguration: try self.transportSecurity.makeClientConfiguration())
  116. }
  117. func makeServerConfiguration() throws -> Server.Configuration {
  118. return .init(
  119. target: .hostAndPort("localhost", 0),
  120. eventLoopGroup: self.serverEventLoopGroup,
  121. serviceProviders: [makeEchoProvider()],
  122. errorDelegate: self.makeErrorDelegate(),
  123. tlsConfiguration: try self.transportSecurity.makeServerConfiguration())
  124. }
  125. func makeServer() throws -> Server {
  126. return try Server.start(configuration: self.makeServerConfiguration()).wait()
  127. }
  128. func makeClientConnection(port: Int) throws -> ClientConnection {
  129. return try ClientConnection.start(self.makeClientConfiguration(port: port)).wait()
  130. }
  131. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  132. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  133. func makeEchoClient(port: Int) throws -> Echo_EchoServiceClient {
  134. return Echo_EchoServiceClient(connection: try self.makeClientConnection(port: port))
  135. }
  136. override func setUp() {
  137. super.setUp()
  138. self.serverEventLoopGroup = GRPCNIO.makeEventLoopGroup(
  139. loopCount: 1,
  140. networkPreference: self.networkPreference)
  141. self.server = try! self.makeServer()
  142. self.port = self.server.channel.localAddress!.port!
  143. self.clientEventLoopGroup = GRPCNIO.makeEventLoopGroup(
  144. loopCount: 1,
  145. networkPreference: self.networkPreference)
  146. self.client = try! self.makeEchoClient(port: self.port)
  147. }
  148. override func tearDown() {
  149. // Some tests close the channel, so would throw here if called twice.
  150. try? self.client.connection.close().wait()
  151. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  152. self.client = nil
  153. self.clientEventLoopGroup = nil
  154. XCTAssertNoThrow(try self.server.close().wait())
  155. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  156. self.server = nil
  157. self.serverEventLoopGroup = nil
  158. self.port = nil
  159. super.tearDown()
  160. }
  161. }
  162. extension EchoTestCaseBase {
  163. func makeExpectation(description: String, expectedFulfillmentCount: Int = 1, assertForOverFulfill: Bool = true) -> XCTestExpectation {
  164. let expectation = self.expectation(description: description)
  165. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  166. expectation.assertForOverFulfill = assertForOverFulfill
  167. return expectation
  168. }
  169. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  170. return makeExpectation(description: "Expecting status received",
  171. expectedFulfillmentCount: expectedFulfillmentCount)
  172. }
  173. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  174. return makeExpectation(description: "Expecting \(expectedFulfillmentCount) response(s)",
  175. expectedFulfillmentCount: expectedFulfillmentCount)
  176. }
  177. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  178. return makeExpectation(
  179. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  180. expectedFulfillmentCount: expectedFulfillmentCount)
  181. }
  182. func makeInitialMetadataExpectation() -> XCTestExpectation {
  183. return makeExpectation(description: "Expecting initial metadata")
  184. }
  185. }