BasicEchoTestCase.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 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.secure(group: self.clientEventLoopGroup)
  65. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  66. case .mutualAuthentication:
  67. return ClientConnection.secure(group: 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, .mutualAuthentication:
  78. return Server.secure(
  79. group: self.serverEventLoopGroup,
  80. certificateChain: [SampleCertificate.server.certificate],
  81. privateKey: SamplePrivateKey.server
  82. ).withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  83. }
  84. }
  85. func makeServer() throws -> Server {
  86. return try self.serverBuilder()
  87. .withErrorDelegate(makeErrorDelegate())
  88. .withServiceProviders([makeEchoProvider()])
  89. .bind(host: "localhost", port: 0)
  90. .wait()
  91. }
  92. func makeClientConnection(port: Int) throws -> ClientConnection {
  93. return self.connectionBuilder().connect(
  94. host: "localhost",
  95. port: port
  96. )
  97. }
  98. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  99. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  100. func makeEchoClient(port: Int) throws -> Echo_EchoClient {
  101. return Echo_EchoClient(channel: try self.makeClientConnection(port: port))
  102. }
  103. override func setUp() {
  104. super.setUp()
  105. self.serverEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  106. loopCount: 1,
  107. networkPreference: self.networkPreference)
  108. self.server = try! self.makeServer()
  109. self.port = self.server.channel.localAddress!.port!
  110. self.clientEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  111. loopCount: 1,
  112. networkPreference: self.networkPreference)
  113. self.client = try! self.makeEchoClient(port: self.port)
  114. }
  115. override func tearDown() {
  116. // Some tests close the channel, so would throw here if called twice.
  117. try? self.client.channel.close().wait()
  118. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  119. self.client = nil
  120. self.clientEventLoopGroup = nil
  121. XCTAssertNoThrow(try self.server.close().wait())
  122. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  123. self.server = nil
  124. self.serverEventLoopGroup = nil
  125. self.port = nil
  126. super.tearDown()
  127. }
  128. }
  129. extension EchoTestCaseBase {
  130. func makeExpectation(description: String, expectedFulfillmentCount: Int = 1, assertForOverFulfill: Bool = true) -> XCTestExpectation {
  131. let expectation = self.expectation(description: description)
  132. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  133. expectation.assertForOverFulfill = assertForOverFulfill
  134. return expectation
  135. }
  136. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  137. return makeExpectation(description: "Expecting status received",
  138. expectedFulfillmentCount: expectedFulfillmentCount)
  139. }
  140. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  141. return makeExpectation(description: "Expecting \(expectedFulfillmentCount) response(s)",
  142. expectedFulfillmentCount: expectedFulfillmentCount)
  143. }
  144. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  145. return makeExpectation(
  146. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  147. expectedFulfillmentCount: expectedFulfillmentCount)
  148. }
  149. func makeInitialMetadataExpectation() -> XCTestExpectation {
  150. return makeExpectation(description: "Expecting initial metadata")
  151. }
  152. }