NIOBasicEchoTestCase.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  68. }
  69. func makeClientTLS() throws -> GRPCClientConnection.TLSMode {
  70. return try makeClientTLSConfiguration().map { .custom(try NIOSSLContext(configuration: $0)) } ?? .none
  71. }
  72. func makeClientTLSConfiguration() throws -> TLSConfiguration? {
  73. switch self {
  74. case .none:
  75. return nil
  76. case .anonymousClient:
  77. return .forClient(certificateVerification: .noHostnameVerification,
  78. trustRoots: .certificates([self.caCert]))
  79. case .mutualAuthentication:
  80. return .forClient(certificateVerification: .noHostnameVerification,
  81. trustRoots: .certificates([self.caCert]),
  82. certificateChain: [.certificate(self.clientCert)],
  83. privateKey: .privateKey(SamplePrivateKey.client))
  84. }
  85. }
  86. }
  87. class NIOEchoTestCaseBase: XCTestCase {
  88. var defaultTestTimeout: TimeInterval = 1.0
  89. let serverEventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  90. let clientEventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  91. var transportSecurity: TransportSecurity { return .none }
  92. var server: GRPCServer!
  93. var client: Echo_EchoService_NIOClient!
  94. func makeServer() throws -> GRPCServer {
  95. return try GRPCServer.start(
  96. hostname: "localhost",
  97. port: 5050,
  98. eventLoopGroup: self.serverEventLoopGroup,
  99. serviceProviders: [makeEchoProvider()],
  100. errorDelegate: makeErrorDelegate(),
  101. tls: try self.transportSecurity.makeServerTLS()
  102. ).wait()
  103. }
  104. func makeClientConnection() throws -> GRPCClientConnection {
  105. return try GRPCClientConnection.start(
  106. host: "localhost",
  107. port: 5050,
  108. eventLoopGroup: self.clientEventLoopGroup,
  109. tls: try self.transportSecurity.makeClientTLS()
  110. ).wait()
  111. }
  112. func makeEchoProvider() -> Echo_EchoProvider_NIO { return EchoProviderNIO() }
  113. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  114. func makeEchoClient() throws -> Echo_EchoService_NIOClient {
  115. return Echo_EchoService_NIOClient(connection: try self.makeClientConnection())
  116. }
  117. override func setUp() {
  118. super.setUp()
  119. self.server = try! self.makeServer()
  120. self.client = try! self.makeEchoClient()
  121. }
  122. override func tearDown() {
  123. XCTAssertNoThrow(try self.client.connection.close().wait())
  124. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  125. self.client = nil
  126. XCTAssertNoThrow(try self.server.close().wait())
  127. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  128. self.server = nil
  129. super.tearDown()
  130. }
  131. }