BasicEchoTestCase.swift 6.6 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. @testable 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 makeServerTLS() throws -> Server.TLSMode {
  57. return try makeServerTLSConfiguration().map { .custom(try NIOSSLContext(configuration: $0)) } ?? .none
  58. }
  59. func makeServerTLSConfiguration() throws -> TLSConfiguration? {
  60. switch self {
  61. case .none:
  62. return nil
  63. case .anonymousClient, .mutualAuthentication:
  64. return .forServer(certificateChain: [.certificate(self.serverCert)],
  65. privateKey: .privateKey(SamplePrivateKey.server),
  66. trustRoots: .certificates ([self.caCert]),
  67. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  68. }
  69. }
  70. func makeConfiguration() throws -> ClientConnection.TLSConfiguration? {
  71. guard let config = try self.makeClientTLSConfiguration() else {
  72. return nil
  73. }
  74. let context = try NIOSSLContext(configuration: config)
  75. return ClientConnection.TLSConfiguration(sslContext: context)
  76. }
  77. func makeClientTLSConfiguration() throws -> TLSConfiguration? {
  78. switch self {
  79. case .none:
  80. return nil
  81. case .anonymousClient:
  82. return .forClient(certificateVerification: .noHostnameVerification,
  83. trustRoots: .certificates([self.caCert]),
  84. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  85. case .mutualAuthentication:
  86. return .forClient(certificateVerification: .noHostnameVerification,
  87. trustRoots: .certificates([self.caCert]),
  88. certificateChain: [.certificate(self.clientCert)],
  89. privateKey: .privateKey(SamplePrivateKey.client),
  90. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  91. }
  92. }
  93. }
  94. class EchoTestCaseBase: XCTestCase {
  95. var defaultTestTimeout: TimeInterval = 1.0
  96. var serverEventLoopGroup: EventLoopGroup!
  97. var clientEventLoopGroup: EventLoopGroup!
  98. var transportSecurity: TransportSecurity { return .none }
  99. var server: Server!
  100. var client: Echo_EchoServiceClient!
  101. func makeClientConfiguration() throws -> ClientConnection.Configuration {
  102. return .init(
  103. target: .hostAndPort("localhost", 5050),
  104. eventLoopGroup: self.clientEventLoopGroup,
  105. tlsConfiguration: try self.transportSecurity.makeConfiguration())
  106. }
  107. func makeServer() throws -> Server {
  108. return try Server.start(
  109. hostname: "localhost",
  110. port: 5050,
  111. eventLoopGroup: self.serverEventLoopGroup,
  112. serviceProviders: [makeEchoProvider()],
  113. errorDelegate: makeErrorDelegate(),
  114. tls: try self.transportSecurity.makeServerTLS()
  115. ).wait()
  116. }
  117. func makeClientConnection() throws -> ClientConnection {
  118. return try ClientConnection.start(self.makeClientConfiguration()).wait()
  119. }
  120. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  121. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  122. func makeEchoClient() throws -> Echo_EchoServiceClient {
  123. return Echo_EchoServiceClient(connection: try self.makeClientConnection())
  124. }
  125. override func setUp() {
  126. super.setUp()
  127. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  128. self.server = try! self.makeServer()
  129. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  130. self.client = try! self.makeEchoClient()
  131. }
  132. override func tearDown() {
  133. // Some tests close the channel, so would throw here if called twice.
  134. if self.client.connection.channel.isActive {
  135. XCTAssertNoThrow(try self.client.connection.close().wait())
  136. }
  137. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  138. self.client = nil
  139. self.clientEventLoopGroup = nil
  140. XCTAssertNoThrow(try self.server.close().wait())
  141. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  142. self.server = nil
  143. self.serverEventLoopGroup = 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. }