BasicEchoTestCase.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 EchoModel
  23. import EchoImplementation
  24. import XCTest
  25. extension Echo_EchoRequest {
  26. init(text: String) {
  27. self = .with {
  28. $0.text = text
  29. }
  30. }
  31. }
  32. extension Echo_EchoResponse {
  33. init(text: String) {
  34. self = .with {
  35. $0.text = text
  36. }
  37. }
  38. }
  39. enum TransportSecurity {
  40. case none
  41. case anonymousClient
  42. case mutualAuthentication
  43. }
  44. extension TransportSecurity {
  45. var caCert: NIOSSLCertificate {
  46. let cert = SampleCertificate.ca
  47. cert.assertNotExpired()
  48. return cert.certificate
  49. }
  50. var clientCert: NIOSSLCertificate {
  51. let cert = SampleCertificate.client
  52. cert.assertNotExpired()
  53. return cert.certificate
  54. }
  55. var serverCert: NIOSSLCertificate {
  56. let cert = SampleCertificate.server
  57. cert.assertNotExpired()
  58. return cert.certificate
  59. }
  60. }
  61. extension TransportSecurity {
  62. func makeServerTLSConfiguration() -> Server.Configuration.TLS? {
  63. switch self {
  64. case .none:
  65. return nil
  66. case .anonymousClient, .mutualAuthentication:
  67. return .init(certificateChain: [.certificate(self.serverCert)],
  68. privateKey: .privateKey(SamplePrivateKey.server),
  69. trustRoots: .certificates ([self.caCert]))
  70. }
  71. }
  72. func makeClientTLSConfiguration() -> ClientConnection.Configuration.TLS? {
  73. switch self {
  74. case .none:
  75. return nil
  76. case .anonymousClient:
  77. return .init(trustRoots: .certificates([self.caCert]))
  78. case .mutualAuthentication:
  79. return .init(
  80. certificateChain: [.certificate(self.clientCert)],
  81. privateKey: .privateKey(SamplePrivateKey.client),
  82. trustRoots: .certificates([self.caCert])
  83. )
  84. }
  85. }
  86. }
  87. class EchoTestCaseBase: GRPCTestCase {
  88. var defaultTestTimeout: TimeInterval = 1.0
  89. var serverEventLoopGroup: EventLoopGroup!
  90. var clientEventLoopGroup: EventLoopGroup!
  91. var transportSecurity: TransportSecurity { return .none }
  92. var server: Server!
  93. var client: Echo_EchoClient!
  94. var port: Int!
  95. // Prefer POSIX: subclasses can override this and add availability checks to ensure NIOTS
  96. // variants run where possible.
  97. var networkPreference: NetworkPreference {
  98. return .userDefined(.posix)
  99. }
  100. func connectionBuilder() -> ClientConnection.Builder {
  101. switch self.transportSecurity {
  102. case .none:
  103. return ClientConnection.insecure(group: self.clientEventLoopGroup)
  104. case .anonymousClient:
  105. return ClientConnection.secure(group: self.clientEventLoopGroup)
  106. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  107. case .mutualAuthentication:
  108. return ClientConnection.secure(group: self.clientEventLoopGroup)
  109. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  110. .withTLS(certificateChain: [SampleCertificate.client.certificate])
  111. .withTLS(privateKey: SamplePrivateKey.client)
  112. }
  113. }
  114. func makeServerConfiguration() throws -> Server.Configuration {
  115. return .init(
  116. target: .hostAndPort("localhost", 0),
  117. eventLoopGroup: self.serverEventLoopGroup,
  118. serviceProviders: [makeEchoProvider()],
  119. errorDelegate: self.makeErrorDelegate(),
  120. tls: self.transportSecurity.makeServerTLSConfiguration())
  121. }
  122. func makeServer() throws -> Server {
  123. return try Server.start(configuration: self.makeServerConfiguration()).wait()
  124. }
  125. func makeClientConnection(port: Int) throws -> ClientConnection {
  126. return self.connectionBuilder().connect(
  127. host: "localhost",
  128. port: port
  129. )
  130. }
  131. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  132. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  133. func makeEchoClient(port: Int) throws -> Echo_EchoClient {
  134. return Echo_EchoClient(channel: try self.makeClientConnection(port: port))
  135. }
  136. override func setUp() {
  137. super.setUp()
  138. self.serverEventLoopGroup = PlatformSupport.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 = PlatformSupport.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.channel.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. }