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 EchoImplementation
  18. import EchoModel
  19. import Foundation
  20. import GRPC
  21. import GRPCSampleData
  22. import NIOCore
  23. import NIOSSL
  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. class EchoTestCaseBase: GRPCTestCase {
  45. // Things can be slow when running under TSAN; bias towards a really long timeout so that we know
  46. // for sure a test is wedged rather than simply slow.
  47. var defaultTestTimeout: TimeInterval = 120.0
  48. var serverEventLoopGroup: EventLoopGroup!
  49. var clientEventLoopGroup: EventLoopGroup!
  50. var transportSecurity: TransportSecurity { return .none }
  51. var server: Server!
  52. var client: Echo_EchoClient!
  53. var port: Int!
  54. // Prefer POSIX: subclasses can override this and add availability checks to ensure NIOTS
  55. // variants run where possible.
  56. var networkPreference: NetworkPreference {
  57. return .userDefined(.posix)
  58. }
  59. func connectionBuilder() -> ClientConnection.Builder {
  60. switch self.transportSecurity {
  61. case .none:
  62. return ClientConnection.insecure(group: self.clientEventLoopGroup)
  63. case .anonymousClient:
  64. return ClientConnection.usingTLSBackedByNIOSSL(on: self.clientEventLoopGroup)
  65. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  66. case .mutualAuthentication:
  67. return ClientConnection.usingTLSBackedByNIOSSL(on: self.clientEventLoopGroup)
  68. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  69. .withTLS(certificateChain: [SampleCertificate.client.certificate])
  70. .withTLS(privateKey: SamplePrivateKey.client)
  71. }
  72. }
  73. func serverBuilder() -> Server.Builder {
  74. switch self.transportSecurity {
  75. case .none:
  76. return Server.insecure(group: self.serverEventLoopGroup)
  77. case .anonymousClient:
  78. return Server.usingTLSBackedByNIOSSL(
  79. on: self.serverEventLoopGroup,
  80. certificateChain: [SampleCertificate.server.certificate],
  81. privateKey: SamplePrivateKey.server
  82. ).withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  83. case .mutualAuthentication:
  84. return Server.usingTLSBackedByNIOSSL(
  85. on: self.serverEventLoopGroup,
  86. certificateChain: [SampleCertificate.server.certificate],
  87. privateKey: SamplePrivateKey.server
  88. )
  89. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  90. .withTLS(certificateVerification: .noHostnameVerification)
  91. }
  92. }
  93. func makeServer() throws -> Server {
  94. return try self.serverBuilder()
  95. .withErrorDelegate(self.makeErrorDelegate())
  96. .withServiceProviders([self.makeEchoProvider()])
  97. .withLogger(self.serverLogger)
  98. .bind(host: "localhost", port: 0)
  99. .wait()
  100. }
  101. func makeClientConnection(port: Int) throws -> ClientConnection {
  102. return self.connectionBuilder()
  103. .withBackgroundActivityLogger(self.clientLogger)
  104. .connect(host: "localhost", port: port)
  105. }
  106. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  107. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  108. func makeEchoClient(port: Int) throws -> Echo_EchoClient {
  109. return Echo_EchoClient(
  110. channel: try self.makeClientConnection(port: port),
  111. defaultCallOptions: self.callOptionsWithLogger
  112. )
  113. }
  114. override func setUp() {
  115. super.setUp()
  116. self.serverEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  117. loopCount: 1,
  118. networkPreference: self.networkPreference
  119. )
  120. self.server = try! self.makeServer()
  121. self.port = self.server.channel.localAddress!.port!
  122. self.clientEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  123. loopCount: 1,
  124. networkPreference: self.networkPreference
  125. )
  126. self.client = try! self.makeEchoClient(port: self.port)
  127. }
  128. override func tearDown() {
  129. // Some tests close the channel, so would throw here if called twice.
  130. try? self.client.channel.close().wait()
  131. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  132. self.client = nil
  133. self.clientEventLoopGroup = nil
  134. XCTAssertNoThrow(try self.server.close().wait())
  135. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  136. self.server = nil
  137. self.serverEventLoopGroup = nil
  138. self.port = nil
  139. super.tearDown()
  140. }
  141. }
  142. extension EchoTestCaseBase {
  143. func makeExpectation(
  144. description: String,
  145. expectedFulfillmentCount: Int = 1,
  146. assertForOverFulfill: Bool = true
  147. ) -> XCTestExpectation {
  148. let expectation = self.expectation(description: description)
  149. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  150. expectation.assertForOverFulfill = assertForOverFulfill
  151. return expectation
  152. }
  153. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  154. return self.makeExpectation(
  155. description: "Expecting status received",
  156. expectedFulfillmentCount: expectedFulfillmentCount
  157. )
  158. }
  159. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  160. return self.makeExpectation(
  161. description: "Expecting \(expectedFulfillmentCount) response(s)",
  162. expectedFulfillmentCount: expectedFulfillmentCount
  163. )
  164. }
  165. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  166. return self.makeExpectation(
  167. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  168. expectedFulfillmentCount: expectedFulfillmentCount
  169. )
  170. }
  171. func makeInitialMetadataExpectation() -> XCTestExpectation {
  172. return self.makeExpectation(description: "Expecting initial metadata")
  173. }
  174. }