NIOBasicEchoTestCase.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 -> GRPCClient.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 {
  92. return .none
  93. }
  94. var server: GRPCServer!
  95. var client: Echo_EchoService_NIOClient!
  96. func makeServer() throws -> GRPCServer {
  97. return try GRPCServer.start(
  98. hostname: "localhost",
  99. port: 5050,
  100. eventLoopGroup: self.serverEventLoopGroup,
  101. serviceProviders: [makeEchoProvider()],
  102. tls: try self.transportSecurity.makeServerTLS()
  103. ).wait()
  104. }
  105. func makeClient() throws -> GRPCClient {
  106. return try GRPCClient.start(
  107. host: "localhost",
  108. port: 5050,
  109. eventLoopGroup: self.clientEventLoopGroup,
  110. tls: try self.transportSecurity.makeClientTLS()
  111. ).wait()
  112. }
  113. func makeEchoProvider() -> Echo_EchoProvider_NIO {
  114. return EchoProviderNIO()
  115. }
  116. func makeEchoClient() throws -> Echo_EchoService_NIOClient {
  117. return Echo_EchoService_NIOClient(client: try self.makeClient())
  118. }
  119. override func setUp() {
  120. super.setUp()
  121. self.server = try! self.makeServer()
  122. self.client = try! self.makeEchoClient()
  123. }
  124. override func tearDown() {
  125. XCTAssertNoThrow(try self.client.client.close().wait())
  126. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  127. self.client = nil
  128. XCTAssertNoThrow(try self.server.close().wait())
  129. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  130. self.server = nil
  131. super.tearDown()
  132. }
  133. }