NIOBasicEchoTestCase.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. @testable import SwiftGRPCNIO
  21. import SwiftGRPCNIOSampleData
  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 makeServerTLS() throws -> GRPCServer.TLSMode {
  57. return try makeServerTLSConfiguration().map { .custom(try NIOSSLContext(configuration: $0)) } ?? .none
  58. }
  59. func makeServerTLSConfiguration() throws -> TLSConfiguration? {
  60. switch self {
  61. case .none:
  62. return nil
  63. case .anonymousClient, .mutualAuthentication:
  64. return .forServer(certificateChain: [.certificate(self.serverCert)],
  65. privateKey: .privateKey(SamplePrivateKey.server),
  66. trustRoots: .certificates ([self.caCert]),
  67. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  68. }
  69. }
  70. func makeClientTLS() throws -> GRPCClientConnection.TLSMode {
  71. return try makeClientTLSConfiguration().map { .custom(try NIOSSLContext(configuration: $0)) } ?? .none
  72. }
  73. func makeClientTLSConfiguration() throws -> TLSConfiguration? {
  74. switch self {
  75. case .none:
  76. return nil
  77. case .anonymousClient:
  78. return .forClient(certificateVerification: .noHostnameVerification,
  79. trustRoots: .certificates([self.caCert]),
  80. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  81. case .mutualAuthentication:
  82. return .forClient(certificateVerification: .noHostnameVerification,
  83. trustRoots: .certificates([self.caCert]),
  84. certificateChain: [.certificate(self.clientCert)],
  85. privateKey: .privateKey(SamplePrivateKey.client),
  86. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  87. }
  88. }
  89. }
  90. class NIOEchoTestCaseBase: XCTestCase {
  91. var defaultTestTimeout: TimeInterval = 1.0
  92. var serverEventLoopGroup: EventLoopGroup!
  93. var clientEventLoopGroup: EventLoopGroup!
  94. var transportSecurity: TransportSecurity { return .none }
  95. var server: GRPCServer!
  96. var client: Echo_EchoService_NIOClient!
  97. func makeServer() throws -> GRPCServer {
  98. return try GRPCServer.start(
  99. hostname: "localhost",
  100. port: 5050,
  101. eventLoopGroup: self.serverEventLoopGroup,
  102. serviceProviders: [makeEchoProvider()],
  103. errorDelegate: makeErrorDelegate(),
  104. tls: try self.transportSecurity.makeServerTLS()
  105. ).wait()
  106. }
  107. func makeClientConnection() throws -> GRPCClientConnection {
  108. return try GRPCClientConnection.start(
  109. host: "localhost",
  110. port: 5050,
  111. eventLoopGroup: self.clientEventLoopGroup,
  112. tls: try self.transportSecurity.makeClientTLS()
  113. ).wait()
  114. }
  115. func makeEchoProvider() -> Echo_EchoProvider_NIO { return EchoProviderNIO() }
  116. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  117. func makeEchoClient() throws -> Echo_EchoService_NIOClient {
  118. return Echo_EchoService_NIOClient(connection: try self.makeClientConnection())
  119. }
  120. override func setUp() {
  121. super.setUp()
  122. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  123. self.server = try! self.makeServer()
  124. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  125. self.client = try! self.makeEchoClient()
  126. }
  127. override func tearDown() {
  128. // Some tests close the channel, so would throw here if called twice.
  129. if self.client.connection.channel.isActive {
  130. XCTAssertNoThrow(try self.client.connection.close().wait())
  131. }
  132. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  133. self.client = nil
  134. self.clientEventLoopGroup = nil
  135. XCTAssertNoThrow(try self.server.close().wait())
  136. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  137. self.server = nil
  138. self.serverEventLoopGroup = nil
  139. super.tearDown()
  140. }
  141. }
  142. extension NIOEchoTestCaseBase {
  143. func makeExpectation(description: String, expectedFulfillmentCount: Int = 1, assertForOverFulfill: Bool = true) -> XCTestExpectation {
  144. let expectation = self.expectation(description: description)
  145. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  146. expectation.assertForOverFulfill = assertForOverFulfill
  147. return expectation
  148. }
  149. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  150. return makeExpectation(description: "Expecting status received",
  151. expectedFulfillmentCount: expectedFulfillmentCount)
  152. }
  153. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  154. return makeExpectation(description: "Expecting \(expectedFulfillmentCount) response(s)",
  155. expectedFulfillmentCount: expectedFulfillmentCount)
  156. }
  157. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  158. return makeExpectation(
  159. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  160. expectedFulfillmentCount: expectedFulfillmentCount)
  161. }
  162. func makeInitialMetadataExpectation() -> XCTestExpectation {
  163. return makeExpectation(description: "Expecting initial metadata")
  164. }
  165. }