2
0

ClientTLSTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 doTestUnary() throws {
  39. let client = Echo_EchoClient(channel: self.connection)
  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. .bind(host: "localhost", port: 0)
  54. .wait()
  55. guard let port = self.server.channel.localAddress?.port else {
  56. XCTFail("could not get server port")
  57. return
  58. }
  59. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  60. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  61. .withTLS(serverHostnameOverride: "example.com")
  62. .connect(host: "localhost", port: port)
  63. try self.doTestUnary()
  64. }
  65. func testTLSWithoutHostnameOverride() throws {
  66. // Run a server presenting a certificate for localhost on localhost.
  67. let cert = SampleCertificate.server.certificate
  68. let key = SamplePrivateKey.server
  69. self.server = try Server.secure(group: self.eventLoopGroup, certificateChain: [cert], privateKey: key)
  70. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  71. .withServiceProviders([EchoProvider()])
  72. .bind(host: "localhost", port: 0)
  73. .wait()
  74. guard let port = self.server.channel.localAddress?.port else {
  75. XCTFail("could not get server port")
  76. return
  77. }
  78. self.connection = ClientConnection.secure(group: self.eventLoopGroup)
  79. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  80. .connect(host: "localhost", port: port)
  81. try self.doTestUnary()
  82. }
  83. }