InteroperabilityTestServer.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2019, 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 GRPC
  17. import Logging
  18. import NIOCore
  19. #if canImport(NIOSSL)
  20. import NIOSSL
  21. #endif
  22. /// Makes a server for gRPC interoperability testing.
  23. ///
  24. /// - Parameters:
  25. /// - host: The host to bind the server socket to, defaults to "localhost".
  26. /// - port: The port to bind the server socket to.
  27. /// - eventLoopGroup: Event loop group to run the server on.
  28. /// - serviceProviders: Service providers to handle requests with, defaults to provider for the
  29. /// "Test" service.
  30. /// - useTLS: Whether to use TLS or not. If `true` then the server will use the "server1"
  31. /// certificate and CA as set out in the interoperability test specification. The common name
  32. /// is "*.test.google.fr"; clients should set their hostname override accordingly.
  33. /// - Returns: A future `Server` configured to serve the test service.
  34. public func makeInteroperabilityTestServer(
  35. host: String = "localhost",
  36. port: Int,
  37. eventLoopGroup: EventLoopGroup,
  38. serviceProviders: [CallHandlerProvider] = [TestServiceProvider()],
  39. useTLS: Bool,
  40. logger: Logger? = nil
  41. ) throws -> EventLoopFuture<Server> {
  42. let builder: Server.Builder
  43. if useTLS {
  44. #if canImport(NIOSSL)
  45. let caCert = InteroperabilityTestCredentials.caCertificate
  46. let serverCert = InteroperabilityTestCredentials.server1Certificate
  47. let serverKey = InteroperabilityTestCredentials.server1Key
  48. builder = Server.usingTLSBackedByNIOSSL(
  49. on: eventLoopGroup,
  50. certificateChain: [serverCert],
  51. privateKey: serverKey
  52. )
  53. .withTLS(trustRoots: .certificates([caCert]))
  54. #else
  55. fatalError("'useTLS: true' passed to \(#function) but NIOSSL is not available")
  56. #endif // canImport(NIOSSL)
  57. } else {
  58. builder = Server.insecure(group: eventLoopGroup)
  59. }
  60. if let logger = logger {
  61. builder.withLogger(logger)
  62. }
  63. return
  64. builder
  65. .withMessageCompression(.enabled(.init(decompressionLimit: .absolute(1024 * 1024))))
  66. .withServiceProviders(serviceProviders)
  67. .bind(host: host, port: port)
  68. }