ALPNConfigurationTests.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2021, 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 GRPC
  17. import NIOSSL
  18. import XCTest
  19. class ALPNConfigurationTests: GRPCTestCase {
  20. private func assertExpectedClientALPNTokens(in tokens: [String]) {
  21. XCTAssertEqual(tokens, ["grpc-exp", "h2"])
  22. }
  23. private func assertExpectedServerALPNTokens(in tokens: [String]) {
  24. XCTAssertEqual(tokens, ["grpc-exp", "h2", "http/1.1"])
  25. }
  26. func testClientDefaultALPN() {
  27. let config = ClientConnection.Configuration.TLS()
  28. self.assertExpectedClientALPNTokens(in: config.configuration.applicationProtocols)
  29. }
  30. func testClientAddsTokensFromEmptyNIOSSLConfig() {
  31. let tlsConfig = TLSConfiguration.forClient()
  32. XCTAssertTrue(tlsConfig.applicationProtocols.isEmpty)
  33. let config = ClientConnection.Configuration.TLS(configuration: tlsConfig)
  34. // Should now contain expected config.
  35. self.assertExpectedClientALPNTokens(in: config.configuration.applicationProtocols)
  36. }
  37. func testClientDoesNotOverrideNonEmptyNIOSSLConfig() {
  38. let tlsConfig = TLSConfiguration.forClient(applicationProtocols: ["foo"])
  39. let config = ClientConnection.Configuration.TLS(configuration: tlsConfig)
  40. // Should not be overridden.
  41. XCTAssertEqual(config.configuration.applicationProtocols, ["foo"])
  42. }
  43. func testServerDefaultALPN() {
  44. let config = Server.Configuration.TLS(certificateChain: [], privateKey: .file(""))
  45. self.assertExpectedServerALPNTokens(in: config.configuration.applicationProtocols)
  46. }
  47. func testServerAddsTokensFromEmptyNIOSSLConfig() {
  48. let tlsConfig = TLSConfiguration.forServer(certificateChain: [], privateKey: .file(""))
  49. XCTAssertTrue(tlsConfig.applicationProtocols.isEmpty)
  50. let config = Server.Configuration.TLS(configuration: tlsConfig)
  51. // Should now contain expected config.
  52. self.assertExpectedServerALPNTokens(in: config.configuration.applicationProtocols)
  53. }
  54. func testServerDoesNotOverrideNonEmptyNIOSSLConfig() {
  55. let tlsConfig = TLSConfiguration.forServer(
  56. certificateChain: [],
  57. privateKey: .file(""),
  58. applicationProtocols: ["foo"]
  59. )
  60. let config = ClientConnection.Configuration.TLS(configuration: tlsConfig)
  61. // Should not be overridden.
  62. XCTAssertEqual(config.configuration.applicationProtocols, ["foo"])
  63. }
  64. }