BasicEchoTestCase.swift 6.1 KB

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