BasicEchoTestCase.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 makeServerTLSConfiguration() -> Server.Configuration.TLS? {
  57. switch self {
  58. case .none:
  59. return nil
  60. case .anonymousClient, .mutualAuthentication:
  61. return .init(certificateChain: [.certificate(self.serverCert)],
  62. privateKey: .privateKey(SamplePrivateKey.server),
  63. trustRoots: .certificates ([self.caCert]))
  64. }
  65. }
  66. func makeClientTLSConfiguration() -> ClientConnection.Configuration.TLS? {
  67. switch self {
  68. case .none:
  69. return nil
  70. case .anonymousClient:
  71. return .init(
  72. trustRoots: .certificates([self.caCert]),
  73. certificateVerification: .noHostnameVerification)
  74. case .mutualAuthentication:
  75. return .init(
  76. certificateChain: [.certificate(self.clientCert)],
  77. privateKey: .privateKey(SamplePrivateKey.client),
  78. trustRoots: .certificates([self.caCert]),
  79. certificateVerification: .noHostnameVerification)
  80. }
  81. }
  82. }
  83. class EchoTestCaseBase: GRPCTestCase {
  84. var defaultTestTimeout: TimeInterval = 1.0
  85. var serverEventLoopGroup: EventLoopGroup!
  86. var clientEventLoopGroup: EventLoopGroup!
  87. var transportSecurity: TransportSecurity { return .none }
  88. var server: Server!
  89. var client: Echo_EchoServiceClient!
  90. var port: Int!
  91. // Prefer POSIX: subclasses can override this and add availability checks to ensure NIOTS
  92. // variants run where possible.
  93. var networkPreference: NetworkPreference {
  94. return .userDefined(.posix)
  95. }
  96. func makeClientConfiguration(port: Int) throws -> ClientConnection.Configuration {
  97. return .init(
  98. target: .hostAndPort("localhost", port),
  99. eventLoopGroup: self.clientEventLoopGroup,
  100. tls: self.transportSecurity.makeClientTLSConfiguration())
  101. }
  102. func makeServerConfiguration() throws -> Server.Configuration {
  103. return .init(
  104. target: .hostAndPort("localhost", 0),
  105. eventLoopGroup: self.serverEventLoopGroup,
  106. serviceProviders: [makeEchoProvider()],
  107. errorDelegate: self.makeErrorDelegate(),
  108. tls: self.transportSecurity.makeServerTLSConfiguration())
  109. }
  110. func makeServer() throws -> Server {
  111. return try Server.start(configuration: self.makeServerConfiguration()).wait()
  112. }
  113. func makeClientConnection(port: Int) throws -> ClientConnection {
  114. return try ClientConnection(configuration: self.makeClientConfiguration(port: port))
  115. }
  116. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  117. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  118. func makeEchoClient(port: Int) throws -> Echo_EchoServiceClient {
  119. return Echo_EchoServiceClient(connection: try self.makeClientConnection(port: port))
  120. }
  121. override func setUp() {
  122. super.setUp()
  123. self.serverEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  124. loopCount: 1,
  125. networkPreference: self.networkPreference)
  126. self.server = try! self.makeServer()
  127. self.port = self.server.channel.localAddress!.port!
  128. self.clientEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  129. loopCount: 1,
  130. networkPreference: self.networkPreference)
  131. self.client = try! self.makeEchoClient(port: self.port)
  132. }
  133. override func tearDown() {
  134. // Some tests close the channel, so would throw here if called twice.
  135. try? self.client.connection.close().wait()
  136. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  137. self.client = nil
  138. self.clientEventLoopGroup = nil
  139. XCTAssertNoThrow(try self.server.close().wait())
  140. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  141. self.server = nil
  142. self.serverEventLoopGroup = nil
  143. self.port = nil
  144. super.tearDown()
  145. }
  146. }
  147. extension EchoTestCaseBase {
  148. func makeExpectation(description: String, expectedFulfillmentCount: Int = 1, assertForOverFulfill: Bool = true) -> XCTestExpectation {
  149. let expectation = self.expectation(description: description)
  150. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  151. expectation.assertForOverFulfill = assertForOverFulfill
  152. return expectation
  153. }
  154. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  155. return makeExpectation(description: "Expecting status received",
  156. expectedFulfillmentCount: expectedFulfillmentCount)
  157. }
  158. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  159. return makeExpectation(description: "Expecting \(expectedFulfillmentCount) response(s)",
  160. expectedFulfillmentCount: expectedFulfillmentCount)
  161. }
  162. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  163. return makeExpectation(
  164. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  165. expectedFulfillmentCount: expectedFulfillmentCount)
  166. }
  167. func makeInitialMetadataExpectation() -> XCTestExpectation {
  168. return makeExpectation(description: "Expecting initial metadata")
  169. }
  170. }