mirror of
https://github.com/apple/swift-system
synced 2026-11-24 04:38:14 +00:00
Add socket APIs to swift-system (#281) Port the long-standing sockets implementation from #30 to the new staging branch. This adds comprehensive low-level Swift APIs for POSIX socket programming. Original Co-authored-by: Karoy Lorentey <karoly@apple.com>
226 lines
6.8 KiB
Swift
226 lines
6.8 KiB
Swift
/*
|
|
This source file is part of the Swift System open source project
|
|
|
|
Copyright (c) 2021 - 2025 Apple Inc. and the Swift System project authors
|
|
Licensed under Apache License v2.0 with Runtime Library Exception
|
|
|
|
See https://swift.org/LICENSE.txt for license information
|
|
*/
|
|
|
|
#if !os(Windows)
|
|
|
|
import Testing
|
|
|
|
#if SYSTEM_PACKAGE_DARWIN
|
|
import Darwin
|
|
#elseif canImport(Glibc)
|
|
import Glibc
|
|
#elseif canImport(Musl)
|
|
import Musl
|
|
#elseif canImport(Android)
|
|
import Android
|
|
#else
|
|
#error("Unsupported Platform")
|
|
#endif
|
|
|
|
@testable import SystemSockets
|
|
import SystemPackage
|
|
|
|
@Suite("Socket Operations")
|
|
private struct SocketOperationsTests {
|
|
|
|
// MARK: - Bind Tests
|
|
|
|
@available(System 99, *)
|
|
@Test func bindToAnyPort() throws {
|
|
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? socket.close() }
|
|
|
|
let address = SocketAddress(ipv4: IPv4Address.any(port: 0))
|
|
try socket.bind(to: address)
|
|
|
|
// Verify we can get the local address after binding
|
|
var localAddr = SocketAddress()
|
|
try socket.getLocalAddress(into: &localAddr)
|
|
#expect(localAddr.family == SocketDescriptor.Domain.ipv4)
|
|
#expect(localAddr.ipv4?.port != 0) // Port should be assigned
|
|
}
|
|
|
|
@available(System 99, *)
|
|
@Test func bindToLoopback() throws {
|
|
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? socket.close() }
|
|
|
|
let address = SocketAddress(ipv4: IPv4Address.loopback(port: 0))
|
|
try socket.bind(to: address)
|
|
|
|
var localAddr = SocketAddress()
|
|
try socket.getLocalAddress(into: &localAddr)
|
|
#expect(localAddr.ipv4?.addressString == "127.0.0.1")
|
|
}
|
|
|
|
// MARK: - Listen Tests
|
|
|
|
@available(System 99, *)
|
|
@Test func listenWithDefaultBacklog() throws {
|
|
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? socket.close() }
|
|
|
|
let address = SocketAddress(ipv4: IPv4Address.loopback(port: 0))
|
|
try socket.bind(to: address)
|
|
try socket.listen(backlog: 5)
|
|
}
|
|
|
|
// MARK: - Connect and Accept Tests
|
|
|
|
@available(System 99, *)
|
|
@Test func tcpConnectAndAccept() throws {
|
|
// Create server socket
|
|
let server = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? server.close() }
|
|
|
|
let serverAddr = SocketAddress(ipv4: IPv4Address.loopback(port: 0))
|
|
try server.bind(to: serverAddr)
|
|
try server.listen(backlog: 1)
|
|
|
|
// Get the actual port
|
|
var boundAddr = SocketAddress()
|
|
try server.getLocalAddress(into: &boundAddr)
|
|
let port = boundAddr.ipv4!.port
|
|
|
|
// Create client socket and connect
|
|
let client = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? client.close() }
|
|
|
|
let connectAddr = SocketAddress(ipv4: IPv4Address.loopback(port: port))
|
|
try client.connect(to: connectAddr)
|
|
|
|
// Accept connection
|
|
var peerAddr = SocketAddress()
|
|
let acceptedSocket = try server.accept(client: &peerAddr)
|
|
defer { try? acceptedSocket.close() }
|
|
|
|
#expect(peerAddr.family == SocketDescriptor.Domain.ipv4)
|
|
#expect(peerAddr.ipv4?.addressString == "127.0.0.1")
|
|
}
|
|
|
|
// MARK: - Send and Receive Tests
|
|
|
|
@available(System 99, *)
|
|
@Test func tcpSendReceive() throws {
|
|
// Create server socket
|
|
let server = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? server.close() }
|
|
|
|
let serverAddr = SocketAddress(ipv4: IPv4Address.loopback(port: 0))
|
|
try server.bind(to: serverAddr)
|
|
try server.listen(backlog: 1)
|
|
|
|
var boundAddr = SocketAddress()
|
|
try server.getLocalAddress(into: &boundAddr)
|
|
let port = boundAddr.ipv4!.port
|
|
|
|
// Create client socket and connect
|
|
let client = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? client.close() }
|
|
|
|
let connectAddr = SocketAddress(ipv4: IPv4Address.loopback(port: port))
|
|
try client.connect(to: connectAddr)
|
|
|
|
// Accept connection
|
|
let acceptedSocket = try server.accept()
|
|
defer { try? acceptedSocket.close() }
|
|
|
|
// Send data from client
|
|
let message = "Hello, World!"
|
|
let messageBytes = Array(message.utf8)
|
|
let sent = try messageBytes.withUnsafeBytes { buffer in
|
|
try client.send(UnsafeRawBufferPointer(buffer))
|
|
}
|
|
#expect(sent == messageBytes.count)
|
|
|
|
// Receive data on server
|
|
var buffer = [UInt8](repeating: 0, count: 1024)
|
|
let received = try buffer.withUnsafeMutableBytes { buffer in
|
|
try acceptedSocket.receive(into: buffer)
|
|
}
|
|
#expect(received == messageBytes.count)
|
|
|
|
let receivedMessage = String(decoding: buffer.prefix(received), as: UTF8.self)
|
|
#expect(receivedMessage == message)
|
|
}
|
|
|
|
// MARK: - UDP Tests
|
|
|
|
@available(System 99, *)
|
|
@Test func udpSendReceive() throws {
|
|
// Create receiver socket
|
|
let receiver = try SocketDescriptor.open(.ipv4, .datagram, protocol: .udp)
|
|
defer { try? receiver.close() }
|
|
|
|
let receiverAddr = SocketAddress(ipv4: IPv4Address.loopback(port: 0))
|
|
try receiver.bind(to: receiverAddr)
|
|
|
|
var boundAddr = SocketAddress()
|
|
try receiver.getLocalAddress(into: &boundAddr)
|
|
let port = boundAddr.ipv4!.port
|
|
|
|
// Create sender socket
|
|
let sender = try SocketDescriptor.open(.ipv4, .datagram, protocol: .udp)
|
|
defer { try? sender.close() }
|
|
|
|
// Send datagram
|
|
let message = "UDP Message"
|
|
let messageBytes = Array(message.utf8)
|
|
let targetAddr = SocketAddress(ipv4: IPv4Address.loopback(port: port))
|
|
let sent = try messageBytes.withUnsafeBytes { buffer in
|
|
try sender.send(UnsafeRawBufferPointer(buffer), to: targetAddr)
|
|
}
|
|
#expect(sent == messageBytes.count)
|
|
|
|
// Receive datagram
|
|
var buffer = [UInt8](repeating: 0, count: 1024)
|
|
var fromAddr = SocketAddress()
|
|
let received = try buffer.withUnsafeMutableBytes { buffer in
|
|
try receiver.receive(into: buffer, sender: &fromAddr)
|
|
}
|
|
#expect(received == messageBytes.count)
|
|
#expect(fromAddr.family == SocketDescriptor.Domain.ipv4)
|
|
|
|
let receivedMessage = String(decoding: buffer.prefix(received), as: UTF8.self)
|
|
#expect(receivedMessage == message)
|
|
}
|
|
|
|
// MARK: - Socket Options Tests
|
|
|
|
@available(System 99, *)
|
|
@Test func setReuseAddress() throws {
|
|
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? socket.close() }
|
|
|
|
try socket.setReuseAddress(true)
|
|
let value = try socket.getSocketOption(SocketDescriptor.SocketOption.reuseAddress)
|
|
#expect(value != 0)
|
|
}
|
|
|
|
@available(System 99, *)
|
|
@Test func getSendBufferSize() throws {
|
|
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? socket.close() }
|
|
|
|
let size = try socket.getSocketOption(SocketDescriptor.SocketOption.sendBufferSize)
|
|
#expect(size > 0)
|
|
}
|
|
|
|
@available(System 99, *)
|
|
@Test func getReceiveBufferSize() throws {
|
|
let socket = try SocketDescriptor.open(.ipv4, .stream, protocol: .tcp)
|
|
defer { try? socket.close() }
|
|
|
|
let size = try socket.getSocketOption(SocketDescriptor.SocketOption.receiveBufferSize)
|
|
#expect(size > 0)
|
|
}
|
|
}
|
|
|
|
#endif
|