ClientTLSFailureTests.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 NIO
  20. import NIOSSL
  21. import XCTest
  22. class ClientTLSFailureTests: XCTestCase {
  23. let defaultServerTLSConfiguration = TLSConfiguration.forServer(
  24. certificateChain: [.certificate(SampleCertificate.server.certificate)],
  25. privateKey: .privateKey(SamplePrivateKey.server),
  26. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  27. let defaultClientTLSConfiguration = TLSConfiguration.forClient(
  28. trustRoots: .certificates([SampleCertificate.ca.certificate]),
  29. certificateChain: [.certificate(SampleCertificate.client.certificate)],
  30. privateKey: .privateKey(SamplePrivateKey.client),
  31. applicationProtocols: GRPCApplicationProtocolIdentifier.allCases.map { $0.rawValue })
  32. var defaultTestTimeout: TimeInterval = 1.0
  33. var clientEventLoopGroup: EventLoopGroup!
  34. var serverEventLoopGroup: EventLoopGroup!
  35. var server: GRPCServer!
  36. var port: Int!
  37. func makeClientConnection(
  38. configuration: TLSConfiguration,
  39. hostOverride: String? = SampleCertificate.server.commonName
  40. ) throws -> EventLoopFuture<GRPCClientConnection> {
  41. let context = try NIOSSLContext(configuration: configuration)
  42. let clientConfiguration = GRPCClientConnection.Configuration(
  43. target: .hostAndPort("localhost", self.port),
  44. eventLoopGroup: self.clientEventLoopGroup,
  45. tlsConfiguration: GRPCClientConnection.TLSConfiguration(
  46. sslContext: context,
  47. hostnameOverride: hostOverride))
  48. return GRPCClientConnection.start(clientConfiguration)
  49. }
  50. func makeClientConnectionExpectation() -> XCTestExpectation {
  51. return self.expectation(description: "EventLoopFuture<GRPCClientConnection> resolved")
  52. }
  53. override func setUp() {
  54. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  55. self.server = try! GRPCServer.start(
  56. hostname: "localhost",
  57. port: 0,
  58. eventLoopGroup: self.serverEventLoopGroup,
  59. serviceProviders: [EchoProvider()],
  60. errorDelegate: nil,
  61. tls: .custom(try NIOSSLContext(configuration: defaultServerTLSConfiguration))
  62. ).wait()
  63. self.port = self.server.channel.localAddress?.port
  64. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  65. // Delay the client connection creation until the test.
  66. }
  67. override func tearDown() {
  68. self.port = nil
  69. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  70. self.clientEventLoopGroup = nil
  71. XCTAssertNoThrow(try self.server.close().wait())
  72. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  73. self.server = nil
  74. self.serverEventLoopGroup = nil
  75. }
  76. func testClientConnectionFailsWhenProtocolCanNotBeNegotiated() throws {
  77. var configuration = defaultClientTLSConfiguration
  78. configuration.applicationProtocols = ["not-h2", "not-grpc-ext"]
  79. let connection = try self.makeClientConnection(configuration: configuration)
  80. let connectionExpectation = self.makeClientConnectionExpectation()
  81. connection.assertError(fulfill: connectionExpectation) { error in
  82. let clientError = (error as? GRPCError)?.wrappedError as? GRPCClientError
  83. XCTAssertEqual(clientError, .applicationLevelProtocolNegotiationFailed)
  84. }
  85. self.wait(for: [connectionExpectation], timeout: self.defaultTestTimeout)
  86. }
  87. func testClientConnectionFailsWhenServerIsUnknown() throws {
  88. var configuration = defaultClientTLSConfiguration
  89. configuration.trustRoots = .certificates([])
  90. let connection = try self.makeClientConnection(configuration: configuration)
  91. let connectionExpectation = self.makeClientConnectionExpectation()
  92. connection.assertError(fulfill: connectionExpectation) { error in
  93. guard case .some(.handshakeFailed(.sslError)) = error as? NIOSSLError else {
  94. XCTFail("Expected NIOSSLError.handshakeFailed(BoringSSL.sslError) but got \(error)")
  95. return
  96. }
  97. }
  98. self.wait(for: [connectionExpectation], timeout: self.defaultTestTimeout)
  99. }
  100. func testClientConnectionFailsWhenHostnameIsNotValid() throws {
  101. let connection = try self.makeClientConnection(
  102. configuration: self.defaultClientTLSConfiguration,
  103. hostOverride: "not-the-server-hostname")
  104. let connectionExpectation = self.makeClientConnectionExpectation()
  105. connection.assertError(fulfill: connectionExpectation) { error in
  106. guard case .some(.unableToValidateCertificate) = error as? NIOSSLError else {
  107. XCTFail("Expected NIOSSLError.unableToValidateCertificate but got \(error)")
  108. return
  109. }
  110. }
  111. self.wait(for: [connectionExpectation], timeout: self.defaultTestTimeout)
  112. }
  113. }