ALPNConfigurationTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. @testable 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 = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL()
  28. self.assertExpectedClientALPNTokens(
  29. in: config.nioConfiguration!.configuration.applicationProtocols
  30. )
  31. }
  32. func testClientAddsTokensFromEmptyNIOSSLConfig() {
  33. let tlsConfig = TLSConfiguration.makeClientConfiguration()
  34. XCTAssertTrue(tlsConfig.applicationProtocols.isEmpty)
  35. let config = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL(
  36. configuration: tlsConfig
  37. )
  38. // Should now contain expected config.
  39. self.assertExpectedClientALPNTokens(
  40. in: config.nioConfiguration!.configuration.applicationProtocols
  41. )
  42. }
  43. func testClientDoesNotOverrideNonEmptyNIOSSLConfig() {
  44. var tlsConfig = TLSConfiguration.makeClientConfiguration()
  45. tlsConfig.applicationProtocols = ["foo"]
  46. let config = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL(
  47. configuration: tlsConfig
  48. )
  49. // Should not be overridden.
  50. XCTAssertEqual(config.nioConfiguration!.configuration.applicationProtocols, ["foo"])
  51. }
  52. func testServerDefaultALPN() {
  53. let config = GRPCTLSConfiguration.makeServerConfigurationBackedByNIOSSL(
  54. certificateChain: [],
  55. privateKey: .file("")
  56. )
  57. self.assertExpectedServerALPNTokens(
  58. in: config.nioConfiguration!.configuration.applicationProtocols
  59. )
  60. }
  61. func testServerAddsTokensFromEmptyNIOSSLConfig() {
  62. let tlsConfig = TLSConfiguration.makeServerConfiguration(
  63. certificateChain: [],
  64. privateKey: .file("")
  65. )
  66. XCTAssertTrue(tlsConfig.applicationProtocols.isEmpty)
  67. let config = GRPCTLSConfiguration.makeServerConfigurationBackedByNIOSSL(
  68. configuration: tlsConfig
  69. )
  70. // Should now contain expected config.
  71. self.assertExpectedServerALPNTokens(
  72. in: config.nioConfiguration!.configuration.applicationProtocols
  73. )
  74. }
  75. func testServerDoesNotOverrideNonEmptyNIOSSLConfig() {
  76. var tlsConfig = TLSConfiguration.makeServerConfiguration(
  77. certificateChain: [],
  78. privateKey: .file("")
  79. )
  80. tlsConfig.applicationProtocols = ["foo"]
  81. let config = GRPCTLSConfiguration.makeServerConfigurationBackedByNIOSSL(
  82. configuration: tlsConfig
  83. )
  84. // Should not be overridden.
  85. XCTAssertEqual(config.nioConfiguration!.configuration.applicationProtocols, ["foo"])
  86. }
  87. }