ClientTLSTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Foundation
  17. import GRPC
  18. import GRPCSampleData
  19. import EchoModel
  20. import EchoImplementation
  21. import NIO
  22. import NIOSSL
  23. import XCTest
  24. class ClientTLSHostnameOverrideTests: GRPCTestCase {
  25. var eventLoopGroup: EventLoopGroup!
  26. var server: Server!
  27. var connection: ClientConnection!
  28. override func setUp() {
  29. super.setUp()
  30. self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  31. }
  32. override func tearDown() {
  33. XCTAssertNoThrow(try self.server.close().wait())
  34. XCTAssertNoThrow(try connection.close().wait())
  35. XCTAssertNoThrow(try self.eventLoopGroup.syncShutdownGracefully())
  36. super.tearDown()
  37. }
  38. func doTestUnary() throws {
  39. let client = Echo_EchoClient(channel: self.connection, defaultCallOptions: self.callOptionsWithLogger)
  40. let get = client.get(.with { $0.text = "foo" })
  41. let response = try get.response.wait()
  42. XCTAssertEqual(response.text, "Swift echo get: foo")
  43. let status = try get.status.wait()
  44. XCTAssertEqual(status.code, .ok)
  45. }
  46. func testTLSWithHostnameOverride() throws {
  47. // Run a server presenting a certificate for example.com on localhost.
  48. let cert = SampleCertificate.exampleServer.certificate
  49. let key = SamplePrivateKey.exampleServer
  50. self.server = try Server.secure(group: self.eventLoopGroup, certificateChain: [cert], privateKey: key)
  51. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  52. .withServiceProviders([EchoProvider()])
  53. .withLogger(self.serverLogger)
  54. .bind(host: "localhost", port: 0)
  55. .wait()
  56. guard let port = self.server.channel.localAddress?.port else {
  57. XCTFail("could not get server port")
  58. return
  59. }
  60. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  61. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  62. .withTLS(serverHostnameOverride: "example.com")
  63. .withBackgroundActivityLogger(self.clientLogger)
  64. .connect(host: "localhost", port: port)
  65. try self.doTestUnary()
  66. }
  67. func testTLSWithoutHostnameOverride() throws {
  68. // Run a server presenting a certificate for localhost on localhost.
  69. let cert = SampleCertificate.server.certificate
  70. let key = SamplePrivateKey.server
  71. self.server = try Server.secure(group: self.eventLoopGroup, certificateChain: [cert], privateKey: key)
  72. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  73. .withServiceProviders([EchoProvider()])
  74. .withLogger(self.serverLogger)
  75. .bind(host: "localhost", port: 0)
  76. .wait()
  77. guard let port = self.server.channel.localAddress?.port else {
  78. XCTFail("could not get server port")
  79. return
  80. }
  81. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  82. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  83. .withBackgroundActivityLogger(self.clientLogger)
  84. .connect(host: "localhost", port: port)
  85. try self.doTestUnary()
  86. }
  87. }