HTTP2TransportNIOPosixTests.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2024, 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. private import GRPCCore
  17. private import GRPCHTTP2Core
  18. private import GRPCHTTP2TransportNIOPosix
  19. internal import XCTest
  20. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  21. final class HTTP2TransportNIOPosixTests: XCTestCase {
  22. func testGetListeningAddress_IPv4() async throws {
  23. let transport = GRPCHTTP2Core.HTTP2ServerTransport.Posix(
  24. address: .ipv4(host: "0.0.0.0", port: 0)
  25. )
  26. try await withThrowingDiscardingTaskGroup { group in
  27. group.addTask {
  28. try await transport.listen { _ in }
  29. }
  30. group.addTask {
  31. let address = try await transport.listeningAddress
  32. let ipv4Address = try XCTUnwrap(address.ipv4)
  33. XCTAssertNotEqual(ipv4Address.port, 0)
  34. transport.stopListening()
  35. }
  36. }
  37. }
  38. func testGetListeningAddress_IPv6() async throws {
  39. let transport = GRPCHTTP2Core.HTTP2ServerTransport.Posix(
  40. address: .ipv6(host: "::1", port: 0)
  41. )
  42. try await withThrowingDiscardingTaskGroup { group in
  43. group.addTask {
  44. try await transport.listen { _ in }
  45. }
  46. group.addTask {
  47. let address = try await transport.listeningAddress
  48. let ipv6Address = try XCTUnwrap(address.ipv6)
  49. XCTAssertNotEqual(ipv6Address.port, 0)
  50. transport.stopListening()
  51. }
  52. }
  53. }
  54. func testGetListeningAddress_UnixDomainSocket() async throws {
  55. let transport = GRPCHTTP2Core.HTTP2ServerTransport.Posix(
  56. address: .unixDomainSocket(path: "/tmp/posix-uds-test")
  57. )
  58. try await withThrowingDiscardingTaskGroup { group in
  59. group.addTask {
  60. try await transport.listen { _ in }
  61. }
  62. group.addTask {
  63. let address = try await transport.listeningAddress
  64. XCTAssertEqual(
  65. address.unixDomainSocket,
  66. GRPCHTTP2Core.SocketAddress.UnixDomainSocket(path: "/tmp/posix-uds-test")
  67. )
  68. transport.stopListening()
  69. }
  70. }
  71. }
  72. func testGetListeningAddress_Vsock() async throws {
  73. try XCTSkipUnless(self.vsockAvailable(), "Vsock unavailable")
  74. let transport = GRPCHTTP2Core.HTTP2ServerTransport.Posix(
  75. address: .vsock(contextID: .any, port: .any)
  76. )
  77. try await withThrowingDiscardingTaskGroup { group in
  78. group.addTask {
  79. try await transport.listen { _ in }
  80. }
  81. group.addTask {
  82. let address = try await transport.listeningAddress
  83. XCTAssertNotNil(address.virtualSocket)
  84. transport.stopListening()
  85. }
  86. }
  87. }
  88. func testGetListeningAddress_InvalidAddress() async {
  89. let transport = GRPCHTTP2Core.HTTP2ServerTransport.Posix(
  90. address: .unixDomainSocket(path: "/this/should/be/an/invalid/path")
  91. )
  92. try? await withThrowingDiscardingTaskGroup { group in
  93. group.addTask {
  94. try await transport.listen { _ in }
  95. }
  96. group.addTask {
  97. do {
  98. _ = try await transport.listeningAddress
  99. XCTFail("Should have thrown a RuntimeError")
  100. } catch let error as RuntimeError {
  101. XCTAssertEqual(error.code, .serverIsStopped)
  102. XCTAssertEqual(
  103. error.message,
  104. """
  105. There is no listening address bound for this server: there may have \
  106. been an error which caused the transport to close, or it may have shut down.
  107. """
  108. )
  109. }
  110. }
  111. }
  112. }
  113. func testGetListeningAddress_StoppedListening() async throws {
  114. let transport = GRPCHTTP2Core.HTTP2ServerTransport.Posix(
  115. address: .ipv4(host: "0.0.0.0", port: 0)
  116. )
  117. try? await withThrowingDiscardingTaskGroup { group in
  118. group.addTask {
  119. try await transport.listen { _ in }
  120. do {
  121. _ = try await transport.listeningAddress
  122. XCTFail("Should have thrown a RuntimeError")
  123. } catch let error as RuntimeError {
  124. XCTAssertEqual(error.code, .serverIsStopped)
  125. XCTAssertEqual(
  126. error.message,
  127. """
  128. There is no listening address bound for this server: there may have \
  129. been an error which caused the transport to close, or it may have shut down.
  130. """
  131. )
  132. }
  133. }
  134. group.addTask {
  135. let address = try await transport.listeningAddress
  136. XCTAssertNotNil(address.ipv4)
  137. transport.stopListening()
  138. }
  139. }
  140. }
  141. }