TLSVerificationHandlerTests.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. import Foundation
  17. @testable import GRPC
  18. import NIO
  19. import NIOSSL
  20. import XCTest
  21. import NIOTLS
  22. class TLSVerificationHandlerTests: GRPCTestCase {
  23. func testTLSValidationSucceededWithUnspecifiedProtocol() {
  24. let expectation = self.expectation(description: "tls handshake success")
  25. let tlsVerificationHandler = TLSVerificationHandler(logger: self.logger)
  26. let handshakeEvent = TLSUserEvent.handshakeCompleted(negotiatedProtocol: nil)
  27. let channel = EmbeddedChannel(handler: tlsVerificationHandler)
  28. channel.pipeline.fireUserInboundEventTriggered(handshakeEvent)
  29. tlsVerificationHandler.verification.assertSuccess(fulfill: expectation)
  30. self.wait(for: [expectation], timeout: 1.0)
  31. }
  32. func testTLSValidationSucceededWithGRPCApplicationProtocols() {
  33. var expectations = [XCTestExpectation]()
  34. GRPCApplicationProtocolIdentifier.allCases.forEach {
  35. let exp = self.expectation(description: "tls \(String(describing:$0)) protocol success")
  36. expectations.append(exp)
  37. let tlsVerificationHandler = TLSVerificationHandler(logger: self.logger)
  38. let channel = EmbeddedChannel(handler: tlsVerificationHandler)
  39. let handshakeEvent = TLSUserEvent.handshakeCompleted(negotiatedProtocol: $0.rawValue)
  40. channel.pipeline.fireUserInboundEventTriggered(handshakeEvent)
  41. tlsVerificationHandler.verification.assertSuccess(fulfill: exp)
  42. }
  43. self.wait(for: expectations, timeout: 1.0)
  44. }
  45. func testTLSValidationSucceededWithCustomProtocol() {
  46. let expectation = self.expectation(description: "tls custom protocol success")
  47. let tlsVerificationHandler = TLSVerificationHandler(logger: self.logger)
  48. let handshakeEvent = TLSUserEvent.handshakeCompleted(negotiatedProtocol: "some-protocol")
  49. let channel = EmbeddedChannel(handler: tlsVerificationHandler)
  50. channel.pipeline.fireUserInboundEventTriggered(handshakeEvent)
  51. tlsVerificationHandler.verification.assertSuccess(fulfill: expectation)
  52. self.wait(for: [expectation], timeout: 1.0)
  53. }
  54. }