ClientTLSTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Foundation
  2. import GRPC
  3. import GRPCSampleData
  4. import EchoModel
  5. import EchoImplementation
  6. import NIO
  7. import NIOSSL
  8. import XCTest
  9. class ClientTLSHostnameOverrideTests: GRPCTestCase {
  10. var eventLoopGroup: EventLoopGroup!
  11. var server: Server!
  12. var connection: ClientConnection!
  13. override func setUp() {
  14. super.setUp()
  15. self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  16. }
  17. override func tearDown() {
  18. super.tearDown()
  19. XCTAssertNoThrow(try self.server.close().wait())
  20. XCTAssertNoThrow(try connection.close().wait())
  21. XCTAssertNoThrow(try self.eventLoopGroup.syncShutdownGracefully())
  22. }
  23. func makeEchoServer(tls: Server.Configuration.TLS) throws -> Server {
  24. let configuration: Server.Configuration = .init(
  25. target: .hostAndPort("localhost", 0),
  26. eventLoopGroup: self.eventLoopGroup,
  27. serviceProviders: [EchoProvider()],
  28. tls: tls
  29. )
  30. return try Server.start(configuration: configuration).wait()
  31. }
  32. func makeConnection(port: Int, tls: ClientConnection.Configuration.TLS) -> ClientConnection {
  33. let configuration: ClientConnection.Configuration = .init(
  34. target: .hostAndPort("localhost", port),
  35. eventLoopGroup: self.eventLoopGroup,
  36. tls: tls
  37. )
  38. return ClientConnection(configuration: configuration)
  39. }
  40. func doTestUnary() throws {
  41. let client = Echo_EchoServiceClient(connection: self.connection)
  42. let get = client.get(.with { $0.text = "foo" })
  43. let response = try get.response.wait()
  44. XCTAssertEqual(response.text, "Swift echo get: foo")
  45. let status = try get.status.wait()
  46. XCTAssertEqual(status.code, .ok)
  47. }
  48. func testTLSWithHostnameOverride() throws {
  49. // Run a server presenting a certificate for example.com on localhost.
  50. let serverTLS: Server.Configuration.TLS = .init(
  51. certificateChain: [.certificate(SampleCertificate.exampleServer.certificate)],
  52. privateKey: .privateKey(SamplePrivateKey.exampleServer),
  53. trustRoots: .certificates([SampleCertificate.ca.certificate])
  54. )
  55. self.server = try makeEchoServer(tls: serverTLS)
  56. guard let port = self.server.channel.localAddress?.port else {
  57. XCTFail("could not get server port")
  58. return
  59. }
  60. let clientTLS: ClientConnection.Configuration.TLS = .init(
  61. trustRoots: .certificates([SampleCertificate.ca.certificate]),
  62. hostnameOverride: "example.com"
  63. )
  64. self.connection = self.makeConnection(port: port, tls: clientTLS)
  65. try self.doTestUnary()
  66. }
  67. func testTLSWithoutHostnameOverride() throws {
  68. // Run a server presenting a certificate for localhost on localhost.
  69. let serverTLS: Server.Configuration.TLS = .init(
  70. certificateChain: [.certificate(SampleCertificate.server.certificate)],
  71. privateKey: .privateKey(SamplePrivateKey.server),
  72. trustRoots: .certificates([SampleCertificate.ca.certificate])
  73. )
  74. self.server = try makeEchoServer(tls: serverTLS)
  75. guard let port = self.server.channel.localAddress?.port else {
  76. XCTFail("could not get server port")
  77. return
  78. }
  79. let clientTLS: ClientConnection.Configuration.TLS = .init(
  80. trustRoots: .certificates([SampleCertificate.ca.certificate])
  81. )
  82. self.connection = self.makeConnection(port: port, tls: clientTLS)
  83. try self.doTestUnary()
  84. }
  85. }