NIOClientTLSFailureTests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 SwiftGRPCNIO
  18. import SwiftGRPCNIOSampleData
  19. import NIO
  20. import NIOSSL
  21. import XCTest
  22. class NIOClientTLSFailureTests: 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. return try GRPCClientConnection.start(
  42. host: "localhost",
  43. port: self.port,
  44. eventLoopGroup: self.clientEventLoopGroup,
  45. tls: .custom(try NIOSSLContext(configuration: configuration)),
  46. hostOverride: hostOverride)
  47. }
  48. func makeClientConnectionExpectation() -> XCTestExpectation {
  49. return self.expectation(description: "EventLoopFuture<GRPCClientConnection> resolved")
  50. }
  51. override func setUp() {
  52. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  53. self.server = try! GRPCServer.start(
  54. hostname: "localhost",
  55. port: 0,
  56. eventLoopGroup: self.serverEventLoopGroup,
  57. serviceProviders: [EchoProviderNIO()],
  58. errorDelegate: nil,
  59. tls: .custom(try NIOSSLContext(configuration: defaultServerTLSConfiguration))
  60. ).wait()
  61. self.port = self.server.channel.localAddress?.port
  62. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  63. // Delay the client connection creation until the test.
  64. }
  65. override func tearDown() {
  66. self.port = nil
  67. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  68. self.clientEventLoopGroup = nil
  69. XCTAssertNoThrow(try self.server.close().wait())
  70. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  71. self.server = nil
  72. self.serverEventLoopGroup = nil
  73. }
  74. func testClientConnectionFailsWhenProtocolCanNotBeNegotiated() throws {
  75. var configuration = defaultClientTLSConfiguration
  76. configuration.applicationProtocols = ["not-h2", "not-grpc-ext"]
  77. let connection = try self.makeClientConnection(configuration: configuration)
  78. let connectionExpectation = self.makeClientConnectionExpectation()
  79. connection.assertError(fulfill: connectionExpectation) { error in
  80. let clientError = (error as? GRPCError)?.error as? GRPCClientError
  81. XCTAssertEqual(clientError, .applicationLevelProtocolNegotiationFailed)
  82. }
  83. self.wait(for: [connectionExpectation], timeout: self.defaultTestTimeout)
  84. }
  85. func testClientConnectionFailsWhenServerIsUnknown() throws {
  86. var configuration = defaultClientTLSConfiguration
  87. configuration.trustRoots = .certificates([])
  88. let connection = try self.makeClientConnection(configuration: configuration)
  89. let connectionExpectation = self.makeClientConnectionExpectation()
  90. connection.assertError(fulfill: connectionExpectation) { error in
  91. guard case .some(.handshakeFailed(.sslError)) = error as? NIOSSLError else {
  92. XCTFail("Expected NIOSSLError.handshakeFailed(BoringSSL.sslError) but got \(error)")
  93. return
  94. }
  95. }
  96. self.wait(for: [connectionExpectation], timeout: self.defaultTestTimeout)
  97. }
  98. func testClientConnectionFailsWhenHostnameIsNotValid() throws {
  99. let connection = try self.makeClientConnection(
  100. configuration: self.defaultClientTLSConfiguration,
  101. hostOverride: "not-the-server-hostname")
  102. let connectionExpectation = self.makeClientConnectionExpectation()
  103. connection.assertError(fulfill: connectionExpectation) { error in
  104. guard case .some(.unableToValidateCertificate) = error as? NIOSSLError else {
  105. XCTFail("Expected NIOSSLError.unableToValidateCertificate but got \(error)")
  106. return
  107. }
  108. }
  109. self.wait(for: [connectionExpectation], timeout: self.defaultTestTimeout)
  110. }
  111. }