Skip to content

Commit d92b92c

Browse files
committed
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. Fix server_test.v to use 127.0.0.1 when connecting to the HTTP server, as Windows does not accept a connection to 0.0.0.0 (it works on Linux/BSD.)
1 parent 7d29afe commit d92b92c

File tree

4 files changed

+62
-65
lines changed

4 files changed

+62
-65
lines changed

vlib/net/address.c.v

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,20 @@ pub fn resolve_addrs_fuzzy(addr string, @type SocketType) ![]Addr {
163163

164164
// resolve_ipaddrs converts the given `addr`, `family` and `typ` to a list of addresses
165165
pub fn resolve_ipaddrs(addr string, family AddrFamily, typ SocketType) ![]Addr {
166-
address, port := split_address(addr)!
166+
mut address, port := split_address(addr)!
167167

168-
if addr[0] == `:` {
168+
if address.starts_with('[') && address.ends_with(']') {
169+
address = address[1..address.len - 1]
170+
}
171+
172+
// This is to allow a ":8080" shorthand notation similar to HTTPie
173+
// [URL shortcuts for localhost](https://httpie.io/docs/cli/url-shortcuts-for-localhost)
174+
// and (net.Dial)(https://pkg.go.dev/net#Dial) in Go.
175+
if address == '' {
169176
match family {
170-
.ip6 {
171-
return [new_ip6(port, net.addr_ip6_any)]
172-
}
173-
.ip, .unspec {
174-
return [new_ip(port, net.addr_ip_any)]
175-
}
176-
else {}
177+
.ip6 { return [new_ip6(port, addr_ip6_any)] }
178+
.ip, .unspec { return [new_ip(port, addr_ip_any)] }
179+
.unix { error('unix address must not be empty') }
177180
}
178181
}
179182

vlib/net/http/server_test.v

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,21 @@ fn test_server_custom_handler() {
127127
}
128128
t := spawn server.listen_and_serve()
129129
server.wait_till_running()!
130-
x := http.fetch(url: 'http://${server.addr}/endpoint?abc=xyz', data: 'my data')!
130+
x := http.fetch(url: 'http://127.0.0.1:${cport}/endpoint?abc=xyz', data: 'my data')!
131131
assert x.body == 'my data, /endpoint?abc=xyz'
132132
assert x.status_code == 200
133133
assert x.status_msg == 'OK'
134134
assert x.http_version == '1.1'
135-
y := http.fetch(url: 'http://${server.addr}/another/endpoint', data: 'abcde')!
135+
y := http.fetch(url: 'http://127.0.0.1:${cport}/another/endpoint', data: 'abcde')!
136136
assert y.body == 'abcde, /another/endpoint'
137137
assert y.status_code == 200
138138
assert x.status_msg == 'OK'
139139
assert y.status() == .ok
140140
assert y.http_version == '1.1'
141141
//
142-
http.fetch(url: 'http://${server.addr}/something/else')!
142+
http.fetch(url: 'http://127.0.0.1:${cport}/something/else')!
143143
//
144-
big_url := 'http://${server.addr}/redirect_to_big'
144+
big_url := 'http://127.0.0.1:${cport}/redirect_to_big'
145145
mut progress_calls := &ProgressCalls{}
146146
z := http.fetch(
147147
url: big_url
@@ -174,7 +174,7 @@ fn test_server_custom_handler() {
174174
assert progress_calls.chunks[0].bytestr().starts_with('HTTP/1.1 301 Moved permanently')
175175
assert progress_calls.chunks[1].bytestr().starts_with('HTTP/1.1 200 OK')
176176
assert progress_calls.chunks.last().bytestr().contains('xyz def')
177-
assert progress_calls.redirected_to == ['http://${server.addr}/big']
177+
assert progress_calls.redirected_to == ['http://127.0.0.1:${cport}/big']
178178
//
179179
server.stop()
180180
t.wait()
@@ -232,7 +232,8 @@ fn test_my_counting_handler_on_random_port() {
232232
on_running: fn (mut server http.Server) {
233233
spawn fn (mut server http.Server) {
234234
log.warn('server started')
235-
url := 'http://${server.addr}/count'
235+
_, server_port := net.split_address(server.addr) or { panic(err) }
236+
url := 'http://127.0.0.1:${server_port}/count'
236237
log.info('fetching from url: ${url}')
237238
for _ in 0 .. 5 {
238239
x := http.fetch(url: url, data: 'my data') or { panic(err) }

vlib/net/tcp.c.v

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ mut:
2121

2222
pub fn dial_tcp(oaddress string) !&TcpConn {
2323
mut address := oaddress
24-
$if windows {
25-
// resolving 0.0.0.0 to localhost, works on linux and macos, but not on windows, so try to emulate it:
26-
if address.starts_with(':::') {
27-
address = address.replace_once(':::', 'localhost:')
28-
}
29-
if address.starts_with('0.0.0.0:') {
30-
address = address.replace_once('0.0.0.0:', 'localhost:')
31-
}
32-
}
3324
addrs := resolve_addrs_fuzzy(address, .tcp) or {
3425
return error('${err.msg()}; could not resolve address ${address} in dial_tcp')
3526
}

vlib/net/tcp_test.v

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
// vtest flaky: true
2-
// vtest retry: 8
31
import net
4-
import os
52

63
const test_port = 45123
74

8-
fn handle_conn(mut c net.TcpConn) {
5+
fn handle_conn(mut c net.TcpConn) ! {
96
for {
107
mut buf := []u8{len: 100, init: 0}
118
read := c.read(mut buf) or {
12-
println('Server: connection dropped')
13-
return
9+
eprintln('Server: connection dropped')
10+
return err
1411
}
1512
c.write(buf[..read]) or {
16-
println('Server: connection dropped')
17-
return
13+
eprintln('Server: connection dropped')
14+
return err
1815
}
1916
}
2017
}
@@ -24,40 +21,45 @@ fn one_shot_echo_server(mut l net.TcpListener, ch_started chan int) ! {
2421
ch_started <- 1
2522
mut new_conn := l.accept() or { return error('could not accept') }
2623
eprintln(' > new_conn: ${new_conn}')
27-
handle_conn(mut new_conn)
28-
new_conn.close() or {}
24+
handle_conn(mut new_conn)!
25+
new_conn.close()!
2926
}
3027

3128
fn echo(address string) ! {
3229
mut c := net.dial_tcp(address)!
33-
defer {
34-
c.close() or {}
35-
}
3630

37-
println('local: ' + c.addr()!.str())
38-
println(' peer: ' + c.peer_addr()!.str())
31+
eprintln('local: ' + c.addr()!.str())
32+
eprintln(' peer: ' + c.peer_addr()!.str())
3933

4034
data := 'Hello from vlib/net!'
4135
c.write_string(data)!
4236
mut buf := []u8{len: 4096}
43-
read := c.read(mut buf) or { panic(err) }
37+
read := c.read(mut buf)!
4438
assert read == data.len
4539
for i := 0; i < read; i++ {
4640
assert buf[i] == data[i]
4741
}
48-
println('Got "${buf.bytestr()}"')
42+
c.close()!
4943
}
5044

5145
fn test_tcp_ip6() {
5246
eprintln('\n>>> ${@FN}')
53-
address := 'localhost:${test_port}'
54-
mut l := net.listen_tcp(.ip6, ':${test_port}') or { panic(err) }
47+
address := ':${test_port}'
48+
mut l := net.listen_tcp(.ip6, address)!
5549
dump(l)
5650
start_echo_server(mut l)
57-
echo(address) or { panic(err) }
58-
l.close() or {}
59-
// ensure there is at least one new socket created before the next test
60-
l = net.listen_tcp(.ip6, ':${test_port + 1}') or { panic(err) }
51+
echo(address)!
52+
l.close()!
53+
}
54+
55+
fn test_tcp_ip6_localhost() {
56+
eprintln('\n>>> ${@FN}')
57+
address := '[::1]:${test_port}'
58+
mut l := net.listen_tcp(.ip6, address)!
59+
dump(l)
60+
start_echo_server(mut l)
61+
echo(address)!
62+
l.close()!
6163
}
6264

6365
fn start_echo_server(mut l net.TcpListener) {
@@ -68,31 +70,31 @@ fn start_echo_server(mut l net.TcpListener) {
6870

6971
fn test_tcp_ip() {
7072
eprintln('\n>>> ${@FN}')
71-
address := 'localhost:${test_port}'
72-
mut l := net.listen_tcp(.ip, address) or { panic(err) }
73+
address := ':${test_port}'
74+
mut l := net.listen_tcp(.ip, address)!
7375
dump(l)
7476
start_echo_server(mut l)
75-
echo(address) or { panic(err) }
76-
l.close() or {}
77+
echo(address)!
78+
l.close()!
7779
}
7880

79-
fn test_tcp_unix() {
81+
fn test_tcp_ip_localhost() {
8082
eprintln('\n>>> ${@FN}')
81-
// TODO(emily):
82-
// whilst windows supposedly supports unix sockets
83-
// this doesnt work (wsaeopnotsupp at the call to bind())
84-
$if !windows {
85-
address := os.real_path('tcp-test.sock')
86-
// address := 'tcp-test.sock'
87-
println('${address}')
83+
address := '127.0.0.1:${test_port}'
84+
mut l := net.listen_tcp(.ip, address)!
85+
dump(l)
86+
start_echo_server(mut l)
87+
echo(address)!
88+
l.close()!
89+
}
8890

89-
mut l := net.listen_tcp(.unix, address) or { panic(err) }
90-
start_echo_server(mut l)
91-
echo(address) or { panic(err) }
92-
l.close() or {}
91+
fn test_tcp_unix() {
92+
eprintln('\n>>> ${@FN}')
93+
address := 'tcp-test.sock'
94+
eprintln('${address}')
9395

94-
os.rm(address) or { panic('failed to remove socket file') }
95-
}
96+
mut l := net.listen_tcp(.unix, address) or { return }
97+
assert false
9698
}
9799

98100
fn testsuite_end() {

0 commit comments

Comments
 (0)