From 0ea8f4df26c19835809a95ea29900cf75b022a2b Mon Sep 17 00:00:00 2001 From: Simon Whitty Date: Sun, 4 Aug 2024 09:16:37 +1000 Subject: [PATCH] move makeStorage() to tests --- FlyingFox/Sources/HTTPServer.swift | 6 +++--- FlyingSocks/Sources/AsyncSocket.swift | 2 +- FlyingSocks/Sources/Socket.swift | 13 ------------- FlyingSocks/Sources/SocketAddress.swift | 9 --------- FlyingSocks/Tests/SocketAddressTests.swift | 13 +++++++++++++ FlyingSocks/Tests/SocketTests.swift | 16 ++-------------- 6 files changed, 19 insertions(+), 40 deletions(-) diff --git a/FlyingFox/Sources/HTTPServer.swift b/FlyingFox/Sources/HTTPServer.swift index 28988ed..56ba095 100644 --- a/FlyingFox/Sources/HTTPServer.swift +++ b/FlyingFox/Sources/HTTPServer.swift @@ -38,7 +38,7 @@ import WinSDK.WinSock2 public final actor HTTPServer { let pool: any AsyncSocketPool - private let address: sockaddr_storage + private let address: any SocketAddress private let timeout: TimeInterval private let logger: any Logging private var handlers: RoutedHTTPHandler @@ -48,7 +48,7 @@ public final actor HTTPServer { pool: some AsyncSocketPool = defaultPool(), logger: any Logging = defaultLogger(), handler: (any HTTPHandler)? = nil) { - self.address = address.makeStorage() + self.address = address self.timeout = timeout self.pool = pool self.logger = logger @@ -129,7 +129,7 @@ public final actor HTTPServer { } func makeSocketAndListen() throws -> Socket { - let socket = try Socket(domain: Int32(address.ss_family)) + let socket = try Socket(domain: Int32(type(of: address).family)) try socket.setValue(true, for: .localAddressReuse) #if canImport(Darwin) try socket.setValue(true, for: .noSIGPIPE) diff --git a/FlyingSocks/Sources/AsyncSocket.swift b/FlyingSocks/Sources/AsyncSocket.swift index b4a5072..7233d58 100644 --- a/FlyingSocks/Sources/AsyncSocket.swift +++ b/FlyingSocks/Sources/AsyncSocket.swift @@ -83,7 +83,7 @@ public struct AsyncSocket: Sendable { pool: some AsyncSocketPool, timeout: TimeInterval = 5) async throws -> Self { try await withThrowingTimeout(seconds: timeout) { - let socket = try Socket(domain: Int32(address.makeStorage().ss_family), type: Socket.stream) + let socket = try Socket(domain: Int32(type(of: address).family), type: Socket.stream) let asyncSocket = try AsyncSocket(socket: socket, pool: pool) try await asyncSocket.connect(to: address) return asyncSocket diff --git a/FlyingSocks/Sources/Socket.swift b/FlyingSocks/Sources/Socket.swift index 1f96dad..354b9d9 100644 --- a/FlyingSocks/Sources/Socket.swift +++ b/FlyingSocks/Sources/Socket.swift @@ -109,19 +109,6 @@ public struct Socket: Sendable, Hashable { } } - public func bind(to storage: sockaddr_storage) throws { - switch Int32(storage.ss_family) { - case AF_INET: - try bind(to: sockaddr_in.make(from: storage)) - case AF_INET6: - try bind(to: sockaddr_in6.make(from: storage)) - case AF_UNIX: - try bind(to: sockaddr_un.make(from: storage)) - default: - throw SocketError.unsupportedAddress - } - } - public func listen(maxPendingConnection: Int32 = SOMAXCONN) throws { if Socket.listen(file.rawValue, maxPendingConnection) == -1 { let error = SocketError.makeFailed("Listen") diff --git a/FlyingSocks/Sources/SocketAddress.swift b/FlyingSocks/Sources/SocketAddress.swift index a9f8d1a..276c75a 100644 --- a/FlyingSocks/Sources/SocketAddress.swift +++ b/FlyingSocks/Sources/SocketAddress.swift @@ -114,15 +114,6 @@ public extension SocketAddress { } } } - - func makeStorage() -> sockaddr_storage { - var addr = self - return withUnsafePointer(to: &addr) { - $0.withMemoryRebound(to: sockaddr_storage.self, capacity: 1) { - $0.pointee - } - } - } } extension Socket { diff --git a/FlyingSocks/Tests/SocketAddressTests.swift b/FlyingSocks/Tests/SocketAddressTests.swift index 33e1aa7..4b9cdce 100644 --- a/FlyingSocks/Tests/SocketAddressTests.swift +++ b/FlyingSocks/Tests/SocketAddressTests.swift @@ -181,3 +181,16 @@ final class SocketAddressTests: XCTestCase { } } } + + +private extension SocketAddress { + + func makeStorage() -> sockaddr_storage { + var addr = self + return withUnsafePointer(to: &addr) { + $0.withMemoryRebound(to: sockaddr_storage.self, capacity: 1) { + $0.pointee + } + } + } +} diff --git a/FlyingSocks/Tests/SocketTests.swift b/FlyingSocks/Tests/SocketTests.swift index 88f4706..b34063d 100644 --- a/FlyingSocks/Tests/SocketTests.swift +++ b/FlyingSocks/Tests/SocketTests.swift @@ -175,7 +175,7 @@ final class SocketTests: XCTestCase { func testSocketBind_ToINET() throws { let socket = try Socket(domain: AF_INET, type: Socket.stream) try socket.setValue(true, for: .localAddressReuse) - let address = Socket.makeAddressINET(port:5050).makeStorage() + let address = Socket.makeAddressINET(port:5050) XCTAssertNoThrow( try socket.bind(to: address) ) @@ -191,21 +191,9 @@ final class SocketTests: XCTestCase { ) } - func testSocketBind_ToInvalidStorage_ThrowsUnsupported() { - let socket = Socket(file: -1) - var addr = Socket.makeAddressUnix(path: "/var/fox/xyz").makeStorage() - addr.ss_family = sa_family_t(AF_IPX) - XCTAssertThrowsError( - try socket.bind(to: addr), - of: SocketError.self - ) { - XCTAssertEqual($0, .unsupportedAddress) - } - } - func testSocketBind_ToStorage_ThrowsError_WhenInvalid() { let socket = Socket(file: -1) - let address = Socket.makeAddressINET6(port: 8080).makeStorage() + let address = Socket.makeAddressINET6(port: 8080) XCTAssertThrowsError( try socket.bind(to: address) )