Skip to content

Commit

Permalink
move makeStorage() to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Aug 3, 2024
1 parent 3d58c54 commit 0ea8f4d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 40 deletions.
6 changes: 3 additions & 3 deletions FlyingFox/Sources/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion FlyingSocks/Sources/AsyncSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 0 additions & 9 deletions FlyingSocks/Sources/SocketAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions FlyingSocks/Tests/SocketAddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
16 changes: 2 additions & 14 deletions FlyingSocks/Tests/SocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand All @@ -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)
)
Expand Down

0 comments on commit 0ea8f4d

Please sign in to comment.