ci: fix unix sockets test on macos

pull/8689/head
Delyan Angelov 2021-02-11 22:34:38 +02:00
parent 9b4f2edbfa
commit 89521584a2
3 changed files with 14 additions and 11 deletions

View File

@ -18,10 +18,15 @@ struct C.sockaddr {
sa_family u16 sa_family u16
} }
const max_sun_path = 104
// 104 for macos, 108 for linux => use the minimum
struct C.sockaddr_un { struct C.sockaddr_un {
mut: mut:
sun_len byte
sun_family int sun_family int
sun_path charptr sun_path [104]char
} }
struct C.addrinfo { struct C.addrinfo {

View File

@ -58,13 +58,13 @@ fn (mut s StreamSocket) @select(test Select, timeout time.Duration) ?bool {
} }
fn (mut s StreamSocket) connect(a string) ? { fn (mut s StreamSocket) connect(a string) ? {
if a.len >= 108 { if a.len >= max_sun_path {
return error('Socket path too long! Max length: 107 chars.') return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
} }
mut addr := C.sockaddr_un{} mut addr := C.sockaddr_un{}
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) } unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) }
addr.sun_family = C.AF_UNIX addr.sun_family = C.AF_UNIX
C.strncpy(addr.sun_path, a.str, 108) unsafe { C.strncpy(addr.sun_path, a.str, max_sun_path) }
size := C.SUN_LEN(&addr) size := C.SUN_LEN(&addr)
sockaddr := unsafe { &C.sockaddr(&addr) } sockaddr := unsafe { &C.sockaddr(&addr) }
res := C.connect(s.handle, sockaddr, size) res := C.connect(s.handle, sockaddr, size)
@ -89,15 +89,15 @@ fn (mut s StreamSocket) connect(a string) ? {
} }
pub fn listen_stream(sock string) ?&StreamListener { pub fn listen_stream(sock string) ?&StreamListener {
if sock.len >= 108 { if sock.len >= max_sun_path {
return error('Socket path too long! Max length: 107 chars.') return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
} }
mut s := new_stream_socket() ? mut s := new_stream_socket() ?
s.path = sock s.path = sock
mut addr := C.sockaddr_un{} mut addr := C.sockaddr_un{}
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) } unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) }
addr.sun_family = C.AF_UNIX addr.sun_family = C.AF_UNIX
C.strncpy(addr.sun_path, sock.str, 108) unsafe { C.strncpy(addr.sun_path, sock.str, max_sun_path) }
size := C.SUN_LEN(&addr) size := C.SUN_LEN(&addr)
sockaddr := unsafe { &C.sockaddr(&addr) } sockaddr := unsafe { &C.sockaddr(&addr) }
net.socket_error(C.bind(s.handle, sockaddr, size)) ? net.socket_error(C.bind(s.handle, sockaddr, size)) ?

View File

@ -1,8 +1,6 @@
import net.unix import net.unix
const ( const test_port = 'unix_domain_socket'
test_port = 'test'
)
fn handle_conn(mut c unix.StreamConn) { fn handle_conn(mut c unix.StreamConn) {
for { for {
@ -27,7 +25,7 @@ fn echo_server(mut l unix.StreamListener) ? {
} }
fn echo() ? { fn echo() ? {
mut c := unix.connect_stream('test') ? mut c := unix.connect_stream(test_port) ?
defer { defer {
c.close() or { } c.close() or { }
} }