From 1234b8866f022d5fbfc1d6e3fdfa6f063ea3219b Mon Sep 17 00:00:00 2001 From: Henrik Holst <6200749+hholst80@users.noreply.github.com> Date: Wed, 3 Jan 2024 19:11:16 +0100 Subject: [PATCH] vlib: fix tcp_test.v Test that: the listen_tcp method should not accept .unix af.; and it indeed fails when invoked like so. Remove $windows specific code from tcp.c.v, that on surface inspection seems to be invalid. --- vlib/net/tcp.c.v | 9 ------- vlib/net/tcp_test.v | 62 ++++++++++++++++----------------------------- 2 files changed, 22 insertions(+), 49 deletions(-) diff --git a/vlib/net/tcp.c.v b/vlib/net/tcp.c.v index 8943fc8a75e484..a108104e0a4d68 100644 --- a/vlib/net/tcp.c.v +++ b/vlib/net/tcp.c.v @@ -21,15 +21,6 @@ mut: pub fn dial_tcp(oaddress string) !&TcpConn { mut address := oaddress - $if windows { - // resolving 0.0.0.0 to localhost, works on linux and macos, but not on windows, so try to emulate it: - if address.starts_with(':::') { - address = address.replace_once(':::', 'localhost:') - } - if address.starts_with('0.0.0.0:') { - address = address.replace_once('0.0.0.0:', 'localhost:') - } - } addrs := resolve_addrs_fuzzy(address, .tcp) or { return error('${err.msg()}; could not resolve address ${address} in dial_tcp') } diff --git a/vlib/net/tcp_test.v b/vlib/net/tcp_test.v index f0141aeb160586..fc90ca2624f3d1 100644 --- a/vlib/net/tcp_test.v +++ b/vlib/net/tcp_test.v @@ -1,20 +1,17 @@ -// vtest flaky: true -// vtest retry: 8 import net -import os const test_port = 45123 -fn handle_conn(mut c net.TcpConn) { +fn handle_conn(mut c net.TcpConn) ! { for { mut buf := []u8{len: 100, init: 0} read := c.read(mut buf) or { - println('Server: connection dropped') - return + eprintln('Server: connection dropped') + return err } c.write(buf[..read]) or { - println('Server: connection dropped') - return + eprintln('Server: connection dropped') + return err } } } @@ -24,40 +21,35 @@ fn one_shot_echo_server(mut l net.TcpListener, ch_started chan int) ! { ch_started <- 1 mut new_conn := l.accept() or { return error('could not accept') } eprintln(' > new_conn: ${new_conn}') - handle_conn(mut new_conn) - new_conn.close() or {} + handle_conn(mut new_conn)! + new_conn.close()! } fn echo(address string) ! { mut c := net.dial_tcp(address)! - defer { - c.close() or {} - } - println('local: ' + c.addr()!.str()) - println(' peer: ' + c.peer_addr()!.str()) + eprintln('local: ' + c.addr()!.str()) + eprintln(' peer: ' + c.peer_addr()!.str()) data := 'Hello from vlib/net!' c.write_string(data)! mut buf := []u8{len: 4096} - read := c.read(mut buf) or { panic(err) } + read := c.read(mut buf)! assert read == data.len for i := 0; i < read; i++ { assert buf[i] == data[i] } - println('Got "${buf.bytestr()}"') + c.close()! } fn test_tcp_ip6() { eprintln('\n>>> ${@FN}') - address := 'localhost:${test_port}' - mut l := net.listen_tcp(.ip6, ':${test_port}') or { panic(err) } + address := '::1:${test_port}' + mut l := net.listen_tcp(.ip6, address) or { panic(err) } dump(l) start_echo_server(mut l) echo(address) or { panic(err) } - l.close() or {} - // ensure there is at least one new socket created before the next test - l = net.listen_tcp(.ip6, ':${test_port + 1}') or { panic(err) } + l.close() or { panic(err) } } fn start_echo_server(mut l net.TcpListener) { @@ -68,31 +60,21 @@ fn start_echo_server(mut l net.TcpListener) { fn test_tcp_ip() { eprintln('\n>>> ${@FN}') - address := 'localhost:${test_port}' + address := '127.0.0.1:${test_port}' mut l := net.listen_tcp(.ip, address) or { panic(err) } dump(l) start_echo_server(mut l) echo(address) or { panic(err) } - l.close() or {} + l.close() or { panic(err) } } fn test_tcp_unix() { eprintln('\n>>> ${@FN}') - // TODO(emily): - // whilst windows supposedly supports unix sockets - // this doesnt work (wsaeopnotsupp at the call to bind()) - $if !windows { - address := os.real_path('tcp-test.sock') - // address := 'tcp-test.sock' - println('${address}') - - mut l := net.listen_tcp(.unix, address) or { panic(err) } - start_echo_server(mut l) - echo(address) or { panic(err) } - l.close() or {} + address := 'tcp-test.sock' + eprintln('${address}') - os.rm(address) or { panic('failed to remove socket file') } - } + mut l := net.listen_tcp(.unix, address) or { return } + assert false } fn testsuite_end() { @@ -103,6 +85,6 @@ fn test_bind() { $if !network ? { return } - mut conn := net.dial_tcp_with_bind('vlang.io:80', '127.0.0.1:0')! - conn.close()! + mut conn := net.dial_tcp_with_bind('vlang.io:80', '127.0.0.1:0') or { panic(err) } + conn.close() or { panic(err) } }