ALPNConfigurationTests.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.makeClientConfiguration()
  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. var tlsConfig = TLSConfiguration.makeClientConfiguration()
  39. tlsConfig.applicationProtocols = ["foo"]
  40. let config = ClientConnection.Configuration.TLS(configuration: tlsConfig)
  41. // Should not be overridden.
  42. XCTAssertEqual(config.configuration.applicationProtocols, ["foo"])
  43. }
  44. func testServerDefaultALPN() {
  45. let config = Server.Configuration.TLS(certificateChain: [], privateKey: .file(""))
  46. self.assertExpectedServerALPNTokens(in: config.configuration.applicationProtocols)
  47. }
  48. func testServerAddsTokensFromEmptyNIOSSLConfig() {
  49. let tlsConfig = TLSConfiguration.makeServerConfiguration(
  50. certificateChain: [],
  51. privateKey: .file("")
  52. )
  53. XCTAssertTrue(tlsConfig.applicationProtocols.isEmpty)
  54. let config = Server.Configuration.TLS(configuration: tlsConfig)
  55. // Should now contain expected config.
  56. self.assertExpectedServerALPNTokens(in: config.configuration.applicationProtocols)
  57. }
  58. func testServerDoesNotOverrideNonEmptyNIOSSLConfig() {
  59. var tlsConfig = TLSConfiguration.makeServerConfiguration(
  60. certificateChain: [],
  61. privateKey: .file("")
  62. )
  63. tlsConfig.applicationProtocols = ["foo"]
  64. let config = ClientConnection.Configuration.TLS(configuration: tlsConfig)
  65. // Should not be overridden.
  66. XCTAssertEqual(config.configuration.applicationProtocols, ["foo"])
  67. }
  68. }