BasicEchoTestCase.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. var defaultTestTimeout: TimeInterval = 1.0
  46. var serverEventLoopGroup: EventLoopGroup!
  47. var clientEventLoopGroup: EventLoopGroup!
  48. var transportSecurity: TransportSecurity { return .none }
  49. var server: Server!
  50. var client: Echo_EchoClient!
  51. var port: Int!
  52. // Prefer POSIX: subclasses can override this and add availability checks to ensure NIOTS
  53. // variants run where possible.
  54. var networkPreference: NetworkPreference {
  55. return .userDefined(.posix)
  56. }
  57. func connectionBuilder() -> ClientConnection.Builder {
  58. switch self.transportSecurity {
  59. case .none:
  60. return ClientConnection.insecure(group: self.clientEventLoopGroup)
  61. case .anonymousClient:
  62. return ClientConnection.secure(group: self.clientEventLoopGroup)
  63. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  64. case .mutualAuthentication:
  65. return ClientConnection.secure(group: self.clientEventLoopGroup)
  66. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  67. .withTLS(certificateChain: [SampleCertificate.client.certificate])
  68. .withTLS(privateKey: SamplePrivateKey.client)
  69. }
  70. }
  71. func serverBuilder() -> Server.Builder {
  72. switch self.transportSecurity {
  73. case .none:
  74. return Server.insecure(group: self.serverEventLoopGroup)
  75. case .anonymousClient, .mutualAuthentication:
  76. return Server.secure(
  77. group: self.serverEventLoopGroup,
  78. certificateChain: [SampleCertificate.server.certificate],
  79. privateKey: SamplePrivateKey.server
  80. ).withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  81. }
  82. }
  83. func makeServer() throws -> Server {
  84. return try self.serverBuilder()
  85. .withErrorDelegate(makeErrorDelegate())
  86. .withServiceProviders([makeEchoProvider()])
  87. .bind(host: "localhost", port: 0)
  88. .wait()
  89. }
  90. func makeClientConnection(port: Int) throws -> ClientConnection {
  91. return self.connectionBuilder().connect(
  92. host: "localhost",
  93. port: port
  94. )
  95. }
  96. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  97. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  98. func makeEchoClient(port: Int) throws -> Echo_EchoClient {
  99. return Echo_EchoClient(channel: try self.makeClientConnection(port: port))
  100. }
  101. override func setUp() {
  102. super.setUp()
  103. self.serverEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  104. loopCount: 1,
  105. networkPreference: self.networkPreference)
  106. self.server = try! self.makeServer()
  107. self.port = self.server.channel.localAddress!.port!
  108. self.clientEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  109. loopCount: 1,
  110. networkPreference: self.networkPreference)
  111. self.client = try! self.makeEchoClient(port: self.port)
  112. }
  113. override func tearDown() {
  114. // Some tests close the channel, so would throw here if called twice.
  115. try? self.client.channel.close().wait()
  116. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  117. self.client = nil
  118. self.clientEventLoopGroup = nil
  119. XCTAssertNoThrow(try self.server.close().wait())
  120. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  121. self.server = nil
  122. self.serverEventLoopGroup = nil
  123. self.port = nil
  124. super.tearDown()
  125. }
  126. }
  127. extension EchoTestCaseBase {
  128. func makeExpectation(description: String, expectedFulfillmentCount: Int = 1, assertForOverFulfill: Bool = true) -> XCTestExpectation {
  129. let expectation = self.expectation(description: description)
  130. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  131. expectation.assertForOverFulfill = assertForOverFulfill
  132. return expectation
  133. }
  134. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  135. return makeExpectation(description: "Expecting status received",
  136. expectedFulfillmentCount: expectedFulfillmentCount)
  137. }
  138. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  139. return makeExpectation(description: "Expecting \(expectedFulfillmentCount) response(s)",
  140. expectedFulfillmentCount: expectedFulfillmentCount)
  141. }
  142. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  143. return makeExpectation(
  144. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  145. expectedFulfillmentCount: expectedFulfillmentCount)
  146. }
  147. func makeInitialMetadataExpectation() -> XCTestExpectation {
  148. return makeExpectation(description: "Expecting initial metadata")
  149. }
  150. }