ClientTLSTests.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 makeConnection(port: Int, tls: ClientConnection.Configuration.TLS) -> ClientConnection {
  48. let configuration: ClientConnection.Configuration = .init(
  49. target: .hostAndPort("localhost", port),
  50. eventLoopGroup: self.eventLoopGroup,
  51. tls: tls
  52. )
  53. return ClientConnection(configuration: configuration)
  54. }
  55. func doTestUnary() throws {
  56. let client = Echo_EchoServiceClient(connection: self.connection)
  57. let get = client.get(.with { $0.text = "foo" })
  58. let response = try get.response.wait()
  59. XCTAssertEqual(response.text, "Swift echo get: foo")
  60. let status = try get.status.wait()
  61. XCTAssertEqual(status.code, .ok)
  62. }
  63. func testTLSWithHostnameOverride() throws {
  64. // Run a server presenting a certificate for example.com on localhost.
  65. let serverTLS: Server.Configuration.TLS = .init(
  66. certificateChain: [.certificate(SampleCertificate.exampleServer.certificate)],
  67. privateKey: .privateKey(SamplePrivateKey.exampleServer),
  68. trustRoots: .certificates([SampleCertificate.ca.certificate])
  69. )
  70. self.server = try makeEchoServer(tls: serverTLS)
  71. guard let port = self.server.channel.localAddress?.port else {
  72. XCTFail("could not get server port")
  73. return
  74. }
  75. let clientTLS: ClientConnection.Configuration.TLS = .init(
  76. trustRoots: .certificates([SampleCertificate.ca.certificate]),
  77. hostnameOverride: "example.com"
  78. )
  79. self.connection = self.makeConnection(port: port, tls: clientTLS)
  80. try self.doTestUnary()
  81. }
  82. func testTLSWithoutHostnameOverride() throws {
  83. // Run a server presenting a certificate for localhost on localhost.
  84. let serverTLS: Server.Configuration.TLS = .init(
  85. certificateChain: [.certificate(SampleCertificate.server.certificate)],
  86. privateKey: .privateKey(SamplePrivateKey.server),
  87. trustRoots: .certificates([SampleCertificate.ca.certificate])
  88. )
  89. self.server = try makeEchoServer(tls: serverTLS)
  90. guard let port = self.server.channel.localAddress?.port else {
  91. XCTFail("could not get server port")
  92. return
  93. }
  94. let clientTLS: ClientConnection.Configuration.TLS = .init(
  95. trustRoots: .certificates([SampleCertificate.ca.certificate])
  96. )
  97. self.connection = self.makeConnection(port: port, tls: clientTLS)
  98. try self.doTestUnary()
  99. }
  100. }