ClientTLSTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. super.tearDown()
  34. XCTAssertNoThrow(try self.server.close().wait())
  35. XCTAssertNoThrow(try connection.close().wait())
  36. XCTAssertNoThrow(try self.eventLoopGroup.syncShutdownGracefully())
  37. }
  38. func makeEchoServer(tls: Server.Configuration.TLS) throws -> Server {
  39. let configuration: Server.Configuration = .init(
  40. target: .hostAndPort("localhost", 0),
  41. eventLoopGroup: self.eventLoopGroup,
  42. serviceProviders: [EchoProvider()],
  43. tls: tls
  44. )
  45. return try Server.start(configuration: configuration).wait()
  46. }
  47. func doTestUnary() throws {
  48. let client = Echo_EchoClient(channel: self.connection)
  49. let get = client.get(.with { $0.text = "foo" })
  50. let response = try get.response.wait()
  51. XCTAssertEqual(response.text, "Swift echo get: foo")
  52. let status = try get.status.wait()
  53. XCTAssertEqual(status.code, .ok)
  54. }
  55. func testTLSWithHostnameOverride() throws {
  56. // Run a server presenting a certificate for example.com on localhost.
  57. let serverTLS: Server.Configuration.TLS = .init(
  58. certificateChain: [.certificate(SampleCertificate.exampleServer.certificate)],
  59. privateKey: .privateKey(SamplePrivateKey.exampleServer),
  60. trustRoots: .certificates([SampleCertificate.ca.certificate])
  61. )
  62. self.server = try makeEchoServer(tls: serverTLS)
  63. guard let port = self.server.channel.localAddress?.port else {
  64. XCTFail("could not get server port")
  65. return
  66. }
  67. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  68. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  69. .withTLS(serverHostnameOverride: "example.com")
  70. .connect(host: "localhost", port: port)
  71. try self.doTestUnary()
  72. }
  73. func testTLSWithoutHostnameOverride() throws {
  74. // Run a server presenting a certificate for localhost on localhost.
  75. let serverTLS: Server.Configuration.TLS = .init(
  76. certificateChain: [.certificate(SampleCertificate.server.certificate)],
  77. privateKey: .privateKey(SamplePrivateKey.server),
  78. trustRoots: .certificates([SampleCertificate.ca.certificate])
  79. )
  80. self.server = try makeEchoServer(tls: serverTLS)
  81. guard let port = self.server.channel.localAddress?.port else {
  82. XCTFail("could not get server port")
  83. return
  84. }
  85. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  86. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  87. .connect(host: "localhost", port: port)
  88. try self.doTestUnary()
  89. }
  90. }