2022-04-30 13:27:50 +03:00
|
|
|
// vtest flaky: true
|
2022-01-28 11:07:37 +03:00
|
|
|
// vtest retry: 8
|
2020-11-15 23:54:47 +03:00
|
|
|
import net
|
2021-06-13 23:53:38 +03:00
|
|
|
import os
|
2020-08-21 00:01:37 +03:00
|
|
|
|
2020-08-22 15:29:29 +03:00
|
|
|
const (
|
|
|
|
test_port = 45123
|
|
|
|
)
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
fn handle_conn(mut c net.TcpConn) {
|
2020-08-21 00:01:37 +03:00
|
|
|
for {
|
2022-04-15 15:35:35 +03:00
|
|
|
mut buf := []u8{len: 100, init: 0}
|
2020-11-15 23:54:47 +03:00
|
|
|
read := c.read(mut buf) or {
|
2020-08-21 00:01:37 +03:00
|
|
|
println('Server: connection dropped')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.write(buf[..read]) or {
|
|
|
|
println('Server: connection dropped')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn one_shot_echo_server(mut l net.TcpListener, ch_started chan int) ? {
|
|
|
|
eprintln('> one_shot_echo_server')
|
|
|
|
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 {}
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn echo(address string) ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut c := net.dial_tcp(address)?
|
2020-08-22 15:29:29 +03:00
|
|
|
defer {
|
2021-06-13 23:53:38 +03:00
|
|
|
c.close() or {}
|
2020-08-22 15:29:29 +03:00
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
println('local: ' + c.addr()?.str())
|
|
|
|
println(' peer: ' + c.peer_addr()?.str())
|
2021-06-13 23:53:38 +03:00
|
|
|
|
2020-08-21 00:01:37 +03:00
|
|
|
data := 'Hello from vlib/net!'
|
2022-05-13 06:56:21 +03:00
|
|
|
c.write_string(data)?
|
2022-04-15 15:35:35 +03:00
|
|
|
mut buf := []u8{len: 4096}
|
2022-08-15 15:17:00 +03:00
|
|
|
read := c.read(mut buf) or { panic(err) }
|
2020-08-21 00:01:37 +03:00
|
|
|
assert read == data.len
|
|
|
|
for i := 0; i < read; i++ {
|
|
|
|
assert buf[i] == data[i]
|
|
|
|
}
|
2020-08-22 15:29:29 +03:00
|
|
|
println('Got "$buf.bytestr()"')
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn test_tcp_ip6() {
|
|
|
|
eprintln('\n>>> ${@FN}')
|
|
|
|
address := 'localhost:$test_port'
|
|
|
|
mut l := net.listen_tcp(.ip6, ':$test_port') 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) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_echo_server(mut l net.TcpListener) {
|
|
|
|
ch_server_started := chan int{}
|
2022-11-05 10:46:40 +03:00
|
|
|
spawn one_shot_echo_server(mut l, ch_server_started)
|
2021-06-13 23:53:38 +03:00
|
|
|
_ := <-ch_server_started
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_tcp_ip() {
|
|
|
|
eprintln('\n>>> ${@FN}')
|
|
|
|
address := 'localhost:$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 {}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {}
|
|
|
|
|
|
|
|
os.rm(address) or { panic('failed to remove socket file') }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testsuite_end() {
|
|
|
|
eprintln('\ndone')
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
2022-07-15 12:38:17 +03:00
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_bind() {
|
2022-07-21 22:04:51 +03:00
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
2022-07-23 23:33:43 +03:00
|
|
|
mut conn := net.dial_tcp_with_bind('vlang.io:80', '127.0.0.1:0')?
|
|
|
|
conn.close()?
|
2022-07-15 12:38:17 +03:00
|
|
|
}
|