2
0

BasicEchoTestCase.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_EchoServiceClient!
  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 makeClientConfiguration(port: Int) throws -> ClientConnection.Configuration {
  101. return .init(
  102. target: .hostAndPort("localhost", port),
  103. eventLoopGroup: self.clientEventLoopGroup,
  104. tls: self.transportSecurity.makeClientTLSConfiguration())
  105. }
  106. func makeServerConfiguration() throws -> Server.Configuration {
  107. return .init(
  108. target: .hostAndPort("localhost", 0),
  109. eventLoopGroup: self.serverEventLoopGroup,
  110. serviceProviders: [makeEchoProvider()],
  111. errorDelegate: self.makeErrorDelegate(),
  112. tls: self.transportSecurity.makeServerTLSConfiguration())
  113. }
  114. func makeServer() throws -> Server {
  115. return try Server.start(configuration: self.makeServerConfiguration()).wait()
  116. }
  117. func makeClientConnection(port: Int) throws -> ClientConnection {
  118. return try ClientConnection(configuration: self.makeClientConfiguration(port: port))
  119. }
  120. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  121. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  122. func makeEchoClient(port: Int) throws -> Echo_EchoServiceClient {
  123. return Echo_EchoServiceClient(connection: try self.makeClientConnection(port: port))
  124. }
  125. override func setUp() {
  126. super.setUp()
  127. self.serverEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  128. loopCount: 1,
  129. networkPreference: self.networkPreference)
  130. self.server = try! self.makeServer()
  131. self.port = self.server.channel.localAddress!.port!
  132. self.clientEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  133. loopCount: 1,
  134. networkPreference: self.networkPreference)
  135. self.client = try! self.makeEchoClient(port: self.port)
  136. }
  137. override func tearDown() {
  138. // Some tests close the channel, so would throw here if called twice.
  139. try? self.client.connection.close().wait()
  140. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  141. self.client = nil
  142. self.clientEventLoopGroup = nil
  143. XCTAssertNoThrow(try self.server.close().wait())
  144. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  145. self.server = nil
  146. self.serverEventLoopGroup = nil
  147. self.port = nil
  148. super.tearDown()
  149. }
  150. }
  151. extension EchoTestCaseBase {
  152. func makeExpectation(description: String, expectedFulfillmentCount: Int = 1, assertForOverFulfill: Bool = true) -> XCTestExpectation {
  153. let expectation = self.expectation(description: description)
  154. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  155. expectation.assertForOverFulfill = assertForOverFulfill
  156. return expectation
  157. }
  158. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  159. return makeExpectation(description: "Expecting status received",
  160. expectedFulfillmentCount: expectedFulfillmentCount)
  161. }
  162. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  163. return makeExpectation(description: "Expecting \(expectedFulfillmentCount) response(s)",
  164. expectedFulfillmentCount: expectedFulfillmentCount)
  165. }
  166. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  167. return makeExpectation(
  168. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  169. expectedFulfillmentCount: expectedFulfillmentCount)
  170. }
  171. func makeInitialMetadataExpectation() -> XCTestExpectation {
  172. return makeExpectation(description: "Expecting initial metadata")
  173. }
  174. }