InteroperabilityTestServer.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. import NIOSSL
  20. /// Makes a server for gRPC interoperability testing.
  21. ///
  22. /// - Parameters:
  23. /// - host: The host to bind the server socket to, defaults to "localhost".
  24. /// - port: The port to bind the server socket to.
  25. /// - eventLoopGroup: Event loop group to run the server on.
  26. /// - serviceProviders: Service providers to handle requests with, defaults to provider for the
  27. /// "Test" service.
  28. /// - useTLS: Whether to use TLS or not. If `true` then the server will use the "server1"
  29. /// certificate and CA as set out in the interoperability test specification. The common name
  30. /// is "*.test.google.fr"; clients should set their hostname override accordingly.
  31. /// - Returns: A future `Server` configured to serve the test service.
  32. public func makeInteroperabilityTestServer(
  33. host: String = "localhost",
  34. port: Int,
  35. eventLoopGroup: EventLoopGroup,
  36. serviceProviders: [CallHandlerProvider] = [TestServiceProvider()],
  37. useTLS: Bool,
  38. logger: Logger? = nil
  39. ) throws -> EventLoopFuture<Server> {
  40. let builder: Server.Builder
  41. if useTLS {
  42. print(
  43. "Using the gRPC interop testing CA for TLS; clients should expect the host to be '*.test.google.fr'"
  44. )
  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. builder = Server.insecure(group: eventLoopGroup)
  56. }
  57. if let logger = logger {
  58. builder.withLogger(logger)
  59. }
  60. return builder
  61. .withMessageCompression(.enabled(.init(decompressionLimit: .absolute(1024 * 1024))))
  62. .withServiceProviders(serviceProviders)
  63. .bind(host: host, port: port)
  64. }