chore
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
fn run(entrypoint string) os.Result {
|
||||
cmd := 'v -Wfatal-errors -path "${@VMODROOT}/../|@vlib" run ${entrypoint}'
|
||||
return os.execute(cmd)
|
||||
}
|
||||
|
||||
fn test_example_host_fqdn() {
|
||||
r := run('examples/host_fqdn.v')
|
||||
dump(r.output)
|
||||
// host_fqdn may return empty string and this is also fine. So check exit_code only.
|
||||
assert r.exit_code == 0
|
||||
}
|
||||
|
||||
fn test_example_tcp_echo_server() {
|
||||
expect_server := 'Listening on 127.0.0.1:1088...
|
||||
|Accpeted connection. Remote address: 127.0.0.1, remote port: 1001
|
||||
|Received from client: 18 bytes, data: Hello from client!
|
||||
|Sent to the client: 18 bytes, data: Hello from client!'.strip_margin()
|
||||
|
||||
expect_client := 'Connected to server 127.0.0.1:1088...
|
||||
|Sent to the server: 18 bytes, data: Hello from client!
|
||||
|Received from server: 18 bytes, data: Hello from client!'.strip_margin()
|
||||
|
||||
mut threads := []thread os.Result{}
|
||||
threads << spawn run('examples/tcp_echo_server.v -test')
|
||||
time.sleep(time.second * 1)
|
||||
threads << spawn run('examples/tcp_echo_client.v')
|
||||
results := threads.wait()
|
||||
|
||||
// result.output contains the string with lots of trailing zeros, so we
|
||||
// use limit() to shrink the output string to the expected length.
|
||||
for result in results {
|
||||
dump(result)
|
||||
assert result.exit_code == 0
|
||||
if result.output.contains('Listening') {
|
||||
assert result.output.limit(expect_server.len) == expect_server
|
||||
} else {
|
||||
assert result.output.limit(expect_client.len) == expect_client
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import netio
|
||||
|
||||
fn test_network_interfaces() {
|
||||
ifs := netio.network_interfaces()!
|
||||
// dump(ifs)
|
||||
assert ifs.len > 0
|
||||
}
|
||||
|
||||
fn test_name_to_index() {
|
||||
ifs := netio.network_interfaces()!
|
||||
iface := ifs[0]
|
||||
dump(iface)
|
||||
assert netio.name_to_index(iface.name)! == iface.index
|
||||
}
|
||||
|
||||
fn test_index_to_name() {
|
||||
ifs := netio.network_interfaces()!
|
||||
iface := ifs[0]
|
||||
dump(iface)
|
||||
assert netio.index_to_name(iface.index)! == iface.name
|
||||
}
|
||||
|
||||
fn test_find_network_interface() {
|
||||
ifs := netio.network_interfaces()!
|
||||
iface := ifs[0]
|
||||
dump(iface)
|
||||
assert netio.find_network_interface(iface.name)!.name == iface.name
|
||||
assert netio.find_network_interface(iface.index.str())!.index == iface.index
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import netio.protocol
|
||||
|
||||
fn test_protocols() {
|
||||
assert protocol.protocols().len > 0
|
||||
}
|
||||
|
||||
fn test_protocol_by_name() {
|
||||
assert 'TCP' in protocol.protocol_by_name('tcp')!.aliases
|
||||
}
|
||||
|
||||
fn test_protocol_by_number() {
|
||||
tcp_proto := protocol.protocol_by_name('tcp')!
|
||||
assert protocol.protocol_by_number(tcp_proto.number)!.name == 'tcp'
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import netio
|
||||
|
||||
fn test_socket_addr_new_ipv4() {
|
||||
addr := netio.SocketAddr.new_ipv4([u8(127), 0, 0, 1]!, 1080)
|
||||
assert addr.str() == '127.0.0.1:1080'
|
||||
}
|
||||
|
||||
fn test_socket_addr_new_ipv6() {
|
||||
addr := netio.SocketAddr.new_ipv6([u8(0xfd), 0xf1, 0x72, 0xd1, 0x00, 0x33, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x47]!, 25535, 0, 0)
|
||||
assert addr.str() == '[fdf1:72d1:0033:0000:0000:0000:0000:0247]:25535'
|
||||
}
|
||||
|
||||
fn test_socket_addr_new_unix() {
|
||||
addr := netio.SocketAddr.new_unix('/run/app.sock')!
|
||||
assert addr.str() == '/run/app.sock'
|
||||
}
|
||||
|
||||
fn test_socket_addr_is_empty() {
|
||||
assert unsafe { netio.SocketAddr.new(netio.af_unspec, 16).is_empty() }
|
||||
assert netio.SocketAddr{}.is_empty()
|
||||
assert !netio.SocketAddr.new_ipv4([u8(127), 0, 0, 1]!, 16).is_empty()
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import netio
|
||||
|
||||
fn test_socket_new() {
|
||||
socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
|
||||
socket.close() or { panic(err) }
|
||||
assert socket.fd != -1
|
||||
}
|
||||
|
||||
fn test_socket_type() {
|
||||
socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
|
||||
socket_type := socket.type()!
|
||||
socket.close() or { panic(err) }
|
||||
assert socket_type == netio.sock_stream
|
||||
}
|
||||
|
||||
fn test_socket_option() {
|
||||
mut socket := netio.Socket.new(netio.af_inet, netio.sock_stream, 0)!
|
||||
socket.set_option(netio.sol_socket, netio.so_reuseaddr, true)!
|
||||
opt_val_int := socket.get_option[int](netio.sol_socket, netio.so_reuseaddr)!
|
||||
opt_val_bool := socket.get_option[bool](netio.sol_socket, netio.so_reuseaddr)!
|
||||
socket.close() or { panic(err) }
|
||||
assert opt_val_int == 1
|
||||
assert opt_val_bool == true
|
||||
}
|
||||
Reference in New Issue
Block a user