ServerTLSErrorTests.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2020, 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. @testable import GRPC
  17. import GRPCSampleData
  18. import EchoImplementation
  19. import Logging
  20. import NIO
  21. import NIOSSL
  22. import XCTest
  23. class ServerErrorRecordingDelegate: ServerErrorDelegate {
  24. var errors: [Error] = []
  25. var expectation: XCTestExpectation
  26. init(expectation: XCTestExpectation) {
  27. self.expectation = expectation
  28. }
  29. func observeLibraryError(_ error: Error) {
  30. self.errors.append(error)
  31. self.expectation.fulfill()
  32. }
  33. }
  34. class ServerTLSErrorTests: GRPCTestCase {
  35. let defaultClientTLSConfiguration = ClientConnection.Configuration.TLS(
  36. certificateChain: [.certificate(SampleCertificate.client.certificate)],
  37. privateKey: .privateKey(SamplePrivateKey.client),
  38. trustRoots: .certificates([SampleCertificate.ca.certificate]),
  39. hostnameOverride: SampleCertificate.server.commonName)
  40. var defaultTestTimeout: TimeInterval = 1.0
  41. var clientEventLoopGroup: EventLoopGroup!
  42. var serverEventLoopGroup: EventLoopGroup!
  43. func makeClientConfiguration(
  44. tls: ClientConnection.Configuration.TLS,
  45. port: Int
  46. ) -> ClientConnection.Configuration {
  47. return .init(
  48. target: .hostAndPort("localhost", port),
  49. eventLoopGroup: self.clientEventLoopGroup,
  50. tls: tls,
  51. // No need to retry connecting.
  52. connectionBackoff: nil
  53. )
  54. }
  55. func makeClientConnectionExpectation() -> XCTestExpectation {
  56. return self.expectation(description: "EventLoopFuture<ClientConnection> resolved")
  57. }
  58. override func setUp() {
  59. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  60. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  61. }
  62. override func tearDown() {
  63. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  64. self.clientEventLoopGroup = nil
  65. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  66. self.serverEventLoopGroup = nil
  67. }
  68. func testErrorIsLoggedWhenSSLContextErrors() throws {
  69. let clientShutdownExpectation = self.expectation(description: "client shutdown")
  70. let errorExpectation = self.expectation(description: "error")
  71. let errorDelegate = ServerErrorRecordingDelegate(expectation: errorExpectation)
  72. let server = try! Server.secure(
  73. group: self.serverEventLoopGroup,
  74. certificateChain: [SampleCertificate.exampleServerWithExplicitCurve.certificate],
  75. privateKey: SamplePrivateKey.exampleServerWithExplicitCurve
  76. ).withServiceProviders([EchoProvider()])
  77. .withErrorDelegate(errorDelegate)
  78. .bind(host: "localhost", port: 0)
  79. .wait()
  80. defer {
  81. XCTAssertNoThrow(try server.close().wait())
  82. }
  83. let port = server.channel.localAddress!.port!
  84. var tls = self.defaultClientTLSConfiguration
  85. tls.trustRoots = .certificates([SampleCertificate.exampleServerWithExplicitCurve.certificate])
  86. var configuration = self.makeClientConfiguration(tls: tls, port: port)
  87. let stateChangeDelegate = ConnectivityStateCollectionDelegate(shutdown: clientShutdownExpectation)
  88. configuration.connectivityStateDelegate = stateChangeDelegate
  89. _ = ClientConnection(configuration: configuration)
  90. self.wait(for: [clientShutdownExpectation, errorExpectation], timeout: self.defaultTestTimeout)
  91. if let nioSSLError = errorDelegate.errors.first as? NIOSSLError,
  92. case .failedToLoadCertificate = nioSSLError {
  93. // Expected case.
  94. } else {
  95. XCTFail("Expected NIOSSLError.handshakeFailed(BoringSSL.sslError)")
  96. }
  97. }
  98. }