SocketAddressTests.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import GRPCHTTP2Core
  17. import XCTest
  18. final class SocketAddressTests: XCTestCase {
  19. func testSocketAddressUnwrapping() {
  20. var address: SocketAddress = .ipv4(host: "foo", port: 42)
  21. XCTAssertEqual(address.ipv4, SocketAddress.IPv4(host: "foo", port: 42))
  22. XCTAssertNil(address.ipv6)
  23. XCTAssertNil(address.unixDomainSocket)
  24. XCTAssertNil(address.virtualSocket)
  25. address = .ipv6(host: "bar", port: 42)
  26. XCTAssertEqual(address.ipv6, SocketAddress.IPv6(host: "bar", port: 42))
  27. XCTAssertNil(address.ipv4)
  28. XCTAssertNil(address.unixDomainSocket)
  29. XCTAssertNil(address.virtualSocket)
  30. address = .unixDomainSocket(path: "baz")
  31. XCTAssertEqual(address.unixDomainSocket, SocketAddress.UnixDomainSocket(path: "baz"))
  32. XCTAssertNil(address.ipv4)
  33. XCTAssertNil(address.ipv6)
  34. XCTAssertNil(address.virtualSocket)
  35. address = .vsock(contextID: .any, port: .any)
  36. XCTAssertEqual(address.virtualSocket, SocketAddress.VirtualSocket(contextID: .any, port: .any))
  37. XCTAssertNil(address.ipv4)
  38. XCTAssertNil(address.ipv6)
  39. XCTAssertNil(address.unixDomainSocket)
  40. }
  41. func testSocketAddressDescription() {
  42. var address: SocketAddress = .ipv4(host: "127.0.0.1", port: 42)
  43. XCTAssertDescription(address, "[ipv4]127.0.0.1:42")
  44. address = .ipv6(host: "::1", port: 42)
  45. XCTAssertDescription(address, "[ipv6]::1:42")
  46. address = .unixDomainSocket(path: "baz")
  47. XCTAssertDescription(address, "[unix]baz")
  48. address = .vsock(contextID: 314, port: 159)
  49. XCTAssertDescription(address, "[vsock]314:159")
  50. address = .vsock(contextID: .any, port: .any)
  51. XCTAssertDescription(address, "[vsock]-1:-1")
  52. }
  53. func testSocketAddressSubTypesDescription() {
  54. let ipv4 = SocketAddress.IPv4(host: "127.0.0.1", port: 42)
  55. XCTAssertDescription(ipv4, "[ipv4]127.0.0.1:42")
  56. let ipv6 = SocketAddress.IPv6(host: "foo", port: 42)
  57. XCTAssertDescription(ipv6, "[ipv6]foo:42")
  58. let uds = SocketAddress.UnixDomainSocket(path: "baz")
  59. XCTAssertDescription(uds, "[unix]baz")
  60. var vsock = SocketAddress.VirtualSocket(contextID: 314, port: 159)
  61. XCTAssertDescription(vsock, "[vsock]314:159")
  62. vsock.contextID = .any
  63. vsock.port = .any
  64. XCTAssertDescription(vsock, "[vsock]-1:-1")
  65. }
  66. }